1 23 24 package org.enhydra.xml.xmlc.dom.generic; 25 26 import org.enhydra.xml.xmlc.codegen.JavaClass; 27 import org.enhydra.xml.xmlc.codegen.JavaCode; 28 import org.enhydra.xml.xmlc.codegen.JavaField; 29 import org.enhydra.xml.xmlc.codegen.JavaMethod; 30 import org.enhydra.xml.xmlc.codegen.JavaModifiers; 31 import org.enhydra.xml.xmlc.codegen.JavaParameter; 32 import org.enhydra.xml.xmlc.compiler.ClassGenerator; 33 import org.enhydra.xml.xmlc.compiler.ElementInfo; 34 import org.enhydra.xml.xmlc.dom.AccessorGenerator; 35 36 41 public class GenericAccessorGenerator implements AccessorGenerator { 42 46 private static final JavaParameter[] setTextParam = { 47 new JavaParameter("text", "String", 48 "New value for text child.") 49 }; 50 51 54 protected String getElementField(ElementInfo elementInfo) { 55 return "$element_" + elementInfo.getJavaId(); 56 } 57 58 63 public void createAccessMethodInit(ElementInfo elementInfo, 64 String elementVarName, 65 JavaCode body) { 66 body.add(getElementField(elementInfo) 67 + " = (" + elementInfo.getReferenceName() + ")" 68 + elementVarName + ";"); 69 } 70 71 76 public void createNullElementAccess(ElementInfo elementInfo, 77 JavaCode body) { 78 body.add(getElementField(elementInfo) + " = null;"); 79 } 80 81 85 public void createResetElementAccess(ElementInfo elementInfo, 86 String nodeVarName, 87 JavaCode body) { 88 body.addln(getElementField(elementInfo) + " = (" 89 + elementInfo.getReferenceName() + ")" + nodeVarName + ";"); 90 } 91 92 96 protected JavaMethod setupGetElementMethod(ElementInfo elementInfo, 97 ElementInfo.AccessorInfo accessor, 98 boolean createDelegation, 99 JavaClass docClass) { 100 String [] doc = { 101 "Get the element with id <CODE>" + elementInfo.getXmlId() + "</CODE>.", 102 "@see " + accessor.fReturnType, 103 }; 104 JavaMethod method 105 = new JavaMethod(accessor.fName, 106 accessor.fReturnType, 107 JavaModifiers.PUBLIC, 108 null, doc); 109 docClass.addMethod(method); 110 111 JavaCode body = method.getCode(); 113 if (createDelegation) { 114 body.addln(new String [] { 115 "if (" + ClassGenerator.DELEGATE_FIELD_NAME + " != null) {", 116 " return " + ClassGenerator.DELEGATE_FIELD_NAME + "." + method.getName() + "();", 117 "}" 118 }); 119 } 120 return method; 121 } 122 123 127 public JavaMethod createAccessorMethod(ElementInfo elementInfo, 128 ElementInfo.AccessorInfo accessor, 129 boolean createDelegation, 130 JavaClass docClass) { 131 JavaMethod method = setupGetElementMethod(elementInfo, accessor, 132 createDelegation, 133 docClass); 134 135 JavaCode body = method.getCode(); 137 body.addln("return " + getElementField(elementInfo) + ";"); 138 139 JavaField field = new JavaField(getElementField(elementInfo), 141 elementInfo.getReferenceName(), 142 JavaModifiers.PRIVATE, 143 (String )null, null); 144 docClass.addField(field); 145 return method; 146 } 147 148 152 protected JavaMethod setupSetTextMethod(ElementInfo elementInfo, 153 boolean createDelegation, 154 JavaClass docClass) { 155 String [] doc = { 156 "Get the value of text child of element <CODE>" + elementInfo.getXmlId() + "</CODE>.", 157 "@see org.w3c.dom.Text" 158 }; 159 160 JavaMethod method 161 = new JavaMethod("setText" + elementInfo.getJavaId(), 162 "void", 163 JavaModifiers.PUBLIC, 164 setTextParam, doc); 165 docClass.addMethod(method); 166 167 JavaCode body = method.getCode(); 169 if (createDelegation) { 170 body.addln(new String [] { 171 "if (" + ClassGenerator.DELEGATE_FIELD_NAME + " != null) {", 172 " " + ClassGenerator.DELEGATE_FIELD_NAME + "." + method.getName() + "(text);", 173 " return;", 174 "}" 175 }); 176 } 177 return method; 178 } 179 180 184 public JavaMethod createSetTextMethod(ElementInfo elementInfo, 185 boolean createDelegation, 186 JavaClass docClass) { 187 JavaMethod method = setupSetTextMethod(elementInfo, 188 createDelegation, 189 docClass); 190 191 JavaCode body = method.getCode(); 193 body.addln("doSetText(" + getElementField(elementInfo) + ", text);"); 194 return method; 195 } 196 } 197 | Popular Tags |