📄 digesterruleparser.java
字号:
digester.parse(fileName);
includedFiles.remove(fileName);
}
/**
* Creates an instance of the indicated class. The class must implement
* the DigesterRulesSource interface. Passes the target digester to
* that instance. The DigesterRulesSource instance is supposed to add
* rules into the digester. The contents of the current pattern stack
* will be automatically prepended to all of the pattern strings added
* by the DigesterRulesSource instance.
*/
private void includeProgrammaticRules(String className)
throws ClassNotFoundException, ClassCastException,
InstantiationException, IllegalAccessException {
Class cls = Class.forName(className);
DigesterRulesSource rulesSource = (DigesterRulesSource) cls.newInstance();
// wrap the digester's Rules object, to prepend pattern
Rules digesterRules = targetDigester.getRules();
Rules prefixWrapper =
new RulesPrefixAdapter(patternStack.toString(), digesterRules);
targetDigester.setRules(prefixWrapper);
try {
rulesSource.getRules(targetDigester);
} finally {
// Put the unwrapped rules back
targetDigester.setRules(digesterRules);
}
}
}
/**
* Wraps a Rules object. Delegates all the Rules interface methods
* to the underlying Rules object. Overrides the add method to prepend
* a prefix to the pattern string.
*/
private class RulesPrefixAdapter implements Rules {
private Rules delegate;
private String prefix;
/**
* @param patternPrefix the pattern string to prepend to the pattern
* passed to the add method.
* @param rules The wrapped Rules object. All of this class's methods
* pass through to this object.
*/
public RulesPrefixAdapter(String patternPrefix, Rules rules) {
prefix = patternPrefix;
delegate = rules;
}
/**
* Register a new Rule instance matching a pattern which is constructed
* by concatenating the pattern prefix with the given pattern.
*/
public void add(String pattern, Rule rule) {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
if (!pattern.startsWith("/")) {
buffer.append('/');
}
buffer.append(pattern);
delegate.add(buffer.toString(), rule);
}
/**
* This method passes through to the underlying Rules object.
*/
public void clear() {
delegate.clear();
}
/**
* This method passes through to the underlying Rules object.
*/
public Digester getDigester() {
return delegate.getDigester();
}
/**
* This method passes through to the underlying Rules object.
*/
public String getNamespaceURI() {
return delegate.getNamespaceURI();
}
/**
* @deprecated Call match(namespaceURI,pattern) instead.
*/
public List match(String pattern) {
return delegate.match(pattern);
}
/**
* This method passes through to the underlying Rules object.
*/
public List match(String namespaceURI, String pattern) {
return delegate.match(namespaceURI, pattern);
}
/**
* This method passes through to the underlying Rules object.
*/
public List rules() {
return delegate.rules();
}
/**
* This method passes through to the underlying Rules object.
*/
public void setDigester(Digester digester) {
delegate.setDigester(digester);
}
/**
* This method passes through to the underlying Rules object.
*/
public void setNamespaceURI(String namespaceURI) {
delegate.setNamespaceURI(namespaceURI);
}
}
///////////////////////////////////////////////////////////////////////
// Classes beyond this point are ObjectCreationFactory implementations,
// used to create Rule objects and initialize them from SAX attributes.
///////////////////////////////////////////////////////////////////////
/**
* Factory for creating a BeanPropertySetterRule.
*/
private class BeanPropertySetterRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) throws Exception {
Rule beanPropertySetterRule = null;
String propertyname = attributes.getValue("propertyname");
if (propertyname == null) {
// call the setter method corresponding to the element name.
beanPropertySetterRule = new BeanPropertySetterRule();
} else {
beanPropertySetterRule = new BeanPropertySetterRule(propertyname);
}
return beanPropertySetterRule;
}
}
/**
* Factory for creating a CallMethodRule.
*/
protected class CallMethodRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
Rule callMethodRule = null;
String methodName = attributes.getValue("methodname");
if (attributes.getValue("paramcount") == null) {
// call against empty method
callMethodRule = new CallMethodRule(methodName);
} else {
int paramCount = Integer.parseInt(attributes.getValue("paramcount"));
String paramTypesAttr = attributes.getValue("paramtypes");
if (paramTypesAttr == null || paramTypesAttr.length() == 0) {
callMethodRule = new CallMethodRule(methodName, paramCount);
} else {
// Process the comma separated list or paramTypes
// into an array of String class names
ArrayList paramTypes = new ArrayList();
StringTokenizer tokens = new StringTokenizer(paramTypesAttr, " \t\n\r,");
while (tokens.hasMoreTokens()) {
paramTypes.add(tokens.nextToken());
}
callMethodRule = new CallMethodRule( methodName,
paramCount,
(String[])paramTypes.toArray(new String[0]));
}
}
return callMethodRule;
}
}
/**
* Factory for creating a CallParamRule.
*/
protected class CallParamRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
// create callparamrule
int paramIndex = Integer.parseInt(attributes.getValue("paramnumber"));
String attributeName = attributes.getValue("attrname");
String fromStack = attributes.getValue("from-stack");
Rule callParamRule = null;
if (attributeName == null) {
if (fromStack == null) {
callParamRule = new CallParamRule( paramIndex );
} else {
callParamRule = new CallParamRule( paramIndex, Boolean.valueOf(fromStack).booleanValue());
}
} else {
if (fromStack == null) {
callParamRule = new CallParamRule( paramIndex, attributeName );
} else {
// specifying both from-stack and attribute name is not allowed
throw new RuntimeException("Attributes from-stack and attrname cannot both be present.");
}
}
return callParamRule;
}
}
/**
* Factory for creating a ObjectParamRule
*/
protected class ObjectParamRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) throws Exception {
// create callparamrule
int paramIndex = Integer.parseInt(attributes.getValue("paramnumber"));
String attributeName = attributes.getValue("attrname");
String type = attributes.getValue("type");
String value = attributes.getValue("value");
Rule objectParamRule = null;
// type name is requried
if (type == null) {
throw new RuntimeException("Attribute 'type' is required.");
}
// create object instance
Object param = null;
Class clazz = Class.forName(type);
if (value == null) {
param = clazz.newInstance();
} else {
param = ConvertUtils.convert(value, clazz);
}
if (attributeName == null) {
objectParamRule = new ObjectParamRule(paramIndex, param);
} else {
objectParamRule = new ObjectParamRule(paramIndex, attributeName, param);
}
return objectParamRule;
}
}
/**
* Factory for creating a FactoryCreateRule
*/
protected class FactoryCreateRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String className = attributes.getValue("classname");
String attrName = attributes.getValue("attrname");
boolean ignoreExceptions =
"true".equalsIgnoreCase(attributes.getValue("ignore-exceptions"));
return (attrName == null || attrName.length() == 0) ?
new FactoryCreateRule( className, ignoreExceptions) :
new FactoryCreateRule( className, attrName, ignoreExceptions);
}
}
/**
* Factory for creating a ObjectCreateRule
*/
protected class ObjectCreateRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String className = attributes.getValue("classname");
String attrName = attributes.getValue("attrname");
return (attrName == null || attrName.length() == 0) ?
new ObjectCreateRule( className) :
new ObjectCreateRule( className, attrName);
}
}
/**
* Factory for creating a SetPropertiesRule
*/
protected class SetPropertiesRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
return new SetPropertiesRule();
}
}
/**
* Factory for creating a SetPropertyRule
*/
protected class SetPropertyRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String name = attributes.getValue("name");
String value = attributes.getValue("value");
return new SetPropertyRule( name, value);
}
}
/**
* Factory for creating a SetTopRuleFactory
*/
protected class SetTopRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String methodName = attributes.getValue("methodname");
String paramType = attributes.getValue("paramtype");
return (paramType == null || paramType.length() == 0) ?
new SetTopRule( methodName) :
new SetTopRule( methodName, paramType);
}
}
/**
* Factory for creating a SetNextRuleFactory
*/
protected class SetNextRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String methodName = attributes.getValue("methodname");
String paramType = attributes.getValue("paramtype");
return (paramType == null || paramType.length() == 0) ?
new SetNextRule( methodName) :
new SetNextRule( methodName, paramType);
}
}
/**
* Factory for creating a SetRootRuleFactory
*/
protected class SetRootRuleFactory extends AbstractObjectCreationFactory {
public Object createObject(Attributes attributes) {
String methodName = attributes.getValue("methodname");
String paramType = attributes.getValue("paramtype");
return (paramType == null || paramType.length() == 0) ?
new SetRootRule( methodName) :
new SetRootRule( methodName, paramType);
}
}
/**
* A rule for adding a attribute-property alias to the custom alias mappings of
* the containing SetPropertiesRule rule.
*/
protected class SetPropertiesAliasRule extends Rule {
/**
* <p>Base constructor.</p>
*/
public SetPropertiesAliasRule() {
super();
}
/**
* Add the alias to the SetPropertiesRule object created by the
* enclosing <set-properties-rule> tag.
*/
public void begin(Attributes attributes) {
String attrName = attributes.getValue("attr-name");
String propName = attributes.getValue("prop-name");
SetPropertiesRule rule = (SetPropertiesRule) digester.peek();
rule.addAlias(attrName, propName);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -