Externalize strings in Commit command
[jgit.git] / org.eclipse.jgit.pgm / src / org / eclipse / jgit / pgm / CommandRef.java
blobeace2e1f2583ec12ebc13706edafbcab0170813b
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * and other copyright owners as documented in the project's IP log.
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 package org.eclipse.jgit.pgm;
46 import java.lang.reflect.Constructor;
47 import java.lang.reflect.InvocationTargetException;
48 import java.text.MessageFormat;
50 /**
51 * Description of a command (a {@link TextBuiltin} subclass.
52 * <p>
53 * These descriptions are lightweight compared to creating a command instance
54 * and are therefore suitable for catalogs of "known" commands without linking
55 * the command's implementation and creating a dummy instance of the command.
57 public class CommandRef {
58 private final Class<? extends TextBuiltin> impl;
60 private final String name;
62 private String usage;
64 boolean common;
66 CommandRef(final Class<? extends TextBuiltin> clazz) {
67 this(clazz, guessName(clazz));
70 CommandRef(final Class<? extends TextBuiltin> clazz, final Command cmd) {
71 this(clazz, cmd.name().length() > 0 ? cmd.name() : guessName(clazz));
72 usage = cmd.usage();
73 common = cmd.common();
76 private CommandRef(final Class<? extends TextBuiltin> clazz, final String cn) {
77 impl = clazz;
78 name = cn;
79 usage = "";
82 private static String guessName(final Class<? extends TextBuiltin> clazz) {
83 final StringBuilder s = new StringBuilder();
84 if (clazz.getName().startsWith("org.eclipse.jgit.pgm.debug."))
85 s.append("debug-");
87 boolean lastWasDash = true;
88 for (final char c : clazz.getSimpleName().toCharArray()) {
89 if (Character.isUpperCase(c)) {
90 if (!lastWasDash)
91 s.append('-');
92 lastWasDash = !lastWasDash;
93 s.append(Character.toLowerCase(c));
94 } else {
95 s.append(c);
98 return s.toString();
102 * @return name the command is invoked as from the command line.
104 public String getName() {
105 return name;
109 * @return one line description of the command's feature set.
111 public String getUsage() {
112 return usage;
116 * @return true if this command is considered to be commonly used.
118 public boolean isCommon() {
119 return common;
123 * @return name of the Java class which implements this command.
125 public String getImplementationClassName() {
126 return impl.getName();
130 * @return loader for {@link #getImplementationClassName()}.
132 public ClassLoader getImplementationClassLoader() {
133 return impl.getClassLoader();
137 * @return a new instance of the command implementation.
139 public TextBuiltin create() {
140 final Constructor<? extends TextBuiltin> c;
141 try {
142 c = impl.getDeclaredConstructor();
143 } catch (SecurityException e) {
144 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
145 } catch (NoSuchMethodException e) {
146 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
148 c.setAccessible(true);
150 final TextBuiltin r;
151 try {
152 r = c.newInstance();
153 } catch (InstantiationException e) {
154 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
155 } catch (IllegalAccessException e) {
156 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
157 } catch (IllegalArgumentException e) {
158 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
159 } catch (InvocationTargetException e) {
160 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
162 r.setCommandName(getName());
163 return r;