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