1 package org.objectweb.celtix.tools; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Properties ; 10 import java.util.Set ; 11 import java.util.StringTokenizer ; 12 import java.util.logging.Logger ; 13 14 import org.objectweb.celtix.common.logging.LogUtils; 15 import org.objectweb.celtix.tools.common.ToolConstants; 16 import org.objectweb.celtix.tools.common.ToolException; 17 import org.objectweb.celtix.tools.common.toolspec.AbstractToolContainer; 18 import org.objectweb.celtix.tools.common.toolspec.ToolSpec; 19 import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException; 20 import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument; 21 import org.objectweb.celtix.tools.common.toolspec.parser.CommandLineParser; 22 import org.objectweb.celtix.tools.common.toolspec.parser.ErrorVisitor; 23 import org.objectweb.celtix.version.Version; 24 25 public abstract class AbstractCeltixToolContainer extends AbstractToolContainer { 26 protected static final Logger LOG = LogUtils.getL7dLogger(AbstractCeltixToolContainer.class); 27 private static AbstractCeltixToolContainer instance; 28 29 private final String name; 30 private CommandDocument commandDocument; 31 private boolean verbose; 32 private String usage; 33 private final ErrorVisitor errors = new ErrorVisitor(); 34 35 36 public AbstractCeltixToolContainer(String nm, ToolSpec toolspec) throws Exception { 37 super(toolspec); 38 name = nm; 39 instance = this; 40 } 41 42 public static AbstractCeltixToolContainer getInstance() { 43 return instance; 44 } 45 public boolean hasInfoOption() throws ToolException { 46 boolean result = false; 47 commandDocument = getCommandDocument(); 48 if ((commandDocument.hasParameter("help")) || (commandDocument.hasParameter("version"))) { 49 result = true; 50 } 51 return result; 52 } 53 54 public void execute(boolean exitOnFinish) throws ToolException { 55 if (hasInfoOption()) { 56 outputInfo(); 57 } else { 58 if (commandDocument.hasParameter("verbose")) { 59 verbose = true; 60 outputFullCommandLine(); 61 outputVersion(); 62 63 } 64 checkParams(errors); 65 } 66 } 67 68 private void outputInfo() { 69 CommandLineParser parser = getCommandLineParser(); 70 71 if (commandDocument.hasParameter("help")) { 72 try { 73 System.out.println(name + " " + getUsage()); 74 System.out.println(); 75 System.out.println("Options : "); 76 System.out.println(); 77 System.out.println(parser.getDetailedUsage()); 78 String toolUsage = parser.getToolUsage(); 79 80 if (toolUsage != null) { 81 System.out.println(toolUsage); 82 System.out.println(); 83 } 84 } catch (Exception ex) { 85 System.err.println("Error : Could not output detailed usage"); 86 System.err.println(); 87 } 88 } 89 if (commandDocument.hasParameter("version")) { 90 outputVersion(); 91 } 92 } 93 94 public abstract void checkParams(ErrorVisitor err) throws ToolException; 95 96 public boolean isVerboseOn() { 97 return verbose; 98 } 99 100 public String getToolName() { 101 return name; 102 } 103 104 public String getUsage() { 105 if (usage == null) { 106 try { 107 CommandLineParser parser = getCommandLineParser(); 108 109 if (parser != null) { 110 usage = parser.getUsage(); 111 } 112 } catch (Exception ex) { 113 usage = "Could not get usage for the tool"; 114 } 115 } 116 return usage; 117 } 118 119 public void outputVersion() { 120 System.out.println(name + " - " + Version.getCompleteVersionString()); 121 System.out.println(); 122 } 123 124 public void outputFullCommandLine() { 125 System.out.print(name); 126 for (int i = 0; i < getArgument().length; i++) { 127 System.out.print(" " + getArgument()[i]); 128 } 129 System.out.println(); 130 } 131 132 public String getFileBase(String wsdlUrl) { 133 String fileBase = wsdlUrl; 134 StringTokenizer tok = new StringTokenizer (wsdlUrl, "\\/"); 135 136 while (tok.hasMoreTokens()) { 137 fileBase = tok.nextToken(); 138 } 139 if (fileBase.endsWith(".wsdl")) { 140 fileBase = new String (fileBase.substring(0, fileBase.length() - 5)); 141 } 142 return fileBase; 143 } 144 145 public void printUsageException(String toolName, BadUsageException ex) { 146 if (getInstance().verbose) { 147 getInstance().outputFullCommandLine(); 148 } 149 System.err.println(ex.getMessage()); 150 System.err.println("Usage : " + toolName + " " + ex.getUsage()); 151 if (getInstance().verbose) { 152 getInstance().outputVersion(); 153 } 154 System.err.println(); 155 } 156 157 public String getFileName(String loc) { 158 int idx = loc.lastIndexOf("/"); 159 160 if (idx != -1) { 161 loc = loc.substring(idx + 1); 162 } 163 idx = loc.lastIndexOf("\\"); 164 if (idx != -1) { 165 loc = loc.substring(idx + 1); 166 } 167 168 idx = loc.lastIndexOf("."); 169 if (idx != -1) { 170 loc = loc.substring(0, idx); 171 } 172 173 StringTokenizer strToken = new StringTokenizer (loc, "-.!~*'();?:@&=+$,"); 174 StringBuffer strBuf = new StringBuffer (); 175 176 if (!strToken.hasMoreTokens()) { 177 strBuf.append(loc); 178 } 179 180 while (strToken.hasMoreTokens()) { 181 strBuf.append(strToken.nextToken()); 182 if (strToken.countTokens() != 0) { 183 strBuf.append("_"); 184 } 185 } 186 187 return strBuf.toString(); 188 } 189 190 private InputStream getResourceAsStream(String resource) { 191 ClassLoader cl = AbstractCeltixToolContainer.class.getClassLoader(); 192 InputStream ins = cl.getResourceAsStream(resource); 193 if (ins == null && resource.startsWith("/")) { 194 ins = cl.getResourceAsStream(resource.substring(1)); 195 } 196 return ins; 197 } 198 199 public Properties loadProperties(String propertyFile) { 200 Properties p = new Properties (); 201 202 try { 203 InputStream ins = getResourceAsStream(ToolConstants.TOOLSPECS_BASE + propertyFile); 204 205 p.load(ins); 206 ins.close(); 207 } catch (IOException ex) { 208 } 210 return p; 211 } 212 213 protected String [] getDefaultExcludedNamespaces(String excludeProps) { 214 List <String > result = new ArrayList <String >(); 215 Properties props = loadProperties(excludeProps); 216 java.util.Enumeration nexcludes = props.propertyNames(); 217 218 while (nexcludes.hasMoreElements()) { 219 result.add(props.getProperty((String )nexcludes.nextElement())); 220 } 221 return result.toArray(new String [result.size()]); 222 } 223 224 228 protected Map <String , Object > getParametersMap(Set stringArrayKeys) { 229 Map <String , Object > map = new HashMap <String , Object >(); 230 CommandDocument doc = getCommandDocument(); 231 String [] keys = doc.getParameterNames(); 232 if (keys == null) { 233 return map; 234 } 235 for (int i = 0; i < keys.length; i++) { 236 if (stringArrayKeys.contains(keys[i])) { 237 map.put(keys[i], doc.getParameters(keys[i])); 238 } else { 239 map.put(keys[i], doc.getParameter(keys[i])); 240 } 241 } 242 return map; 243 } 244 } 245 | Popular Tags |