| 1 18 19 package org.apache.tomcat.util.digester; 20 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.Reader ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.EmptyStackException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.parsers.SAXParser ; 37 import javax.xml.parsers.SAXParserFactory ; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.apache.tomcat.util.IntrospectionUtils; 42 import org.xml.sax.Attributes ; 43 import org.xml.sax.EntityResolver ; 44 import org.xml.sax.ErrorHandler ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.Locator ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXNotRecognizedException ; 49 import org.xml.sax.SAXNotSupportedException ; 50 import org.xml.sax.SAXParseException ; 51 import org.xml.sax.XMLReader ; 52 import org.xml.sax.helpers.AttributesImpl ; 53 import org.xml.sax.helpers.DefaultHandler ; 54 55 56 57 58 77 78 public class Digester extends DefaultHandler { 79 80 81 83 84 private static class SystemPropertySource 85 implements IntrospectionUtils.PropertySource { 86 public String getProperty( String key ) { 87 return System.getProperty(key); 88 } 89 } 90 91 protected static IntrospectionUtils.PropertySource source[] = 92 new IntrospectionUtils.PropertySource[] { new SystemPropertySource() }; 93 94 95 97 98 101 public Digester() { 102 103 super(); 104 105 } 106 107 108 115 public Digester(SAXParser parser) { 116 117 super(); 118 119 this.parser = parser; 120 121 } 122 123 124 131 public Digester(XMLReader reader) { 132 133 super(); 134 135 this.reader = reader; 136 137 } 138 139 140 142 143 146 protected StringBuffer bodyText = new StringBuffer (); 147 148 149 152 protected ArrayStack bodyTexts = new ArrayStack(); 153 154 155 165 protected ArrayStack matches = new ArrayStack(10); 166 167 173 protected ClassLoader classLoader = null; 174 175 176 179 protected boolean configured = false; 180 181 182 185 protected EntityResolver entityResolver; 186 187 191 protected HashMap entityValidator = new HashMap (); 192 193 194 198 protected ErrorHandler errorHandler = null; 199 200 201 204 protected SAXParserFactory factory = null; 205 206 209 protected String JAXP_SCHEMA_LANGUAGE = 210 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 211 212 213 216 protected Locator locator = null; 217 218 219 222 protected String match = ""; 223 224 225 228 protected boolean namespaceAware = false; 229 230 231 239 protected HashMap namespaces = new HashMap (); 240 241 242 246 protected ArrayStack params = new ArrayStack(); 247 248 249 252 protected SAXParser parser = null; 253 254 255 259 protected String publicId = null; 260 261 262 265 protected XMLReader reader = null; 266 267 268 272 protected Object root = null; 273 274 275 281 protected Rules rules = null; 282 283 287 protected String schemaLanguage = W3C_XML_SCHEMA; 288 289 290 293 protected String schemaLocation = null; 294 295 296 299 protected ArrayStack stack = new ArrayStack(); 300 301 302 306 protected boolean useContextClassLoader = false; 307 308 309 312 protected boolean validating = false; 313 314 315 318 protected Log log = 319 LogFactory.getLog("org.apache.commons.digester.Digester"); 320 321 322 325 protected Log saxLog = 326 LogFactory.getLog("org.apache.commons.digester.Digester.sax"); 327 328 329 332 protected static final String W3C_XML_SCHEMA = 333 "http://www.w3.org/2001/XMLSchema"; 334 335 336 private HashMap stacksByName = new HashMap (); 337 338 340 347 public String findNamespaceURI(String prefix) { 348 349 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 350 if (stack == null) { 351 return (null); 352 } 353 try { 354 return ((String ) stack.peek()); 355 } catch (EmptyStackException e) { 356 return (null); 357 } 358 359 } 360 361 362 372 public ClassLoader getClassLoader() { 373 374 if (this.classLoader != null) { 375 return (this.classLoader); 376 } 377 if (this.useContextClassLoader) { 378 ClassLoader classLoader = 379 Thread.currentThread().getContextClassLoader(); 380 if (classLoader != null) { 381 return (classLoader); 382 } 383 } 384 return (this.getClass().getClassLoader()); 385 386 } 387 388 389 396 public void setClassLoader(ClassLoader classLoader) { 397 398 this.classLoader = classLoader; 399 400 } 401 402 403 406 public int getCount() { 407 408 return (stack.size()); 409 410 } 411 412 413 416 public String getCurrentElementName() { 417 418 String elementName = match; 419 int lastSlash = elementName.lastIndexOf('/'); 420 if (lastSlash >= 0) { 421 elementName = elementName.substring(lastSlash + 1); 422 } 423 return (elementName); 424 425 } 426 427 428 435 public int getDebug() { 436 437 return (0); 438 439 } 440 441 442 452 public void setDebug(int debug) { 453 454 ; 456 } 457 458 459 462 public ErrorHandler getErrorHandler() { 463 464 return (this.errorHandler); 465 466 } 467 468 469 474 public void setErrorHandler(ErrorHandler errorHandler) { 475 476 this.errorHandler = errorHandler; 477 478 } 479 480 481 484 public SAXParserFactory getFactory() { 485 486 if (factory == null) { 487 factory = SAXParserFactory.newInstance(); 488 factory.setNamespaceAware(namespaceAware); 489 factory.setValidating(validating); 490 } 491 return (factory); 492 493 } 494 495 496 512 public boolean getFeature(String feature) 513 throws ParserConfigurationException , SAXNotRecognizedException , 514 SAXNotSupportedException { 515 516 return (getFactory().getFeature(feature)); 517 518 } 519 520 521 541 public void setFeature(String feature, boolean value) 542 throws ParserConfigurationException , SAXNotRecognizedException , 543 SAXNotSupportedException { 544 545 getFactory().setFeature(feature, value); 546 547 } 548 549 550 553 public Log getLogger() { 554 555 return log; 556 557 } 558 559 560 563 public void setLogger(Log log) { 564 565 this.log = log; 566 567 } 568 569 575 public Log getSAXLogger() { 576 577 return saxLog; 578 } 579 580 581 588 public void setSAXLogger(Log saxLog) { 589 590 this.saxLog = saxLog; 591 } 592 593 596 public String getMatch() { 597 598 return match; 599 600 } 601 602 603 606 public boolean getNamespaceAware() { 607 608 return (this.namespaceAware); 609 610 } 611 612 613 618 public void setNamespaceAware(boolean namespaceAware) { 619 620 this.namespaceAware = namespaceAware; 621 622 } 623 624 625 629 public void setPublicId(String publicId){ 630 this.publicId = publicId; 631 } 632 633 634 638 public String getPublicId() { 639 640 return (this.publicId); 641 642 } 643 644 645 649 public String getRuleNamespaceURI() { 650 651 return (getRules().getNamespaceURI()); 652 653 } 654 655 656 664 public void setRuleNamespaceURI(String ruleNamespaceURI) { 665 666 getRules().setNamespaceURI(ruleNamespaceURI); 667 668 } 669 670 671 675 public SAXParser getParser() { 676 677 if (parser != null) { 679 return (parser); 680 } 681 682 try { 684 if (validating) { 685 Properties properties = new Properties (); 686 properties.put("SAXParserFactory", getFactory()); 687 if (schemaLocation != null) { 688 properties.put("schemaLocation", schemaLocation); 689 properties.put("schemaLanguage", schemaLanguage); 690 } 691 parser = ParserFeatureSetterFactory.newSAXParser(properties); } else { 692 parser = getFactory().newSAXParser(); 693 } 694 } catch (Exception e) { 695 log.error("Digester.getParser: ", e); 696 return (null); 697 } 698 699 return (parser); 700 701 } 702 703 704 718 public Object getProperty(String property) 719 throws SAXNotRecognizedException , SAXNotSupportedException { 720 721 return (getParser().getProperty(property)); 722 723 } 724 725 726 741 public void setProperty(String property, Object value) 742 throws SAXNotRecognizedException , SAXNotSupportedException { 743 744 getParser().setProperty(property, value); 745 746 } 747 748 749 756 public XMLReader getReader() { 757 758 try { 759 return (getXMLReader()); 760 } catch (SAXException e) { 761 log.error("Cannot get XMLReader", e); 762 return (null); 763 } 764 765 } 766 767 768 773 public Rules getRules() { 774 775 if (this.rules == null) { 776 this.rules = new RulesBase(); 777 this.rules.setDigester(this); 778 } 779 return (this.rules); 780 781 } 782 783 784 790 public void setRules(Rules rules) { 791 792 this.rules = rules; 793 this.rules.setDigester(this); 794 795 } 796 797 798 801 public String getSchema() { 802 803 return (this.schemaLocation); 804 805 } 806 807 808 813 public void setSchema(String schemaLocation){ 814 815 this.schemaLocation = schemaLocation; 816 817 } 818 819 820 823 public String getSchemaLanguage() { 824 825 return (this.schemaLanguage); 826 827 } 828 829 830 835 public void setSchemaLanguage(String schemaLanguage){ 836 837 this.schemaLanguage = schemaLanguage; 838 839 } 840 841 842 845 public boolean getUseContextClassLoader() { 846 847 return useContextClassLoader; 848 849 } 850 851 852 861 public void setUseContextClassLoader(boolean use) { 862 863 useContextClassLoader = use; 864 865 } 866 867 868 871 public boolean getValidating() { 872 873 return (this.validating); 874 875 } 876 877 878 884 public void setValidating(boolean validating) { 885 886 this.validating = validating; 887 888 } 889 890 891 898 public XMLReader getXMLReader() throws SAXException { 899 if (reader == null){ 900 reader = getParser().getXMLReader(); 901 } 902 903 reader.setDTDHandler(this); 904 reader.setContentHandler(this); 905 906 if (entityResolver == null){ 907 reader.setEntityResolver(this); 908 } else { 909 reader.setEntityResolver(entityResolver); 910 } 911 912 reader.setErrorHandler(this); 913 return reader; 914 } 915 916 918 919 929 public void characters(char buffer[], int start, int length) 930 throws SAXException { 931 932 if (saxLog.isDebugEnabled()) { 933 saxLog.debug("characters(" + new String (buffer, start, length) + ")"); 934 } 935 936 bodyText.append(buffer, start, length); 937 938 } 939 940 941 946 public void endDocument() throws SAXException { 947 948 if (saxLog.isDebugEnabled()) { 949 if (getCount() > 1) { 950 saxLog.debug("endDocument(): " + getCount() + 951 " elements left"); 952 } else { 953 saxLog.debug("endDocument()"); 954 } 955 } 956 957 while (getCount() > 1) { 958 pop(); 959 } 960 961 Iterator rules = getRules().rules().iterator(); 963 while (rules.hasNext()) { 964 Rule rule = (Rule) rules.next(); 965 try { 966 rule.finish(); 967 } catch (Exception e) { 968 log.error("Finish event threw exception", e); 969 throw createSAXException(e); 970 } catch (Error e) { 971 log.error("Finish event threw error", e); 972 throw e; 973 } 974 } 975 976 clear(); 978 979 } 980 981 982 994 public void endElement(String namespaceURI, String localName, 995 String qName) throws SAXException { 996 997 boolean debug = log.isDebugEnabled(); 998 999 if (debug) { 1000 if (saxLog.isDebugEnabled()) { 1001 saxLog.debug("endElement(" + namespaceURI + "," + localName + 1002 |