1 23 24 package org.enhydra.xml.xmlc.dom.generic; 25 26 import org.enhydra.xml.xmlc.XMLCError; 27 import org.enhydra.xml.xmlc.XMLCException; 28 import org.enhydra.xml.xmlc.codegen.JavaCode; 29 import org.enhydra.xml.xmlc.codegen.JavaLang; 30 import org.enhydra.xml.xmlc.compiler.ClassGenerator; 31 import org.enhydra.xml.xmlc.dom.XMLCDocument; 32 import org.w3c.dom.Attr ; 33 import org.w3c.dom.CDATASection ; 34 import org.w3c.dom.Comment ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.DocumentType ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Entity ; 39 import org.w3c.dom.EntityReference ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.Notation ; 42 import org.w3c.dom.ProcessingInstruction ; 43 import org.w3c.dom.Text ; 44 45 51 public class NodeCreateGenerator { 52 53 private Document fDocument; 54 55 56 private Element fDocElement; 57 58 59 private static final String ENTITIES_UNSUPPORTED 60 = "creating entity nodes not implemented for this DOM"; 61 private static final String NOTATION_UNSUPPORTED 62 = "creating notation nodes not implemented for this DOM"; 63 64 67 public NodeCreateGenerator(XMLCDocument xmlcDoc) { 68 fDocument = xmlcDoc.getDocument(); 69 fDocElement = fDocument.getDocumentElement(); 70 } 71 72 73 protected boolean isDocElement(Element element) { 74 return (element == fDocElement); 75 } 76 77 80 protected void genDocumentCreate(Document document, 81 String docVar, 82 String docTypeVar, 83 JavaCode body) { 84 DocumentType docType = document.getDoctype(); 85 String docTypeRef; 86 if (docType == null) { 87 docTypeRef = "null"; 88 } else { 89 docTypeRef = docTypeVar; 90 genDocumentTypeCreate(docType, docVar, docTypeVar, body); 91 } 92 93 String docClass = document.getClass().getName(); 94 body.addln(docClass + " " + docVar + " = (" + docClass + ")" 95 + ClassGenerator.DOM_FACTORY_FIELD_NAME + ".createDocument(" 96 + JavaLang.createStringConst(fDocElement.getNamespaceURI()) + ", " 97 + JavaLang.createStringConst(fDocElement.getNodeName()) + ", " 98 + docTypeRef + ");"); 99 } 100 101 104 protected void genDocumentTypeCreate(DocumentType docType, 105 String docVar, 106 String varName, 107 JavaCode body) { 108 String docTypeClass = docType.getClass().getName(); 109 body.addln(docTypeClass + " " + varName 110 + " = (" + docTypeClass + ")" 111 + ClassGenerator.DOM_FACTORY_FIELD_NAME 112 + ".createDocumentType(" 113 + JavaLang.createStringConst(docType.getNodeName()) + ", " 114 + JavaLang.createStringConst(docType.getPublicId()) + ", " 115 + JavaLang.createStringConst(docType.getSystemId()) + ", " 116 + JavaLang.createStringConst(docType.getInternalSubset()) + ");"); 117 } 118 119 122 protected void genEntityCreate(Entity entity, 123 String docVar, 124 String varName, 125 JavaCode body) { 126 throw new XMLCError(ENTITIES_UNSUPPORTED); 127 } 128 129 132 protected void genAttrCreate(Attr attr, 133 String docVar, 134 String varName, 135 JavaCode body) { 136 body.add(varName + " = "); 137 if (attr.getNamespaceURI() == null) { 138 body.addln(docVar + ".createAttribute(" 139 + JavaLang.createStringConst(attr.getName()) + ");"); 140 } else { 141 body.addln(docVar + ".createAttributeNS(" 142 + JavaLang.createStringConst(attr.getNamespaceURI()) + ", " 143 + JavaLang.createStringConst(attr.getName()) + ");"); 144 } 145 } 146 147 150 protected void genCDATASectionCreate(CDATASection cdataSection, 151 String docVar, 152 String varName, 153 JavaCode body) { 154 body.addln(varName + " = " + docVar + ".createCDATASection(" 155 + JavaLang.createStringConst(cdataSection.getData()) + ");"); 156 } 157 158 161 protected void genCommentCreate(Comment comment, 162 String docVar, 163 String varName, 164 JavaCode body) { 165 body.addln(varName + " = " + docVar + ".createComment(" 166 + JavaLang.createStringConst(comment.getData()) + ");"); 167 } 168 169 172 protected void genElementCreate(Element element, 173 String docVar, 174 String varName, 175 JavaCode body) { 176 body.add(varName + " = "); 177 if (isDocElement(element)) { 178 body.addln(docVar + ".getDocumentElement();"); 181 } if (element.getNamespaceURI() == null) { 182 body.addln(docVar + ".createElement(" 183 + JavaLang.createStringConst(element.getTagName()) + ");"); 184 } else { 185 body.addln(docVar + ".createElementNS(" 186 + JavaLang.createStringConst(element.getNamespaceURI()) + ", " 187 + JavaLang.createStringConst(element.getTagName()) + ");"); 188 } 189 } 190 191 194 protected void genEntityReferenceCreate(EntityReference entityRef, 195 String docVar, 196 String varName, 197 JavaCode body) { 198 body.addln(varName + " = " + docVar 199 + ".createEntityReference(" 200 + JavaLang.createStringConst(entityRef.getNodeName()) 201 + ");"); 202 } 203 204 207 protected void genNotationCreate(Notation notation, 208 String docVar, 209 String varName, 210 JavaCode body) { 211 throw new XMLCError(NOTATION_UNSUPPORTED); 212 } 213 214 217 protected void genProcessInstructionCreate(ProcessingInstruction procInstr, 218 String docVar, 219 String varName, 220 JavaCode body) { 221 body.addln(varName + " = " + docVar 222 + ".createProcessingInstruction(" 223 + JavaLang.createStringConst(procInstr.getNodeName()) + ", " 224 + JavaLang.createStringConst(procInstr.getData()) + ");"); 225 } 226 227 230 protected void genTextCreate(Text text, 231 String docVar, 232 String varName, 233 JavaCode body) { 234 body.addln(varName + " = " + docVar + ".createTextNode(" 235 + JavaLang.createStringConst(text.getData()) + ");"); 236 } 237 238 242 public void genNodeCreate(String docVar, 243 String varName, 244 Node node, 245 JavaCode body) throws XMLCException { 246 switch (node.getNodeType()) { 247 case Node.DOCUMENT_NODE: 248 case Node.DOCUMENT_TYPE_NODE: 249 throw new XMLCError("Creating node of this type must be done directly: " 250 + node.getClass().getName()); 251 252 case Node.ATTRIBUTE_NODE: 253 genAttrCreate((Attr )node, docVar, varName, body); 254 break; 255 256 case Node.CDATA_SECTION_NODE: 257 genCDATASectionCreate((CDATASection )node, docVar, varName, 258 body); 259 break; 260 261 case Node.COMMENT_NODE: 262 genCommentCreate((Comment )node, docVar, varName, body); 263 break; 264 265 case Node.ELEMENT_NODE: 266 genElementCreate((Element )node, docVar, varName, body); 267 break; 268 269 case Node.ENTITY_NODE: 270 genEntityCreate((Entity )node, docVar, varName, body); 271 break; 272 273 case Node.ENTITY_REFERENCE_NODE: 274 genEntityReferenceCreate((EntityReference )node, docVar, varName, 275 body); 276 break; 277 278 case Node.NOTATION_NODE: 279 genNotationCreate((Notation )node, docVar, varName, body); 280 break; 281 282 case Node.PROCESSING_INSTRUCTION_NODE: 283 genProcessInstructionCreate((ProcessingInstruction )node, 284 docVar, varName, body); 285 break; 286 287 case Node.TEXT_NODE: 288 genTextCreate((Text )node, docVar, varName, body); 289 break; 290 291 case Node.DOCUMENT_FRAGMENT_NODE: 292 throw new XMLCError("Creating node of this type not supported: " 293 + node.getClass().getName()); 294 295 default: 296 throw new XMLCError("Unknown node type: " + node.getNodeType()); 297 } 298 } 299 300 304 private boolean preceedsDocElement(Node node) { 305 for (Node sibling = node.getNextSibling(); sibling != null; 307 sibling = sibling.getNextSibling()) { 308 if (sibling == fDocElement) { 309 return true; } 311 } 312 return false; 313 } 314 315 318 private void genAppendDocumentChild(String parentVar, 319 String childVar, 320 Node child, 321 JavaCode body) { 322 if (child != fDocElement) { 324 if (preceedsDocElement(child)) { 326 body.addln(parentVar + ".insertBefore(" + childVar + ", " 327 + parentVar + ".getDocumentElement());"); 328 } else { 329 body.addln(parentVar + ".appendChild(" + childVar + ");"); 330 } 331 } 332 } 333 334 337 public void genAppendChild(String parentVar, 338 String childVar, 339 Node child, 340 JavaCode body) { 341 if (child.getParentNode() == fDocument) { 342 genAppendDocumentChild(parentVar, childVar, child, body); 344 } else { 345 body.addln(parentVar + ".appendChild(" + childVar + ");"); 346 } 347 } 348 349 352 public void genAddAttribute(String elementVar, 353 String attrVar, 354 JavaCode body) { 355 body.addln(elementVar + ".setAttributeNode(" + attrVar + ");"); 356 } 357 358 361 public void genAddEntity(String docTypeVar, 362 String entityVar, 363 Entity entity, 364 JavaCode body) { 365 throw new XMLCError(ENTITIES_UNSUPPORTED); 366 } 367 368 371 public void genAddNotation(String docTypeVar, 372 String notationVar, 373 Notation notation, 374 JavaCode body) { 375 throw new XMLCError(NOTATION_UNSUPPORTED); 376 } 377 } 378 | Popular Tags |