| 1 17 18 package org.apache.commons.digester; 19 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.util.EmptyStackException ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.parsers.SAXParser ; 36 import javax.xml.parsers.SAXParserFactory ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.commons.collections.ArrayStack; 41 42 import org.xml.sax.Attributes ; 43 import org.xml.sax.ContentHandler ; 44 import org.xml.sax.EntityResolver ; 45 import org.xml.sax.ErrorHandler ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.Locator ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.SAXNotRecognizedException ; 50 import org.xml.sax.SAXNotSupportedException ; 51 import org.xml.sax.SAXParseException ; 52 import org.xml.sax.XMLReader ; 53 import org.xml.sax.helpers.DefaultHandler ; 54 55 56 57 58 77 78 public class Digester extends DefaultHandler { 79 80 81 83 84 87 public Digester() { 88 89 super(); 90 91 } 92 93 94 101 public Digester(SAXParser parser) { 102 103 super(); 104 105 this.parser = parser; 106 107 } 108 109 110 117 public Digester(XMLReader reader) { 118 119 super(); 120 121 this.reader = reader; 122 123 } 124 125 126 128 129 132 protected StringBuffer bodyText = new StringBuffer (); 133 134 135 138 protected ArrayStack bodyTexts = new ArrayStack(); 139 140 141 151 protected ArrayStack matches = new ArrayStack(10); 152 153 159 protected ClassLoader classLoader = null; 160 161 162 165 protected boolean configured = false; 166 167 168 171 protected EntityResolver entityResolver; 172 173 177 protected HashMap entityValidator = new HashMap (); 178 179 180 184 protected ErrorHandler errorHandler = null; 185 186 187 190 protected SAXParserFactory factory = null; 191 192 195 protected String JAXP_SCHEMA_LANGUAGE = 196 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 197 198 199 202 protected Locator locator = null; 203 204 205 208 protected String match = ""; 209 210 211 214 protected boolean namespaceAware = false; 215 216 217 225 protected HashMap namespaces = new HashMap (); 226 227 228 232 protected ArrayStack params = new ArrayStack(); 233 234 235 238 protected SAXParser parser = null; 239 240 241 245 protected String publicId = null; 246 247 248 251 protected XMLReader reader = null; 252 253 254 258 protected Object root = null; 259 260 261 267 protected Rules rules = null; 268 269 273 protected String schemaLanguage = W3C_XML_SCHEMA; 274 275 276 279 protected String schemaLocation = null; 280 281 282 285 protected ArrayStack stack = new ArrayStack(); 286 287 288 292 protected boolean useContextClassLoader = false; 293 294 295 298 protected boolean validating = false; 299 300 301 304 protected Log log = 305 LogFactory.getLog("org.apache.commons.digester.Digester"); 306 307 308 311 protected Log saxLog = 312 LogFactory.getLog("org.apache.commons.digester.Digester.sax"); 313 314 315 318 protected static final String W3C_XML_SCHEMA = 319 "http://www.w3.org/2001/XMLSchema"; 320 321 325 protected Substitutor substitutor; 326 327 328 private HashMap stacksByName = new HashMap (); 329 330 339 private ContentHandler customContentHandler = null; 340 341 343 350 public String findNamespaceURI(String prefix) { 351 352 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 353 if (stack == null) { 354 return (null); 355 } 356 try { 357 return ((String ) stack.peek()); 358 } catch (EmptyStackException e) { 359 return (null); 360 } 361 362 } 363 364 365 375 public ClassLoader getClassLoader() { 376 377 if (this.classLoader != null) { 378 return (this.classLoader); 379 } 380 if (this.useContextClassLoader) { 381 ClassLoader classLoader = 382 Thread.currentThread().getContextClassLoader(); 383 if (classLoader != null) { 384 return (classLoader); 385 } 386 } 387 return (this.getClass().getClassLoader()); 388 389 } 390 391 392 399 public void setClassLoader(ClassLoader classLoader) { 400 401 this.classLoader = classLoader; 402 403 } 404 405 406 409 public int getCount() { 410 411 return (stack.size()); 412 413 } 414 415 416 419 public String getCurrentElementName() { 420 421 String elementName = match; 422 int lastSlash = elementName.lastIndexOf('/'); 423 if (lastSlash >= 0) { 424 elementName = elementName.substring(lastSlash + 1); 425 } 426 return (elementName); 427 428 } 429 430 431 438 public int getDebug() { 439 440 return (0); 441 442 } 443 444 445 455 public void setDebug(int debug) { 456 457 ; 459 } 460 461 462 465 public ErrorHandler getErrorHandler() { 466 467 return (this.errorHandler); 468 469 } 470 471 472 477 public void setErrorHandler(ErrorHandler errorHandler) { 478 479 this.errorHandler = errorHandler; 480 481 } 482 483 484 487 public SAXParserFactory getFactory() { 488 489 if (factory == null) { 490 factory = SAXParserFactory.newInstance(); 491 factory.setNamespaceAware(namespaceAware); 492 factory.setValidating(validating); 493 } 494 return (factory); 495 496 } 497 498 499 514 public boolean getFeature(String feature) 515 throws ParserConfigurationException , SAXNotRecognizedException , 516 SAXNotSupportedException { 517 518 return (getFactory().getFeature(feature)); 519 520 } 521 522 523 542 public void setFeature(String feature, boolean value) 543 throws ParserConfigurationException , SAXNotRecognizedException , 544 SAXNotSupportedException { 545 546 getFactory().setFeature(feature, value); 547 548 } 549 550 551 554 public Log getLogger() { 555 556 return log; 557 558 } 559 560 561 564 public void setLogger(Log log) { 565 566 this.log = log; 567 568 } 569 570 576 public Log getSAXLogger() { 577 578 return saxLog; 579 } 580 581 582 589 public void setSAXLogger(Log saxLog) { 590 591 this.saxLog = saxLog; 592 } 593 594 597 public String getMatch() { 598 599 return match; 600 601 } 602 603 604 607 public boolean getNamespaceAware() { 608 609 return (this.namespaceAware); 610 611 } 612 613 614 619 public void setNamespaceAware(boolean namespaceAware) { 620 621 this.namespaceAware = namespaceAware; 622 623 } 624 625 626 630 public void setPublicId(String publicId){ 631 this.publicId = publicId; 632 } 633 634 635 639 public String getPublicId() { 640 641 return (this.publicId); 642 643 } 644 645 646 650 public String getRuleNamespaceURI() { 651 652 return (getRules().getNamespaceURI()); 653 654 } 655 656 657 665 public void setRuleNamespaceURI(String ruleNamespaceURI) { 666 667 getRules().setNamespaceURI(ruleNamespaceURI); 668 669 } 670 671 672 676 public SAXParser getParser() { 677 678 if (parser != null) { 680 return (parser); 681 } 682 683 try { 685 if (validating) { 686 Properties properties = new Properties (); 687 properties.put("SAXParserFactory", getFactory()); 688 if (schemaLocation != null) { 689 properties.put("schemaLocation", schemaLocation); 690 properties.put("schemaLanguage", schemaLanguage); 691 } 692 parser = ParserFeatureSetterFactory.newSAXParser(properties); } else { 693 parser = getFactory().newSAXParser(); 694 } 695 } catch (Exception e) { 696 log.error("Digester.getParser: ", e); 697 return (null); 698 } 699 700 return (parser); 701 702 } 703 704 705 718 public Object getProperty(String property) 719 throws SAXNotRecognizedException , SAXNotSupportedException { 720 721 return (getParser().getProperty(property)); 722 723 } 724 725 726 740 public void setProperty(String property, Object value) 741 throws SAXNotRecognizedException , SAXNotSupportedException { 742 743 getParser().setProperty(property, value); 744 745 } 746 747 748 755 public XMLReader getReader() { 756 757 try { 758 return (getXMLReader()); 759 } catch (SAXException e) { 760 log.error("Cannot get XMLReader", e); 761 return (null); 762 } 763 764 } 765 766 767 772 public Rules getRules() { 773 774 if (this.rules == null) { 775 this.rules = new RulesBase(); 776 this.rules.setDigester(this); 777 } 778 return (this.rules); 779 780 } 781 782 783 789 public void setRules(Rules rules) { 790 791 this.rules = rules; 792 this.rules.setDigester(this); 793 794 } 795 796 797 800 public String getSchema() { 801 802 return (this.schemaLocation); 803 804 } 805 806 807 812 public void setSchema(String schemaLocation){ 813 814 this.schemaLocation = schemaLocation; 815 816 } 817 818 819 822 public String getSchemaLanguage() { 823 824 return (this.schemaLanguage); 825 826 } 827 828 829 834 public void setSchemaLanguage(String schemaLanguage){ 835 836 this.schemaLanguage = schemaLanguage; 837 838 } 839 840 841 844 public boolean getUseContextClassLoader() { 845 846 return useContextClassLoader; 847 848 } 849 850 851 860 public void setUseContextClassLoader(boolean use) { 861 862 useContextClassLoader = use; 863 864 } 865 866 867 870 public boolean getValidating() { 871 872 return (this.validating); 873 874 } 875 876 877 883 public void setValidating(boolean validating) { 884 885 this.validating = validating; 886 887 } 888 889 890 897 public XMLReader getXMLReader() throws SAXException { 898 if (reader == null){ 899 reader = getParser().getXMLReader(); 900 } 901 902 reader.setDTDHandler(this); 903 reader.setContentHandler(this); 904 905 if (entityResolver == null){ 906 reader.setEntityResolver(this); 907 } else { 908 reader.setEntityResolver(entityResolver); 909 } 910 911 reader.setErrorHandler(this); 912 return reader; 913 } 914 915 919 public Substitutor getSubstitutor() { 920 return substitutor; 921 } 922 923 928 public void setSubstitutor(Substitutor substitutor) { 929 this.substitutor = substitutor; 930 } 931 932 937 public ContentHandler getCustomContentHandler() { 938 return customContentHandler; 939 } 940 941 975 public void setCustomContentHandler(ContentHandler handler) { 976 customContentHandler = handler; 977 } 978 979 981 982 992 public void characters(char buffer[], int start, int length) 993 throws SAXException { 994 995 if (customContentHandler != null) { 996 customContentHandler.characters(buffer, start, length); 998 return; 999 } 1000 1001 if (saxLog.isDebugEnabled()) { 1002 saxLog.debug("characters(" + new String (buffer, start, length) + ")"); 1003 } 1004 1005 bodyText.append(buffer, start, length); 1006 1007 } 1008 1009 1010 1015 public void endDocument() throws SAXException { 1016 1017 if (saxLog.isDebugEnabled()) { 1018 if (getCount() > 1) { 1019 saxLog.debug("endDocument(): " + getCount() + 1020 " elements left"); 1021 } else { 1022 saxLog.debug("endDocument()"); 1023 } 1024 } 1025 1026 Iterator rules = getRules().rules().
|