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().iterator(); 1028 while (rules.hasNext()) { 1029 Rule rule = (Rule) rules.next(); 1030 try { 1031 rule.finish(); 1032 } catch (Exception e) { 1033 log.error("Finish event threw exception", e); 1034 throw createSAXException(e); 1035 } catch (Error e) { 1036 log.error("Finish event threw error", e); 1037 throw e; 1038 } 1039 } 1040 1041 clear(); 1043 1044 } 1045 1046 1047 1059 public void endElement(String namespaceURI, String localName, 1060 String qName) throws SAXException { 1061 1062 if (customContentHandler != null) { 1063 customContentHandler.endElement(namespaceURI, localName, qName); 1065 return; 1066 } 1067 1068 boolean debug = log.isDebugEnabled(); 1069 1070 if (debug) { 1071 if (saxLog.isDebugEnabled()) { 1072 saxLog.debug("endElement(" + namespaceURI + "," + localName + 1073 "," + qName + ")"); 1074 } 1075 log.debug(" match='" + match + "'"); 1076 log.debug(" bodyText='" + bodyText + "'"); 1077 } 1078 1079 String name = localName; 1082 if ((name == null) || (name.length() < 1)) { 1083 name = qName; 1084 } 1085 1086 List rules = (List ) matches.pop(); 1088 if ((rules != null) && (rules.size() > 0)) { 1089 String bodyText = this.bodyText.toString(); 1090 Substitutor substitutor = getSubstitutor(); 1091 if (substitutor!= null) { 1092 bodyText = substitutor.substitute(bodyText); 1093 } 1094 for (int i = 0; i < rules.size(); i++) { 1095 try { 1096 Rule rule = (Rule) rules.get(i); 1097 if (debug) { 1098 log.debug(" Fire body() for " + rule); 1099 } 1100 rule.body(namespaceURI, name, bodyText); 1101 } catch (Exception e) { 1102 log.error("Body event threw exception", e); 1103 throw createSAXException(e); 1104 } catch (Error e) { 1105 log.error("Body event threw error", e); 1106 throw e; 1107 } 1108 } 1109 } else { 1110 if (debug) { 1111 log.debug(" No rules found matching '" + match + "'."); 1112 } 1113 } 1114 1115 bodyText = (StringBuffer ) bodyTexts.pop(); 1117 if (debug) { 1118 log.debug(" Popping body text '" + bodyText.toString() + "'"); 1119 } 1120 1121 if (rules != null) { 1123 for (int i = 0; i < rules.size(); i++) { 1124 int j = (rules.size() - i) - 1; 1125 try { 1126 Rule rule = (Rule) rules.get(j); 1127 if (debug) { 1128 log.debug(" Fire end() for " + rule); 1129 } 1130 rule.end(namespaceURI, name); 1131 } catch (Exception e) { 1132 log.error("End event threw exception", e); 1133 throw createSAXException(e); 1134 } catch (Error e) { 1135 log.error("End event threw error", e); 1136 throw e; 1137 } 1138 } 1139 } 1140 1141 int slash = match.lastIndexOf('/'); 1143 if (slash >= 0) { 1144 match = match.substring(0, slash); 1145 } else { 1146 match = ""; 1147 } 1148 1149 } 1150 1151 1152 1159 public void endPrefixMapping(String prefix) throws SAXException { 1160 1161 if (saxLog.isDebugEnabled()) { 1162 saxLog.debug("endPrefixMapping(" + prefix + ")"); 1163 } 1164 1165 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 1167 if (stack == null) { 1168 return; 1169 } 1170 try { 1171 stack.pop(); 1172 if (stack.empty()) 1173 namespaces.remove(prefix); 1174 } catch (EmptyStackException e) { 1175 throw createSAXException("endPrefixMapping popped too many times"); 1176 } 1177 1178 } 1179 1180 1181 1191 public void ignorableWhitespace(char buffer[], int start, int len) 1192 throws SAXException { 1193 1194 if (saxLog.isDebugEnabled()) { 1195 saxLog.debug("ignorableWhitespace(" + 1196 new String (buffer, start, len) + ")"); 1197 } 1198 1199 ; 1201 } 1202 1203 1204 1212 public void processingInstruction(String target, String data) 1213 throws SAXException { 1214 1215 if (customContentHandler != null) { 1216 customContentHandler.processingInstruction(target, data); 1218 return; 1219 } 1220 1221 if (saxLog.isDebugEnabled()) { 1222 saxLog.debug("processingInstruction('" + target + "','" + data + "')"); 1223 } 1224 1225 ; 1227 } 1228 1229 1230 1235 public Locator getDocumentLocator() { 1236 1237 return locator; 1238 1239 } 1240 1241 1246 public void setDocumentLocator(Locator locator) { 1247 1248 if (saxLog.isDebugEnabled()) { 1249 saxLog.debug("setDocumentLocator(" + locator + ")"); 1250 } 1251 1252 this.locator = locator; 1253 1254 } 1255 1256 1257 1264 public void skippedEntity(String name) throws SAXException { 1265 1266 if (saxLog.isDebugEnabled()) { 1267 saxLog.debug("skippedEntity(" + name + ")"); 1268 } 1269 1270 ; 1272 } 1273 1274 1275 1280 public void startDocument() throws SAXException { 1281 1282 if (saxLog.isDebugEnabled()) { 1283 saxLog.debug("startDocument()"); 1284 } 1285 1286 configure(); 1290 } 1291 1292 1293 1306 public void startElement(String namespaceURI, String localName, 1307 String qName, Attributes list) 1308 throws SAXException { 1309 boolean debug = log.isDebugEnabled(); 1310 1311 if (customContentHandler != null) { 1312 customContentHandler.startElement(namespaceURI, localName, qName, list); 1314 return; 1315 } 1316 1317 if (saxLog.isDebugEnabled()) { 1318 saxLog.debug("startElement(" + namespaceURI + "," + localName + "," + 1319 qName + ")"); 1320 } 1321 1322 bodyTexts.push(bodyText); 1324 if (debug) { 1325 log.debug(" Pushing body text '" + bodyText.toString() + "'"); 1326 } 1327 bodyText = new StringBuffer (); 1328 1329 String name = localName; 1332 if ((name == null) || (name.length() < 1)) { 1333 name = qName; 1334 } 1335 1336 StringBuffer sb = new StringBuffer (match); 1338 if (match.length() > 0) { 1339 sb.append('/'); 1340 } 1341 sb.append(name); 1342 match = sb.toString(); 1343 if (debug) { 1344 log.debug(" New match='" + match + "'"); 1345 } 1346 1347 List rules = getRules().match(namespaceURI, match); 1349 matches.push(rules); 1350 if ((rules != null) && (rules.size() > 0)) { 1351 Substitutor substitutor = getSubstitutor(); 1352 if (substitutor!= null) { 1353 list = substitutor.substitute(list); 1354 } 1355 for (int i = 0; i < rules.size(); i++) { 1356 try { 1357 Rule rule = (Rule) rules.get(i); 1358 if (debug) { 1359 log.debug(" Fire begin() for " + rule); 1360 } 1361 rule.begin(namespaceURI, name, list); 1362 } catch (Exception e) { 1363 log.error("Begin event threw exception", e); 1364 throw createSAXException(e); 1365 } catch (Error e) { 1366 log.error("Begin event threw error", e); 1367 throw e; 1368 } 1369 } 1370 } else { 1371 if (debug) { 1372 log.debug(" No rules found matching '" + match + "'."); 1373 } 1374 } 1375 1376 } 1377 1378 1379 1387 public void startPrefixMapping(String prefix, String namespaceURI) 1388 throws SAXException { 1389 1390 if (saxLog.isDebugEnabled()) { 1391 saxLog.debug("startPrefixMapping(" + prefix + "," + namespaceURI + ")"); 1392 } 1393 1394 ArrayStack stack = (ArrayStack) namespaces.get(prefix); 1396 if (stack == null) { 1397 stack = new ArrayStack(); 1398 namespaces.put(prefix, stack); 1399 } 1400 stack.push(namespaceURI); 1401 1402 } 1403 1404 1405 1407 1408 1415 public void notationDecl(String name, String publicId, String systemId) { 1416 1417 if (saxLog.isDebugEnabled()) { 1418 saxLog.debug("notationDecl(" + name + "," + publicId + "," + 1419 systemId + ")"); 1420 } 1421 1422 } 1423 1424 1425 1433 public void unparsedEntityDecl(String name, String publicId, 1434 String systemId, String notation) { 1435 1436 if (saxLog.isDebugEnabled()) { 1437 saxLog.debug("unparsedEntityDecl(" + name + "," + publicId + "," + 1438 systemId + "," + notation + ")"); 1439 } 1440 1441 } 1442 1443 1444 1446 1452 public void setEntityResolver(EntityResolver entityResolver){ 1453 this.entityResolver = entityResolver; 1454 } 1455 1456 1457 1461 public EntityResolver getEntityResolver(){ 1462 return entityResolver; 1463 } 1464 1465 1474 public InputSource resolveEntity(String publicId, String systemId) 1475 throws SAXException { 1476 1477 if (saxLog.isDebugEnabled()) { 1478 saxLog.debug("resolveEntity('" + publicId + "', '" + systemId + "')"); 1479 } 1480 1481 if (publicId != null) 1482 this.publicId = publicId; 1483 1484 String entityURL = null; 1486 if (publicId != null) { 1487 entityURL = (String ) entityValidator.get(publicId); 1488 } 1489 1490 if (schemaLocation != null && entityURL == null && systemId != null){ 1492 entityURL = (String )entityValidator.get(systemId); 1493 } 1494 1495 if (entityURL == null) { 1496 if (systemId == null) { 1497 if (log.isDebugEnabled()) { 1499 log.debug(" Cannot resolve entity: '" + entityURL + "'"); 1500 } 1501 return (null); 1502 1503 } else { 1504 if (log.isDebugEnabled()) { 1506 log.debug(" Trying to resolve using system ID '" + systemId + "'"); 1507 } 1508 entityURL = systemId; 1509 } 1510 } 1511 1512 if (log.isDebugEnabled()) { 1514 log.debug(" Resolving to alternate DTD '" + entityURL + "'"); 1515 } 1516 1517 try { 1518 return (new InputSource (entityURL)); 1519 } catch (Exception e) { 1520 throw createSAXException(e); 1521 } 1522 } 1523 1524 1525 1527 1528 1536 public void error(SAXParseException exception) throws SAXException { 1537 1538 log.error("Parse Error at line " + exception.getLineNumber() + 1539 " column " + exception.getColumnNumber() + ": " + 1540 exception.getMessage(), exception); 1541 if (errorHandler != null) { 1542 errorHandler.error(exception); 1543 } 1544 1545 } 1546 1547 1548 1556 public void fatalError(SAXParseException exception) throws SAXException { 1557 1558 log.error("Parse Fatal Error at line " + exception.getLineNumber() + 1559 " column " + exception.getColumnNumber() + ": " + 1560 exception.getMessage(), exception); 1561 if (errorHandler != null) { 1562 errorHandler.fatalError(exception); 1563 } 1564 1565 } 1566 1567 1568 1576 public void warning(SAXParseException exception) throws SAXException { 1577 if (errorHandler != null) { 1578 log.warn("Parse Warning Error at line " + exception.getLineNumber() + 1579 " column " + exception.getColumnNumber() + ": " + 1580 exception.getMessage(), exception); 1581 1582 errorHandler.warning(exception); 1583 } 1584 1585 } 1586 1587 1588 1590 1591 1597 public void log(String message) { 1598 1599 log.info(message); 1600 1601 } 1602 1603 1604 1610 public void log(String message, Throwable exception) { 1611 1612 log.error(message, exception); 1613 1614 } 1615 1616 1617 1626 public Object parse(File file) throws IOException , SAXException { 1627 1628 configure(); 1629 InputSource input = new InputSource (new FileInputStream (file)); 1630 input.setSystemId(file.toURL().toString()); 1631 getXMLReader().parse(input); 1632 return (root); 1633 1634 } 1635 1644 public Object parse(InputSource input) throws IOException , SAXException { 1645 1646 configure(); 1647 getXMLReader().parse(input); 1648 return (root); 1649 1650 } 1651 1652 1653 1662 public Object parse(InputStream input) throws IOException , SAXException { 1663 1664 configure(); 1665 InputSource is = new InputSource (input); 1666 getXMLReader().parse(is); 1667 return (root); 1668 1669 } 1670 1671 1672 1681 public Object parse(Reader reader) throws IOException , SAXException { 1682 1683 configure(); 1684 InputSource is = new InputSource (reader); 1685 getXMLReader().parse(is); 1686 return (root); 1687 1688 } 1689 1690 1691 1700 public Object parse(String uri) throws IOException , SAXException { 1701 1702 configure(); 1703 InputSource is = new InputSource (uri); 1704 getXMLReader().parse(is); 1705 return (root); 1706 1707 } 1708 1709 1710 1731 public void register(String publicId, String entityURL) { 1732 1733 if (log.isDebugEnabled()) { 1734 log.debug("register('" + publicId + "', '" + entityURL + "'"); 1735 } 1736 entityValidator.put(publicId, entityURL); 1737 1738 } 1739 1740 1741 1743 1744 1751 public void addRule(String pattern, Rule rule) { 1752 1753 rule.setDigester(this); 1754 getRules().add(pattern, rule); 1755 1756 } 1757 1758 1759 1764 public void addRuleSet(RuleSet ruleSet) { 1765 1766 String oldNamespaceURI = getRuleNamespaceURI(); 1767 String newNamespaceURI = ruleSet.getNamespaceURI(); 1768 if (log.isDebugEnabled()) { 1769 if (newNamespaceURI == null) { 1770 log.debug("addRuleSet() with no namespace URI"); 1771 } else { 1772 log.debug("addRuleSet() with namespace URI " + newNamespaceURI); 1773 } 1774 } 1775 setRuleNamespaceURI(newNamespaceURI); 1776 ruleSet.addRuleInstances(this); 1777 setRuleNamespaceURI(oldNamespaceURI); 1778 1779 } 1780 1781 1782 1788 public void addBeanPropertySetter(String pattern) { 1789 1790 addRule(pattern, 1791 new BeanPropertySetterRule()); 1792 1793 } 1794 1795 1796 1803 public void addBeanPropertySetter(String pattern, 1804 String propertyName) { 1805 1806 addRule(pattern, 1807 new BeanPropertySetterRule(propertyName)); 1808 1809 } 1810 1811 1818 public void addCallMethod(String pattern, String methodName) { 1819 1820 addRule( 1821 pattern, 1822 new CallMethodRule(methodName)); 1823 1824 } 1825 1826 1835 public void addCallMethod(String pattern, String methodName, 1836 int paramCount) { 1837 1838 addRule(pattern, 1839 new CallMethodRule(methodName, paramCount)); 1840 1841 } 1842 1843 1844 1862 public void addCallMethod(String pattern, String methodName, 1863 int paramCount, String paramTypes[]) { 1864 1865 addRule(pattern, 1866 new CallMethodRule( 1867 methodName, 1868 paramCount, 1869 paramTypes)); 1870 1871 } 1872 1873 1874 1891 public void addCallMethod(String pattern, String methodName, 1892 int paramCount, Class paramTypes[]) { 1893 1894 addRule(pattern, 1895 new CallMethodRule( 1896 methodName, 1897 paramCount, 1898 paramTypes)); 1899 1900 } 1901 1902 1903 1911 public void addCallParam(String pattern, int paramIndex) { 1912 1913 addRule(pattern, 1914 new CallParamRule(paramIndex)); 1915 1916 } 1917 1918 1919 1929 public void addCallParam(String pattern, int paramIndex, 1930 String attributeName) { 1931 1932 addRule(pattern, 1933 new CallParamRule(paramIndex, attributeName)); 1934 1935 } 1936 1937 1938 1947 public void addCallParam(String pattern, int paramIndex, boolean fromStack) { 1948 1949 addRule(pattern, 1950 new CallParamRule(paramIndex, fromStack)); 1951 1952 } 1953 1954 1963 public void addCallParam(String pattern, int paramIndex, int stackIndex) { 1964 1965 addRule(pattern, 1966 new CallParamRule(paramIndex, stackIndex)); 1967 1968 } 1969 1970 1979 public void addCallParamPath(String pattern,int paramIndex) { 1980 addRule(pattern, new PathCallParamRule(paramIndex)); 1981 } 1982 1983 2003 public void addObjectParam(String pattern, int paramIndex, 2004 Object paramObj) { 2005 2006 addRule(pattern, 2007 new ObjectParamRule(paramIndex, paramObj)); 2008 2009 } 2010 2011 2019 public void addFactoryCreate(String pattern, String className) { 2020 2021 addFactoryCreate(pattern, className, false); 2022 2023 } 2024 2025 2026 2034 public void addFactoryCreate(String pattern, Class clazz) { 2035 2036 addFactoryCreate(pattern, clazz, false); 2037 2038 } 2039 2040 2041 2051 public void addFactoryCreate(String pattern, String className, 2052 String attributeName) { 2053 2054 addFactoryCreate(pattern, className, attributeName, false); 2055 2056 } 2057 2058 2059 2069 public void addFactoryCreate(String pattern, Class clazz, 2070 String attributeName) { 2071 2072 addFactoryCreate(pattern, clazz, attributeName, false); 2073 2074 } 2075 2076 2077 2086 public void addFactoryCreate(String pattern, 2087 ObjectCreationFactory creationFactory) { 2088 2089 addFactoryCreate(pattern, creationFactory, false); 2090 2091 } 2092 2093 2102 public void addFactoryCreate( 2103 String pattern, 2104 String className, 2105 boolean ignoreCreateExceptions) { 2106 2107 addRule( 2108 pattern, 2109 new FactoryCreateRule(className, ignoreCreateExceptions)); 2110 2111 } 2112 2113 2114 2123 public void addFactoryCreate( 2124 String pattern, 2125 Class clazz, 2126 boolean ignoreCreateExceptions) { 2127 2128 addRule( 2129 pattern, 2130 new FactoryCreateRule(clazz, ignoreCreateExceptions)); 2131 2132 } 2133 2134 2135 2146 public void addFactoryCreate( 2147 String pattern, 2148 String className, 2149 String attributeName, 2150 boolean ignoreCreateExceptions) { 2151 2152 addRule( 2153 pattern, 2154 new FactoryCreateRule(className, attributeName, ignoreCreateExceptions)); 2155 2156 } 2157 2158 2159 2170 public void addFactoryCreate( 2171 String pattern, 2172 Class clazz, 2173 String attributeName, 2174 boolean ignoreCreateExceptions) { 2175 2176 addRule( 2177 pattern, 2178 new FactoryCreateRule(clazz, attributeName, ignoreCreateExceptions)); 2179 2180 } 2181 2182 2183 2193 public void addFactoryCreate(String pattern, 2194 ObjectCreationFactory creationFactory, 2195 boolean ignoreCreateExceptions) { 2196 2197 creationFactory.setDigester(this); 2198 addRule(pattern, 2199 new FactoryCreateRule(creationFactory, ignoreCreateExceptions)); 2200 2201 } 2202 2203 2210 public void addObjectCreate(String pattern, String className) { 2211 2212 addRule(pattern, 2213 new ObjectCreateRule(className)); 2214 2215 } 2216 2217 2218 2225 public void addObjectCreate(String pattern, Class clazz) { 2226 2227 addRule(pattern, 2228 new ObjectCreateRule(clazz)); 2229 2230 } 2231 2232 2233 2242 public void addObjectCreate(String pattern, String className, 2243 String attributeName) { 2244 2245 addRule(pattern, 2246 new ObjectCreateRule(className, attributeName)); 2247 2248 } 2249 2250 2251 2260 public void addObjectCreate(String pattern, 2261 String attributeName, 2262 Class clazz) { 2263 2264 addRule(pattern, 2265 new ObjectCreateRule(attributeName, clazz)); 2266 2267 } 2268 2269 2276 public void addSetNestedProperties(String pattern) { 2277 2278 addRule(pattern, new SetNestedPropertiesRule()); 2279 } 2280 2281 2290 public void addSetNestedProperties(String pattern, String elementName, String propertyName) { 2291 2292 addRule(pattern, new SetNestedPropertiesRule(elementName, propertyName)); 2293 } 2294 2295 2304 public void addSetNestedProperties(String pattern, String [] elementNames, String [] propertyNames) { 2305 2306 addRule(pattern, new SetNestedPropertiesRule(elementNames, propertyNames)); 2307 } 2308 2309 2310 2317 public void addSetNext(String pattern, String methodName) { 2318 2319 addRule(pattern, 2320 new SetNextRule(methodName)); 2321 2322 } 2323 2324 2325 2336 public void addSetNext(String pattern, String methodName, 2337 String paramType) { 2338 2339 addRule(pattern, 2340 new SetNextRule(methodName, paramType)); 2341 2342 } 2343 2344 2345 2352 public void addSetRoot(String pattern, String methodName) { 2353 2354 addRule(pattern, 2355 new SetRootRule(methodName)); 2356 2357 } 2358 2359 2360 2368 public void addSetRoot(String pattern, String methodName, 2369 String paramType) { 2370 2371 addRule(pattern, 2372 new SetRootRule(methodName, paramType)); 2373 2374 } 2375 2376 2382 public void addSetProperties(String pattern) { 2383 2384 addRule(pattern, 2385 new SetPropertiesRule()); 2386 2387 } 2388 2389 2398 public void addSetProperties( 2399 String pattern, 2400 String attributeName, 2401 String propertyName) { 2402 2403 addRule(pattern, 2404 new SetPropertiesRule(attributeName, propertyName)); 2405 2406 } 2407 2408 2417 public void addSetProperties( 2418 String pattern, 2419 String [] attributeNames, 2420 String [] propertyNames) { 2421 2422 addRule(pattern, 2423 new SetPropertiesRule(attributeNames, propertyNames)); 2424 2425 } 2426 2427 2428 2436 public void addSetProperty(String pattern, String name, String value) { 2437 2438 addRule(pattern, 2439 new SetPropertyRule(name, value)); 2440 2441 } 2442 2443 2444 2451 public void addSetTop(String pattern, String methodName) { 2452 2453 addRule(pattern, 2454 new SetTopRule(methodName)); 2455 2456 } 2457 2458 2459 2470 public void addSetTop(String pattern, String methodName, 2471 String paramType) { 2472 2473 addRule(pattern, 2474 new SetTopRule(methodName, paramType)); 2475 2476 } 2477 2478 2479 2481 2482 2490 public void clear() { 2491 2492 match = ""; 2493 bodyTexts.clear(); 2494 params.clear(); 2495 publicId = null; 2496 stack.clear(); 2497 stacksByName.clear(); 2498 customContentHandler = null; 2499 } 2500 2501 2502 2506 public Object peek() { 2507 2508 try { 2509 return (stack.peek()); 2510 } catch (EmptyStackException e) { 2511 log.warn("Empty stack (returning null)"); 2512 return (null); 2513 } 2514 2515 } 2516 2517 2518 2526 public Object peek(int n) { 2527 2528 try { 2529 return (stack.peek(n)); 2530 } catch (EmptyStackException e) { 2531 log.warn("Empty stack (returning null)"); 2532 return (null); 2533 } 2534 2535 } 2536 2537 2538 2542 public Object pop() { 2543 2544 try { 2545 return (stack.pop()); 2546 } catch (EmptyStackException e) { 2547 log.warn("Empty stack (returning null)"); 2548 return (null); 2549 } 2550 2551 } 2552 2553 2554 2559 public void push(Object object) { 2560 2561 if (stack.size() == 0) { 2562 root = object; 2563 } 2564 stack.push(object); 2565 2566 } 2567 2568 2577 public void push(String stackName, Object value) { 2578 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2579 if (namedStack == null) { 2580 namedStack = new ArrayStack(); 2581 stacksByName.put(stackName, namedStack); 2582 } 2583 namedStack.push(value); 2584 } 2585 2586 2599 public Object pop(String stackName) { 2600 Object result = null; 2601 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2602 if (namedStack == null) { 2603 if (log.isDebugEnabled()) { 2604 log.debug("Stack '" + stackName + "' is empty"); 2605 } 2606 throw new EmptyStackException (); 2607 2608 } else { 2609 2610 result = namedStack.pop(); 2611 } 2612 return result; 2613 } 2614 2615 2629 public Object peek(String stackName) { 2630 return peek(stackName, 0); 2631 } 2632 2633 2648 public Object peek(String stackName, int n) { 2649 Object result = null; 2650 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2651 if (namedStack == null ) { 2652 if (log.isDebugEnabled()) { 2653 log.debug("Stack '" + stackName + "' is empty"); 2654 } 2655 throw new EmptyStackException (); 2656 2657 } else { 2658 2659 result = namedStack.peek(n); 2660 } 2661 return result; 2662 } 2663 2664 2674 public boolean isEmpty(String stackName) { 2675 boolean result = true; 2676 ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName); 2677 if (namedStack != null ) { 2678 result = namedStack.isEmpty(); 2679 } 2680 return result; 2681 } 2682 2683 2709 public Object getRoot() { 2710 return root; 2711 } 2712 2713 2723 public void resetRoot() { 2724 root = null; 2725 } 2726 2727 2729 2730 2732 2733 2747 protected void configure() { 2748 2749 if (configured) { 2751 return; 2752 } 2753 2754 initialize(); 2758 configured = true; 2760 2761 } 2762 2763 2781 protected void initialize() { 2782 2783 ; 2786 } 2787 2788 2790 2791 2794 Map getRegistrations() { 2795 2796 return (entityValidator); 2797 2798 } 2799 2800 2801 2814 List getRules(String match) { 2815 2816 return (getRules().match(match)); 2817 2818 } 2819 2820 2821 2828 public Object peekParams() { 2829 2830 try { 2831 return (params.peek()); 2832 } catch (EmptyStackException e) { 2833 log.warn("Empty stack (returning null)"); 2834 return (null); 2835 } 2836 2837 } 2838 2839 2840 2851 public Object peekParams(int n) { 2852 2853 try { 2854 return (params.peek(n)); 2855 } catch (EmptyStackException e) { 2856 log.warn("Empty stack (returning null)"); 2857 return (null); 2858 } 2859 2860 } 2861 2862 2863 2870 public Object popParams() { 2871 2872 try { 2873 if (log.isTraceEnabled()) { 2874 log.trace("Popping params"); 2875 } 2876 return (params.pop()); 2877 } catch (EmptyStackException e) { 2878 log.warn("Empty stack (returning null)"); 2879 return (null); 2880 } 2881 2882 } 2883 2884 2885 2893 public void pushParams(Object object) { 2894 if (log.isTraceEnabled()) { 2895 log.trace("Pushing params"); 2896 } 2897 params.push(object); 2898 2899 } 2900 2901 2907 public SAXException createSAXException(String message, Exception e) { 2908 if ((e != null) && 2909 (e instanceof InvocationTargetException )) { 2910 Throwable t = ((InvocationTargetException ) e).getTargetException(); 2911 if ((t != null) && (t instanceof Exception )) { 2912 e = (Exception ) t; 2913 } 2914 } 2915 if (locator != null) { 2916 String error = "Error at line " + locator.getLineNumber() + " char " + 2917 locator.getColumnNumber() + ": " + message; 2918 if (e != null) { 2919 return new SAXParseException (error, locator, e); 2920 } else { 2921 return new SAXParseException (error, locator); 2922 } 2923 } 2924 log.error("No Locator!"); 2925 if (e != null) { 2926 return new SAXException (message, e); 2927 } else { 2928 return new SAXException (message); 2929 } 2930 } 2931 2932 2938 public SAXException createSAXException(Exception e) { 2939 if (e instanceof InvocationTargetException ) { 2940 Throwable t = ((InvocationTargetException ) e).getTargetException(); 2941 if ((t != null) && (t instanceof Exception )) { 2942 e = (Exception ) t; 2943 } 2944 } 2945 return createSAXException(e.getMessage(), e); 2946 } 2947 2948 2954 public SAXException createSAXException(String message) { 2955 return createSAXException(message, null); 2956 } 2957 2958} 2959 | Popular Tags |