📄 setnestedpropertiesrule.java
字号:
/**
* When set to true, any child element for which there is no
* corresponding object property will cause an error to be reported.
* The default value of this attribute is false (not allowed).
*/
public void setAllowUnknownChildElements(boolean allowUnknownChildElements) {
this.allowUnknownChildElements = allowUnknownChildElements;
}
/** See {@link #setAllowUnknownChildElements}. */
public boolean getAllowUnknownChildElements() {
return allowUnknownChildElements;
}
/**
* Process the beginning of this element.
*
* @param namespace is the namespace this attribute is in, or null
* @param name is the name of the current xml element
* @param attributes is the attribute list of this element
*/
public void begin(String namespace, String name, Attributes attributes)
throws Exception {
oldRules = digester.getRules();
newRules.init(digester.getMatch()+"/", oldRules);
digester.setRules(newRules);
}
/**
* This is only invoked after all child elements have been processed,
* so we can remove the custom Rules object that does the
* child-element-matching.
*/
public void body(String bodyText) throws Exception {
digester.setRules(oldRules);
}
/**
* <p>Add an additional element name to property name mapping.
* This is intended to be used from the xml rules.
*/
public void addAlias(String elementName, String propertyName) {
if (propertyName == null) {
elementNames.put(elementName, PROP_IGNORE);
}
else {
elementNames.put(elementName, propertyName);
}
}
/**
* Render a printable version of this Rule.
*/
public String toString() {
return ("SetNestedPropertiesRule");
}
//----------------------------------------- local classes
/** Private Rules implementation */
private class AnyChildRules implements Rules {
private String matchPrefix = null;
private Rules decoratedRules = null;
private ArrayList rules = new ArrayList(1);
private AnyChildRule rule;
public AnyChildRules(AnyChildRule rule) {
this.rule = rule;
rules.add(rule);
}
public Digester getDigester() { return null; }
public void setDigester(Digester digester) {}
public String getNamespaceURI() {return null;}
public void setNamespaceURI(String namespaceURI) {}
public void add(String pattern, Rule rule) {}
public void clear() {}
public List match(String matchPath) {
return match(null,matchPath);
}
public List match(String namespaceURI, String matchPath) {
List match = decoratedRules.match(namespaceURI, matchPath);
if ((matchPath.startsWith(matchPrefix)) &&
(matchPath.indexOf('/', matchPrefix.length()) == -1)) {
// The current element is a direct child of the element
// specified in the init method, so include it as the
// first rule in the matches list. The way that
// SetNestedPropertiesRule is used, it is in fact very
// likely to be the only match, so we optimise that
// solution by keeping a list with only the AnyChildRule
// instance in it.
if ((match == null || match.size()==0)) {
return rules;
}
else {
// it might not be safe to modify the returned list,
// so clone it first.
LinkedList newMatch = new LinkedList(match);
//newMatch.addFirst(rule);
newMatch.addLast(rule);
return newMatch;
}
}
else {
return match;
}
}
public List rules() {
// This is not actually expected to be called.
throw new RuntimeException(
"AnyChildRules.rules not implemented.");
}
public void init(String prefix, Rules rules) {
matchPrefix = prefix;
decoratedRules = rules;
}
}
private class AnyChildRule extends Rule {
private String currChildNamespaceURI = null;
private String currChildElementName = null;
public void begin(String namespaceURI, String name,
Attributes attributes) throws Exception {
currChildNamespaceURI = namespaceURI;
currChildElementName = name;
}
public void body(String value) throws Exception {
boolean debug = log.isDebugEnabled();
String propName = (String) elementNames.get(currChildElementName);
if (propName == PROP_IGNORE) {
// note: above deliberately tests for IDENTITY, not EQUALITY
return;
}
if (propName == null) {
propName = currChildElementName;
}
if (digester.log.isDebugEnabled()) {
digester.log.debug("[SetNestedPropertiesRule]{" + digester.match +
"} Setting property '" + propName + "' to '" +
value + "'");
}
// Populate the corresponding properties of the top object
Object top = digester.peek();
if (digester.log.isDebugEnabled()) {
if (top != null) {
digester.log.debug("[SetNestedPropertiesRule]{" + digester.match +
"} Set " + top.getClass().getName() +
" properties");
} else {
digester.log.debug("[SetPropertiesRule]{" + digester.match +
"} Set NULL properties");
}
}
if (trimData) {
value = value.trim();
}
if (!allowUnknownChildElements) {
// Force an exception if the property does not exist
// (BeanUtils.setProperty() silently returns in this case)
if (top instanceof DynaBean) {
DynaProperty desc =
((DynaBean) top).getDynaClass().getDynaProperty(propName);
if (desc == null) {
throw new NoSuchMethodException
("Bean has no property named " + propName);
}
} else /* this is a standard JavaBean */ {
PropertyDescriptor desc =
PropertyUtils.getPropertyDescriptor(top, propName);
if (desc == null) {
throw new NoSuchMethodException
("Bean has no property named " + propName);
}
}
}
BeanUtils.setProperty(top, propName, value);
}
public void end(String namespace, String name) throws Exception {
currChildElementName = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -