1 23 24 package org.enhydra.xml.xmlc.metadata; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.lang.reflect.Constructor ; 30 import java.util.HashMap ; 31 32 import org.enhydra.apache.xerces.dom.DOMImplementationImpl; 33 import org.enhydra.apache.xerces.dom.DocumentImpl; 34 import org.enhydra.xml.dom.DOMInfo; 35 import org.enhydra.xml.io.DOMFormatter; 36 import org.enhydra.xml.io.DOMParser; 37 import org.enhydra.xml.io.ErrorReporter; 38 import org.enhydra.xml.io.InputSourceOps; 39 import org.enhydra.xml.io.OutputOptions; 40 import org.enhydra.xml.io.XMLEntityResolver; 41 import org.enhydra.xml.xmlc.XMLCError; 42 import org.enhydra.xml.xmlc.XMLCException; 43 import org.w3c.dom.DOMException ; 44 import org.w3c.dom.DOMImplementation ; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.DocumentType ; 47 import org.w3c.dom.Element ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.SAXException ; 50 51 54 58 public class MetaDataDocument extends DocumentImpl { 59 60 public static final String DOC_TYPE_NAME = "xmlc"; 61 62 63 public static final String PUBLIC_ID 64 = "-//ENHYDRA//DTD XMLC 1.1//EN"; 65 66 67 public static final String SYSTEM_ID 68 = "http://www.enhydra.org/xml/xmlc/xmlc-1.1.dtd"; 69 70 71 private static final String ERR_PARSE_FAILED 72 = "XMLC metadata file parse failed: "; 73 private static final String ERR_WRITE_FAILED 74 = "XMLC metadata file write failed: "; 75 76 79 private static final boolean DEBUG = false; 80 81 84 public static final String METADATA_FILE_EXTENSION = ".xmlc"; 85 86 89 private static HashMap tagClassTable = new HashMap (); 90 91 94 private static final Class [] elemClassConstructorSign 95 = new Class []{Document .class}; 96 97 100 static { 101 tagClassTable.put(JavaCompilerSection.TAG_NAME, JavaCompilerSection.class); 103 tagClassTable.put(CompileOptions.TAG_NAME, CompileOptions.class); 104 tagClassTable.put(InputDocument.TAG_NAME, InputDocument.class); 105 tagClassTable.put(Include.TAG_NAME, Include.class); 106 tagClassTable.put(DeleteElement.TAG_NAME, DeleteElement.class); 107 tagClassTable.put(DOMEdits.TAG_NAME, DOMEdits.class); 108 tagClassTable.put(DocumentClass.TAG_NAME, DocumentClass.class); 109 tagClassTable.put(DocumentSection.TAG_NAME, DocumentSection.class); 110 tagClassTable.put(ElementDef.TAG_NAME, ElementDef.class); 111 tagClassTable.put(HTMLAttr.TAG_NAME, HTMLAttr.class); 112 tagClassTable.put(HTMLTag.TAG_NAME, HTMLTag.class); 113 tagClassTable.put(HTMLTagSet.TAG_NAME, HTMLTagSet.class); 114 tagClassTable.put(HTMLCompatibility.TAG_NAME, HTMLCompatibility.class); 115 tagClassTable.put(HTMLSection.TAG_NAME, HTMLSection.class); 116 tagClassTable.put(Implements.TAG_NAME, Implements.class); 117 tagClassTable.put(JavacOption.TAG_NAME, JavacOption.class); 118 tagClassTable.put(Parser.TAG_NAME, Parser.class); 119 tagClassTable.put(TagClass.TAG_NAME, TagClass.class); 120 tagClassTable.put(URLRegExpMapping.TAG_NAME, URLRegExpMapping.class); 121 tagClassTable.put(URLMapping.TAG_NAME, URLMapping.class); 122 tagClassTable.put(XCatalog.TAG_NAME, XCatalog.class); 123 tagClassTable.put(MetaData.TAG_NAME, MetaData.class); 124 } 125 126 129 static private MetaDataDocument doParseMetaData(InputSource inputSource, 130 ErrorReporter reporter, 131 ClassLoader classLoader) 132 throws XMLCException, IOException , SAXException { 133 134 DOMParser parser = new DOMParser(); 135 XMLEntityResolver resolver = new XMLEntityResolver(); 136 if (DEBUG) { 137 resolver.setDebugWriter(new PrintWriter (System.err, true)); 138 } 139 140 if (classLoader != null) { 141 resolver.addClassLoader(classLoader); 142 } 143 resolver.setDefaultResolving(); 144 parser.setEntityResolver(resolver); 145 146 parser.setErrorHandler(reporter); 147 parser.setDocumentClassName(MetaDataDocument.class.getName()); 148 149 MetaDataDocument document 150 = (MetaDataDocument)parser.parse(inputSource); 151 if (reporter.getErrorCnt() > 0) { 152 throw new XMLCException(ERR_PARSE_FAILED + reporter.getErrorCnt() 153 + " errors, (see log file): " 154 + InputSourceOps.getName(inputSource)); 155 } 156 if (DEBUG) { 157 DOMInfo.printTree("Document after parse: " 158 + InputSourceOps.getName(inputSource), 159 document, System.err); 160 } 161 162 document.completeModifications(); 163 164 if (DEBUG) { 165 DOMInfo.printTree("Document after completeModifications: " + InputSourceOps.getName(inputSource), 166 document, System.err); 167 } 168 return document; 169 } 170 171 179 static public MetaDataDocument parseMetaData(InputSource inputSource, 180 ErrorReporter reporter, 181 ClassLoader classLoader) 182 throws XMLCException { 183 try { 184 return doParseMetaData(inputSource, reporter, classLoader); 185 } catch (IOException except) { 186 throw new XMLCException(ERR_PARSE_FAILED + InputSourceOps.getName(inputSource), 187 except); 188 } catch (SAXException except) { 189 throw new XMLCException(ERR_PARSE_FAILED + InputSourceOps.getName(inputSource), 190 except); 191 } 192 } 193 194 198 public MetaDataDocument() { 199 } 200 201 204 private MetaDataDocument(DocumentType docType) { 205 super(docType); 206 } 207 208 212 public static MetaDataDocument newInstance() { 213 DOMImplementation domImpl = DOMImplementationImpl.getDOMImplementation(); 214 DocumentType docType = domImpl.createDocumentType(DOC_TYPE_NAME, 215 PUBLIC_ID, 216 SYSTEM_ID); 217 return new MetaDataDocument(docType); 218 } 219 220 223 public void serialize(File file) throws XMLCException { 224 if (DEBUG) { 225 DOMInfo.printTree("Document before write: " + file, 226 this, System.err); 227 } 228 OutputOptions outputOptions = new OutputOptions(); 229 outputOptions.setPrettyPrinting(true); 230 outputOptions.setPreserveSpace(false); 231 DOMFormatter domFormatter = new DOMFormatter(outputOptions); 232 233 try { 234 domFormatter.write(this, file); 235 } catch (IOException except) { 236 throw new XMLCException(ERR_WRITE_FAILED + file, except); 237 } 238 } 239 240 245 public File getMetadataOutputFile () { 246 DocumentClass documentClass = getMetaData().getDocumentClass(); 247 String fileName; 248 249 if (documentClass.getGenerate().generateInterface()) { 250 fileName = documentClass.getUnqualInterfaceName() + METADATA_FILE_EXTENSION; 251 } else { 252 fileName = documentClass.getUnqualClassName() + METADATA_FILE_EXTENSION; 253 } 254 255 File metaDataOutputDir = documentClass.getJavaClassSource().getParentFile(); 256 if (metaDataOutputDir == null) { 257 metaDataOutputDir = new File (getMetaData().getCompileOptions().getClassOutputRoot()); 258 String packageName = documentClass.getPackageName(); 259 if (packageName != null) { 260 packageName = packageName.replace('.', File.separatorChar); 261 metaDataOutputDir = new File (getMetaData().getCompileOptions().getClassOutputRoot(), packageName); 262 } 263 } 264 265 metaDataOutputDir.mkdirs(); 266 return new File (metaDataOutputDir, fileName); 267 } 268 269 272 public void serialize() throws XMLCException { 273 DocumentClass documentClass = getMetaData().getDocumentClass(); 274 if (documentClass == null) { 275 throw new XMLCError("No DocumentClass metadata object; can't determine default name"); 276 } 277 278 serialize(getMetadataOutputFile()); 279 } 280 281 284 public MetaData getMetaData() { 285 MetaData root = (MetaData)getDocumentElement(); 286 if (root == null) { 287 root = (MetaData)createElement(MetaData.TAG_NAME); 288 appendChild(root); 289 } 290 return root; 291 } 292 293 296 public Element createElement(Class elementClass) { 297 try { 298 Constructor constr = elementClass.getConstructor(elemClassConstructorSign); 299 return (Element )constr.newInstance(new Object []{this}); 300 } catch (Exception except) { 301 throw new XMLCError(except); 302 } 303 } 304 305 308 public Element createElement(String tagName) throws DOMException { 309 Class elementClass = (Class )tagClassTable.get(tagName); 310 if (elementClass == null) { 311 throw new XMLCError("Invalid tag name: \"" + tagName + "\""); 312 } 313 return createElement(elementClass); 314 } 315 316 319 public Element createElementNS(String namespaceURI, 320 String qualifiedName) { 321 if ((namespaceURI == null) || (namespaceURI.length() == 0)) { 322 return createElement(qualifiedName); 323 } else { 324 throw new XMLCError("createElementNS not implemented"); 326 } 327 } 328 329 334 public void completeModifications() throws XMLCException { 335 getMetaData().completeModifications(); 336 } 337 338 341 public void merge(MetaDataDocument srcDocument) { 342 getMetaData().mergeElement(srcDocument.getMetaData()); 343 } 344 } 345 | Popular Tags |