1 23 24 package org.enhydra.xml.xmlc.compiler; 25 26 import org.enhydra.xml.xmlc.codegen.JavaLang; 27 import org.enhydra.xml.xmlc.dom.XMLCDocument; 28 import org.enhydra.xml.xmlc.metadata.DocumentClass; 29 import org.enhydra.xml.xmlc.metadata.MetaData; 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.html.HTMLElement; 33 34 39 44 49 public final class ElementInfo { 50 54 public class AccessorInfo { 55 56 public final String fName; 57 58 59 public final String fReturnType; 60 61 64 public AccessorInfo(String name, 65 String returnType) { 66 fName = name; 67 fReturnType = returnType; 68 } 69 } 70 71 74 private XMLCDocument fXmlcDoc; 75 76 79 private Element fElement; 80 81 84 private String fReferenceName; 85 86 89 private String fXmlId; 90 91 95 private String fJavaId; 96 97 100 private AccessorInfo[] fAccessorInfo;; 101 102 105 private boolean fCreateSetText; 106 107 110 private int fNodeSize; 111 112 115 private int fSubTreeNodeSize; 116 117 122 private int fAdjustedSubTreeNodeSize; 123 124 127 public ElementInfo(Element element, 128 int nodeSize, 129 int subTreeNodeSize, 130 MetaData metaData, 131 XMLCDocument xmlcDoc) { 132 if (element == null) { 133 throw new IllegalArgumentException ("element null"); 134 } 135 fElement = element; 136 fNodeSize = nodeSize; 137 fSubTreeNodeSize = subTreeNodeSize; 138 fAdjustedSubTreeNodeSize = subTreeNodeSize; 139 fXmlcDoc = xmlcDoc; 140 141 determineIds(); 142 initAccessorInfo(metaData); 143 144 fCreateSetText = (fJavaId != null) 147 && fXmlcDoc.hasPCDataInContentModel(fElement); 148 } 150 151 155 private String getAccessorReturnType(String typeSpec) { 156 if (typeSpec.equals(DocumentClass.ACCESSOR_TYPE_INTERFACE)) { 157 return fXmlcDoc.getDomFactory().nodeClassToInterface(fElement); 158 } else if (typeSpec.equals(DocumentClass.ACCESSOR_TYPE_CLASS)) { 159 return fElement.getClass().getName(); 160 } else if (typeSpec.equals(DocumentClass.ACCESSOR_TYPE_ELEMENT)) { 161 return Element .class.getName(); 162 } else if (typeSpec.equals(DocumentClass.ACCESSOR_TYPE_HTML_ELEMENT)) { 163 return HTMLElement.class.getName(); 164 } else { 165 return typeSpec; 166 } 167 } 168 169 172 private void initAccessorInfo(MetaData metaData) { 173 boolean createGetElement = (fJavaId != null); 174 boolean createGetTag = (fJavaId != null) 175 && metaData.getDocumentClass().getCreateGetTagMethods(); 176 177 int numAccessors = (createGetElement ? 1 : 0) 178 + (createGetTag ? 1 : 0); 179 if (numAccessors == 0) { 180 return; 181 } 182 fAccessorInfo = new AccessorInfo[numAccessors]; 183 int idx = 0; 184 185 fReferenceName = metaData.getDocumentClass().getGetTagReturnType(); 186 if (createGetElement) { 187 fReferenceName = 188 fXmlcDoc.getDomFactory().nodeClassToInterface(fElement); 189 fAccessorInfo[idx++] 190 = new AccessorInfo("getElement" + fJavaId, fReferenceName); 191 192 193 } 194 if (createGetTag) { 195 fAccessorInfo[idx++] 196 = new AccessorInfo("getTag" + fJavaId, 197 getAccessorReturnType(metaData.getDocumentClass().getGetTagReturnType())); 198 } 199 } 200 201 205 private String adjustElementId(String id) { 206 return id.substring(0, 1).toUpperCase() + id.substring(1); 207 } 208 209 212 private void determineIds() { 213 String idAttrName = fXmlcDoc.getIdAttrName(fElement); 215 if (idAttrName != null) { 216 Attr idAttr = fElement.getAttributeNode(idAttrName); 217 if (idAttr != null) { 218 fXmlId = idAttr.getNodeValue(); 219 if (JavaLang.legalJavaIdentifier(fXmlId)) { 220 fJavaId = adjustElementId(fXmlId); 221 } 222 } 223 } 224 } 225 226 229 public Element getElement() { 230 return fElement; 231 } 232 233 236 public String getClassName() { 237 return fElement.getClass().getName(); 238 } 239 240 244 public String getReferenceName() { 245 return fReferenceName; 246 } 247 248 251 public String getXmlId() { 252 return fXmlId; 253 } 254 255 258 public String getJavaId() { 259 return fJavaId; 260 } 261 262 265 public String [] getElementClassNames() { 266 return fXmlcDoc.getElementClassNames(fElement); 267 } 268 269 272 public String getElementName() { 273 return fXmlcDoc.getElementName(fElement); 274 } 275 276 279 public boolean hasInvalidJavaId() { 280 return (fXmlId != null) && (fJavaId == null); 281 } 282 283 286 public int getNumAccessMethods() { 287 return ((fAccessorInfo != null) ? fAccessorInfo.length : 0) 288 + (fCreateSetText ? 1 : 0); 289 } 290 291 294 public AccessorInfo[] getAccessors() { 295 return fAccessorInfo; 296 } 297 298 301 public boolean createSetText() { 302 return fCreateSetText; 303 } 304 305 309 public int getNodeSize() { 310 return fNodeSize; 311 } 312 313 316 public int getSubTreeNodeSize() { 317 return fSubTreeNodeSize; 318 } 319 320 325 public int getAdjustedSubTreeNodeSize() { 326 return fAdjustedSubTreeNodeSize; 327 } 328 329 332 public void setAdjustedSubTreeNodeSize(int size) { 333 fAdjustedSubTreeNodeSize = size; 334 } 335 } 336 | Popular Tags |