1 16 17 package de.gulden.application.beautyj; 18 19 import de.gulden.framework.amoda.environment.commandline.*; 20 import de.gulden.framework.amoda.generic.core.*; 21 import de.gulden.framework.amoda.generic.option.*; 22 import de.gulden.framework.amoda.generic.data.GenericValue; 23 import de.gulden.framework.amoda.model.core.ApplicationEnvironment; 24 import de.gulden.framework.amoda.model.data.*; 25 import de.gulden.framework.amoda.model.option.*; 26 import de.gulden.util.javasource.LogPerformer; 27 import de.gulden.util.javasource.sourclet.*; 28 import de.gulden.util.javasource.jjt.ParseException; 29 import java.io.*; 30 import java.util.*; 31 32 46 public class BeautyJ extends CommandLineApplication implements SourcletOptions, LogPerformer { 47 48 52 55 public static final String OPTION_SOURCLET = "sourclet"; 56 57 60 public static final String OPTION_INPUT = "input"; 61 62 65 public static final String OPTION_OUTDIR = "output-directory"; 66 67 70 public static final String OPTION_XML_INPUT = "xml.in"; 71 72 75 public static final String OPTION_XML_VALIDATE = "xml.validate"; 76 77 80 public static final String OPTION_XML_OUTPUT = "xml.out"; 81 82 85 public static final String OPTION_XML_DOCTYPE = "xml.doctype"; 86 87 90 public static final String MESSAGE_SUCCESS = "message.success"; 91 92 95 public static final String MESSAGE_XML_CREATED = "message.xmlcreated"; 96 97 98 102 107 protected HashMap multiValueCache; 108 109 110 114 public void init(ApplicationEnvironment env) { 115 super.init(env); 116 if (getOptions().getBoolean("gui")) { 117 ((GenericOptionEntry)getOptions().getOptionEntry("input")).setSystem(false); } 119 } 120 121 129 public void perform() { 130 Sourclet sourclet=null; 132 Value[] params=getEnvironment().getInputValues(); 133 File input=getOptions().getFile(OPTION_INPUT); 134 File outDir=getOptions().getFile(OPTION_OUTDIR); 135 File xmlIn=getOptions().getFile(OPTION_XML_INPUT); 136 File xmlOut=getOptions().getFile(OPTION_XML_OUTPUT); 137 multiValueCache = new HashMap(); 138 139 if (xmlOut==null) { if (outDir==null) { 141 error("output directory must be specified" + (getOptions().getBoolean("gui") ? "" : " using option '-d'"),null,true); 142 } 143 if (!outDir.isDirectory()) { 144 error(outDir.getAbsolutePath()+" is not a valid output directory.",null,true); 145 } 146 sourclet = (Sourclet)getOptions().getClassInstance(OPTION_SOURCLET); 147 sourclet.init(this); 148 } else { if (outDir!=null) { 150 error("output directory cannot be specified when using XML output",null,true); 151 } 152 } 153 154 try { 155 de.gulden.util.javasource.Package pakkage=null; 156 de.gulden.util.javasource.SourceParser.verbose= this.isVerbose() || this.getOptions().getBoolean("gui"); 157 de.gulden.util.javasource.SourceParser.logPerformer = this; 158 boolean test = this.getOptions().getBoolean("test"); 159 File[] inFiles = null; 160 if (xmlIn!=null) { if (params.length==0 && input==null) { 162 InputStream in; 163 if (!xmlIn.equals(new File(""))) { 164 in=new FileInputStream(xmlIn); 165 } else { 166 in=System.in; 167 } 168 de.gulden.util.javasource.SourceParser.validateXML=isOption(OPTION_XML_VALIDATE); 169 pakkage=de.gulden.util.javasource.SourceParser.parseXML(in); 170 if (in instanceof FileInputStream) { 171 in.close(); 172 } 173 } else { 174 error("input files or directories cannot be specified when using XML input",null,true); 175 } 176 } else { if (input != null) { 178 inFiles = new File[] {input}; 179 } else if (params.length>0) { 180 inFiles=new File[params.length]; 181 for (int i=0;i<inFiles.length;i++) { 182 inFiles[i]=params[i].getFile(); 183 } 184 } else { 185 error("nothing to do - please specify at least one input file or directory",null,true); 186 } 187 if (inFiles != null) { 188 pakkage=de.gulden.util.javasource.SourceParser.parse(inFiles,null); 189 } 190 } 191 192 if (!test) { 193 if (xmlOut==null) { 194 if (pakkage != null) { 195 de.gulden.util.javasource.SourceParser.buildSource(pakkage, outDir, inFiles, sourclet); getMessage(MESSAGE_SUCCESS).perform(); 197 } 198 } else { de.gulden.util.javasource.SourceParser.includeXMLDoctype=isOption(OPTION_XML_DOCTYPE); 200 org.w3c.dom.Document doc=de.gulden.util.javasource.SourceParser.buildXML(pakkage); 201 OutputStream out; 202 if (!xmlOut.equals(new File(""))) { 203 out=new FileOutputStream(xmlOut); 204 } else { 205 out=System.out; 206 } 207 if ((out instanceof FileOutputStream)&&this.isVerbose()) { 208 System.out.println("writing "+xmlOut.getAbsolutePath()); 209 } 210 org.apache.xml.serialize.OutputFormat outputFormat=new org.apache.xml.serialize.OutputFormat(); 211 outputFormat.setIndenting(true); 212 outputFormat.setPreserveSpace(true); 213 org.apache.xml.serialize.XMLSerializer serializer=new org.apache.xml.serialize.XMLSerializer(out,outputFormat); 214 org.apache.xml.serialize.DOMSerializer domSerializer=serializer.asDOMSerializer(); 215 domSerializer.serialize(doc); 216 if (out instanceof FileOutputStream) { 217 out.close(); 218 } 219 getMessage(MESSAGE_XML_CREATED).perform(); 220 } 221 } 222 } catch (IOException ioe) { 223 error("an i/o exception occurred",ioe,true); 224 } catch (ParseException pe) { 225 error("Java parser error",pe,true); 226 } catch (org.xml.sax.SAXException se) { 227 error("XML parser error",se,true); 228 } 229 } 230 231 234 public boolean isQuiet() { 235 File xmlOut=getOptions().getFile(OPTION_XML_OUTPUT); 236 boolean xml_to_stdout=(xmlOut!=null)&&(xmlOut.equals(new File(""))); 237 return super.isQuiet()||xml_to_stdout; 239 } 240 241 246 public String getOption(String name) { 247 return getOptions().getString(name); 248 } 249 250 255 public int getIntOption(String name) { 256 return getOptions().getInt(name); 257 } 258 259 264 public boolean isOption(String name) { 265 return getOptions().getBoolean(name); 266 } 267 268 274 public boolean isOption(String name, String value) { 275 return getOptions().getBoolean(name); 276 } 277 278 285 public boolean hasOption(String name, String value) { 286 TreeSet cached = (TreeSet)multiValueCache.get(name); 287 if (cached == null) { 288 cached = new TreeSet(); 289 String s = getOption(name); 290 if (s != null) { 291 StringTokenizer st = new StringTokenizer(s, ",", false); 292 while (st.hasMoreTokens()) { 293 cached.add(st.nextToken().trim()); 294 } 295 } 296 multiValueCache.put(name, cached); 297 } 298 return cached.contains(value); 299 } 300 301 } | Popular Tags |