1 23 24 package org.enhydra.xml.xmlc.dom.lazydom; 25 26 import org.enhydra.xml.io.PreFormattedText; 27 import org.enhydra.xml.lazydom.LazyElement; 28 import org.enhydra.xml.lazydom.LazyNode; 29 import org.enhydra.xml.xmlc.codegen.JavaCode; 30 import org.enhydra.xml.xmlc.codegen.JavaLang; 31 import org.enhydra.xml.xmlc.compiler.ClassGenerator; 32 import org.enhydra.xml.xmlc.dom.XMLCDocument; 33 import org.enhydra.xml.xmlc.dom.generic.NodeCreateGenerator; 34 import org.w3c.dom.Attr ; 35 import org.w3c.dom.CDATASection ; 36 import org.w3c.dom.Comment ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.DocumentType ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Entity ; 41 import org.w3c.dom.EntityReference ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.Notation ; 44 import org.w3c.dom.ProcessingInstruction ; 45 import org.w3c.dom.Text ; 46 47 51 class LazyDOMNodeCreateGenerator extends NodeCreateGenerator { 52 55 private PreFormatter fPreFormatter; 56 57 60 public LazyDOMNodeCreateGenerator(XMLCDocument xmlcDoc, 61 PreFormatter preFormatter) { 62 super(xmlcDoc); 63 fPreFormatter = preFormatter; 64 } 65 66 70 private String getPreFormattedText(Node node) { 71 String text = null; 72 if ((node instanceof PreFormattedText) && (fPreFormatter != null)) { 73 text = fPreFormatter.preFormatNode(node); 74 } 75 return JavaLang.createStringConst(text); 76 } 77 78 81 protected void genDocumentCreate(Document document, 82 String docVar, 83 String docTypeVar, 84 JavaCode body) { 85 DocumentType docType = document.getDoctype(); 86 String docTypeRef; 87 if (docType == null) { 88 docTypeRef = "null"; 89 } else { 90 docTypeRef = docTypeVar; 91 genDocumentTypeCreate(docType, docVar, docTypeVar, body); 92 } 93 94 int nodeId = ((LazyNode)document).getNodeId(); 95 String docClass = document.getClass().getName(); 96 Element docElem = document.getDocumentElement(); 97 body.addln(docClass + " " + docVar + " = (" + docClass + ")" 98 + ClassGenerator.DOM_FACTORY_FIELD_NAME + ".createDocument(" 99 + JavaLang.createStringConst(docElem.getNamespaceURI()) + ", " 100 + JavaLang.createStringConst(docElem.getNodeName()) + ", " 101 + docTypeRef + ");"); 102 body.addln(docVar + ".makeTemplateNode(" + nodeId + ");"); 103 } 104 105 108 protected void genDocumentTypeCreate(DocumentType docType, 109 String docVarName, 110 String varName, 111 JavaCode body) { 112 String docTypeClass = docType.getClass().getName(); 113 int nodeId = ((LazyNode)docType).getNodeId(); 114 body.addln(docTypeClass + " " + varName 115 + " = (" + docTypeClass + ")" 116 + ClassGenerator.DOM_FACTORY_FIELD_NAME 117 + ".createDocumentType(" 118 + JavaLang.createStringConst(docType.getNodeName()) + ", " 119 + JavaLang.createStringConst(docType.getPublicId()) + ", " 120 + JavaLang.createStringConst(docType.getSystemId()) + ", " 121 + JavaLang.createStringConst(docType.getInternalSubset()) + ");"); 122 body.addln("((" + LazyNode.class.getName() + ")" + varName 123 + ").makeTemplateNode(" + nodeId + ");"); 124 } 125 126 129 protected void genEntityCreate(Entity entity, 130 String docVarName, 131 String varName, 132 JavaCode body) { 133 int nodeId = ((LazyNode)entity).getNodeId(); 134 body.addln(varName + " = " + docVarName + ".createTemplateEntity(" 135 + JavaLang.createStringConst(entity.getNodeName()) + ", " 136 + JavaLang.createStringConst(entity.getPublicId()) + ", " 137 + JavaLang.createStringConst(entity.getSystemId()) + ", " 138 + JavaLang.createStringConst(entity.getNotationName()) + ", " 139 + nodeId + ");"); 140 } 141 142 145 protected void genAttrCreate(Attr attr, 146 String docVarName, 147 String varName, 148 JavaCode body) { 149 int nodeId = ((LazyNode)attr).getNodeId(); 150 body.add(varName + " = "); 151 if (attr.getNamespaceURI() == null) { 152 body.addln(docVarName + ".createTemplateAttribute(" 153 + JavaLang.createStringConst(attr.getName()) + ", " 154 + nodeId + ");"); 155 } else { 156 body.addln(docVarName + ".createTemplateAttributeNS(" 157 + JavaLang.createStringConst(attr.getNamespaceURI()) + ", " 158 + JavaLang.createStringConst(attr.getName()) + ", " 159 + nodeId + ");"); 160 } 161 } 162 163 166 protected void genCDATASectionCreate(CDATASection cdataSection, 167 String docVarName, 168 String varName, 169 JavaCode body) { 170 int nodeId = ((LazyNode)cdataSection).getNodeId(); 171 body.addln(varName + " = " + docVarName 172 + ".createTemplateCDATASection(" 173 + JavaLang.createStringConst(cdataSection.getData()) + ", " 174 + nodeId + ");"); 175 } 176 177 180 protected void genCommentCreate(Comment comment, 181 String docVarName, 182 String varName, 183 JavaCode body) { 184 int nodeId = ((LazyNode)comment).getNodeId(); 185 body.addln(varName + " = " + docVarName + ".createTemplateComment(" 186 + JavaLang.createStringConst(comment.getData()) + ", " 187 + nodeId + ");"); 188 } 189 190 193 protected void genElementCreate(Element element, 194 String docVarName, 195 String varName, 196 JavaCode body) { 197 int nodeId = ((LazyNode)element).getNodeId(); 198 body.add(varName + " = "); 199 if (isDocElement(element)) { 200 String castVar = "((" + LazyElement.class.getName() + ")" 201 + varName + ")"; 202 body.addln(docVarName + ".getDocumentElement();"); 203 body.addln(castVar + ".makeTemplateNode(" + nodeId + ");"); 204 body.addln(castVar + ".setPreFormattedText(" 205 + getPreFormattedText(element) + ");"); 206 } else if (element.getNamespaceURI() == null) { 207 body.addln(docVarName + ".createTemplateElement(" 208 + JavaLang.createStringConst(element.getTagName()) + ", " 209 + nodeId + ", " + getPreFormattedText(element) + ");"); 210 } else { 211 body.addln(docVarName + ".createTemplateElementNS(" 212 + JavaLang.createStringConst(element.getNamespaceURI()) + ", " 213 + JavaLang.createStringConst(element.getTagName()) + ", " 214 + nodeId + ", " + getPreFormattedText(element) + ");"); 215 } 216 } 217 218 221 protected void genEntityReferenceCreate(EntityReference entityRef, 222 String docVarName, 223 String varName, 224 JavaCode body) { 225 int nodeId = ((LazyNode)entityRef).getNodeId(); 226 body.addln(varName + " = " + docVarName 227 + ".createTemplateEntityReference(" 228 + JavaLang.createStringConst(entityRef.getNodeName()) + ", " 229 + nodeId + ");"); 230 } 231 232 235 protected void genNotationCreate(Notation notation, 236 String docVarName, 237 String varName, 238 JavaCode body) { 239 int nodeId = ((LazyNode)notation).getNodeId(); 240 body.addln(varName + " = " + docVarName + ".createNotation(" 241 + JavaLang.createStringConst(notation.getNodeName()) + ", " 242 + JavaLang.createStringConst(notation.getPublicId()) + ", " 243 + JavaLang.createStringConst(notation.getSystemId()) + ", " 244 + nodeId + ");"); 245 } 246 247 250 protected void genProcessInstructionCreate(ProcessingInstruction procInstr, 251 String docVarName, 252 String varName, 253 JavaCode body) { 254 int nodeId = ((LazyNode)procInstr).getNodeId(); 255 body.addln(varName + " = " + docVarName 256 + ".createTemplateProcessingInstruction(" 257 + JavaLang.createStringConst(procInstr.getNodeName()) + ", " 258 + JavaLang.createStringConst(procInstr.getData()) + ", " 259 + nodeId + ");"); 260 } 261 262 265 protected void genTextCreate(Text text, 266 String docVarName, 267 String varName, 268 JavaCode body) { 269 int nodeId = ((LazyNode)text).getNodeId(); 270 body.addln(varName + " = " + docVarName + ".createTemplateTextNode(" 271 + JavaLang.createStringConst(text.getData()) + ", " 272 + nodeId + ", " + getPreFormattedText(text) + ");"); 273 } 274 } 275 | Popular Tags |