1 16 17 package org.apache.taglibs.tools.ultradev.ctlx; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.FactoryConfigurationError ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.SAXParseException ; 30 import org.xml.sax.InputSource ; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.Text ; 35 import org.w3c.dom.DOMException ; 36 import java.io.File ; 37 import java.io.PrintWriter ; 38 import java.io.IOException ; 39 import java.util.Arrays ; 40 import java.util.ArrayList ; 41 import java.util.Hashtable ; 42 import java.net.URL ; 43 44 45 56 57 58 public class TLDParser extends HttpServlet { 59 60 61 public final int ULTRADEV = 1; 62 public final int OTHER = 2; 63 public final int TLD_LIST = 3; 64 65 66 public final int FAILURE = 0; 67 public final int SUCCESS = 1; 68 69 70 private final String URL_SEP = "/"; 71 private final String PATH_TO_TLDS = URL_SEP + "tlds"; 72 private final String TLD_SUFFIX = ".tld"; 73 private final String UNDEFINED = "UNDEFINED"; 74 private final String DEFAULT_OUTPUT_FILE = "tagLibData.js"; 75 private final int DEFAULT_OUTPUT_MODE = ULTRADEV; 76 77 78 private int result = FAILURE; private int mode; private String outputFilePrefix; private PrintWriter out; 82 private Hashtable tags; 83 84 public void init(ServletConfig config) throws ServletException 85 { 86 super.init(config); 87 } 88 89 public void doGet(HttpServletRequest req, HttpServletResponse res) 90 throws ServletException , IOException 91 { 92 tags = new Hashtable (); 93 out = new PrintWriter (res.getOutputStream()); 94 String userMode; 95 URL tld = null; 96 97 userMode = req.getParameter("mode").toLowerCase(); 98 outputFilePrefix = req.getParameter("prefix"); 99 String tldPath = PATH_TO_TLDS + URL_SEP + outputFilePrefix + TLD_SUFFIX; 100 101 102 tld = getServletConfig().getServletContext().getResource(tldPath); 103 104 System.out.println( "tld = " + tld ); 105 106 if (userMode.equals("ultradev")) mode = ULTRADEV; 107 else if (userMode.equals("other")) mode = OTHER; 108 else if (userMode.equals("tldlist")) mode = TLD_LIST; 109 110 if (mode == TLD_LIST) outputTLDList(req); 111 else parseTLD(tld); 112 113 out.close(); 114 } 115 116 117 118 124 125 private void parseTLD(URL TLD) { 126 System.out.println("You entered " + TLD); 127 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 128 factory.setValidating(true); 129 factory.setNamespaceAware(true); 130 131 try { 132 DocumentBuilder builder = factory.newDocumentBuilder(); 133 Document doc = builder.parse(new InputSource (TLD.openStream())); 134 Element root = doc.getDocumentElement(); 135 System.out.println("Creating attributes table"); 136 createAttTable(root); 137 writeOutput(); 138 result = SUCCESS; 139 } 140 141 catch (org.xml.sax.SAXException e) { 142 e.printStackTrace(); 143 } 144 145 catch (javax.xml.parsers.ParserConfigurationException e) { 146 e.printStackTrace(); 147 } 148 149 catch (java.io.IOException e) { 150 e.printStackTrace(); 151 } 152 } 153 154 155 161 private void createAttTable (Element root) { 162 Node curTag = root.getFirstChild(); 163 164 curTag = getNextTag(curTag, "tag"); Tag thetag; 166 167 while (curTag != null) { 168 System.out.println("Current tag name is: " + getTagName(curTag)); 169 if (curTag.getNodeName().toLowerCase().equals("tag")) { 170 thetag = analyzeAtts(curTag); 171 tags.put(getTagName(curTag), thetag); 172 } 173 curTag = getNextTag(curTag, "tag"); 174 } 175 } 176 177 178 184 185 private Tag analyzeAtts(Node tagNode) { 186 Node bodyContentNode; 187 Node curAtt = getNextTag(tagNode.getFirstChild(), "attribute"); Node curField; Node curFieldContent; String curFieldText; String curFieldName; String attName = UNDEFINED; String attReq = UNDEFINED; Tag thetag = new Tag(); 195 196 bodyContentNode = getNextTag(tagNode.getFirstChild(), "bodycontent"); if (bodyContentNode == null) 198 bodyContentNode = getNextTag(tagNode.getFirstChild(), "body-content"); if (bodyContentNode != null) { String bodyContentText = ((Text )bodyContentNode.getFirstChild()).getData(); 201 thetag.setBodyContent(bodyContentText.toLowerCase().equals("empty") ? false : true); 202 } 203 204 System.out.println("Analyzing attributes..."); 205 206 while (curAtt != null) { 207 curField = curAtt.getFirstChild(); 208 209 while (curField != null) { if (curField.hasChildNodes()) { 211 curFieldContent = curField.getFirstChild(); 212 curFieldName = curField.getNodeName().toLowerCase(); 213 curFieldText = ((Text )curFieldContent).getData(); 214 215 if (curFieldName != null && curFieldName.equals("name")) { 216 attName = curFieldText; 217 } 218 219 else if (curFieldName != null && curFieldName.equals("required")) { 220 attReq = curFieldText; 221 } 222 } 223 curField = curField.getNextSibling(); 224 } 225 if (attName == null | attReq == null) { 226 228 System.err.println("Warning - Malformed tag: " + getTagName(tagNode)); 229 System.err.println("<name> and <required> are required fields in TLD"); 230 attName = attReq = UNDEFINED; 231 } 232 else if (attName != UNDEFINED && attReq != UNDEFINED){ 233 thetag.addAtt(attName); 235 if (attReq.toLowerCase().equals("true")) { 237 thetag.reqAtts.add(attName); 238 } 239 System.out.println("Attribute = " + attName); 240 System.out.println("Required = " + attReq); 241 attName = attReq = UNDEFINED; 242 } 243 curAtt = curAtt.getNextSibling(); 244 } 245 return thetag; 246 } 247 248 249 255 256 private Node getNextTag(Node curField, String tagName) { 257 Node nextTag = curField.getNextSibling(); 258 259 while (nextTag != null && 261 !(nextTag.getNodeName().toLowerCase().equals(tagName.toLowerCase()))) 262 { 263 nextTag = nextTag.getNextSibling(); 264 } 265 266 return nextTag; 267 } 268 269 270 276 277 private String getTagName(Node tag) { 278 Node nameTag = getNextTag(tag.getFirstChild(), "name").getFirstChild(); 279 return ((Text )nameTag).getData(); 280 } 281 282 283 288 289 private void writeOutput() { 290 switch(mode) { 291 case ULTRADEV: 292 outputForUltraDev(); 293 break; 294 default: 295 outputForUltraDev(); 296 break; 297 } 298 } 299 300 305 306 private void outputForUltraDev() { 307 outputTaglibData(); 308 } 309 310 311 317 318 private void outputTaglibData() { 319 Object [] tagNames = (tags.keySet().toArray()); 320 Arrays.sort(tagNames); 321 String curName; 322 Object [] attributes, reqAttributes; 323 Tag curTag; 324 325 out.println("taglibs[\"" + outputFilePrefix + "\"]=["); 326 for (int i = 0; i < tagNames.length; i++) { 327 curName = (String )tagNames[i]; 328 curTag = ((Tag)tags.get(curName)); 329 out.print("\t[\"" + curName + "\", " + curTag.hasBodyContent() + ", ["); 330 331 reqAttributes = ((ArrayList )(curTag.getReqAtts())).toArray(); 332 for (int j = 0; j < reqAttributes.length; j++) { 333 out.print("\"" + reqAttributes[j] + "\""); 334 if (j < reqAttributes.length - 1) out.print(", "); 335 } 336 out.print("], ["); 337 338 attributes = ((ArrayList )(curTag.getAtts())).toArray(); 339 for (int k = 0; k < attributes.length; k++) { 340 out.print("\"" + attributes[k] + "\""); 341 if (k < attributes.length - 1) out.print(", "); 342 } 343 344 out.print("]"); 345 346 347 348 if ( i >= tagNames.length - 1 ) out.println( "]];"); 349 else 350 out.println("],"); 351 } 352 } 353 354 355 361 362 private void outputTLDList(HttpServletRequest req) { 363 String tldpath = getServletConfig().getServletContext().getRealPath(PATH_TO_TLDS); 364 File dir = new File (tldpath); 365 String [] files = dir.list(); 366 367 for (int i = 0; i < files.length; i++) { 368 out.print(files[i].substring(0,files[i].indexOf("."))); 369 if (i < files.length - 1) out.print("\t"); 370 } 371 } 372 373 374 375 376 377 378 379 384 385 public void setOutputPrefix(String prefix) { 386 outputFilePrefix = prefix.toLowerCase(); 387 } 388 389 390 396 397 public void setOutputMode(int outputMode) { 398 if (outputMode != ULTRADEV && 399 outputMode != OTHER) { 400 System.err.println("Invalid output mode."); 401 } 402 403 else { 404 mode = outputMode; 405 } 406 } 407 408 409 414 415 public int getOutputMode() { 416 return mode; 417 } 418 419 420 425 426 public int getResult() { 427 return result; 428 } 429 430 431 432 433 434 435 436 442 443 private class Tag { 444 private ArrayList atts = new ArrayList (); 445 private ArrayList reqAtts = new ArrayList (); 446 private boolean bodyContent; 447 448 public void addAtt (String att) { 449 if (att != null) { 450 System.out.println("Adding attribute: " + att); 451 atts.add(att); 452 } 453 } 454 455 public void addReqAtt (String att) { 456 if (att != null ) { 457 reqAtts.add(att); 458 } 459 } 460 461 public void setBodyContent(boolean bc) { 462 if (bc == true || bc == false) { 463 bodyContent = bc; 464 } 465 } 466 467 public ArrayList getAtts () { 468 return atts; 469 } 470 471 public ArrayList getReqAtts () { 472 return reqAtts; 473 } 474 475 public boolean hasBodyContent() { 476 return bodyContent; 477 } 478 } 479 } 480 | Popular Tags |