1 22 23 package org.enhydra.kelp.common.bridge; 24 25 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptionsParser; 27 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions; 28 import org.enhydra.xml.xmlc.commands.options.OptionSet; 29 import org.enhydra.xml.xmlc.commands.options.OptionsParser; 30 import org.enhydra.xml.xmlc.XMLCException; 31 import org.enhydra.xml.xmlc.metadata.DeleteElement; 32 import org.enhydra.xml.xmlc.metadata.CompileOptions; 33 import org.enhydra.xml.xmlc.metadata.DocumentClass; 34 import org.enhydra.xml.xmlc.metadata.DOMEdits; 35 import org.enhydra.xml.xmlc.metadata.MetaData; 36 import org.enhydra.xml.xmlc.metadata.MetaDataDocument; 37 38 import org.enhydra.tool.common.PathHandle; 40 41 import org.enhydra.kelp.common.Constants; 43 import org.enhydra.kelp.common.node.OtterXMLCNode; 44 45 import java.io.File ; 47 import java.io.IOException ; 48 import java.io.PrintWriter ; 49 import java.util.List ; 50 import java.util.Arrays ; 51 52 public class MetaDataHandlerV2 implements MetaDataHandler { 54 55 private final String [] CUSTOM_INPUT_DOC_TYPES = { 57 "chtml", "htm", "html", "xhtml" 58 }; private final String FILE_PROTOCOL = "file:"; 61 private MetaData metaData = null; 63 64 public MetaDataHandlerV2() { 66 MetaDataDocument doc = new MetaDataDocument(); 67 68 metaData = doc.getMetaData(); 69 metaData.setCompileOptions(new CompileOptions(doc)); 70 metaData.setDocumentClass(new DocumentClass(doc)); 71 metaData.setDOMEdits(new DOMEdits(doc)); 72 } 73 74 public boolean getCompileSource() { 76 return metaData.getCompileOptions().getCompileSource(); 77 } 78 79 public void setCompileSource(boolean b) { 80 setCompileSource(metaData, b); 81 } 82 83 private void setCompileSource(MetaData meta, boolean b) { 84 meta.getCompileOptions().setCompileSource(b); 85 } 86 87 public String getDocumentOutput() { 88 return PathHandle.createPathString(metaData.getCompileOptions().getDocumentOutput()); 89 } 90 91 public void setDocumentOutput(String s) { 92 setDocumentOutput(metaData, s); 93 } 94 95 private void setDocumentOutput(MetaData meta, String s) { 96 String path = PathHandle.createPathString(s); 97 98 meta.getCompileOptions().setDocumentOutput(path); 99 } 100 101 public String getInputDocument() { 102 String doc = null; 103 104 doc = metaData.getCompileOptions().getInputDocument(); 105 if (doc.startsWith(FILE_PROTOCOL)) { 106 doc = doc.substring(FILE_PROTOCOL.length()); 107 } 108 return PathHandle.createPathString(doc); 109 } 110 111 public void setInputDocument(String s) { 112 setInputDocument(metaData, s); 113 } 114 115 private void setInputDocument(MetaData meta, String s) { 116 PathHandle handle = null; 117 StringBuffer buf = new StringBuffer (); 118 119 handle = PathHandle.createPathHandle(s); 120 if (!handle.hasExtension(CUSTOM_INPUT_DOC_TYPES)) { 121 buf.append(FILE_PROTOCOL); 122 } 123 buf.append(handle.getPath()); 124 meta.getCompileOptions().setInputDocument(buf.toString()); 125 } 126 127 public boolean getKeepGeneratedSource() { 128 return metaData.getCompileOptions().getKeepGeneratedSource(); 129 } 130 131 public void setKeepGeneratedSource(boolean b) { 132 setKeepGeneratedSource(metaData, b); 133 } 134 135 private void setKeepGeneratedSource(MetaData meta, boolean b) { 136 meta.getCompileOptions().setKeepGeneratedSource(b); 137 } 138 139 public boolean getPrintAccessorInfo() { 140 return metaData.getCompileOptions().getPrintAccessorInfo(); 141 } 142 143 public void setPrintAccessorInfo(boolean b) { 144 setPrintAccessorInfo(metaData, b); 145 } 146 147 private void setPrintAccessorInfo(MetaData meta, boolean b) { 148 meta.getCompileOptions().setPrintAccessorInfo(b); 149 } 150 151 public boolean getPrintDocumentInfo() { 152 return metaData.getCompileOptions().getPrintDocumentInfo(); 153 } 154 155 public void setPrintDocumentInfo(boolean b) { 156 setPrintDocumentInfo(metaData, b); 157 } 158 159 private void setPrintDocumentInfo(MetaData meta, boolean b) { 160 meta.getCompileOptions().setPrintDocumentInfo(b); 161 } 162 163 public boolean getPrintDOM() { 164 return metaData.getCompileOptions().getPrintDOM(); 165 } 166 167 public void setPrintDOM(boolean b) { 168 setPrintDOM(metaData, b); 169 } 170 171 private void setPrintDOM(MetaData meta, boolean b) { 172 meta.getCompileOptions().setPrintDOM(b); 173 } 174 175 public boolean getPrintParseInfo() { 176 return metaData.getCompileOptions().getPrintParseInfo(); 177 } 178 179 public void setPrintParseInfo(boolean b) { 180 setPrintParseInfo(metaData, b); 181 } 182 183 private void setPrintParseInfo(MetaData meta, boolean b) { 184 meta.getCompileOptions().setPrintParseInfo(b); 185 } 186 187 public boolean getPrintVersion() { 188 return metaData.getCompileOptions().getPrintVersion(); 189 } 190 191 public void setPrintVersion(boolean b) { 192 setPrintVersion(metaData, b); 193 } 194 195 private void setPrintVersion(MetaData meta, boolean b) { 196 meta.getCompileOptions().setPrintVersion(b); 197 } 198 199 public boolean getVerbose() { 200 return metaData.getCompileOptions().getVerbose(); 201 } 202 203 public void setVerbose(boolean b) { 204 setVerbose(metaData, b); 205 } 206 207 private void setVerbose(MetaData meta, boolean b) { 208 meta.getCompileOptions().setVerbose(b); 209 } 210 211 public String getClassName() { 213 return metaData.getDocumentClass().getName(); 214 } 215 216 public void setClassName(String n) { 217 setClassName(metaData, n); 218 } 219 220 private void setClassName(MetaData meta, String n) { 221 meta.getDocumentClass().setName(n); 222 } 223 224 public File getJavaClassSource() { 225 return metaData.getDocumentClass().getJavaClassSource(); 226 } 227 228 public void setJavaClassSource(File f, OtterXMLCNode node) { 229 setJavaClassSource(metaData, f, node); 230 } 231 232 private void setJavaClassSource(MetaData meta, File f, 233 OtterXMLCNode node) { 234 String outputRoot = node.getGenerateToRoot(); 235 String path = f.getAbsolutePath(); 236 MetaDataDocument doc = meta.getDocument(); 237 238 outputRoot = outputRoot.replace('\\', '/').trim(); 239 path = path.replace('\\', '/').trim(); 240 String name = pathToClass(outputRoot, path); 241 242 meta.getDocumentClass().setName(name); 243 meta.getCompileOptions().setClassOutputRoot(outputRoot); 244 meta.getCompileOptions().setSourceOutputRoot(outputRoot); 245 try { 246 doc.completeModifications(); 247 } catch (XMLCException e) { 248 e.printStackTrace(); 249 } 250 } 251 252 public File getJavaInterfaceSource() { 253 return metaData.getDocumentClass().getJavaInterfaceSource(); 254 } 255 256 public void setJavaInterfaceSource(File f, OtterXMLCNode node) { 257 setJavaInterfaceSource(metaData); 258 } 259 260 private void setJavaInterfaceSource(MetaData meta) { 261 262 try { 264 meta.getDocument().completeModifications(); 265 } catch (XMLCException e) { 266 e.printStackTrace(); 267 } 268 } 269 270 public String getPackageName() { 271 String name = null; 272 273 try { 274 name = metaData.getDocumentClass().getPackageName(); 275 } catch (NullPointerException e) { 276 277 name = null; 279 } 280 return name; 281 } 282 283 public boolean getRecompilation() { 284 return metaData.getDocumentClass().getRecompilation(); 285 } 286 287 public void setRecompilation(boolean b) { 288 setRecompilation(metaData, b); 289 } 290 291 private void setRecompilation(MetaData meta, boolean b) { 292 meta.getDocumentClass().setRecompilation(b); 293 } 294 295 public Object [] getDeleteElements() { 297 return metaData.getDOMEdits().getDeleteElements(); 298 } 299 300 private void mergeDeleteElements(MetaData meta, 301 DeleteElement[] elements) { 302 List eList = Arrays.asList(meta.getDOMEdits().getDeleteElements()); 303 304 for (int i = 0; i < elements.length; i++) { 305 if (!eList.contains(elements[i])) { 306 meta.getDOMEdits().addDeleteElement(elements[i]); 307 } 308 } 309 } 310 311 public Object [] getURLMappings() { 312 return metaData.getDOMEdits().getURLMappings(); 313 } 314 315 public Object getMetaData() { 317 return metaData; 318 } 319 320 public void parse(String [] files, String [] args, PrintWriter writer, 321 OtterXMLCNode node) throws XMLCException, IOException { 322 XMLCOptions opParser = null; 323 Reporter reporter = null; 324 String [] parseArgs = new String [files.length + args.length + 1]; 325 MetaData newMeta = null; 326 327 reporter = new Reporter(writer); 328 for (int i = 0; i < args.length; i++) { 329 parseArgs[i] = args[i]; 330 } 331 for (int i = args.length; i < (args.length + files.length); i++) { 332 parseArgs[i] = files[(i - args.length)]; 333 } 334 metaData.getDocument().completeModifications(); 335 if (parseArgs.length > 1) { 336 parseArgs[parseArgs.length - 1] = node.getFilePath().replace('\\', 337 '/'); 338 try { 339 opParser = new XMLCOptions(); 340 newMeta = opParser.parse(parseArgs, reporter); 341 mergeMetaData(newMeta, node); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 if (e instanceof XMLCException) { 345 throw (XMLCException) e; 346 } else if (e instanceof IOException ) { 347 throw (IOException ) e; 348 } else { 349 throw new XMLCException(e); 350 } 351 } 352 } 353 } 354 355 private void mergeMetaData(MetaData newMeta, OtterXMLCNode node) { 356 357 setKeepGeneratedSource(newMeta, getKeepGeneratedSource()); 359 setPrintAccessorInfo(newMeta, getPrintAccessorInfo()); 360 setPrintDOM(newMeta, getPrintDOM()); 361 setPrintParseInfo(newMeta, getPrintParseInfo()); 362 setPrintVersion(newMeta, getPrintVersion()); 363 setRecompilation(newMeta, getRecompilation()); 364 setVerbose(newMeta, getVerbose()); 365 366 setClassName(newMeta, getClassName()); 368 setCompileSource(newMeta, getCompileSource()); 369 mergeDeleteElements(newMeta, (DeleteElement[]) getDeleteElements()); 370 setDocumentOutput(newMeta, getDocumentOutput()); 371 setInputDocument(newMeta, getInputDocument().replace('\\', '/')); 372 setJavaClassSource(newMeta, getJavaClassSource(), node); 373 setJavaInterfaceSource(newMeta); 374 this.metaData = newMeta; 375 } 376 377 public void save(File op) throws IOException { 378 try { 379 metaData.getDocument().serialize(op); 380 } catch (XMLCException e) { 381 e.printStackTrace(); 382 } 383 } 384 385 private String pathToClass(String outputRoot, String in) { 387 String out = in; 388 int index = 0; 389 390 if (out.startsWith(outputRoot)) { 391 out = out.substring(outputRoot.length()); 392 } 393 index = out.lastIndexOf('.' + Constants.TYPE_JAVA); 394 if (index > -1) { 395 out = out.substring(0, index); 396 } 397 out = out.replace('\\', '.'); 398 out = out.replace('/', '.'); 399 while ((out.length() > 0) && (out.charAt(0) == '.')) { 400 out = out.substring(1); 401 } 402 while ((out.length() > 0) && (out.charAt(out.length()-1) == '.')) { 403 out = out.substring(0, out.length() - 1); 404 } 405 return out; 406 } 407 408 } 409 | Popular Tags |