1 23 24 package org.enhydra.xml.xmlc.dom.lazydom; 25 26 import org.enhydra.xml.lazydom.NodeIdMap; 27 import org.enhydra.xml.xmlc.codegen.JavaClass; 28 import org.enhydra.xml.xmlc.codegen.JavaCode; 29 import org.enhydra.xml.xmlc.codegen.JavaField; 30 import org.enhydra.xml.xmlc.codegen.JavaMethod; 31 import org.enhydra.xml.xmlc.codegen.JavaModifiers; 32 import org.enhydra.xml.xmlc.compiler.ElementInfo; 33 import org.enhydra.xml.xmlc.dom.AccessorGenerator; 34 import org.enhydra.xml.xmlc.dom.generic.GenericAccessorGenerator; 35 import org.w3c.dom.Document ; 36 37 43 44 47 public class LazyDOMAccessorGenerator extends GenericAccessorGenerator 48 implements AccessorGenerator { 49 52 private NodeIdMap fNodeIdMap; 53 54 57 protected LazyDOMAccessorGenerator(Document document) { 58 fNodeIdMap = new NodeIdMap(document); 59 } 60 61 64 private String getElementNodeIdField(ElementInfo elementInfo) { 65 return "$elementId_" + elementInfo.getXmlId(); 66 } 67 68 72 public void createAccessMethodInit(ElementInfo elementInfo, 73 String elementVarName, 74 JavaCode body) { 75 } 76 77 82 public void createNullElementAccess(ElementInfo elementInfo, 83 JavaCode body) { 84 body.addln(getElementNodeIdField(elementInfo) + " = -1;"); 85 body.addln(getElementField(elementInfo) + " = null;"); 86 } 87 88 92 public void createResetElementAccess(ElementInfo elementInfo, 93 String nodeVarName, 94 JavaCode body) { 95 body.addln(getElementNodeIdField(elementInfo) + " = " 96 + fNodeIdMap.getIdStr(elementInfo.getElement()) 97 + ";"); 98 body.addln(getElementField(elementInfo) + " = (" 99 + elementInfo.getClassName() + ")" + nodeVarName + ";"); 100 } 101 102 105 private void createExpandedElementCheck(ElementInfo elementInfo, 106 JavaCode body) { 107 String fieldName = getElementField(elementInfo); 108 String idFieldName = getElementNodeIdField(elementInfo); 109 body.addln(new String [] { 110 "if ((" + fieldName + " == null) && (" + idFieldName + " >= 0)) {", 111 " " + fieldName + " = (" + elementInfo.getClassName() + ")" 112 + LazyDOMDocBuilderGenerator.LAZY_DOCUMENT_FIELD + ".getNodeById(" + idFieldName + ");", 113 "}", 114 }); 115 } 116 117 121 public JavaMethod createAccessorMethod(ElementInfo elementInfo, 122 ElementInfo.AccessorInfo accessor, 123 boolean createDelegation, 124 JavaClass docClass) { 125 JavaMethod method = setupGetElementMethod(elementInfo, accessor, 126 createDelegation, 127 docClass); 128 129 JavaCode body = method.getCode(); 131 createExpandedElementCheck(elementInfo, body); 132 body.addln("return " + getElementField(elementInfo) + ";"); 133 134 JavaField field = new JavaField(getElementNodeIdField(elementInfo), 136 "int", 137 JavaModifiers.PRIVATE, 138 (String )null, 139 fNodeIdMap.getIdStr(elementInfo.getElement())); 140 docClass.addField(field); 141 field = new JavaField(getElementField(elementInfo), 142 elementInfo.getClassName(), 143 JavaModifiers.PRIVATE, 144 (String )null, null); 145 docClass.addField(field); 146 147 return method; 148 } 149 150 154 public JavaMethod createSetTextMethod(ElementInfo elementInfo, 155 boolean createDelegation, 156 JavaClass docClass) { 157 JavaMethod method = setupSetTextMethod(elementInfo, 158 createDelegation, 159 docClass); 160 161 JavaCode body = method.getCode(); 163 createExpandedElementCheck(elementInfo, body); 164 body.addln("doSetText(" + getElementField(elementInfo) + ", text);"); 165 166 return method; 167 } 168 } 169 | Popular Tags |