1 23 24 package org.enhydra.xml.xmlc.dom.lazydom; 25 26 import org.enhydra.xml.io.OutputOptions; 27 import org.enhydra.xml.lazydom.LazyDocument; 28 import org.enhydra.xml.lazydom.TemplateDOM; 29 import org.enhydra.xml.xmlc.XMLCException; 30 import org.enhydra.xml.xmlc.codegen.JavaClass; 31 import org.enhydra.xml.xmlc.codegen.JavaCode; 32 import org.enhydra.xml.xmlc.codegen.JavaField; 33 import org.enhydra.xml.xmlc.codegen.JavaLang; 34 import org.enhydra.xml.xmlc.codegen.JavaMethod; 35 import org.enhydra.xml.xmlc.codegen.JavaModifiers; 36 import org.enhydra.xml.xmlc.compiler.ClassGenerator; 37 import org.enhydra.xml.xmlc.compiler.ElementTable; 38 import org.enhydra.xml.xmlc.dom.AccessorGenerator; 39 import org.enhydra.xml.xmlc.dom.DocBuilderGenerator; 40 import org.enhydra.xml.xmlc.dom.XMLCDocument; 41 import org.enhydra.xml.xmlc.dom.generic.DOMBuilderGenerator; 42 import org.w3c.dom.Document ; 43 44 46 54 public class LazyDOMDocBuilderGenerator implements DocBuilderGenerator { 55 59 private static final int MAX_CREATE_COST_PER_BUILD_METHOD = 256; 60 61 64 private static String TEMPLATE_DOCUMENT_FIELD = "fTemplateDocument"; 65 66 69 private static final String DOC_TYPE_VAR = "documentType"; 70 71 74 public static final String DOCUMENT_VAR_NAME = "document"; 75 76 80 private static String PREFORMAT_OUTPUT_OPTIONS_FIELD = "fPreFormatOutputOptions"; 81 82 85 static String LAZY_DOCUMENT_FIELD = "fLazyDocument"; 86 87 90 private static final String SUBDOCUMENT_BUILD_METHOD = "buildTemplateSubDocument"; 91 92 96 private TemplateDOM templateDOM; 97 98 101 private PreFormatter fPreFormatter; 102 103 109 public void generateDocCreateFromTemplate(Document document, 110 String templateFieldName, 111 String docFieldName, 112 JavaCode body) { 113 String docClass = document.getClass().getName(); 114 115 body.addln(docFieldName + " = (" + docClass + ")((" 116 + LazyDomFactory.class.getName() 117 + ")" + ClassGenerator.DOM_FACTORY_FIELD_NAME 118 + ").createDocument(" + templateFieldName + ");"); 119 } 120 121 124 private void preFormatterInit(JavaClass docClass) { 125 docClass.addField(new JavaField(PREFORMAT_OUTPUT_OPTIONS_FIELD, 128 OutputOptions.class.getName(), 129 JavaModifiers.PRIVATE | JavaModifiers.STATIC | JavaModifiers.FINAL, 130 new String []{"Options used to preformat the document when compiled"}, 131 null)); 132 133 fPreFormatter.createOutputOptionsCodeGenerator(PREFORMAT_OUTPUT_OPTIONS_FIELD, 135 docClass.getClassInitializer()); 136 } 137 138 141 private void createTemplateDocInit(XMLCDocument xmlcDoc, 142 AccessorGenerator accessorGenerator, 143 ElementTable elementTable, 144 JavaClass docClass) throws XMLCException { 145 LazyDocument document = (LazyDocument)xmlcDoc.getDocument(); 146 147 templateDOM = new TemplateDOM(document); 149 150 JavaCode body = docClass.getClassInitializer(); 151 DOMBuilderGenerator domBuilderGenerator 152 = new DOMBuilderGenerator(SUBDOCUMENT_BUILD_METHOD, 153 xmlcDoc.getDocument(), 154 LazyDocument.class.getName(), 155 new LazyDOMNodeCreateGenerator(xmlcDoc, 156 fPreFormatter), 157 accessorGenerator, 158 elementTable, 159 docClass, 160 MAX_CREATE_COST_PER_BUILD_METHOD, 161 true); 162 163 JavaField field = new JavaField(TEMPLATE_DOCUMENT_FIELD, 164 TemplateDOM.class.getName(), 165 JavaModifiers.PRIVATE | JavaModifiers.STATIC | JavaModifiers.FINAL, 166 "Template document shared by all instances.", 167 null); 168 docClass.addField(field); 169 body.add(document.getClass().getName() + " " + DOCUMENT_VAR_NAME + " = "); 170 domBuilderGenerator.createMethodCall(body); 171 172 body.addln(TEMPLATE_DOCUMENT_FIELD + " = new " + TemplateDOM.class.getName() 174 + "(" + DOCUMENT_VAR_NAME + ");"); 175 176 if (fPreFormatter != null) { 177 preFormatterInit(docClass); 178 } 179 } 180 181 184 private void createPreformattedTextSetup(JavaCode body) { 185 body.addln(LAZY_DOCUMENT_FIELD + ".setPreFormatOutputOptions(" 186 + PREFORMAT_OUTPUT_OPTIONS_FIELD + ");"); 187 } 188 189 192 private void createBuildDocument(XMLCDocument xmlcDoc, 193 JavaClass docClass, 194 JavaMethod buildDocumentMethod) { 195 JavaField docField = new JavaField(LAZY_DOCUMENT_FIELD, 197 LazyDocument.class.getName(), 198 JavaModifiers.PRIVATE, 199 "Lazy DOM document", null); 200 docClass.addField(docField); 201 202 JavaCode body = buildDocumentMethod.getCode(); 204 205 generateDocCreateFromTemplate(xmlcDoc.getDocument(), 207 TEMPLATE_DOCUMENT_FIELD, 208 LAZY_DOCUMENT_FIELD, 209 body); 210 if (fPreFormatter != null) { 211 createPreformattedTextSetup(body); 212 } 213 214 body.addln("setDocument(" + LAZY_DOCUMENT_FIELD + ", " 216 + JavaLang.createStringConst(xmlcDoc.getDomFactory().getMIMEType()) + ", " 217 + JavaLang.createStringConst(xmlcDoc.getEncoding()) + ");"); 218 } 219 220 224 public void createBuildDocumentMethod(XMLCDocument xmlcDoc, 225 AccessorGenerator accessorGenerator, 226 ElementTable elementTable, 227 JavaClass docClass, 228 JavaMethod buildDocumentMethod) throws XMLCException { 229 fPreFormatter = new PreFormatter(xmlcDoc); 230 createTemplateDocInit(xmlcDoc, accessorGenerator, 231 elementTable, docClass); 232 createBuildDocument(xmlcDoc, docClass, buildDocumentMethod); 233 } 234 } 235 236 | Popular Tags |