1 23 24 package org.enhydra.xml.xmlc.compiler; 25 26 import java.io.PrintWriter ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 30 import org.enhydra.xml.xmlc.codegen.IndentWriter; 31 import org.enhydra.xml.xmlc.codegen.JavaClass; 32 import org.enhydra.xml.xmlc.codegen.JavaCode; 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.codegen.JavaParameter; 37 import org.enhydra.xml.xmlc.dom.AccessorGenerator; 38 import org.enhydra.xml.xmlc.metadata.MetaData; 39 40 43 class AccessMethods { 44 47 private ElementTable fElementTable; 48 49 52 private boolean fCreateDelegation; 53 54 57 private JavaClass fDocClass; 58 59 62 private AccessorGenerator fAccessorGenerator; 63 64 67 private HashMap fAccessorMethods = new HashMap (); 68 69 73 private static final JavaParameter[] syncWithDocParam = { 74 new JavaParameter("node", "Node", 75 "Root of subtree to sync.") 76 }; 77 78 81 public AccessMethods(MetaData metaData, 82 ElementTable elementTable, 83 AccessorGenerator accessorGenerator) { 84 fCreateDelegation = metaData.getDocumentClass().getDelegateSupport(); 85 fElementTable = elementTable; 86 fAccessorGenerator = accessorGenerator; 87 } 88 89 92 private void createAccessMethods(ElementInfo elementInfo) { 93 ElementInfo.AccessorInfo[] accessors = elementInfo.getAccessors(); 94 int methodIdx = 0; 95 JavaMethod[] methods = new JavaMethod[elementInfo.getNumAccessMethods()]; 96 if (accessors != null) { 97 for (int idx = 0; idx < accessors.length; idx++) { 98 methods[methodIdx++] 99 = fAccessorGenerator.createAccessorMethod(elementInfo, 100 accessors[idx], 101 fCreateDelegation, 102 fDocClass); 103 } 104 } 105 if (elementInfo.createSetText()) { 106 methods[methodIdx++] 107 = fAccessorGenerator.createSetTextMethod(elementInfo, 108 fCreateDelegation, 109 fDocClass); 110 } 111 fAccessorMethods.put(elementInfo, methods); 112 } 113 114 115 118 private void createAccessMethods() { 119 JavaMethod method; 120 Iterator elements = fElementTable.getJavaIdElements(); 121 while (elements.hasNext()) { 122 ElementInfo elementInfo = (ElementInfo)elements.next(); 123 if (elementInfo.getNumAccessMethods() > 0) { 124 createAccessMethods(elementInfo); 125 } 126 } 127 } 128 129 132 private void createAccessorFieldInits(JavaCode body, 133 Iterator elements) { 134 while (elements.hasNext()) { 135 ElementInfo elementInfo = (ElementInfo)elements.next(); 136 if (elementInfo.getNumAccessMethods() > 0) { 137 body.addln("} else if (id.equals(" + JavaLang.createStringConst(elementInfo.getXmlId()) + ")) {"); 138 body.enter(); 139 fAccessorGenerator.createResetElementAccess(elementInfo, "node", body); 140 body.leave(); 141 } 142 } 143 } 144 145 148 private void createNullAccessFieldsInit(JavaCode body) { 149 Iterator elements = fElementTable.getJavaIdElements(); 150 while (elements.hasNext()) { 151 ElementInfo elementInfo = (ElementInfo)elements.next(); 152 if (elementInfo.getNumAccessMethods() > 0) { 153 fAccessorGenerator.createNullElementAccess(elementInfo, body); 154 } 155 } 156 body.addln(); 157 } 158 159 163 private void createGlobalIdModeInit(JavaCode body) { 164 String idAttr = fElementTable.getXMLCDocument().getGlobalIdAttribute(); 165 body.addln("if (node instanceof Element) {"); 166 body.enter(); 167 body.addln("String id = ((Element)node).getAttribute(" 168 + JavaLang.createStringConst(idAttr) + ");"); 169 body.addln("if (id.length() == 0) {"); 171 createAccessorFieldInits(body, fElementTable.getJavaIdElements()); 172 173 body.addln("}"); body.leave(); 175 body.addln("}"); 176 } 177 178 181 185 private void createMultipleIdModeTagInit(String tagName, 186 JavaCode body) { 187 String idAttr = fElementTable.getIdAttributeName(tagName); 188 if (idAttr == null) { 189 return; } 191 body.addln("if (tag.equals(" + JavaLang.createStringConst(tagName) + ")) {"); 192 body.enter(); 193 body.addln("String id = element.getAttribute(" 194 + JavaLang.createStringConst(idAttr) + ");"); 195 body.addln("if (id.length() == 0) {"); 197 198 createAccessorFieldInits(body, fElementTable.getElementsByTagName(tagName)); 200 body.addln("}"); body.leave(); 202 body.addln("}"); } 204 205 209 private void createMultipleIdModeInit(JavaCode body) { 210 body.addln(new String [] { 211 "if (node instanceof Element) {", 212 " Element element = (Element)node;", 213 " String tag = element.getTagName();" 214 }); 215 body.enter(); 216 217 Iterator tags = fElementTable.getTagNames(); 220 while (tags.hasNext()) { 221 createMultipleIdModeTagInit((String )tags.next(), body); 222 } 223 body.leave(); 224 body.addln("}"); 225 } 226 227 238 private void createAccessMethodSync() { 239 String [] doc = { 240 "Recursive function to do set access method fields from the DOM.", 241 "Missing ids have fields set to null.", 242 }; 243 JavaMethod method 244 = new JavaMethod("syncWithDocument", 245 "void", 246 JavaModifiers.PROTECTED, 247 syncWithDocParam, doc); 248 fDocClass.addMethod(method); 249 JavaCode body = method.getCode(); 250 251 if (fElementTable.isGlobalIdAttrMode()) { 253 createGlobalIdModeInit(body); 254 } else { 255 createMultipleIdModeInit(body); 256 } 257 258 body.addln(new String [] { 260 "Node child = node.getFirstChild();", 261 "while (child != null) {", 262 " syncWithDocument(child);", 263 " child = child.getNextSibling();", 264 "}" 265 }); 266 } 267 268 272 public void generateCode(JavaClass docClass) { 273 fDocClass = docClass; 274 createAccessMethodSync(); 275 createAccessMethods(); 276 } 277 278 281 public void printAccessMethods(PrintWriter out) { 282 IndentWriter iout = new IndentWriter(out); 283 Iterator elements = fElementTable.getJavaIdElements(); 284 while (elements.hasNext()) { 285 ElementInfo elementInfo = (ElementInfo)elements.next(); 286 JavaMethod[] methods = (JavaMethod[])fAccessorMethods.get(elementInfo); 287 if (methods != null) { 288 for (int idx = 0; idx < methods.length; idx++) { 289 methods[idx].printMethodSignature(iout, false); 290 } 291 } 292 } 293 iout.flush(); 294 } 295 296 300 public void printOmittedIds(PrintWriter out) { 301 Iterator ids = fElementTable.getInvalidElementIds(); 302 if ((ids != null) && ids.hasNext()) { 303 out.println("Access methods not created for these element IDs"); 304 out.println("since they are not legal Java identifiers:"); 305 while (ids.hasNext()) { 306 out.print("`"); 307 out.print((String )ids.next()); 308 out.println("'"); 309 } 310 } 311 } 312 } 313 | Popular Tags |