1 23 24 package com.sun.enterprise.tools.guiframework.view; 25 26 import javax.xml.parsers.*; 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.*; 30 import org.w3c.dom.*; 31 import javax.xml.transform.*; 32 import javax.xml.transform.dom.DOMSource ; 33 import javax.xml.transform.stream.StreamResult ; 34 import org.xml.sax.SAXException ; 35 import org.w3c.dom.Document ; 36 37 import java.io.*; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 import java.lang.reflect.Constructor ; 41 import java.lang.reflect.Array ; 42 import java.util.ArrayList ; 43 import java.util.StringTokenizer ; 44 45 import com.sun.enterprise.tools.guiframework.FrameworkDescriptor; 46 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor; 47 import com.sun.enterprise.tools.guiframework.event.descriptors.HandlerDescriptor; 48 import com.sun.enterprise.tools.guiframework.event.descriptors.IODescriptor; 49 import com.sun.enterprise.tools.guiframework.event.descriptors.UseHandlerDescriptor; 50 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 51 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 52 import com.sun.enterprise.tools.guiframework.util.Util; 53 54 55 58 public class ViewXMLReader { 59 60 public ViewXMLReader(String fileName, String dtdURLBase) throws Exception { 61 this(fileName, dtdURLBase, null); 62 } 63 64 public ViewXMLReader(String fileName, String dtdURLBase, String jspRoot) throws Exception { 65 this(new FileInputStream(new File(fileName)), dtdURLBase, jspRoot); 66 } 67 68 public ViewXMLReader(InputStream is, String dtdURLBase) throws Exception { 69 this(is, dtdURLBase, null); 71 } 72 73 public ViewXMLReader(InputStream is, String dtdURLBase, String jspRoot) throws Exception { 74 this(is, dtdURLBase, jspRoot, null); 76 } 77 78 public ViewXMLReader(InputStream is, String dtdURLBase, String jspRoot, 79 EntityResolver entityResolver) throws Exception { 80 setJSPRoot(jspRoot); 81 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 82 dbf.setNamespaceAware(true); 83 dbf.setValidating(false); 84 dbf.setIgnoringComments(false); 85 dbf.setIgnoringElementContentWhitespace(false); 86 dbf.setCoalescing(false); 87 dbf.setExpandEntityReferences(true); 89 90 DocumentBuilder db = dbf.newDocumentBuilder(); 91 if (entityResolver != null) { 92 db.setEntityResolver(entityResolver); 93 } 94 OutputStreamWriter errorWriter = new OutputStreamWriter(System.err, OUTPUT_ENCODING); 95 db.setErrorHandler(new MyErrorHandler(new PrintWriter(errorWriter, true))); 96 doc = db.parse(is, dtdURLBase); 97 preProcess(); 98 } 99 100 101 private void preProcess() throws Exception { 102 NodeList nodeList = doc.getElementsByTagName(DISPLAY_ITEM_TYPE); 104 for (int i = 0; i < nodeList.getLength(); i++) { 105 Element elem = (Element)nodeList.item(i); 106 typeMap.put(elem.getAttribute(NAME), elem); 107 } 108 } 109 110 111 114 private ViewDescriptor getDescriptor(String name, String type) throws Exception { 115 Element typeNode = (Element)typeMap.get(type); 117 if (typeNode == null) { 118 return new ViewDescriptor(name); } 120 121 Class descriptorClass = 123 Class.forName(typeNode.getAttribute(DESCRIPTOR_CLASS)); 124 125 Constructor constructor = 127 descriptorClass.getConstructor(new Class [] {String .class}); 128 129 return (ViewDescriptor)constructor.newInstance(new Object [] {name}); 131 } 132 133 134 137 private void processEvent(Element node, ViewDescriptor vd) throws Exception { 138 String type = node.getAttribute(TYPE); 139 EventDescriptor eventDesc = new EventDescriptor(vd, type); 140 141 UseHandlerDescriptor useDesc; 142 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 143 if (child.getNodeName().compareToIgnoreCase(CALL) == 0) { 144 useDesc = getUseHandler(eventDesc, (Element)child); 145 eventDesc.addEventHandler(useDesc); 146 } 147 } 148 vd.setEventDescriptor(eventDesc); 149 } 150 151 152 155 private UseHandlerDescriptor getUseHandler(FrameworkDescriptor parent, Element node) { 156 Element handlerNode = getHandlerNode(node.getAttribute(NAME), doc); 158 if (handlerNode == null) { 159 throw new IllegalArgumentException ("'"+CALL+ 160 "' referred to a handler that was not found: '"+ 161 node.getAttribute(NAME)+"'"); 162 } 163 164 HandlerDescriptor handlerDesc = getHandler(handlerNode); 166 UseHandlerDescriptor useDesc = 167 new UseHandlerDescriptor(parent, handlerDesc); 168 169 String ifValue = node.getAttribute(IF); 171 if (Util.hasValue(ifValue)) { 172 useDesc.setIfCheck(ifValue); 173 } 174 175 Element elt; 177 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 178 String childName = child.getNodeName(); 179 if (childName.compareToIgnoreCase(INPUT)==0) { 180 elt = (Element)child; 181 String name = elt.getAttribute(NAME); 183 if (handlerDesc.getInputDescriptor(name) == null) { 184 throw new FrameworkException(CALL+" '"+ 185 handlerDesc.getName()+"' defined "+INPUT+": '"+name+ 186 "', however, '"+name+"' is not a valid "+INPUT+"!"); 187 } 188 useDesc.setInputValue(name, getParameterValue(elt)); 189 } else if (childName.compareToIgnoreCase(OUTPUT)==0) { 190 elt = (Element)child; 191 String name = elt.getAttribute(NAME); 193 if (handlerDesc.getOutputDescriptor(name) == null) { 194 throw new FrameworkException(CALL+" '"+ 195 handlerDesc.getName()+"' defined "+OUTPUT+": '"+name+ 196 "', however, '"+name+"' is not a valid "+OUTPUT+"!"); 197 } 198 useDesc.setOutputMapping( 199 name, elt.getAttribute(TARGET_KEY), elt.getAttribute(TARGET_TYPE)); 200 } 201 } 202 203 return useDesc; 205 } 206 207 208 211 private HandlerDescriptor getHandler(Element node) { 212 String name = node.getAttribute(NAME); 214 HandlerDescriptor handler = (HandlerDescriptor)handlerMap.get(name); 215 if (handler != null) { 216 return handler; 217 } 218 219 handler = new HandlerDescriptor(name); 221 222 handler.setDescription(node.getAttribute(DESCRIPTION)); 224 225 String className = node.getAttribute(FUNCTION_CLASS); 227 String methodName = node.getAttribute(FUNCTION_METHOD); 228 if (Util.hasValue(className) && Util.hasValue(methodName)) { 229 handler.setHandlerMethod(className, methodName); 230 } 231 232 String ifValue = node.getAttribute(IF); 234 if (Util.hasValue(ifValue)) { 235 handler.setIfCheck(ifValue); 236 } 237 238 String nodeName; 240 UseHandlerDescriptor useDesc; 241 IODescriptor ioDesc; 242 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 243 nodeName = child.getNodeName(); 244 if (nodeName.compareToIgnoreCase(CALL) == 0) { 245 useDesc = getUseHandler(handler, (Element)child); 246 handler.addChildHandlerDescriptor(useDesc); 247 } else if (nodeName.compareToIgnoreCase(INPUT_DEF) == 0) { 248 ioDesc = getIODescriptor((Element)child); 249 String defValue = ((Element)child).getAttribute(DEFAULT); 250 if (Util.hasValue(defValue)) { 251 ioDesc.setDefault(defValue); 252 } 253 handler.addInputDescriptor(ioDesc); 254 } else if (nodeName.compareToIgnoreCase(OUTPUT_DEF) == 0) { 255 ioDesc = getIODescriptor((Element)child); 256 handler.addOutputDescriptor(ioDesc); 257 } 258 } 259 260 handlerMap.put(name, handler); 262 return handler; 263 } 264 265 266 269 private IODescriptor getIODescriptor(Element node) { 270 IODescriptor desc = new IODescriptor( 271 node.getAttribute(NAME), node.getAttribute(TYPE)); 272 desc.setDescription(node.getAttribute(DESCRIPTION)); 273 return desc; 274 } 275 276 277 private void processParameter(Element node, ViewDescriptor vd) { 278 vd.addParameter(node.getAttribute(NAME), getParameterValue(node)); 279 } 280 281 282 private void processInclude(Element node, ViewDescriptor vd) throws Exception { 283 284 Node descriptorNode = getViewDescriptorNode(node.getAttribute("item"), doc); 285 if (descriptorNode == null) { 286 throw new Exception ("Invalid include :" + node.getAttribute("item")); 287 } 288 289 vd.addChildDescriptor(process((Element)descriptorNode)); 291 } 292 293 private Object getInputValue(Element node) { 294 return getParameterValue(node); 295 } 296 297 private Object getParameterValue(Element node) { 298 if (Util.hasValue(node.getAttribute(VALUE))) { 299 302 return node.getAttribute(VALUE); 303 } 304 305 ArrayList values = new ArrayList (); 306 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 307 if (child.getNodeName().equalsIgnoreCase(VALUES)) { 308 311 String str = ((Element)child).getAttribute(VALUE); 312 values.add(str); 313 } 314 } 315 316 if (values.size() == 0) { 318 return null; 319 } 320 return values; 321 } 322 323 324 private String getDisplayItem(Node node) { 325 Node parentNode = node; 326 while (true) { 327 parentNode = parentNode.getParentNode(); 328 if (parentNode == null || parentNode.getNodeName().compareToIgnoreCase(DISPLAY_ITEM)==0) { 329 break; 330 } 331 } 332 if (parentNode == null) { 333 return null; 334 } 335 NamedNodeMap attrs = parentNode.getAttributes(); 336 if (attrs == null) { 337 return null; 338 } 339 return attrs.getNamedItem(NAME).getNodeValue(); 340 } 341 342 343 private ViewDescriptor process(Element node) throws Exception { 344 String name = node.getAttribute(NAME); 345 String description = node.getAttribute(DESCRIPTION); 346 String type = node.getAttribute(TYPE); 347 String extendsFrom = node.getAttribute("extends"); 348 String displayURL = node.getAttribute("displayURL"); 349 350 Node extendsViewDescriptor = null; 352 if (Util.hasValue(extendsFrom)) { 353 extendsViewDescriptor = getViewDescriptorNode(extendsFrom, doc); 354 if (extendsViewDescriptor == null) { 355 throw new Exception ("Cannot find " + extendsFrom); 356 } 357 if (type == null || type.length() == 0) { 360 type = ((Element)extendsViewDescriptor).getAttribute(TYPE); 361 } 362 } 363 364 ViewDescriptor vd = getDescriptor(name, type); 365 vd.setDescription(description); 366 367 if ((displayURL == null) || (displayURL.equals(""))) { 369 vd.setDisplayURL(getJSPName(name)); 370 } else { 371 vd.setDisplayURL(displayURL); 372 } 373 374 if (extendsViewDescriptor != null) { 376 processExtends((Element)extendsViewDescriptor, vd); 377 } 378 381 processDisplayItemChildren(node ,vd); 382 return vd; 383 } 384 385 protected void processExtends(Element extendsViewDescriptor, 386 ViewDescriptor vd) throws Exception { 387 String extendsFrom = extendsViewDescriptor.getAttribute("extends"); 388 if (Util.hasValue(extendsFrom)) { 389 Element extendsParentViewDescriptor = getViewDescriptorNode(extendsFrom, doc); 390 if (extendsViewDescriptor == null) { 391 throw new Exception ("Cannot find " + extendsFrom); 392 } 393 processExtends(extendsParentViewDescriptor, vd); 394 } 395 processDisplayItemChildren((Element)extendsViewDescriptor, vd); 396 } 397 398 protected String getJSPName(String name) { 399 return getJSPRoot() + name + ".jsp"; 400 } 401 402 403 protected String getJSPRoot() { 404 return _jspRoot; 405 } 406 407 408 protected void setJSPRoot(String root) { 409 if (root == null) { 410 return; 411 } 412 _jspRoot = root; 413 } 414 415 416 private void processDisplayItemChildren(Node node, ViewDescriptor vd) throws Exception { 417 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 418 String childName = child.getNodeName(); 419 if (childName.compareToIgnoreCase(PARAMETER)==0) { 420 processParameter((Element)child, vd); 421 } 422 else if (childName.compareToIgnoreCase(EVENTS)==0) { 423 processEvent((Element)child, vd); 424 } 425 else if (childName.compareToIgnoreCase(DISPLAY_ITEM)==0) { 426 vd.addChildDescriptor(process((Element)child)); 428 } 429 else if (childName.compareToIgnoreCase(INCLUDE)==0) { 430 processInclude((Element)child, vd); 431 } 432 433 } 434 } 435 436 437 440 private Element getHandlerNode(String key, Node node) { 441 if (node == null) { 442 return null; 443 } 444 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 445 String nodeName = child.getNodeName(); 446 if (nodeName.compareToIgnoreCase(FUNCTION)==0) { 447 if (((Element)child).getAttribute(NAME).equals(key)) { 448 return (Element)child; 449 } 450 } else { 451 Element handlerNode = getHandlerNode(key, child); 452 if (handlerNode != null) { 453 return handlerNode; 454 } 455 } 456 } 457 return null; 458 } 459 460 461 private Element getViewDescriptorNode(String key, Node node) throws Exception { 462 if (node == null) { 463 return null; 464 } 465 466 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 468 String nodeName = child.getNodeName(); 469 if (nodeName.compareToIgnoreCase(DISPLAY_ITEM)==0) { 470 NamedNodeMap attrs = child.getAttributes(); 471 if (attrs == null) { 472 throw new Exception ("No attrs!!!"); 473 } 474 Node name = attrs.getNamedItem(NAME); 475 if (name == null) { 476 throw new Exception ("Name cannot be null!!!"); 477 } 478 if (name.getNodeValue().equals(key)) { 479 return (Element)child; 480 } 481 } 482 else { 483 Element vdNode = getViewDescriptorNode(key, child); 484 if (vdNode != null) { 485 return vdNode; 486 } 487 } 488 } 489 490 if (key.indexOf(".") >= 0) { 493 500 StringTokenizer tokenizer = new StringTokenizer (key, "."); 501 Node childNode = node; 502 while (tokenizer.hasMoreElements()) { 503 key = tokenizer.nextToken(); 504 childNode = getViewDescriptorNode(key, childNode); 505 } 506 return (Element)childNode; 507 } 508 509 return null; 510 } 511 512 513 516 public ViewDescriptor getViewDescriptor(String key) throws Exception { 517 Element node = getViewDescriptorNode(key, doc); 518 if (node == null) { 519 return null; 520 } 522 return process(node); 524 } 525 526 527 private static class MyErrorHandler implements ErrorHandler { 529 530 private PrintWriter out; 531 532 MyErrorHandler(PrintWriter out) { 533 this.out = out; 534 } 535 536 539 private String getParseExceptionInfo(SAXParseException spe) { 540 String systemId = spe.getSystemId(); 541 if (systemId == null) { 542 systemId = "null"; 543 } 544 String info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); 545 return info; 546 } 547 548 551 public void warning(SAXParseException spe) throws SAXException { 552 out.println("Warning: " + getParseExceptionInfo(spe)); 553 } 554 555 public void error(SAXParseException spe) throws SAXException { 556 String message = "Error: " + getParseExceptionInfo(spe); 557 throw new SAXException (message); 558 } 559 560 public void fatalError(SAXParseException spe) throws SAXException { 561 String message = "Fatal Error: " + getParseExceptionInfo(spe); 562 throw new SAXException (message); 563 } 564 } 565 566 567 public HashMap getAllViewDescriptors() throws Exception { 569 NodeList nodeList = doc.getElementsByTagName(DISPLAY_ITEM); 570 HashMap descriptors = new HashMap (); 571 for (int i = 0; i < nodeList.getLength(); i++) { 572 Element displayItem = (Element)nodeList.item(i); 573 Node parent = displayItem.getParentNode(); 574 if (parent != null && parent.getNodeName().equals(DISPLAY_ITEM)) 576 continue; 577 String topLevelDescriptorName = displayItem.getAttribute(NAME); 578 if (descriptors.get(topLevelDescriptorName) != null) { 579 throw new Exception ("DisplayItem " + topLevelDescriptorName + " defined more than once"); 580 } 581 descriptors.put(topLevelDescriptorName, process(displayItem)); 582 } 583 return descriptors; 584 } 585 586 public static void main(String args[]) { 587 try { 588 if (Array.getLength(args) < 2) { 589 System.out.println("Usage ViewXMLReader <XMLFileName> <ViewName>"); 590 return; 591 } 592 ViewXMLReader xmlReader = new ViewXMLReader(args[0], "file://./"); 593 xmlReader.getViewDescriptor(args[1]); 594 } catch (Exception ex) { 595 ex.printStackTrace(); 596 } 597 } 598 599 600 private String _jspRoot = "/jsp/"; 601 603 604 private Document doc; 605 private Map typeMap = new HashMap (); 606 private Map handlerMap = new HashMap (); 607 608 private static final String OUTPUT_ENCODING = "UTF-8"; 609 610 private static final String DESCRIPTOR_CLASS = "descriptorClass"; 611 private static final String DISPLAY_ITEM = "displayItem"; 612 private static final String DISPLAY_ITEM_TYPE = "displayItemType"; 613 private static final String EVENTS = "events"; 614 private static final String FUNCTION = "function"; 615 private static final String FUNCTION_CLASS = "className"; 616 private static final String FUNCTION_METHOD = "methodName"; 617 private static final String INPUT_DEF = "inputDef"; 618 private static final String OUTPUT_DEF = "outputDef"; 619 private static final String CALL = "call"; 620 private static final String INPUT = "input"; 621 private static final String OUTPUT = "output"; 622 private static final String TARGET_KEY = "key"; 623 private static final String TARGET_TYPE = "target"; 624 private static final String IF = "if"; 625 private static final String PARAMETER = "parameter"; 626 private static final String INCLUDE = "include"; 627 private static final String VALUE = "value"; 628 private static final String VALUES = "values"; 629 private static final String NAME = "name"; 630 private static final String TYPE = "type"; 631 private static final String DEFAULT = "default"; 632 private static final String DESCRIPTION = "description"; 633 } 634 | Popular Tags |