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 "," + qName + ")"); 1003 } 1004 log.debug(" match='" + match + "'"); 1005 log.debug(" bodyText='" + bodyText + "'"); 1006 } 1007 1008 bodyText = updateBodyText(bodyText); 1010 1011 String name = localName; 1014 if ((name == null) || (name.length() < 1)) { 1015 name = qName; 1016 } 1017 1018 List rules = (List ) matches.pop(); 1020 if ((rules != null) && (rules.size() > 0)) { 1021 String bodyText = this.bodyText.toString(); 1022 for (int i = 0; i < rules.size(); i++) { 1023 try { 1024 Rule rule = (Rule) rules.get(i); 1025 if (debug) { 1026 log.debug(" Fire body() for " + rule); 1027 } 1028 rule.body(namespaceURI, name, bodyText); 1029 } catch (Exception e) { 1030 log.error("Body event threw exception", e); 1031 throw createSAXException(e); 1032 } catch (Error e) { 1033 log.error("Body event threw error", e); 1034 throw e; 1035 } 1036 } 1037 } else { 1038 if (debug) { 1039 log.debug(" No rules found matching '" + match + "'."); 1040 } 1041 } 1042 1043 bodyText = (StringBuffer ) bodyTexts.pop(); 1045 if (debug) { 1046 log.debug(" Popping body text '" + bodyText.toString() + "'"); 1047 } 1048 1049 if (rules != null) { 1051 for (int i = 0; i < rules.size(); i++) { 1052 int j = (rules.size() - i) - 1; 1053 try { 1054 Rule rule = (Rule) rules.get(j); 1055 if (debug) { 1056 log.debug(" Fire end() for " + rule); 1057 } 1058 rule.end(namespaceURI, name); 1059 } catch (Exception e) { 1060 log.error("End event threw exception", e); 1061 throw createSAXException(e); 1062 } catch (Error e) { 1063 log.error("End event threw error", e); 1064 throw e; 1065 } 1066 } 1067 } 1068 1069 int slash = match.lastIndexOf('/'); 1071 if (slash >= 0) { 1072 match = match.substring(0, slash); 1073 } else { 1074 match = ""; 1075 } 1076 1077 } 1078 1079 1080 1087 public void endPrefixMapping(String prefix) throws SAXException { 1088 1089 if (saxLog.isDebugEnabled()) { 1090 saxLog.debug("endPrefixMapping(" + prefix + ")"); 1091 } 1092 1093 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 1095 if (stack == null) { 1096 return; 1097 } 1098 try { 1099 stack.pop(); 1100 if (stack.empty()) 1101 namespaces.remove(prefix); 1102 } catch (EmptyStackException e) { 1103 throw createSAXException("endPrefixMapping popped too many times"); 1104 } 1105 1106 } 1107 1108 1109 1119 public void ignorableWhitespace(char buffer[], int start, int len) 1120 throws SAXException { 1121 1122 if (saxLog.isDebugEnabled()) { 1123 saxLog.debug("ignorableWhitespace(" + 1124 new String (buffer, start, len) + ")"); 1125 } 1126 1127 ; 1129 } 1130 1131 1132 1140 public void processingInstruction(String target, String data) 1141 throws SAXException { 1142 1143 if (saxLog.isDebugEnabled()) { 1144 saxLog.debug("processingInstruction('" + target + "','" + data + "')"); 1145 } 1146 1147 ; 1149 } 1150 1151 1152 1157 public Locator getDocumentLocator() { 1158 1159 return locator; 1160 1161 } 1162 1163 1168 public void setDocumentLocator(Locator locator) { 1169 1170 if (saxLog.isDebugEnabled()) { 1171 saxLog.debug("setDocumentLocator(" + locator + ")"); 1172 } 1173 1174 this.locator = locator; 1175 1176 } 1177 1178 1179 1186 public void skippedEntity(String name) throws SAXException { 1187 1188 if (saxLog.isDebugEnabled()) { 1189 saxLog.debug("skippedEntity(" + name + ")"); 1190 } 1191 1192 ; 1194 } 1195 1196 1197 1202 public void startDocument() throws SAXException { 1203 1204 if (saxLog.isDebugEnabled()) { 1205 saxLog.debug("startDocument()"); 1206 } 1207 1208 configure(); 1212 } 1213 1214 1215 1228 public void startElement(String namespaceURI, String localName, 1229 String qName, Attributes list) 1230 throws SAXException { 1231 boolean debug = log.isDebugEnabled(); 1232 1233 if (saxLog.isDebugEnabled()) { 1234 saxLog.debug("startElement(" + namespaceURI + "," + localName + "," + 1235 qName + ")"); 1236 } 1237 1238 list = updateAttributes(list); 1240 1241 bodyTexts.push(bodyText); 1243 if (debug) { 1244 log.debug(" Pushing body text '" + bodyText.toString() + "'"); 1245 } 1246 bodyText = new StringBuffer (); 1247 1248 String name = localName; 1251 if ((name == null) || (name.length() < 1)) { 1252 name = qName; 1253 } 1254 1255 StringBuffer sb = new StringBuffer (match); 1257 if (match.length() > 0) { 1258 sb.append('/'); 1259 } 1260 sb.append(name); 1261 match = sb.toString(); 1262 if (debug) { 1263 log.debug(" New match='" + match + "'"); 1264 } 1265 1266 List rules = getRules().match(namespaceURI, match); 1268 matches.push(rules); 1269 if ((rules != null) && (rules.size() > 0)) { 1270 for (int i = 0; i < rules.size(); i++) { 1271 try { 1272 Rule rule = (Rule) rules.get(i); 1273 if (debug) { 1274 log.debug(" Fire begin() for " + rule); 1275 } 1276 rule.begin(namespaceURI, name, list); 1277 } catch (Exception e) { 1278 log.error("Begin event threw exception", e); 1279 throw createSAXException(e); 1280 } catch (Error e) { 1281 log.error("Begin event threw error", e); 1282 throw e; 1283 } 1284 } 1285 } else { 1286 if (debug) { 1287 log.debug(" No rules found matching '" + match + "'."); 1288 } 1289 } 1290 1291 } 1292 1293 1294 1302 public void startPrefixMapping(String prefix, String namespaceURI) 1303 throws SAXException { 1304 1305 if (saxLog.isDebugEnabled()) { 1306 saxLog.debug("startPrefixMapping(" + prefix + "," + namespaceURI + ")"); 1307 } 1308 1309 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 1311 if (stack == null) { 1312 stack = new ArrayStack(); 1313 namespaces.put(prefix, stack); 1314 } 1315 stack.push(namespaceURI); 1316 1317 } 1318 1319 1320 1322 1323 1330 public void notationDecl(String name, String publicId, String systemId) { 1331 1332 if (saxLog.isDebugEnabled()) { 1333 saxLog.debug("notationDecl(" + name + "," + publicId + "," + 1334 systemId + ")"); 1335 } 1336 1337 } 1338 1339 1340 1348 public void unparsedEntityDecl(String name, String publicId, 1349 String systemId, String notation) { 1350 1351 if (saxLog.isDebugEnabled()) { 1352 saxLog.debug("unparsedEntityDecl(" + name + "," + publicId + "," + 1353 systemId + "," + notation + ")"); 1354 } 1355 1356 } 1357 1358 1359 1361 1367 public void setEntityResolver(EntityResolver entityResolver){ 1368 this.entityResolver = entityResolver; 1369 } 1370 1371 1372 1376 public EntityResolver getEntityResolver(){ 1377 return entityResolver; 1378 } 1379 1380 1389 public InputSource resolveEntity(String publicId, String systemId) 1390 throws SAXException { 1391 1392 if (saxLog.isDebugEnabled()) { 1393 saxLog.debug("resolveEntity('" + publicId + "', '" + systemId + "')"); 1394 } 1395 1396 if (publicId != null) 1397 this.publicId = publicId; 1398 1399 String entityURL = null; 1401 if (publicId != null) { 1402 entityURL = (String ) entityValidator.get(publicId); 1403 } 1404 1405 if (schemaLocation != null && entityURL == null && systemId != null){ 1407 entityURL = (String )entityValidator.get(systemId); 1408 } 1409 1410 if (entityURL == null) { 1411 if (systemId == null) { 1412 if (log.isDebugEnabled()) { 1414 log.debug(" Cannot resolve entity: '" + entityURL + "'"); 1415 } 1416 return (null); 1417 1418 } else { 1419 if (log.isDebugEnabled()) { 1421 log.debug(" Trying to resolve using system ID '" + systemId + "'"); 1422 } 1423 entityURL = systemId; 1424 } 1425 } 1426 1427 if (log.isDebugEnabled()) { 1429 log.debug(" Resolving to alternate DTD '" + entityURL + "'"); 1430 } 1431 1432 try { 1433 return (new InputSource (entityURL)); 1434 } catch (Exception e) { 1435 throw createSAXException(e); 1436 } 1437 } 1438 1439 1440 1442 1443 1451 public void error(SAXParseException exception) throws SAXException { 1452 1453 log.error("Parse Error at line " + exception.getLineNumber() + 1454 " column " + exception.getColumnNumber() + ": " + 1455 exception.getMessage(), exception); 1456 if (errorHandler != null) { 1457 errorHandler.error(exception); 1458 } 1459 1460 } 1461 1462 1463 1471 public void fatalError(SAXParseException exception) throws SAXException { 1472 1473 log.error("Parse Fatal Error at line " + exception.getLineNumber() + 1474 " column " + exception.getColumnNumber() + ": " + 1475 exception.getMessage(), exception); 1476 if (errorHandler != null) { 1477 errorHandler.fatalError(exception); 1478 } 1479 1480 } 1481 1482 1483 1491 public void warning(SAXParseException exception) throws SAXException { 1492 if (errorHandler != null) { 1493 log.warn("Parse Warning Error at line " + exception.getLineNumber() + 1494 " column " + exception.getColumnNumber() + ": " + 1495 exception.getMessage(), exception); 1496 1497 errorHandler.warning(exception); 1498 } 1499 1500 } 1501 1502 1503 1505 1506 1512 public void log(String message) { 1513 1514 log.info(message); 1515 1516 } 1517 1518 1519 1525 public void log(String message, Throwable exception) { 1526 1527 log.error(message, exception); 1528 1529 } 1530 1531 1532 1541 public Object parse(File file) throws IOException , SAXException { 1542 1543 configure(); 1544 InputSource input = new InputSource (new FileInputStream (file)); 1545 input.setSystemId("file://" + file.getAbsolutePath()); 1546 getXMLReader().parse(input); 1547 return (root); 1548 1549 } 1550 1559 public Object parse(InputSource input) throws IOException , SAXException { 1560 1561 configure(); 1562 getXMLReader().parse(input); 1563 return (root); 1564 1565 } 1566 1567 1568 1577 public Object parse(InputStream input) throws IOException , SAXException { 1578 1579 configure(); 1580 InputSource is = new InputSource (input); 1581 getXMLReader().parse(is); 1582 return (root); 1583 1584 } 1585 1586 1587 1596 public Object parse(Reader reader) throws IOException , SAXException { 1597 1598 configure(); 1599 InputSource is = new InputSource (reader); 1600 getXMLReader().parse(is); 1601 return (root); 1602 1603 } 1604 1605 1606 1615 public Object parse(String uri) throws IOException , SAXException { 1616 1617 configure(); 1618 InputSource is = new InputSource (uri); 1619 getXMLReader().parse(is); 1620 return (root); 1621 1622 } 1623 1624 1625 1646 public void register(String publicId, String entityURL) { 1647 1648 if (log.isDebugEnabled()) { 1649 log.debug("register('" + publicId + "', '" + entityURL + "'"); 1650 } 1651 entityValidator.put(publicId, entityURL); 1652 1653 } 1654 1655 1656 1658 1659 1666 public void addRule(String pattern, Rule rule) { 1667 1668 rule.setDigester(this); 1669 getRules().add(pattern, rule); 1670 1671 } 1672 1673 1674 1679 public void addRuleSet(RuleSet ruleSet) { 1680 1681 String oldNamespaceURI = getRuleNamespaceURI(); 1682 String newNamespaceURI = ruleSet.getNamespaceURI(); 1683 if (log.isDebugEnabled()) { 1684 if (newNamespaceURI == null) { 1685 log.debug("addRuleSet() with no namespace URI"); 1686 } else { 1687 log.debug("addRuleSet() with namespace URI " + newNamespaceURI); 1688 } 1689 } 1690 setRuleNamespaceURI(newNamespaceURI); 1691 ruleSet.addRuleInstances(this); 1692 setRuleNamespaceURI(oldNamespaceURI); 1693 1694 } 1695 1696 1697 1704 public void addCallMethod(String pattern, String methodName) { 1705 1706 addRule( 1707 pattern, 1708 new CallMethodRule(methodName)); 1709 1710 } 1711 1712 1721 public void addCallMethod(String pattern, String methodName, 1722 int paramCount) { 1723 1724 addRule(pattern, 1725 new CallMethodRule(methodName, paramCount)); 1726 1727 } 1728 1729 1730 1748 public void addCallMethod(String pattern, String methodName, 1749 int paramCount, String paramTypes[]) { 1750 1751 addRule(pattern, 1752 new CallMethodRule( 1753 methodName, 1754 paramCount, 1755 paramTypes)); 1756 1757 } 1758 1759 1760 1777 public void addCallMethod(String pattern, String methodName, 1778 int paramCount, Class paramTypes[]) { 1779 1780 addRule(pattern, 1781 new CallMethodRule( 1782 methodName, 1783 paramCount, 1784 paramTypes)); 1785 1786 } 1787 1788 1789 1797 public void addCallParam(String pattern, int paramIndex) { 1798 1799 addRule(pattern, 1800 new CallParamRule(paramIndex)); 1801 1802 } 1803 1804 1805 1815 public void addCallParam(String pattern, int paramIndex, 1816 String attributeName) { 1817 1818 addRule(pattern, 1819 new CallParamRule(paramIndex, attributeName)); 1820 1821 } 1822 1823 1824 1833 public void addCallParam(String pattern, int paramIndex, boolean fromStack) { 1834 1835 addRule(pattern, 1836 new CallParamRule(paramIndex, fromStack)); 1837 1838 } 1839 1840 1849 public void addCallParam(String pattern, int paramIndex, int stackIndex) { 1850 1851 addRule(pattern, 1852 new CallParamRule(paramIndex, stackIndex)); 1853 1854 } 1855 1856 1865 public void addCallParamPath(String pattern,int paramIndex) { 1866 addRule(pattern, new PathCallParamRule(paramIndex)); 1867 } 1868 1869 1889 public void addObjectParam(String pattern, int paramIndex, 1890 Object paramObj) { 1891 1892 addRule(pattern, 1893 new ObjectParamRule(paramIndex, paramObj)); 1894 1895 } 1896 1897 1905 public void addFactoryCreate(String pattern, String className) { 1906 1907 addFactoryCreate(pattern, className, false); 1908 1909 } 1910 1911 1912 1920 public void addFactoryCreate(String pattern, Class clazz) { 1921 1922 addFactoryCreate(pattern, clazz, false); 1923 1924 } 1925 1926 1927 1937 public void addFactoryCreate(String pattern, String className, 1938 String attributeName) { 1939 1940 addFactoryCreate(pattern, className, attributeName, false); 1941 1942 } 1943 1944 1945 1955 public void addFactoryCreate(String pattern, Class clazz, 1956 String attributeName) { 1957 1958 addFactoryCreate(pattern, clazz, attributeName, false); 1959 1960 } 1961 1962 1963 1972 public void addFactoryCreate(String pattern, 1973 ObjectCreationFactory creationFactory) { 1974 1975 addFactoryCreate(pattern, creationFactory, false); 1976 1977 } 1978 1979 1988 public void addFactoryCreate( 1989 String pattern, 1990 String className, 1991 boolean ignoreCreateExceptions) { 1992 1993 addRule( 1994 pattern, 1995 new FactoryCreateRule(className, ignoreCreateExceptions)); 1996 1997 } 1998 1999 2000 2009 public void addFactoryCreate( 2010 String pattern, 2011 Class clazz, 2012 boolean ignoreCreateExceptions) { 2013 2014 addRule( 2015 pattern, 2016 new FactoryCreateRule(clazz, ignoreCreateExceptions)); 2017 2018 } 2019 2020 2021 2032 public void addFactoryCreate( 2033 String pattern, 2034 String className, 2035 String attributeName, 2036 boolean ignoreCreateExceptions) { 2037 2038 addRule( 2039 pattern, 2040 new FactoryCreateRule(className, attributeName, ignoreCreateExceptions)); 2041 2042 } 2043 2044 2045 2056 public void addFactoryCreate( 2057 String pattern, 2058 Class clazz, 2059 String attributeName, 2060 boolean ignoreCreateExceptions) { 2061 2062 addRule( 2063 pattern, 2064 new FactoryCreateRule(clazz, attributeName, ignoreCreateExceptions)); 2065 2066 } 2067 2068 2069 2079 public void addFactoryCreate(String pattern, 2080 ObjectCreationFactory creationFactory, 2081 boolean ignoreCreateExceptions) { 2082 2083 creationFactory.setDigester(this); 2084 addRule(pattern, 2085 new FactoryCreateRule(creationFactory, ignoreCreateExceptions)); 2086 2087 } 2088 2089 2096 public void addObjectCreate(String pattern, String className) { 2097 2098 addRule(pattern, 2099 new ObjectCreateRule(className)); 2100 2101 } 2102 2103 2104 2111 public void addObjectCreate(String pattern, Class clazz) { 2112 2113 addRule(pattern, 2114 new ObjectCreateRule(clazz)); 2115 2116 } 2117 2118 2119 2128 public void addObjectCreate(String pattern, String className, 2129 String attributeName) { 2130 2131 addRule(pattern, 2132 new ObjectCreateRule(className, attributeName)); 2133 2134 } 2135 2136 2137 2146 public void addObjectCreate(String pattern, 2147 String attributeName, 2148 Class clazz) { 2149 2150 addRule(pattern, 2151 new ObjectCreateRule(attributeName, clazz)); 2152 2153 } 2154 2155 2162 public void addSetNext(String pattern, String methodName) { 2163 2164 addRule(pattern, 2165 new SetNextRule(methodName)); 2166 2167 } 2168 2169 2170 2181 public void addSetNext(String pattern, String methodName, 2182 String paramType) { 2183 2184 addRule(pattern, 2185 new SetNextRule(methodName, paramType)); 2186 2187 } 2188 2189 2190 2197 public void addSetRoot(String pattern, String methodName) { 2198 2199 addRule(pattern, 2200 new SetRootRule(methodName)); 2201 2202 } 2203 2204 2205 2213 public void addSetRoot(String pattern, String methodName, 2214 String paramType) { 2215 2216 addRule(pattern, 2217 new SetRootRule(methodName, paramType)); 2218 2219 } 2220 2221 2227 public void addSetProperties(String pattern) { 2228 2229 addRule(pattern, 2230 new SetPropertiesRule()); 2231 2232 } 2233 2234 2243 public void addSetProperties( 2244 String pattern, 2245 String attributeName, 2246 String propertyName) { 2247 2248 addRule(pattern, 2249 new SetPropertiesRule(attributeName, propertyName)); 2250 2251 } 2252 2253 2262 public void addSetProperties( 2263 String pattern, 2264 String [] attributeNames, 2265 String [] propertyNames) { 2266 2267 addRule(pattern, 2268 new SetPropertiesRule(attributeNames, propertyNames)); 2269 2270 } 2271 2272 2273 2281 public void addSetProperty(String pattern, String name, String value) { 2282 2283 addRule(pattern, 2284 new SetPropertyRule(name, value)); 2285 2286 } 2287 2288 2289 2296 public void addSetTop(String pattern, String methodName) { 2297 2298 addRule(pattern, 2299 new SetTopRule(methodName)); 2300 2301 } 2302 2303 2304 2315 public void addSetTop(String pattern, String methodName, 2316 String paramType) { 2317 2318 addRule(pattern, 2319 new SetTopRule(methodName, paramType)); 2320 2321 } 2322 2323 2324 2326 2327 2335 public void clear() { 2336 2337 match = ""; 2338 bodyTexts.clear(); 2339 params.clear(); 2340 publicId = null; 2341 stack.clear(); 2342 log = null; 2343 saxLog = null; 2344 configured = false; 2345 2346 } 2347 2348 2349 public void reset() { 2350 root = null; 2351 setErrorHandler(null); 2352 clear(); 2353 } 2354 2355 2356 2360 public Object peek() { 2361 2362 try { 2363 return (stack.peek()); 2364 } catch (EmptyStackException e) { 2365 log.warn("Empty stack (returning null)"); 2366 return (null); 2367 } 2368 2369 } 2370 2371 2372 2380 public Object peek(int n) { 2381 2382 try { 2383 return (stack.peek(n)); 2384 } catch (EmptyStackException e) { 2385 log.warn("Empty stack (returning null)"); 2386 return (null); 2387 } 2388 2389 } 2390 2391 2392 2396 public Object pop() { 2397 2398 try { 2399 return (stack.pop()); 2400 } catch (EmptyStackException e) { 2401 log.warn("Empty stack (returning null)"); 2402 return (null); 2403 } 2404 2405 } 2406 2407 2408 2413 public void push(Object object) { 2414 2415 if (stack.size() == 0) { 2416 root = object; 2417 } 2418 stack.push(object); 2419 2420 } 2421 2422 2431 public void push(String stackName, Object value) { 2432 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2433 if (namedStack == null) { 2434 namedStack = new ArrayStack(); 2435 stacksByName.put(stackName, namedStack); 2436 } 2437 namedStack.push(value); 2438 } 2439 2440 2453 public Object pop(String stackName) { 2454 Object result = null; 2455 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2456 if (namedStack == null) { 2457 if (log.isDebugEnabled()) { 2458 log.debug("Stack '" + stackName + "' is empty"); 2459 } 2460 throw new EmptyStackException (); 2461 2462 } else { 2463 2464 result = namedStack.pop(); 2465 } 2466 return result; 2467 } 2468 2469 2483 public Object peek(String stackName) { 2484 Object result = null; 2485 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2486 if (namedStack == null ) { 2487 if (log.isDebugEnabled()) { 2488 log.debug("Stack '" + stackName + "' is empty"); 2489 } 2490 throw new EmptyStackException (); 2491 2492 } else { 2493 2494 result = namedStack.peek(); 2495 } 2496 return result; 2497 } 2498 2499 2509 public boolean isEmpty(String stackName) { 2510 boolean result = true; 2511 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2512 if (namedStack != null ) { 2513 result = namedStack.isEmpty(); 2514 } 2515 return result; 2516 } 2517 2518 2526 public Object getRoot() { 2527 return root; 2528 } 2529 2530 2531 2533 2534 2536 2537 2551 protected void configure() { 2552 2553 if (configured) { 2555 return; 2556 } 2557 2558 log = LogFactory.getLog("org.apache.commons.digester.Digester"); 2559 saxLog = LogFactory.getLog("org.apache.commons.digester.Digester.sax"); 2560 2561 initialize(); 2565 configured = true; 2567 2568 } 2569 2570 2588 protected void initialize() { 2589 2590 ; 2593 } 2594 2595 2597 2598 2601 Map getRegistrations() { 2602 2603 return (entityValidator); 2604 2605 } 2606 2607 2608 2621 List getRules(String match) { 2622 2623 return (getRules().match(match)); 2624 2625 } 2626 2627 2628 2635 public Object peekParams() { 2636 2637 try { 2638 return (params.peek()); 2639 } catch (EmptyStackException e) { 2640 log.warn("Empty stack (returning null)"); 2641 return (null); 2642 } 2643 2644 } 2645 2646 2647 2658 public Object peekParams(int n) { 2659 2660 try { 2661 return (params.peek(n)); 2662 } catch (EmptyStackException e) { 2663 log.warn("Empty stack (returning null)"); 2664 return (null); 2665 } 2666 2667 } 2668 2669 2670 2677 public Object popParams() { 2678 2679 try { 2680 if (log.isTraceEnabled()) { 2681 log.trace("Popping params"); 2682 } 2683 return (params.pop()); 2684 } catch (EmptyStackException e) { 2685 log.warn("Empty stack (returning null)"); 2686 return (null); 2687 } 2688 2689 } 2690 2691 2692 2700 public void pushParams(Object object) { 2701 if (log.isTraceEnabled()) { 2702 log.trace("Pushing params"); 2703 } 2704 params.push(object); 2705 2706 } 2707 2708 2714 public SAXException createSAXException(String message, Exception e) { 2715 if ((e != null) && 2716 (e instanceof InvocationTargetException )) { 2717 Throwable t = ((InvocationTargetException ) e).getTargetException(); 2718 if ((t != null) && (t instanceof Exception )) { 2719 e = (Exception ) t; 2720 } 2721 } 2722 if (locator != null) { 2723 String error = "Error at (" + locator.getLineNumber() + ", " + 2724 locator.getColumnNumber() + ": " + message; 2725 if (e != null) { 2726 return new SAXParseException (error, locator, e); 2727 } else { 2728 return new SAXParseException (error, locator); 2729 } 2730 } 2731 log.error("No Locator!"); 2732 if (e != null) { 2733 return new SAXException (message, e); 2734 } else { 2735 return new SAXException (message); 2736 } 2737 } 2738 2739 2745 public SAXException createSAXException(Exception e) { 2746 if (e instanceof InvocationTargetException ) { 2747 Throwable t = ((InvocationTargetException ) e).getTargetException(); 2748 if ((t != null) && (t instanceof Exception )) { 2749 e = (Exception ) t; 2750 } 2751 } 2752 return createSAXException(e.getMessage(), e); 2753 } 2754 2755 2761 public SAXException createSAXException(String message) { 2762 return createSAXException(message, null); 2763 } 2764 2765 2766 2768 2769 2774 private Attributes updateAttributes(Attributes list) { 2775 2776 if (list.getLength() == 0) { 2777 return list; 2778 } 2779 2780 AttributesImpl newAttrs = new AttributesImpl (list); 2781 int nAttributes = newAttrs.getLength(); 2782 for (int i = 0; i < nAttributes; ++i) { 2783 String value = newAttrs.getValue(i); 2784 try { 2785 String newValue = 2786 IntrospectionUtils.replaceProperties(value, null, source); 2787 if (value != newValue) { 2788 newAttrs.setValue(i, newValue); 2789 } 2790 } 2791 catch (Exception e) { 2792 } 2794 } 2795 2796 return newAttrs; 2797 2798 } 2799 2800 2801 2806 private StringBuffer updateBodyText(StringBuffer bodyText) { 2807 String in = bodyText.toString(); 2808 String out; 2809 try { 2810 out = IntrospectionUtils.replaceProperties(in, null, source); 2811 } catch(Exception e) { 2812 return bodyText; } 2814 2815 if (out == in) { 2816 return bodyText; 2819 } else { 2820 return new StringBuffer (out); 2821 } 2822 } 2823 2824 2825} 2826 | Popular Tags |