1 23 42 43 package org.enhydra.xml.xmlc.compiler; 44 45 import java.io.PrintWriter ; 46 import java.util.Iterator ; 47 import java.util.TreeMap ; 48 49 import org.enhydra.xml.xmlc.codegen.JavaClass; 50 import org.enhydra.xml.xmlc.codegen.JavaField; 51 import org.enhydra.xml.xmlc.codegen.JavaLang; 52 import org.enhydra.xml.xmlc.codegen.JavaModifiers; 53 import org.enhydra.xml.xmlc.metadata.HTMLCompatibility; 54 import org.enhydra.xml.xmlc.metadata.HTMLSection; 55 import org.enhydra.xml.xmlc.metadata.MetaData; 56 57 61 class AccessConsts { 62 65 private ElementTable fElementTable; 66 67 71 private TreeMap fClassNameFields = new TreeMap (); 72 73 76 private TreeMap fInvalidElementClassNames = null; 77 78 82 private TreeMap fElementNameFields = new TreeMap (); 83 84 87 private TreeMap fInvalidElementNames = null; 88 89 92 private boolean fOldClassConstants; 93 94 97 private boolean fOldNameConstants; 98 99 102 private JavaClass fDocClass; 103 104 110 public AccessConsts(MetaData metaData, 111 ElementTable elementTable) { 112 fElementTable = elementTable; 113 114 HTMLSection htmlSection = metaData.getHTMLSection(); 116 HTMLCompatibility htmlCompat = htmlSection.getCompatibility(); 117 if (htmlCompat != null) { 118 fOldClassConstants = htmlCompat.getOldClassConstants(); 119 fOldNameConstants = htmlCompat.getOldNameConstants(); 120 } 121 } 122 123 126 private void createElementClassNameField(String className) { 127 String fieldName = "CLASS_" 128 + (fOldClassConstants ? className.toUpperCase() : className); 129 130 JavaField field 131 = new JavaField(fieldName, 132 "String", 133 JavaModifiers.PUBLIC_CONST, 134 "Class attribute constant for element class " + className, 135 JavaLang.createStringConst(className)); 136 fDocClass.addField(field); 137 fClassNameFields.put(className, field); 138 } 139 140 143 private void processElementClassNames(ElementInfo elementInfo) { 144 String [] names = elementInfo.getElementClassNames(); 145 if (names != null) { 146 for (int idx = 0; idx < names.length; idx++) { 147 if (!fClassNameFields.containsKey(names[idx])) { 148 if (!JavaLang.legalJavaIdentifier(names[idx])) { 149 if (fInvalidElementClassNames == null) { 150 fInvalidElementClassNames = new TreeMap (); 151 } 152 fInvalidElementClassNames.put(names[idx], names[idx]); 153 } else { 154 createElementClassNameField(names[idx]); 155 } 156 } 157 } 158 } 159 } 160 161 164 private void createElementNameField(String name) { 165 String fieldName = "NAME_" 166 + (fOldNameConstants ? name.toUpperCase() : name); 167 JavaField field 168 = new JavaField(fieldName, 169 "String", 170 JavaModifiers.PUBLIC_CONST, 171 "Element name constant for " + name, 172 JavaLang.createStringConst(name)); 173 fDocClass.addField(field); 174 fElementNameFields.put(name, field); 175 } 176 177 180 private void processElementName(ElementInfo elementInfo) { 181 String name = elementInfo.getElementName(); 182 if ((name != null) && (name.length() > 0)) { 183 if (!fElementNameFields.containsKey(name)) { 184 if (!JavaLang.legalJavaIdentifier(name)) { 185 if (fInvalidElementNames == null) { 186 fInvalidElementNames = new TreeMap (); 187 } 188 fInvalidElementNames.put(name, name); 189 } else { 190 createElementNameField(name); 191 } 192 } 193 } 194 } 195 196 200 public void generateCode(JavaClass docClass) { 201 fDocClass = docClass; 202 Iterator elements = fElementTable.getElements(); 203 while (elements.hasNext()) { 204 ElementInfo elementInfo = (ElementInfo)elements.next(); 205 processElementClassNames(elementInfo); 206 processElementName(elementInfo); 207 } 208 } 209 210 213 public void printAccessConstants(PrintWriter out) { 214 Iterator classes = fClassNameFields.values().iterator(); 215 while (classes.hasNext()) { 216 ((JavaField)classes.next()).printDefinition(out); 217 out.println(';'); 218 } 219 Iterator names = fElementNameFields.values().iterator(); 220 while (names.hasNext()) { 221 ((JavaField)names.next()).printDefinition(out); 222 out.println(';'); 223 } 224 } 225 226 230 public void printOmittedConstants(PrintWriter out) { 231 if (fInvalidElementClassNames != null) { 232 out.println("Element class name constants not created for these class names"); 233 out.println("since they are not legal Java identifiers:"); 234 Iterator badClasses = fInvalidElementClassNames.keySet().iterator(); 235 while (badClasses.hasNext()) { 236 out.print("`"); 237 out.print((String )badClasses.next()); 238 out.println("'"); 239 } 240 } 241 if (fInvalidElementNames != null) { 242 out.println("Element name constants not created for these names"); 243 out.println("since they are not legal Java identifiers:"); 244 Iterator badNames = fInvalidElementNames.keySet().iterator(); 245 while (badNames.hasNext()) { 246 out.print("`"); 247 out.print((String )badNames.next()); 248 out.println("'"); 249 } 250 } 251 } 252 } 253 | Popular Tags |