📄 plugincreaterule.java
字号:
* the plugin class are then loaded into that new Rules object.
* Finally, any custom rules that are associated with the current pattern
* (such as SetPropertiesRules) have their begin methods executed.
*
* @param namespace
* @param name
* @param attributes
*
* @throws ClassNotFoundException
* @throws PluginInvalidInputException
* @throws PluginConfigurationException
*/
public void begin(String namespace, String name,
org.xml.sax.Attributes attributes)
throws java.lang.Exception {
Log log = digester.getLogger();
boolean debug = log.isDebugEnabled();
if (debug) {
log.debug("PluginCreateRule.begin" + ": pattern=[" + pattern + "]" +
" match=[" + digester.getMatch() + "]");
}
if (initException != null) {
// we had a problem during initialisation that we could
// not report then; report it now.
throw initException;
}
String path = digester.getMatch();
// create a new Rules object and effectively push it onto a stack of
// rules objects. The stack is actually a linked list; using the
// PluginRules constructor below causes the new instance to link
// to the previous head-of-stack, then the Digester.setRules() makes
// the new instance the new head-of-stack.
PluginRules oldRules = (PluginRules) digester.getRules();
PluginRules newRules = new PluginRules(path, oldRules);
digester.setRules(newRules);
// load any custom rules associated with the plugin
PluginManager pluginManager = newRules.getPluginManager();
Declaration currDeclaration = null;
if (debug) {
log.debug("PluginCreateRule.begin: installing new plugin: " +
"oldrules=" + oldRules.toString() +
", newrules=" + newRules.toString());
}
String pluginClassName;
if (pluginClassAttrNs == null) {
// Yep, this is ugly.
//
// In a namespace-aware parser, the one-param version will
// return attributes with no namespace.
//
// In a non-namespace-aware parser, the two-param version will
// never return any attributes, ever.
pluginClassName = attributes.getValue(pluginClassAttr);
} else {
pluginClassName =
attributes.getValue(pluginClassAttrNs, pluginClassAttr);
}
String pluginId;
if (pluginIdAttrNs == null) {
pluginId = attributes.getValue(pluginIdAttr);
} else {
pluginId =
attributes.getValue(pluginIdAttrNs, pluginIdAttr);
}
if (pluginClassName != null) {
// The user is using a plugin "inline", ie without a previous
// explicit declaration. If they have used the same plugin class
// before, we have already gone to the effort of creating a
// Declaration object, so retrieve it. If there is no existing
// declaration object for this class, then create one.
currDeclaration = pluginManager.getDeclarationByClass(
pluginClassName);
if (currDeclaration == null) {
currDeclaration = new Declaration(pluginClassName);
try {
currDeclaration.init(digester, pluginManager);
} catch(PluginException pwe) {
throw new PluginInvalidInputException(
pwe.getMessage(), pwe.getCause());
}
pluginManager.addDeclaration(currDeclaration);
}
} else if (pluginId != null) {
currDeclaration = pluginManager.getDeclarationById(pluginId);
if (currDeclaration == null) {
throw new PluginInvalidInputException(
"Plugin id [" + pluginId + "] is not defined.");
}
} else if (defaultPlugin != null) {
currDeclaration = defaultPlugin;
} else {
throw new PluginInvalidInputException(
"No plugin class specified for element " +
pattern);
}
// now load up the custom rules
currDeclaration.configure(digester, pattern);
// and now create an instance of the plugin class
Class pluginClass = currDeclaration.getPluginClass();
Object instance = pluginClass.newInstance();
getDigester().push(instance);
if (debug) {
log.debug(
"PluginCreateRule.begin" + ": pattern=[" + pattern + "]" +
" match=[" + digester.getMatch() + "]" +
" pushed instance of plugin [" + pluginClass.getName() + "]");
}
// and now we have to fire any custom rules which would have
// been matched by the same path that matched this rule, had
// they been loaded at that time.
List rules = newRules.getDecoratedRules().match(namespace, path);
fireBeginMethods(rules, namespace, name, attributes);
}
/**
* Process the body text of this element.
*
* @param text The body text of this element
*/
public void body(String namespace, String name, String text)
throws Exception {
// While this class itself has no work to do in the body method,
// we do need to fire the body methods of all dynamically-added
// rules matching the same path as this rule. During begin, we had
// to manually execute the dynamic rules' begin methods because they
// didn't exist in the digester's Rules object when the match begin.
// So in order to ensure consistent ordering of rule execution, the
// PluginRules class deliberately avoids returning any such rules
// in later calls to the match method, instead relying on this
// object to execute them at the appropriate time.
//
// Note that this applies only to rules matching exactly the path
// which is also matched by this PluginCreateRule.
String path = digester.getMatch();
PluginRules newRules = (PluginRules) digester.getRules();
List rules = newRules.getDecoratedRules().match(namespace, path);
fireBodyMethods(rules, namespace, name, text);
}
/**
* Invoked by the digester when the closing tag matching this Rule's
* pattern is encountered.
* </p>
*
* @param namespace Description of the Parameter
* @param name Description of the Parameter
* @exception Exception Description of the Exception
*
* @see #begin
*/
public void end(String namespace, String name)
throws Exception {
// see body method for more info
String path = digester.getMatch();
PluginRules newRules = (PluginRules) digester.getRules();
List rules = newRules.getDecoratedRules().match(namespace, path);
fireEndMethods(rules, namespace, name);
// pop the stack of PluginRules instances, which
// discards all custom rules associated with this plugin
digester.setRules(newRules.getParent());
// and get rid of the instance of the plugin class from the
// digester object stack.
digester.pop();
}
/**
* Return the pattern that this Rule is associated with.
* <p>
* In general, Rule instances <i>can</i> be associated with multiple
* patterns. A PluginCreateRule, however, will only function correctly
* when associated with a single pattern. It is possible to fix this, but
* I can't be bothered just now because this feature is unlikely to be
* used.
* </p>
*
* @return The pattern value
*/
public String getPattern() {
return pattern;
}
/**
* Duplicate the processing that the Digester does when firing the
* begin methods of rules. It would be really nice if the Digester
* class provided a way for this functionality to just be invoked
* directly.
*/
public void fireBeginMethods(List rules,
String namespace, String name,
org.xml.sax.Attributes list)
throws java.lang.Exception {
if ((rules != null) && (rules.size() > 0)) {
Log log = digester.getLogger();
boolean debug = log.isDebugEnabled();
for (int i = 0; i < rules.size(); i++) {
try {
Rule rule = (Rule) rules.get(i);
if (debug) {
log.debug(" Fire begin() for " + rule);
}
rule.begin(namespace, name, list);
} catch (Exception e) {
throw digester.createSAXException(e);
} catch (Error e) {
throw e;
}
}
}
}
/**
* Duplicate the processing that the Digester does when firing the
* body methods of rules. It would be really nice if the Digester
* class provided a way for this functionality to just be invoked
* directly.
*/
private void fireBodyMethods(List rules,
String namespaceURI, String name,
String text) throws Exception {
if ((rules != null) && (rules.size() > 0)) {
Log log = digester.getLogger();
boolean debug = log.isDebugEnabled();
for (int i = 0; i < rules.size(); i++) {
try {
Rule rule = (Rule) rules.get(i);
if (debug) {
log.debug(" Fire body() for " + rule);
}
rule.body(namespaceURI, name, text);
} catch (Exception e) {
throw digester.createSAXException(e);
} catch (Error e) {
throw e;
}
}
}
}
/**
* Duplicate the processing that the Digester does when firing the
* end methods of rules. It would be really nice if the Digester
* class provided a way for this functionality to just be invoked
* directly.
*/
public void fireEndMethods(List rules,
String namespaceURI, String name)
throws Exception {
// Fire "end" events for all relevant rules in reverse order
if (rules != null) {
Log log = digester.getLogger();
boolean debug = log.isDebugEnabled();
for (int i = 0; i < rules.size(); i++) {
int j = (rules.size() - i) - 1;
try {
Rule rule = (Rule) rules.get(j);
if (debug) {
log.debug(" Fire end() for " + rule);
}
rule.end(namespaceURI, name);
} catch (Exception e) {
throw digester.createSAXException(e);
} catch (Error e) {
throw e;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -