001: package org.objectweb.celtix.tools;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Properties;
010: import java.util.Set;
011: import java.util.StringTokenizer;
012: import java.util.logging.Logger;
013:
014: import org.objectweb.celtix.common.logging.LogUtils;
015: import org.objectweb.celtix.tools.common.ToolConstants;
016: import org.objectweb.celtix.tools.common.ToolException;
017: import org.objectweb.celtix.tools.common.toolspec.AbstractToolContainer;
018: import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
019: import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException;
020: import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument;
021: import org.objectweb.celtix.tools.common.toolspec.parser.CommandLineParser;
022: import org.objectweb.celtix.tools.common.toolspec.parser.ErrorVisitor;
023: import org.objectweb.celtix.version.Version;
024:
025: public abstract class AbstractCeltixToolContainer extends
026: AbstractToolContainer {
027: protected static final Logger LOG = LogUtils
028: .getL7dLogger(AbstractCeltixToolContainer.class);
029: private static AbstractCeltixToolContainer instance;
030:
031: private final String name;
032: private CommandDocument commandDocument;
033: private boolean verbose;
034: private String usage;
035: private final ErrorVisitor errors = new ErrorVisitor();
036:
037: public AbstractCeltixToolContainer(String nm, ToolSpec toolspec)
038: throws Exception {
039: super (toolspec);
040: name = nm;
041: instance = this ;
042: }
043:
044: public static AbstractCeltixToolContainer getInstance() {
045: return instance;
046: }
047:
048: public boolean hasInfoOption() throws ToolException {
049: boolean result = false;
050: commandDocument = getCommandDocument();
051: if ((commandDocument.hasParameter("help"))
052: || (commandDocument.hasParameter("version"))) {
053: result = true;
054: }
055: return result;
056: }
057:
058: public void execute(boolean exitOnFinish) throws ToolException {
059: if (hasInfoOption()) {
060: outputInfo();
061: } else {
062: if (commandDocument.hasParameter("verbose")) {
063: verbose = true;
064: outputFullCommandLine();
065: outputVersion();
066:
067: }
068: checkParams(errors);
069: }
070: }
071:
072: private void outputInfo() {
073: CommandLineParser parser = getCommandLineParser();
074:
075: if (commandDocument.hasParameter("help")) {
076: try {
077: System.out.println(name + " " + getUsage());
078: System.out.println();
079: System.out.println("Options : ");
080: System.out.println();
081: System.out.println(parser.getDetailedUsage());
082: String toolUsage = parser.getToolUsage();
083:
084: if (toolUsage != null) {
085: System.out.println(toolUsage);
086: System.out.println();
087: }
088: } catch (Exception ex) {
089: System.err
090: .println("Error : Could not output detailed usage");
091: System.err.println();
092: }
093: }
094: if (commandDocument.hasParameter("version")) {
095: outputVersion();
096: }
097: }
098:
099: public abstract void checkParams(ErrorVisitor err)
100: throws ToolException;
101:
102: public boolean isVerboseOn() {
103: return verbose;
104: }
105:
106: public String getToolName() {
107: return name;
108: }
109:
110: public String getUsage() {
111: if (usage == null) {
112: try {
113: CommandLineParser parser = getCommandLineParser();
114:
115: if (parser != null) {
116: usage = parser.getUsage();
117: }
118: } catch (Exception ex) {
119: usage = "Could not get usage for the tool";
120: }
121: }
122: return usage;
123: }
124:
125: public void outputVersion() {
126: System.out.println(name + " - "
127: + Version.getCompleteVersionString());
128: System.out.println();
129: }
130:
131: public void outputFullCommandLine() {
132: System.out.print(name);
133: for (int i = 0; i < getArgument().length; i++) {
134: System.out.print(" " + getArgument()[i]);
135: }
136: System.out.println();
137: }
138:
139: public String getFileBase(String wsdlUrl) {
140: String fileBase = wsdlUrl;
141: StringTokenizer tok = new StringTokenizer(wsdlUrl, "\\/");
142:
143: while (tok.hasMoreTokens()) {
144: fileBase = tok.nextToken();
145: }
146: if (fileBase.endsWith(".wsdl")) {
147: fileBase = new String(fileBase.substring(0, fileBase
148: .length() - 5));
149: }
150: return fileBase;
151: }
152:
153: public void printUsageException(String toolName,
154: BadUsageException ex) {
155: if (getInstance().verbose) {
156: getInstance().outputFullCommandLine();
157: }
158: System.err.println(ex.getMessage());
159: System.err.println("Usage : " + toolName + " " + ex.getUsage());
160: if (getInstance().verbose) {
161: getInstance().outputVersion();
162: }
163: System.err.println();
164: }
165:
166: public String getFileName(String loc) {
167: int idx = loc.lastIndexOf("/");
168:
169: if (idx != -1) {
170: loc = loc.substring(idx + 1);
171: }
172: idx = loc.lastIndexOf("\\");
173: if (idx != -1) {
174: loc = loc.substring(idx + 1);
175: }
176:
177: idx = loc.lastIndexOf(".");
178: if (idx != -1) {
179: loc = loc.substring(0, idx);
180: }
181:
182: StringTokenizer strToken = new StringTokenizer(loc,
183: "-.!~*'();?:@&=+$,");
184: StringBuffer strBuf = new StringBuffer();
185:
186: if (!strToken.hasMoreTokens()) {
187: strBuf.append(loc);
188: }
189:
190: while (strToken.hasMoreTokens()) {
191: strBuf.append(strToken.nextToken());
192: if (strToken.countTokens() != 0) {
193: strBuf.append("_");
194: }
195: }
196:
197: return strBuf.toString();
198: }
199:
200: private InputStream getResourceAsStream(String resource) {
201: ClassLoader cl = AbstractCeltixToolContainer.class
202: .getClassLoader();
203: InputStream ins = cl.getResourceAsStream(resource);
204: if (ins == null && resource.startsWith("/")) {
205: ins = cl.getResourceAsStream(resource.substring(1));
206: }
207: return ins;
208: }
209:
210: public Properties loadProperties(String propertyFile) {
211: Properties p = new Properties();
212:
213: try {
214: InputStream ins = getResourceAsStream(ToolConstants.TOOLSPECS_BASE
215: + propertyFile);
216:
217: p.load(ins);
218: ins.close();
219: } catch (IOException ex) {
220: // ignore, use defaults
221: }
222: return p;
223: }
224:
225: protected String[] getDefaultExcludedNamespaces(String excludeProps) {
226: List<String> result = new ArrayList<String>();
227: Properties props = loadProperties(excludeProps);
228: java.util.Enumeration nexcludes = props.propertyNames();
229:
230: while (nexcludes.hasMoreElements()) {
231: result.add(props.getProperty((String) nexcludes
232: .nextElement()));
233: }
234: return result.toArray(new String[result.size()]);
235: }
236:
237: /**
238: * get all parameters in a map
239: * @param stringArrayKeys, contains keys, whose value should be string array
240: */
241: protected Map<String, Object> getParametersMap(Set stringArrayKeys) {
242: Map<String, Object> map = new HashMap<String, Object>();
243: CommandDocument doc = getCommandDocument();
244: String[] keys = doc.getParameterNames();
245: if (keys == null) {
246: return map;
247: }
248: for (int i = 0; i < keys.length; i++) {
249: if (stringArrayKeys.contains(keys[i])) {
250: map.put(keys[i], doc.getParameters(keys[i]));
251: } else {
252: map.put(keys[i], doc.getParameter(keys[i]));
253: }
254: }
255: return map;
256: }
257: }
|