first commit

This commit is contained in:
2025-07-28 13:56:49 +05:30
commit e9eb805edb
3438 changed files with 520990 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package org.apache.commons.digester;
import org.xml.sax.Attributes;
public abstract class AbstractObjectCreationFactory implements ObjectCreationFactory {
protected Digester digester = null;
public abstract Object createObject(Attributes paramAttributes) throws Exception;
public Digester getDigester() {
return this.digester;
}
public void setDigester(Digester digester) {
this.digester = digester;
}
}

View File

@@ -0,0 +1,68 @@
package org.apache.commons.digester;
import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanPropertySetterRule extends Rule {
protected String propertyName;
protected String bodyText;
public BeanPropertySetterRule(Digester digester, String propertyName) {
this(propertyName);
}
public BeanPropertySetterRule(Digester digester) {
this();
}
public BeanPropertySetterRule(String propertyName) {
this.propertyName = null;
this.bodyText = null;
this.propertyName = propertyName;
}
public BeanPropertySetterRule() {
this((String)null);
}
public void body(String namespace, String name, String text) throws Exception {
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[BeanPropertySetterRule]{" + this.digester.match + "} Called with text '" + text + "'");
this.bodyText = text.trim();
}
public void end(String namespace, String name) throws Exception {
String property = this.propertyName;
if (property == null)
property = name;
Object top = this.digester.peek();
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[BeanPropertySetterRule]{" + this.digester.match + "} Set " + top.getClass().getName() + " property " + property + " with text " + this.bodyText);
if (top instanceof DynaBean) {
DynaProperty desc = ((DynaBean)top).getDynaClass().getDynaProperty(property);
if (desc == null)
throw new NoSuchMethodException("Bean has no property named " + property);
} else {
PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(top, property);
if (desc == null)
throw new NoSuchMethodException("Bean has no property named " + property);
}
BeanUtils.setProperty(top, property, this.bodyText);
}
public void finish() throws Exception {
this.bodyText = null;
}
public String toString() {
StringBuffer sb = new StringBuffer("BeanPropertySetterRule[");
sb.append("propertyName=");
sb.append(this.propertyName);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,217 @@
package org.apache.commons.digester;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.MethodUtils;
import org.xml.sax.Attributes;
public class CallMethodRule extends Rule {
protected String bodyText;
protected String methodName;
protected int paramCount;
protected Class[] paramTypes;
private String[] paramClassNames;
protected boolean useExactMatch;
public CallMethodRule(Digester digester, String methodName, int paramCount) {
this(methodName, paramCount);
}
public CallMethodRule(Digester digester, String methodName, int paramCount, String[] paramTypes) {
this(methodName, paramCount, paramTypes);
}
public CallMethodRule(Digester digester, String methodName, int paramCount, Class[] paramTypes) {
this(methodName, paramCount, paramTypes);
}
public CallMethodRule(String methodName, int paramCount) {
this.bodyText = null;
this.methodName = null;
this.paramCount = 0;
this.paramTypes = null;
this.paramClassNames = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramCount = paramCount;
if (paramCount == 0) {
this.paramTypes = new Class[] { String.class };
} else {
this.paramTypes = new Class[paramCount];
for (int i = 0; i < this.paramTypes.length; i++)
this.paramTypes[i] = String.class;
}
}
public CallMethodRule(String methodName) {
this(methodName, 0, (Class[])null);
}
public CallMethodRule(String methodName, int paramCount, String[] paramTypes) {
this.bodyText = null;
this.methodName = null;
this.paramCount = 0;
this.paramTypes = null;
this.paramClassNames = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramCount = paramCount;
if (paramTypes == null) {
this.paramTypes = new Class[paramCount];
for (int i = 0; i < this.paramTypes.length; i++)
this.paramTypes[i] = "abc".getClass();
} else {
this.paramClassNames = new String[paramTypes.length];
for (int i = 0; i < this.paramClassNames.length; i++)
this.paramClassNames[i] = paramTypes[i];
}
}
public CallMethodRule(String methodName, int paramCount, Class[] paramTypes) {
this.bodyText = null;
this.methodName = null;
this.paramCount = 0;
this.paramTypes = null;
this.paramClassNames = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramCount = paramCount;
if (paramTypes == null) {
this.paramTypes = new Class[paramCount];
for (int i = 0; i < this.paramTypes.length; i++)
this.paramTypes[i] = "abc".getClass();
} else {
this.paramTypes = new Class[paramTypes.length];
for (int i = 0; i < this.paramTypes.length; i++)
this.paramTypes[i] = paramTypes[i];
}
}
public boolean getUseExactMatch() {
return this.useExactMatch;
}
public void setUseExactMatch(boolean useExactMatch) {
this.useExactMatch = useExactMatch;
}
public void setDigester(Digester digester) {
super.setDigester(digester);
if (this.paramClassNames != null) {
this.paramTypes = new Class[this.paramClassNames.length];
for (int i = 0; i < this.paramClassNames.length; i++) {
try {
this.paramTypes[i] = digester.getClassLoader().loadClass(this.paramClassNames[i]);
} catch (ClassNotFoundException e) {
digester.getLogger().error("(CallMethodRule) Cannot load class " + this.paramClassNames[i], e);
this.paramTypes[i] = null;
}
}
}
}
public void begin(Attributes attributes) throws Exception {
if (this.paramCount > 0) {
Object[] parameters = new Object[this.paramCount];
for (int i = 0; i < parameters.length; i++)
parameters[i] = null;
this.digester.pushParams(parameters);
}
}
public void body(String bodyText) throws Exception {
if (this.paramCount == 0)
this.bodyText = bodyText.trim();
}
public void end() throws Exception {
Object[] parameters = null;
if (this.paramCount > 0) {
parameters = (Object[])this.digester.popParams();
if (this.digester.log.isTraceEnabled())
for (int j = 0, size = parameters.length; j < size; j++)
this.digester.log.trace("[CallMethodRule](" + j + ")" + parameters[j]);
if (this.paramCount == 1 && parameters[0] == null)
return;
} else if (this.paramTypes != null && this.paramTypes.length != 0) {
if (this.bodyText == null)
return;
parameters = new Object[1];
parameters[0] = this.bodyText;
if (this.paramTypes.length == 0) {
this.paramTypes = new Class[1];
this.paramTypes[0] = "abc".getClass();
}
}
Object[] paramValues = new Object[this.paramTypes.length];
for (int i = 0; i < this.paramTypes.length; i++) {
if (parameters[i] == null || (parameters[i] instanceof String && !String.class.isAssignableFrom(this.paramTypes[i]))) {
paramValues[i] = ConvertUtils.convert((String)parameters[i], this.paramTypes[i]);
} else {
paramValues[i] = parameters[i];
}
}
Object top = this.digester.peek();
if (this.digester.log.isDebugEnabled()) {
StringBuffer sb = new StringBuffer("[CallMethodRule]{");
sb.append(this.digester.match);
sb.append("} Call ");
if (top == null) {
sb.append("[NULL TOP]");
} else {
sb.append(top.getClass().getName());
}
sb.append(".");
sb.append(this.methodName);
sb.append("(");
for (int j = 0; j < paramValues.length; j++) {
if (j > 0)
sb.append(",");
if (paramValues[j] == null) {
sb.append("null");
} else {
sb.append(paramValues[j].toString());
}
sb.append("/");
if (this.paramTypes[j] == null) {
sb.append("null");
} else {
sb.append(this.paramTypes[j].getName());
}
}
sb.append(")");
this.digester.log.debug(sb.toString());
}
if (this.useExactMatch) {
MethodUtils.invokeExactMethod(top, this.methodName, paramValues, this.paramTypes);
} else {
MethodUtils.invokeMethod(top, this.methodName, paramValues, this.paramTypes);
}
}
public void finish() throws Exception {
this.bodyText = null;
}
public String toString() {
StringBuffer sb = new StringBuffer("CallMethodRule[");
sb.append("methodName=");
sb.append(this.methodName);
sb.append(", paramCount=");
sb.append(this.paramCount);
sb.append(", paramTypes={");
if (this.paramTypes != null)
for (int i = 0; i < this.paramTypes.length; i++) {
if (i > 0)
sb.append(", ");
sb.append(this.paramTypes[i].getName());
}
sb.append("}");
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,103 @@
package org.apache.commons.digester;
import org.apache.commons.collections.ArrayStack;
import org.xml.sax.Attributes;
public class CallParamRule extends Rule {
protected String attributeName;
protected int paramIndex;
protected boolean fromStack;
protected int stackIndex;
protected ArrayStack bodyTextStack;
public CallParamRule(Digester digester, int paramIndex) {
this(paramIndex);
}
public CallParamRule(Digester digester, int paramIndex, String attributeName) {
this(paramIndex, attributeName);
}
public CallParamRule(int paramIndex) {
this(paramIndex, (String)null);
}
public CallParamRule(int paramIndex, String attributeName) {
this.attributeName = null;
this.paramIndex = 0;
this.fromStack = false;
this.stackIndex = 0;
this.paramIndex = paramIndex;
this.attributeName = attributeName;
}
public CallParamRule(int paramIndex, boolean fromStack) {
this.attributeName = null;
this.paramIndex = 0;
this.fromStack = false;
this.stackIndex = 0;
this.paramIndex = paramIndex;
this.fromStack = fromStack;
}
public CallParamRule(int paramIndex, int stackIndex) {
this.attributeName = null;
this.paramIndex = 0;
this.fromStack = false;
this.stackIndex = 0;
this.paramIndex = paramIndex;
this.fromStack = true;
this.stackIndex = stackIndex;
}
public void begin(Attributes attributes) throws Exception {
Object param = null;
if (this.attributeName != null) {
param = attributes.getValue(this.attributeName);
} else if (this.fromStack) {
param = this.digester.peek(this.stackIndex);
if (this.digester.log.isDebugEnabled()) {
StringBuffer sb = new StringBuffer("[CallParamRule]{");
sb.append(this.digester.match);
sb.append("} Save from stack; from stack?").append(this.fromStack);
sb.append("; object=").append(param);
this.digester.log.debug(sb.toString());
}
}
if (param != null) {
Object[] parameters = (Object[])this.digester.peekParams();
parameters[this.paramIndex] = param;
}
}
public void body(String bodyText) throws Exception {
if (this.attributeName == null && !this.fromStack) {
if (this.bodyTextStack == null)
this.bodyTextStack = new ArrayStack();
this.bodyTextStack.push(bodyText.trim());
}
}
public void end(String namespace, String name) {
if (this.bodyTextStack != null && !this.bodyTextStack.empty()) {
Object[] parameters = (Object[])this.digester.peekParams();
parameters[this.paramIndex] = this.bodyTextStack.pop();
}
}
public String toString() {
StringBuffer sb = new StringBuffer("CallParamRule[");
sb.append("paramIndex=");
sb.append(this.paramIndex);
sb.append(", attributeName=");
sb.append(this.attributeName);
sb.append(", from stack=");
sb.append(this.fromStack);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,874 @@
package org.apache.commons.digester;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Digester extends DefaultHandler {
public Digester() {}
public Digester(SAXParser parser) {
this.parser = parser;
}
public Digester(XMLReader reader) {
this.reader = reader;
}
protected StringBuffer bodyText = new StringBuffer();
protected ArrayStack bodyTexts = new ArrayStack();
protected ClassLoader classLoader = null;
protected boolean configured = false;
protected EntityResolver entityResolver;
protected HashMap entityValidator = new HashMap();
protected ErrorHandler errorHandler = null;
protected SAXParserFactory factory = null;
private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
protected String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
protected Locator locator = null;
protected String match = "";
protected boolean namespaceAware = false;
protected HashMap namespaces = new HashMap();
protected ArrayStack params = new ArrayStack();
protected SAXParser parser = null;
protected String publicId = null;
protected XMLReader reader = null;
protected Object root = null;
protected Rules rules = null;
protected String schemaLanguage = "http://www.w3.org/2001/XMLSchema";
protected String schemaLocation = null;
protected ArrayStack stack = new ArrayStack();
protected boolean useContextClassLoader = false;
protected boolean validating = false;
protected Log log = LogFactory.getLog("org.apache.commons.digester.Digester");
protected Log saxLog = LogFactory.getLog("org.apache.commons.digester.Digester.sax");
protected static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
public String findNamespaceURI(String prefix) {
ArrayStack stack = (ArrayStack)this.namespaces.get(prefix);
if (stack == null)
return null;
try {
return (String)stack.peek();
} catch (EmptyStackException e) {
return null;
}
}
public ClassLoader getClassLoader() {
if (this.classLoader != null)
return this.classLoader;
if (this.useContextClassLoader) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null)
return classLoader;
}
return getClass().getClassLoader();
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public int getCount() {
return this.stack.size();
}
public String getCurrentElementName() {
String elementName = this.match;
int lastSlash = elementName.lastIndexOf('/');
if (lastSlash >= 0)
elementName = elementName.substring(lastSlash + 1);
return elementName;
}
public int getDebug() {
return 0;
}
public void setDebug(int debug) {}
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public SAXParserFactory getFactory() {
if (this.factory == null) {
this.factory = SAXParserFactory.newInstance();
this.factory.setNamespaceAware(this.namespaceAware);
this.factory.setValidating(this.validating);
}
return this.factory;
}
public boolean getFeature(String feature) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
return getFactory().getFeature(feature);
}
public void setFeature(String feature, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
getFactory().setFeature(feature, value);
}
public Log getLogger() {
return this.log;
}
public void setLogger(Log log) {
this.log = log;
}
public String getMatch() {
return this.match;
}
public boolean getNamespaceAware() {
return this.namespaceAware;
}
public void setNamespaceAware(boolean namespaceAware) {
this.namespaceAware = namespaceAware;
}
public void setPublicId(String publicId) {
this.publicId = publicId;
}
public String getPublicId() {
return this.publicId;
}
public String getRuleNamespaceURI() {
return getRules().getNamespaceURI();
}
public void setRuleNamespaceURI(String ruleNamespaceURI) {
getRules().setNamespaceURI(ruleNamespaceURI);
}
public SAXParser getParser() {
if (this.parser != null)
return this.parser;
try {
this.parser = getFactory().newSAXParser();
} catch (Exception e) {
this.log.error("Digester.getParser: ", e);
return null;
}
try {
if (this.schemaLocation != null) {
setProperty(this.JAXP_SCHEMA_LANGUAGE, this.schemaLanguage);
setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", this.schemaLocation);
}
} catch (Exception e) {
this.log.warn("" + e);
}
return this.parser;
}
public Object getProperty(String property) throws SAXNotRecognizedException, SAXNotSupportedException {
return getParser().getProperty(property);
}
public void setProperty(String property, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
getParser().setProperty(property, value);
}
public XMLReader getReader() {
try {
return getXMLReader();
} catch (SAXException e) {
this.log.error("Cannot get XMLReader", e);
return null;
}
}
public Rules getRules() {
if (this.rules == null) {
this.rules = new RulesBase();
this.rules.setDigester(this);
}
return this.rules;
}
public void setRules(Rules rules) {
this.rules = rules;
this.rules.setDigester(this);
}
public String getSchema() {
return this.schemaLocation;
}
public void setSchema(String schemaLocation) {
this.schemaLocation = schemaLocation;
}
public String getSchemaLanguage() {
return this.schemaLanguage;
}
public void setSchemaLanguage(String schemaLanguage) {
this.schemaLanguage = schemaLanguage;
}
public boolean getUseContextClassLoader() {
return this.useContextClassLoader;
}
public void setUseContextClassLoader(boolean use) {
this.useContextClassLoader = use;
}
public boolean getValidating() {
return this.validating;
}
public void setValidating(boolean validating) {
this.validating = validating;
}
public XMLReader getXMLReader() throws SAXException {
if (this.reader == null)
this.reader = getParser().getXMLReader();
this.reader.setDTDHandler(this);
this.reader.setContentHandler(this);
if (this.entityResolver == null) {
this.reader.setEntityResolver(this);
} else {
this.reader.setEntityResolver(this.entityResolver);
}
this.reader.setErrorHandler(this);
return this.reader;
}
public void characters(char[] buffer, int start, int length) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("characters(" + new String(buffer, start, length) + ")");
this.bodyText.append(buffer, start, length);
}
public void endDocument() throws SAXException {
if (this.saxLog.isDebugEnabled())
if (getCount() > 1) {
this.saxLog.debug("endDocument(): " + getCount() + " elements left");
} else {
this.saxLog.debug("endDocument()");
}
while (getCount() > 1)
pop();
Iterator rules = getRules().rules().iterator();
while (rules.hasNext()) {
Rule rule = rules.next();
try {
rule.finish();
} catch (Exception e) {
this.log.error("Finish event threw exception", e);
throw createSAXException(e);
} catch (Error e) {
this.log.error("Finish event threw error", e);
throw e;
}
}
clear();
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
boolean debug = this.log.isDebugEnabled();
if (debug) {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("endElement(" + namespaceURI + "," + localName + "," + qName + ")");
this.log.debug(" match='" + this.match + "'");
this.log.debug(" bodyText='" + this.bodyText + "'");
}
String name = localName;
if (name == null || name.length() < 1)
name = qName;
List rules = getRules().match(namespaceURI, this.match);
if (rules != null && rules.size() > 0) {
String bodyText = this.bodyText.toString();
for (int i = 0; i < rules.size(); i++) {
try {
Rule rule = rules.get(i);
if (debug)
this.log.debug(" Fire body() for " + rule);
rule.body(namespaceURI, name, bodyText);
} catch (Exception e) {
this.log.error("Body event threw exception", e);
throw createSAXException(e);
} catch (Error e) {
this.log.error("Body event threw error", e);
throw e;
}
}
} else if (debug) {
this.log.debug(" No rules found matching '" + this.match + "'.");
}
this.bodyText = (StringBuffer)this.bodyTexts.pop();
if (debug)
this.log.debug(" Popping body text '" + this.bodyText.toString() + "'");
if (rules != null)
for (int i = 0; i < rules.size(); i++) {
int j = rules.size() - i - 1;
try {
Rule rule = rules.get(j);
if (debug)
this.log.debug(" Fire end() for " + rule);
rule.end(namespaceURI, name);
} catch (Exception e) {
this.log.error("End event threw exception", e);
throw createSAXException(e);
} catch (Error e) {
this.log.error("End event threw error", e);
throw e;
}
}
int slash = this.match.lastIndexOf('/');
if (slash >= 0) {
this.match = this.match.substring(0, slash);
} else {
this.match = "";
}
}
public void endPrefixMapping(String prefix) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("endPrefixMapping(" + prefix + ")");
ArrayStack stack = (ArrayStack)this.namespaces.get(prefix);
if (stack == null)
return;
try {
stack.pop();
if (stack.empty())
this.namespaces.remove(prefix);
} catch (EmptyStackException e) {
throw createSAXException("endPrefixMapping popped too many times");
}
}
public void ignorableWhitespace(char[] buffer, int start, int len) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("ignorableWhitespace(" + new String(buffer, start, len) + ")");
}
public void processingInstruction(String target, String data) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("processingInstruction('" + target + "','" + data + "')");
}
public Locator getDocumentLocator() {
return this.locator;
}
public void setDocumentLocator(Locator locator) {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("setDocumentLocator(" + locator + ")");
this.locator = locator;
}
public void skippedEntity(String name) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("skippedEntity(" + name + ")");
}
public void startDocument() throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("startDocument()");
configure();
}
public void startElement(String namespaceURI, String localName, String qName, Attributes list) throws SAXException {
boolean debug = this.log.isDebugEnabled();
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("startElement(" + namespaceURI + "," + localName + "," + qName + ")");
this.bodyTexts.push(this.bodyText);
if (debug)
this.log.debug(" Pushing body text '" + this.bodyText.toString() + "'");
this.bodyText = new StringBuffer();
String name = localName;
if (name == null || name.length() < 1)
name = qName;
StringBuffer sb = new StringBuffer(this.match);
if (this.match.length() > 0)
sb.append('/');
sb.append(name);
this.match = sb.toString();
if (debug)
this.log.debug(" New match='" + this.match + "'");
List rules = getRules().match(namespaceURI, this.match);
if (rules != null && rules.size() > 0) {
String bodyText = this.bodyText.toString();
for (int i = 0; i < rules.size(); i++) {
try {
Rule rule = rules.get(i);
if (debug)
this.log.debug(" Fire begin() for " + rule);
rule.begin(namespaceURI, name, list);
} catch (Exception e) {
this.log.error("Begin event threw exception", e);
throw createSAXException(e);
} catch (Error e) {
this.log.error("Begin event threw error", e);
throw e;
}
}
} else if (debug) {
this.log.debug(" No rules found matching '" + this.match + "'.");
}
}
public void startPrefixMapping(String prefix, String namespaceURI) throws SAXException {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("startPrefixMapping(" + prefix + "," + namespaceURI + ")");
ArrayStack stack = (ArrayStack)this.namespaces.get(prefix);
if (stack == null) {
stack = new ArrayStack();
this.namespaces.put(prefix, stack);
}
stack.push(namespaceURI);
}
public void notationDecl(String name, String publicId, String systemId) {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("notationDecl(" + name + "," + publicId + "," + systemId + ")");
}
public void unparsedEntityDecl(String name, String publicId, String systemId, String notation) {
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("unparsedEntityDecl(" + name + "," + publicId + "," + systemId + "," + notation + ")");
}
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
public EntityResolver getEntityResolver() {
return this.entityResolver;
}
public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
boolean debug = this.log.isDebugEnabled();
if (this.saxLog.isDebugEnabled())
this.saxLog.debug("resolveEntity('" + publicId + "', '" + systemId + "')");
if (publicId != null)
this.publicId = publicId;
String entityURL = null;
if (publicId != null)
entityURL = (String)this.entityValidator.get(publicId);
if (this.schemaLocation != null && entityURL == null && systemId != null)
entityURL = (String)this.entityValidator.get(systemId);
if (entityURL == null)
return null;
if (debug)
this.log.debug(" Resolving to alternate DTD '" + entityURL + "'");
try {
return new InputSource(entityURL);
} catch (Exception e) {
throw createSAXException(e);
}
}
public void error(SAXParseException exception) throws SAXException {
this.log.error("Parse Error at line " + exception.getLineNumber() + " column " + exception.getColumnNumber() + ": " + exception.getMessage(), exception);
if (this.errorHandler != null)
this.errorHandler.error(exception);
}
public void fatalError(SAXParseException exception) throws SAXException {
this.log.error("Parse Fatal Error at line " + exception.getLineNumber() + " column " + exception.getColumnNumber() + ": " + exception.getMessage(), exception);
if (this.errorHandler != null)
this.errorHandler.fatalError(exception);
}
public void warning(SAXParseException exception) throws SAXException {
if (this.errorHandler != null) {
this.log.warn("Parse Warning Error at line " + exception.getLineNumber() + " column " + exception.getColumnNumber() + ": " + exception.getMessage(), exception);
this.errorHandler.warning(exception);
}
}
public void log(String message) {
this.log.info(message);
}
public void log(String message, Throwable exception) {
this.log.error(message, exception);
}
public Object parse(File file) throws IOException, SAXException {
configure();
InputSource input = new InputSource(new FileInputStream(file));
input.setSystemId("file://" + file.getAbsolutePath());
getXMLReader().parse(input);
return this.root;
}
public Object parse(InputSource input) throws IOException, SAXException {
configure();
getXMLReader().parse(input);
return this.root;
}
public Object parse(InputStream input) throws IOException, SAXException {
configure();
InputSource is = new InputSource(input);
getXMLReader().parse(is);
return this.root;
}
public Object parse(Reader reader) throws IOException, SAXException {
configure();
InputSource is = new InputSource(reader);
getXMLReader().parse(is);
return this.root;
}
public Object parse(String uri) throws IOException, SAXException {
configure();
InputSource is = new InputSource(uri);
getXMLReader().parse(is);
return this.root;
}
public void register(String publicId, String entityURL) {
if (this.log.isDebugEnabled())
this.log.debug("register('" + publicId + "', '" + entityURL + "'");
this.entityValidator.put(publicId, entityURL);
}
public void addRule(String pattern, Rule rule) {
rule.setDigester(this);
getRules().add(pattern, rule);
}
public void addRuleSet(RuleSet ruleSet) {
String oldNamespaceURI = getRuleNamespaceURI();
String newNamespaceURI = ruleSet.getNamespaceURI();
if (this.log.isDebugEnabled())
if (newNamespaceURI == null) {
this.log.debug("addRuleSet() with no namespace URI");
} else {
this.log.debug("addRuleSet() with namespace URI " + newNamespaceURI);
}
setRuleNamespaceURI(newNamespaceURI);
ruleSet.addRuleInstances(this);
setRuleNamespaceURI(oldNamespaceURI);
}
public void addBeanPropertySetter(String pattern) {
addRule(pattern, new BeanPropertySetterRule());
}
public void addBeanPropertySetter(String pattern, String propertyName) {
addRule(pattern, new BeanPropertySetterRule(propertyName));
}
public void addCallMethod(String pattern, String methodName) {
addRule(pattern, new CallMethodRule(methodName));
}
public void addCallMethod(String pattern, String methodName, int paramCount) {
addRule(pattern, new CallMethodRule(methodName, paramCount));
}
public void addCallMethod(String pattern, String methodName, int paramCount, String[] paramTypes) {
addRule(pattern, new CallMethodRule(methodName, paramCount, paramTypes));
}
public void addCallMethod(String pattern, String methodName, int paramCount, Class[] paramTypes) {
addRule(pattern, new CallMethodRule(methodName, paramCount, paramTypes));
}
public void addCallParam(String pattern, int paramIndex) {
addRule(pattern, new CallParamRule(paramIndex));
}
public void addCallParam(String pattern, int paramIndex, String attributeName) {
addRule(pattern, new CallParamRule(paramIndex, attributeName));
}
public void addCallParam(String pattern, int paramIndex, boolean fromStack) {
addRule(pattern, new CallParamRule(paramIndex, fromStack));
}
public void addCallParam(String pattern, int paramIndex, int stackIndex) {
addRule(pattern, new CallParamRule(paramIndex, stackIndex));
}
public void addFactoryCreate(String pattern, String className) {
addFactoryCreate(pattern, className, false);
}
public void addFactoryCreate(String pattern, Class clazz) {
addFactoryCreate(pattern, clazz, false);
}
public void addFactoryCreate(String pattern, String className, String attributeName) {
addFactoryCreate(pattern, className, attributeName, false);
}
public void addFactoryCreate(String pattern, Class clazz, String attributeName) {
addFactoryCreate(pattern, clazz, attributeName, false);
}
public void addFactoryCreate(String pattern, ObjectCreationFactory creationFactory) {
addFactoryCreate(pattern, creationFactory, false);
}
public void addFactoryCreate(String pattern, String className, boolean ignoreCreateExceptions) {
addRule(pattern, new FactoryCreateRule(className, ignoreCreateExceptions));
}
public void addFactoryCreate(String pattern, Class clazz, boolean ignoreCreateExceptions) {
addRule(pattern, new FactoryCreateRule(clazz, ignoreCreateExceptions));
}
public void addFactoryCreate(String pattern, String className, String attributeName, boolean ignoreCreateExceptions) {
addRule(pattern, new FactoryCreateRule(className, attributeName, ignoreCreateExceptions));
}
public void addFactoryCreate(String pattern, Class clazz, String attributeName, boolean ignoreCreateExceptions) {
addRule(pattern, new FactoryCreateRule(clazz, attributeName, ignoreCreateExceptions));
}
public void addFactoryCreate(String pattern, ObjectCreationFactory creationFactory, boolean ignoreCreateExceptions) {
creationFactory.setDigester(this);
addRule(pattern, new FactoryCreateRule(creationFactory, ignoreCreateExceptions));
}
public void addObjectCreate(String pattern, String className) {
addRule(pattern, new ObjectCreateRule(className));
}
public void addObjectCreate(String pattern, Class clazz) {
addRule(pattern, new ObjectCreateRule(clazz));
}
public void addObjectCreate(String pattern, String className, String attributeName) {
addRule(pattern, new ObjectCreateRule(className, attributeName));
}
public void addObjectCreate(String pattern, String attributeName, Class clazz) {
addRule(pattern, new ObjectCreateRule(attributeName, clazz));
}
public void addSetNext(String pattern, String methodName) {
addRule(pattern, new SetNextRule(methodName));
}
public void addSetNext(String pattern, String methodName, String paramType) {
addRule(pattern, new SetNextRule(methodName, paramType));
}
public void addSetRoot(String pattern, String methodName) {
addRule(pattern, new SetRootRule(methodName));
}
public void addSetRoot(String pattern, String methodName, String paramType) {
addRule(pattern, new SetRootRule(methodName, paramType));
}
public void addSetProperties(String pattern) {
addRule(pattern, new SetPropertiesRule());
}
public void addSetProperties(String pattern, String attributeName, String propertyName) {
addRule(pattern, new SetPropertiesRule(attributeName, propertyName));
}
public void addSetProperties(String pattern, String[] attributeNames, String[] propertyNames) {
addRule(pattern, new SetPropertiesRule(attributeNames, propertyNames));
}
public void addSetProperty(String pattern, String name, String value) {
addRule(pattern, new SetPropertyRule(name, value));
}
public void addSetTop(String pattern, String methodName) {
addRule(pattern, new SetTopRule(methodName));
}
public void addSetTop(String pattern, String methodName, String paramType) {
addRule(pattern, new SetTopRule(methodName, paramType));
}
public void clear() {
this.match = "";
this.bodyTexts.clear();
this.params.clear();
this.publicId = null;
this.stack.clear();
}
public Object peek() {
try {
return this.stack.peek();
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
public Object peek(int n) {
try {
return this.stack.peek(n);
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
public Object pop() {
try {
return this.stack.pop();
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
public void push(Object object) {
if (this.stack.size() == 0)
this.root = object;
this.stack.push(object);
}
public Object getRoot() {
return this.root;
}
protected void configure() {
if (this.configured)
return;
this.configured = true;
}
Map getRegistrations() {
return this.entityValidator;
}
List getRules(String match) {
return getRules().match(match);
}
Object peekParams() {
try {
return this.params.peek();
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
Object peekParams(int n) {
try {
return this.params.peek(n);
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
Object popParams() {
try {
if (this.log.isTraceEnabled())
this.log.trace("Popping params");
return this.params.pop();
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
}
}
void pushParams(Object object) {
if (this.log.isTraceEnabled())
this.log.trace("Pushing params");
this.params.push(object);
}
protected SAXException createSAXException(String message, Exception e) {
if (e != null && e instanceof InvocationTargetException) {
Throwable t = ((InvocationTargetException)e).getTargetException();
if (t != null && t instanceof Exception)
e = (Exception)t;
}
if (this.locator != null) {
String error = "Error at (" + this.locator.getLineNumber() + ", " + this.locator.getColumnNumber() + ": " + message;
if (e != null)
return new SAXParseException(error, this.locator, e);
return new SAXParseException(error, this.locator);
}
this.log.error("No Locator!");
if (e != null)
return new SAXException(message, e);
return new SAXException(message);
}
protected SAXException createSAXException(Exception e) {
if (e instanceof InvocationTargetException) {
Throwable t = ((InvocationTargetException)e).getTargetException();
if (t != null && t instanceof Exception)
e = (Exception)t;
}
return createSAXException(e.getMessage(), e);
}
protected SAXException createSAXException(String message) {
return createSAXException(message, null);
}
}

View File

@@ -0,0 +1,159 @@
package org.apache.commons.digester;
import org.apache.commons.collections.ArrayStack;
import org.xml.sax.Attributes;
public class FactoryCreateRule extends Rule {
private boolean ignoreCreateExceptions;
private ArrayStack exceptionIgnoredStack;
protected String attributeName;
protected String className;
protected ObjectCreationFactory creationFactory;
public FactoryCreateRule(Digester digester, String className) {
this(className);
}
public FactoryCreateRule(Digester digester, Class clazz) {
this(clazz);
}
public FactoryCreateRule(Digester digester, String className, String attributeName) {
this(className, attributeName);
}
public FactoryCreateRule(Digester digester, Class clazz, String attributeName) {
this(clazz, attributeName);
}
public FactoryCreateRule(Digester digester, ObjectCreationFactory creationFactory) {
this(creationFactory);
}
public FactoryCreateRule(String className) {
this(className, false);
}
public FactoryCreateRule(Class clazz) {
this(clazz, false);
}
public FactoryCreateRule(String className, String attributeName) {
this(className, attributeName, false);
}
public FactoryCreateRule(Class clazz, String attributeName) {
this(clazz, attributeName, false);
}
public FactoryCreateRule(ObjectCreationFactory creationFactory) {
this(creationFactory, false);
}
public FactoryCreateRule(String className, boolean ignoreCreateExceptions) {
this(className, (String)null, ignoreCreateExceptions);
}
public FactoryCreateRule(Class clazz, boolean ignoreCreateExceptions) {
this(clazz, (String)null, ignoreCreateExceptions);
}
public FactoryCreateRule(String className, String attributeName, boolean ignoreCreateExceptions) {
this.attributeName = null;
this.className = null;
this.creationFactory = null;
this.className = className;
this.attributeName = attributeName;
this.ignoreCreateExceptions = ignoreCreateExceptions;
}
public FactoryCreateRule(Class clazz, String attributeName, boolean ignoreCreateExceptions) {
this(clazz.getName(), attributeName, ignoreCreateExceptions);
}
public FactoryCreateRule(ObjectCreationFactory creationFactory, boolean ignoreCreateExceptions) {
this.attributeName = null;
this.className = null;
this.creationFactory = null;
this.creationFactory = creationFactory;
this.ignoreCreateExceptions = ignoreCreateExceptions;
}
public void begin(String namespace, String name, Attributes attributes) throws Exception {
if (this.ignoreCreateExceptions) {
if (this.exceptionIgnoredStack == null)
this.exceptionIgnoredStack = new ArrayStack();
try {
Object instance = getFactory(attributes).createObject(attributes);
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[FactoryCreateRule]{" + this.digester.match + "} New " + instance.getClass().getName());
this.digester.push(instance);
this.exceptionIgnoredStack.push(Boolean.FALSE);
} catch (Exception e) {
if (this.digester.log.isInfoEnabled()) {
this.digester.log.info("[FactoryCreateRule] Create exception ignored: " + ((e.getMessage() == null) ? e.getClass().getName() : e.getMessage()));
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[FactoryCreateRule] Ignored exception:", e);
}
this.exceptionIgnoredStack.push(Boolean.TRUE);
}
} else {
Object instance = getFactory(attributes).createObject(attributes);
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[FactoryCreateRule]{" + this.digester.match + "} New " + instance.getClass().getName());
this.digester.push(instance);
}
}
public void end(String namespace, String name) throws Exception {
if (this.ignoreCreateExceptions && this.exceptionIgnoredStack != null && !this.exceptionIgnoredStack.empty())
if (((Boolean)this.exceptionIgnoredStack.pop()).booleanValue()) {
if (this.digester.log.isTraceEnabled())
this.digester.log.trace("[FactoryCreateRule] No creation so no push so no pop");
return;
}
Object top = this.digester.pop();
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[FactoryCreateRule]{" + this.digester.match + "} Pop " + top.getClass().getName());
}
public void finish() throws Exception {
if (this.attributeName != null)
this.creationFactory = null;
}
public String toString() {
StringBuffer sb = new StringBuffer("FactoryCreateRule[");
sb.append("className=");
sb.append(this.className);
sb.append(", attributeName=");
sb.append(this.attributeName);
if (this.creationFactory != null) {
sb.append(", creationFactory=");
sb.append(this.creationFactory);
}
sb.append("]");
return sb.toString();
}
protected ObjectCreationFactory getFactory(Attributes attributes) throws Exception {
if (this.creationFactory == null) {
String realClassName = this.className;
if (this.attributeName != null) {
String value = attributes.getValue(this.attributeName);
if (value != null)
realClassName = value;
}
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[FactoryCreateRule]{" + this.digester.match + "} New factory " + realClassName);
Class clazz = this.digester.getClassLoader().loadClass(realClassName);
this.creationFactory = (ObjectCreationFactory)clazz.newInstance();
this.creationFactory.setDigester(this.digester);
}
return this.creationFactory;
}
}

View File

@@ -0,0 +1,74 @@
package org.apache.commons.digester;
import org.xml.sax.Attributes;
public class ObjectCreateRule extends Rule {
protected String attributeName;
protected String className;
public ObjectCreateRule(Digester digester, String className) {
this(className);
}
public ObjectCreateRule(Digester digester, Class clazz) {
this(clazz);
}
public ObjectCreateRule(Digester digester, String className, String attributeName) {
this(className, attributeName);
}
public ObjectCreateRule(Digester digester, String attributeName, Class clazz) {
this(attributeName, clazz);
}
public ObjectCreateRule(String className) {
this(className, (String)null);
}
public ObjectCreateRule(Class clazz) {
this(clazz.getName(), (String)null);
}
public ObjectCreateRule(String className, String attributeName) {
this.attributeName = null;
this.className = null;
this.className = className;
this.attributeName = attributeName;
}
public ObjectCreateRule(String attributeName, Class clazz) {
this(clazz.getName(), attributeName);
}
public void begin(Attributes attributes) throws Exception {
String realClassName = this.className;
if (this.attributeName != null) {
String value = attributes.getValue(this.attributeName);
if (value != null)
realClassName = value;
}
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[ObjectCreateRule]{" + this.digester.match + "}New " + realClassName);
Class clazz = this.digester.getClassLoader().loadClass(realClassName);
Object instance = clazz.newInstance();
this.digester.push(instance);
}
public void end() throws Exception {
Object top = this.digester.pop();
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[ObjectCreateRule]{" + this.digester.match + "} Pop " + top.getClass().getName());
}
public String toString() {
StringBuffer sb = new StringBuffer("ObjectCreateRule[");
sb.append("className=");
sb.append(this.className);
sb.append(", attributeName=");
sb.append(this.attributeName);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,11 @@
package org.apache.commons.digester;
import org.xml.sax.Attributes;
public interface ObjectCreationFactory {
Object createObject(Attributes paramAttributes) throws Exception;
Digester getDigester();
void setDigester(Digester paramDigester);
}

View File

@@ -0,0 +1,56 @@
package org.apache.commons.digester;
import org.xml.sax.Attributes;
public abstract class Rule {
protected Digester digester;
protected String namespaceURI;
public Rule(Digester digester) {
this.digester = null;
this.namespaceURI = null;
setDigester(digester);
}
public Rule() {
this.digester = null;
this.namespaceURI = null;
}
public Digester getDigester() {
return this.digester;
}
public void setDigester(Digester digester) {
this.digester = digester;
}
public String getNamespaceURI() {
return this.namespaceURI;
}
public void setNamespaceURI(String namespaceURI) {
this.namespaceURI = namespaceURI;
}
public void begin(Attributes attributes) throws Exception {}
public void begin(String namespace, String name, Attributes attributes) throws Exception {
begin(attributes);
}
public void body(String text) throws Exception {}
public void body(String namespace, String name, String text) throws Exception {
body(text);
}
public void end() throws Exception {}
public void end(String namespace, String name) throws Exception {
end();
}
public void finish() throws Exception {}
}

View File

@@ -0,0 +1,7 @@
package org.apache.commons.digester;
public interface RuleSet {
String getNamespaceURI();
void addRuleInstances(Digester paramDigester);
}

View File

@@ -0,0 +1,11 @@
package org.apache.commons.digester;
public abstract class RuleSetBase implements RuleSet {
protected String namespaceURI = null;
public String getNamespaceURI() {
return this.namespaceURI;
}
public abstract void addRuleInstances(Digester paramDigester);
}

View File

@@ -0,0 +1,23 @@
package org.apache.commons.digester;
import java.util.List;
public interface Rules {
Digester getDigester();
void setDigester(Digester paramDigester);
String getNamespaceURI();
void setNamespaceURI(String paramString);
void add(String paramString, Rule paramRule);
void clear();
List match(String paramString);
List match(String paramString1, String paramString2);
List rules();
}

View File

@@ -0,0 +1,100 @@
package org.apache.commons.digester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class RulesBase implements Rules {
protected HashMap cache = new HashMap();
protected Digester digester = null;
protected String namespaceURI = null;
protected ArrayList rules = new ArrayList();
public Digester getDigester() {
return this.digester;
}
public void setDigester(Digester digester) {
this.digester = digester;
Iterator items = this.rules.iterator();
while (items.hasNext()) {
Rule item = items.next();
item.setDigester(digester);
}
}
public String getNamespaceURI() {
return this.namespaceURI;
}
public void setNamespaceURI(String namespaceURI) {
this.namespaceURI = namespaceURI;
}
public void add(String pattern, Rule rule) {
List list = (List)this.cache.get(pattern);
if (list == null) {
list = new ArrayList();
this.cache.put(pattern, list);
}
list.add(rule);
this.rules.add(rule);
if (this.digester != null)
rule.setDigester(this.digester);
if (this.namespaceURI != null)
rule.setNamespaceURI(this.namespaceURI);
}
public void clear() {
this.cache.clear();
this.rules.clear();
}
public List match(String pattern) {
return match(null, pattern);
}
public List match(String namespaceURI, String pattern) {
List rulesList = lookup(namespaceURI, pattern);
if (rulesList == null || rulesList.size() < 1) {
String longKey = "";
Iterator keys = this.cache.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
if (key.startsWith("*/") && (
pattern.equals(key.substring(2)) || pattern.endsWith(key.substring(1))))
if (key.length() > longKey.length()) {
rulesList = lookup(namespaceURI, key);
longKey = key;
}
}
}
if (rulesList == null)
rulesList = new ArrayList();
return rulesList;
}
public List rules() {
return this.rules;
}
protected List lookup(String namespaceURI, String pattern) {
List list = (List)this.cache.get(pattern);
if (list == null)
return null;
if (namespaceURI == null || namespaceURI.length() == 0)
return list;
ArrayList results = new ArrayList();
Iterator items = list.iterator();
while (items.hasNext()) {
Rule item = items.next();
if (namespaceURI.equals(item.getNamespaceURI()) || item.getNamespaceURI() == null)
results.add(item);
}
return results;
}
}

View File

@@ -0,0 +1,71 @@
package org.apache.commons.digester;
import org.apache.commons.beanutils.MethodUtils;
public class SetNextRule extends Rule {
protected String methodName;
protected String paramType;
protected boolean useExactMatch;
public SetNextRule(Digester digester, String methodName) {
this(methodName);
}
public SetNextRule(Digester digester, String methodName, String paramType) {
this(methodName, paramType);
}
public SetNextRule(String methodName) {
this(methodName, (String)null);
}
public SetNextRule(String methodName, String paramType) {
this.methodName = null;
this.paramType = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramType = paramType;
}
public boolean isExactMatch() {
return this.useExactMatch;
}
public void setExactMatch(boolean useExactMatch) {
this.useExactMatch = useExactMatch;
}
public void end() throws Exception {
Object child = this.digester.peek(0);
Object parent = this.digester.peek(1);
if (this.digester.log.isDebugEnabled())
if (parent == null) {
this.digester.log.debug("[SetNextRule]{" + this.digester.match + "} Call [NULL PARENT]." + this.methodName + "(" + child + ")");
} else {
this.digester.log.debug("[SetNextRule]{" + this.digester.match + "} Call " + parent.getClass().getName() + "." + this.methodName + "(" + child + ")");
}
Class[] paramTypes = new Class[1];
if (this.paramType != null) {
paramTypes[0] = this.digester.getClassLoader().loadClass(this.paramType);
} else {
paramTypes[0] = child.getClass();
}
if (this.useExactMatch) {
MethodUtils.invokeExactMethod(parent, this.methodName, new Object[] { child }, paramTypes);
} else {
MethodUtils.invokeMethod(parent, this.methodName, new Object[] { child }, paramTypes);
}
}
public String toString() {
StringBuffer sb = new StringBuffer("SetNextRule[");
sb.append("methodName=");
sb.append(this.methodName);
sb.append(", paramType=");
sb.append(this.paramType);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,94 @@
package org.apache.commons.digester;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import org.xml.sax.Attributes;
public class SetPropertiesRule extends Rule {
private String[] attributeNames;
private String[] propertyNames;
public SetPropertiesRule(Digester digester) {
this();
}
public SetPropertiesRule() {}
public SetPropertiesRule(String attributeName, String propertyName) {
this.attributeNames = new String[1];
this.attributeNames[0] = attributeName;
this.propertyNames = new String[1];
this.propertyNames[0] = propertyName;
}
public SetPropertiesRule(String[] attributeNames, String[] propertyNames) {
this.attributeNames = new String[attributeNames.length];
for (int i = 0, size = attributeNames.length; i < size; i++)
this.attributeNames[i] = attributeNames[i];
this.propertyNames = new String[propertyNames.length];
for (int j = 0, k = propertyNames.length; j < k; j++)
this.propertyNames[j] = propertyNames[j];
}
public void begin(Attributes attributes) throws Exception {
HashMap values = new HashMap();
int attNamesLength = 0;
if (this.attributeNames != null)
attNamesLength = this.attributeNames.length;
int propNamesLength = 0;
if (this.propertyNames != null)
propNamesLength = this.propertyNames.length;
for (int i = 0; i < attributes.getLength(); i++) {
String name = attributes.getLocalName(i);
if ("".equals(name))
name = attributes.getQName(i);
String value = attributes.getValue(i);
for (int n = 0; n < attNamesLength; n++) {
if (name.equals(this.attributeNames[n])) {
if (n < propNamesLength) {
name = this.propertyNames[n];
break;
}
name = null;
break;
}
}
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[SetPropertiesRule]{" + this.digester.match + "} Setting property '" + name + "' to '" + value + "'");
if (name != null)
values.put(name, value);
}
Object top = this.digester.peek();
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[SetPropertiesRule]{" + this.digester.match + "} Set " + top.getClass().getName() + " properties");
BeanUtils.populate(top, values);
}
public void addAlias(String attributeName, String propertyName) {
if (this.attributeNames == null) {
this.attributeNames = new String[1];
this.attributeNames[0] = attributeName;
this.propertyNames = new String[1];
this.propertyNames[0] = propertyName;
} else {
int length = this.attributeNames.length;
String[] tempAttributes = new String[length + 1];
for (int i = 0; i < length; i++)
tempAttributes[i] = this.attributeNames[i];
tempAttributes[length] = attributeName;
String[] tempProperties = new String[length + 1];
for (int j = 0; j < length && j < this.propertyNames.length; j++)
tempProperties[j] = this.propertyNames[j];
tempProperties[length] = propertyName;
this.propertyNames = tempProperties;
this.attributeNames = tempAttributes;
}
}
public String toString() {
StringBuffer sb = new StringBuffer("SetPropertiesRule[");
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,64 @@
package org.apache.commons.digester;
import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.xml.sax.Attributes;
public class SetPropertyRule extends Rule {
protected String name;
protected String value;
public SetPropertyRule(Digester digester, String name, String value) {
this(name, value);
}
public SetPropertyRule(String name, String value) {
this.name = null;
this.value = null;
this.name = name;
this.value = value;
}
public void begin(Attributes attributes) throws Exception {
String actualName = null;
String actualValue = null;
for (int i = 0; i < attributes.getLength(); i++) {
String name = attributes.getLocalName(i);
if ("".equals(name))
name = attributes.getQName(i);
String value = attributes.getValue(i);
if (name.equals(this.name)) {
actualName = value;
} else if (name.equals(this.value)) {
actualValue = value;
}
}
Object top = this.digester.peek();
if (this.digester.log.isDebugEnabled())
this.digester.log.debug("[SetPropertyRule]{" + this.digester.match + "} Set " + top.getClass().getName() + " property " + actualName + " to " + actualValue);
if (top instanceof DynaBean) {
DynaProperty desc = ((DynaBean)top).getDynaClass().getDynaProperty(actualName);
if (desc == null)
throw new NoSuchMethodException("Bean has no property named " + actualName);
} else {
PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(top, actualName);
if (desc == null)
throw new NoSuchMethodException("Bean has no property named " + actualName);
}
BeanUtils.setProperty(top, actualName, actualValue);
}
public String toString() {
StringBuffer sb = new StringBuffer("SetPropertyRule[");
sb.append("name=");
sb.append(this.name);
sb.append(", value=");
sb.append(this.value);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,71 @@
package org.apache.commons.digester;
import org.apache.commons.beanutils.MethodUtils;
public class SetRootRule extends Rule {
protected String methodName;
protected String paramType;
protected boolean useExactMatch;
public SetRootRule(Digester digester, String methodName) {
this(methodName);
}
public SetRootRule(Digester digester, String methodName, String paramType) {
this(methodName, paramType);
}
public SetRootRule(String methodName) {
this(methodName, (String)null);
}
public SetRootRule(String methodName, String paramType) {
this.methodName = null;
this.paramType = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramType = paramType;
}
public boolean isExactMatch() {
return this.useExactMatch;
}
public void setExactMatch(boolean useExactMatch) {
this.useExactMatch = useExactMatch;
}
public void end() throws Exception {
Object child = this.digester.peek(0);
Object parent = this.digester.root;
if (this.digester.log.isDebugEnabled())
if (parent == null) {
this.digester.log.debug("[SetRootRule]{" + this.digester.match + "} Call [NULL ROOT]." + this.methodName + "(" + child + ")");
} else {
this.digester.log.debug("[SetRootRule]{" + this.digester.match + "} Call " + parent.getClass().getName() + "." + this.methodName + "(" + child + ")");
}
Class[] paramTypes = new Class[1];
if (this.paramType != null) {
paramTypes[0] = this.digester.getClassLoader().loadClass(this.paramType);
} else {
paramTypes[0] = child.getClass();
}
if (this.useExactMatch) {
MethodUtils.invokeExactMethod(parent, this.methodName, new Object[] { child }, paramTypes);
} else {
MethodUtils.invokeMethod(parent, this.methodName, new Object[] { child }, paramTypes);
}
}
public String toString() {
StringBuffer sb = new StringBuffer("SetRootRule[");
sb.append("methodName=");
sb.append(this.methodName);
sb.append(", paramType=");
sb.append(this.paramType);
sb.append("]");
return sb.toString();
}
}

View File

@@ -0,0 +1,71 @@
package org.apache.commons.digester;
import org.apache.commons.beanutils.MethodUtils;
public class SetTopRule extends Rule {
protected String methodName;
protected String paramType;
protected boolean useExactMatch;
public SetTopRule(Digester digester, String methodName) {
this(methodName);
}
public SetTopRule(Digester digester, String methodName, String paramType) {
this(methodName, paramType);
}
public SetTopRule(String methodName) {
this(methodName, (String)null);
}
public SetTopRule(String methodName, String paramType) {
this.methodName = null;
this.paramType = null;
this.useExactMatch = false;
this.methodName = methodName;
this.paramType = paramType;
}
public boolean isExactMatch() {
return this.useExactMatch;
}
public void setExactMatch(boolean useExactMatch) {
this.useExactMatch = useExactMatch;
}
public void end() throws Exception {
Object child = this.digester.peek(0);
Object parent = this.digester.peek(1);
if (this.digester.log.isDebugEnabled())
if (child == null) {
this.digester.log.debug("[SetTopRule]{" + this.digester.match + "} Call [NULL CHILD]." + this.methodName + "(" + parent + ")");
} else {
this.digester.log.debug("[SetTopRule]{" + this.digester.match + "} Call " + child.getClass().getName() + "." + this.methodName + "(" + parent + ")");
}
Class[] paramTypes = new Class[1];
if (this.paramType != null) {
paramTypes[0] = this.digester.getClassLoader().loadClass(this.paramType);
} else {
paramTypes[0] = parent.getClass();
}
if (this.useExactMatch) {
MethodUtils.invokeExactMethod(child, this.methodName, new Object[] { parent }, paramTypes);
} else {
MethodUtils.invokeMethod(child, this.methodName, new Object[] { parent }, paramTypes);
}
}
public String toString() {
StringBuffer sb = new StringBuffer("SetTopRule[");
sb.append("methodName=");
sb.append(this.methodName);
sb.append(", paramType=");
sb.append(this.paramType);
sb.append("]");
return sb.toString();
}
}