1 18 package net.sf.uitags.build; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.LinkedHashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.xml.sax.SAXException ; 36 37 public final class TldUtils { 38 static Document getDocument(String filename) { 39 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 40 factory.setCoalescing(true); 41 factory.setIgnoringElementContentWhitespace(true); 42 43 try { 44 DocumentBuilder builder = factory.newDocumentBuilder(); 45 try { 46 return builder.parse(new File (filename)); 47 } 48 catch (SAXException e) { 49 throw new RuntimeException (e); 50 } 51 catch (IOException e) { 52 throw new RuntimeException (e); 53 } 54 } 55 catch (ParserConfigurationException e) { 56 throw new RuntimeException (e); 57 } 58 } 59 60 private static Node [] findDirectTagsByName(Node rootNode, String tagName) { 61 List resultNodes = new ArrayList (); 62 NodeList childNodes = rootNode.getChildNodes(); 63 for (int i = 0; i < childNodes.getLength(); ++i) { 64 Node currentNode = childNodes.item(i); 65 if (currentNode.getNodeName().equals(tagName)) { 66 resultNodes.add(currentNode); 67 } 68 } 69 return (Node []) resultNodes.toArray(new Node [resultNodes.size()]); 70 } 71 72 static Map extractTags(Document tldDocument, Document elTldDocument) { 73 Map tags = new LinkedHashMap (); 74 NodeList tagNodes = tldDocument.getElementsByTagName(Tag.TAGNAME_TAG); 75 for (int i = 0; i < tagNodes.getLength(); ++i) { 76 Tag tag = constructTag(tagNodes.item(i)); 77 tags.put(tag.getName(), tag); 78 } 79 80 tagNodes = elTldDocument.getElementsByTagName(Tag.TAGNAME_TAG); 81 for (int i = 0; i < tagNodes.getLength(); ++i) { 82 Node node = tagNodes.item(i); 83 Tag tag = (Tag) tags.get(getTagName(node)); 84 tag.setElHandlerName(getTagClassName(node)); 85 } 86 87 return tags; 88 } 89 90 static Map extractTags(Document tldDocument) { 91 Map tags = new LinkedHashMap (); 92 NodeList tagNodes = tldDocument.getElementsByTagName(Tag.TAGNAME_TAG); 93 for (int i = 0; i < tagNodes.getLength(); ++i) { 94 Tag tag = constructTag(tagNodes.item(i)); 95 tags.put(tag.getName(), tag); 96 } 97 98 return tags; 99 } 100 101 private static Tag constructTag(Node node) { 102 Tag tag = new Tag(getTagName(node)); 103 tag.setHandlerName(getTagClassName(node)); 104 105 Node [] attributeNodes = findDirectTagsByName(node, Tag.TAGNAME_ATTRIBUTE); 106 for (int i = 0; i < attributeNodes.length; ++i) { 107 tag.addAttribute(constructAttribute(attributeNodes[i])); 108 } 109 110 return tag; 111 } 112 113 private static String getValueOf(String tagName, Node rootNode) { 114 return findDirectTagsByName(rootNode, 115 tagName)[0].getFirstChild().getNodeValue(); 116 } 117 118 private static String getTagName(Node tagNode) { 119 return getValueOf(Tag.TAGNAME_NAME, tagNode); 120 } 121 122 private static String getTagClassName(Node tagNode) { 123 return getValueOf(Tag.TAGNAME_HANDLER, tagNode); 124 } 125 126 private static Attribute constructAttribute(Node node) { 127 Attribute attribute = new Attribute(getAttributeName(node)); 128 attribute.setType(getAttributeType(node)); 129 return attribute; 130 } 131 132 private static String getAttributeName(Node tagNode) { 133 return getValueOf(Tag.TAGNAME_NAME, tagNode); 134 } 135 136 private static String getAttributeType(Node tagNode) { 137 try { 138 return getValueOf(Tag.TAGNAME_TYPE, tagNode); 139 } 140 catch (ArrayIndexOutOfBoundsException e) { 141 return Attribute.DEFAULT_TYPE; 144 } 145 } 146 147 148 152 public static class Attribute { 154 private static final String DEFAULT_TYPE = "java.lang.String"; 155 private static final String GENERIC_TYPE = "java.lang.Object"; 156 157 private String name; 158 private String type; 159 private static Map predefinedEvaluators; 160 161 static { 162 predefinedEvaluators = new HashMap (); 163 predefinedEvaluators.put("int", "toIntegerValue"); 164 predefinedEvaluators.put("boolean", "toBooleanValue"); 165 predefinedEvaluators.put(DEFAULT_TYPE, "toString"); 166 } 167 168 private Attribute(String name) { 169 this.name = name; 170 } 171 172 private void setType(String type) { 173 this.type = type; 174 } 175 176 178 public String getName() { 179 return this.name; 180 } 181 182 public String getHandlerName() { 183 return "set" + Character.toUpperCase( 184 this.name.charAt(0)) + this.name.substring(1); 185 } 186 187 public String getEvaluatorName() { 188 String evaluator = (String ) predefinedEvaluators.get(this.type); 189 if (evaluator == null) { 190 evaluator = "toObject"; 191 } 192 return evaluator; 193 } 194 195 public String getCastTypeName() { 196 if (GENERIC_TYPE.equals(getCustomClassName())) { 197 return null; 198 } 199 return getCustomClassName(); 200 } 201 202 public String getCustomClassName() { 203 if (predefinedEvaluators.containsKey(this.type)) { 204 return null; 205 } 206 return this.type; 207 } 208 } 209 210 static class Tag { 211 private static final String TAGNAME_TAG = "tag"; 212 private static final String TAGNAME_NAME = "name"; 213 private static final String TAGNAME_TYPE = "type"; 214 private static final String TAGNAME_HANDLER = "tag-class"; 215 private static final String TAGNAME_ATTRIBUTE = "attribute"; 216 217 private String name; 218 private String tagHandlerName; 219 private String elTagHandlerName; 220 private List attributeList; 221 222 private Tag(String name) { 223 this.name = name; 224 this.attributeList = new ArrayList (); 225 } 226 227 private void setHandlerName(String name) { 228 this.tagHandlerName = name; 229 } 230 231 private void setElHandlerName(String name) { 232 this.elTagHandlerName = name; 233 } 234 235 private void addAttribute(Attribute attribute) { 236 this.attributeList.add(attribute); 237 } 238 239 String getName() { 240 return this.name; 241 } 242 243 List getAttributes() { 244 return this.attributeList; 245 } 246 247 String getFullyQualifiedHandlerName() { 248 return this.tagHandlerName; 249 } 250 251 String getFullyQualifiedElHandlerName() { 252 return this.elTagHandlerName; 253 } 254 255 String getHandlerPath() { 256 return this.tagHandlerName.replace('.', '/'); 257 } 258 259 String getElHandlerPath() { 260 return this.elTagHandlerName.replace('.', '/'); 261 } 262 263 String getHandlerNameFraction() { 264 return getClassNameFraction(this.tagHandlerName); 265 } 266 267 String getElHandlerNameFraction() { 268 return getClassNameFraction(this.elTagHandlerName); 269 } 270 271 String getPackageNameFraction() { 272 return getPackageNameFraction(this.tagHandlerName); 273 } 274 275 String getElPackageNameFraction() { 276 return getPackageNameFraction(this.elTagHandlerName); 277 } 278 279 private String getClassNameFraction(String classFullname) { 280 int startIndex = classFullname.lastIndexOf('.') + 1; 281 return classFullname.substring(startIndex); 282 } 283 284 private String getPackageNameFraction(String classFullname) { 285 int endIndex = classFullname.lastIndexOf('.') + 1; 286 return classFullname.substring(0, endIndex - 1); 287 } 288 } 289 } 290 | Popular Tags |