📄 scxmldigester.java
字号:
//---------------------- PRIVATE UTILITY METHODS ----------------------//
/*
* Private utility functions for configuring digester rule base for SCXML.
*/
/**
* Initialize the Digester rules for the current document.
*
* @param scxml The parent SCXML document (or null)
* @param pr The PathResolver
* @param customActions The list of custom actions this digester needs
* to be able to process
*
* @return scxmlRules The rule set to be used for digestion
*/
private static ExtendedBaseRules initRules(final SCXML scxml,
final PathResolver pr, final List customActions) {
ExtendedBaseRules scxmlRules = new ExtendedBaseRules();
scxmlRules.setNamespaceURI(NAMESPACE_SCXML);
//// SCXML
scxmlRules.add(XP_SM, new ObjectCreateRule(SCXML.class));
scxmlRules.add(XP_SM, new SetPropertiesRule());
//// Datamodel at document root i.e. <scxml> datamodel
addDatamodelRules(XP_SM + XPF_DM, scxmlRules, scxml, pr);
//// States
// Level one states
addStateRules(XP_SM_ST, scxmlRules, customActions, scxml, pr, 0);
scxmlRules.add(XP_SM_ST, new SetNextRule("addState"));
// Nested states
addStateRules(XPU_ST_ST, scxmlRules, customActions, scxml, pr, 1);
scxmlRules.add(XPU_ST_ST, new SetNextRule("addChild"));
// Parallel states
addStateRules(XPU_PAR_ST, scxmlRules, customActions, scxml, pr, 1);
scxmlRules.add(XPU_PAR_ST, new SetNextRule("addState"));
// Target states
addStateRules(XPU_TR_TAR_ST, scxmlRules, customActions, scxml, pr, 2);
scxmlRules.add(XPU_TR_TAR_ST, new SetNextRule("setTarget"));
//// Parallels
addParallelRules(XPU_ST_PAR, scxmlRules, pr, customActions, scxml);
//// Ifs
addIfRules(XPU_IF, scxmlRules, pr, customActions);
//// Custom actions
addCustomActionRules(XPU_ONEN, scxmlRules, customActions);
addCustomActionRules(XPU_ONEX, scxmlRules, customActions);
addCustomActionRules(XPU_TR, scxmlRules, customActions);
addCustomActionRules(XPU_IF, scxmlRules, customActions);
addCustomActionRules(XPU_FIN, scxmlRules, customActions);
return scxmlRules;
}
/**
* Add Digester rules for all <state> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param scxml The parent SCXML document (or null)
* @param pr The PathResolver
* @param parent The distance between this state and its parent
* state on the Digester stack
*/
private static void addStateRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final SCXML scxml, final PathResolver pr, final int parent) {
scxmlRules.add(xp, new ObjectCreateRule(State.class));
addStatePropertiesRules(xp, scxmlRules, customActions, pr, scxml);
addDatamodelRules(xp + XPF_DM, scxmlRules, scxml, pr);
addInvokeRules(xp + XPF_INV, scxmlRules, customActions, pr, scxml);
addInitialRules(xp + XPF_INI, scxmlRules, customActions, pr, scxml);
addHistoryRules(xp + XPF_HIST, scxmlRules, customActions, pr, scxml);
addParentRule(xp, scxmlRules, parent);
addTransitionRules(xp + XPF_TR, scxmlRules, "addTransition",
pr, customActions);
addHandlerRules(xp, scxmlRules, pr, customActions);
scxmlRules.add(xp, new UpdateModelRule(scxml));
}
/**
* Add Digester rules for all <parallel> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param pr The {@link PathResolver} for this document
* @param scxml The parent SCXML document (or null)
*/
private static void addParallelRules(final String xp,
final ExtendedBaseRules scxmlRules, final PathResolver pr,
final List customActions, final SCXML scxml) {
addSimpleRulesTuple(xp, scxmlRules, Parallel.class, null, null,
"setParallel");
addHandlerRules(xp, scxmlRules, pr, customActions);
addParentRule(xp, scxmlRules, 1);
scxmlRules.add(xp, new UpdateModelRule(scxml));
}
/**
* Add Digester rules for all <state> element attributes.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param pr The PathResolver
* @param scxml The root document, if this one is src'ed in
*/
private static void addStatePropertiesRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final PathResolver pr, final SCXML scxml) {
scxmlRules.add(xp, new SetPropertiesRule(new String[] {"id", "final"},
new String[] {"id", "isFinal"}));
scxmlRules.add(xp, new DigestSrcAttributeRule(scxml,
customActions, pr));
}
/**
* Add Digester rules for all <datamodel> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param pr The PathResolver
* @param scxml The parent SCXML document (or null)
*/
private static void addDatamodelRules(final String xp,
final ExtendedBaseRules scxmlRules, final SCXML scxml,
final PathResolver pr) {
scxmlRules.add(xp, new ObjectCreateRule(Datamodel.class));
scxmlRules.add(xp + XPF_DATA, new ObjectCreateRule(Data.class));
scxmlRules.add(xp + XPF_DATA, new SetPropertiesRule());
scxmlRules.add(xp + XPF_DATA, new SetCurrentNamespacesRule());
scxmlRules.add(xp + XPF_DATA, new SetNextRule("addData"));
try {
scxmlRules.add(xp + XPF_DATA, new ParseDataRule(pr));
} catch (ParserConfigurationException pce) {
org.apache.commons.logging.Log log = LogFactory.
getLog(SCXMLDigester.class);
log.error(ERR_PARSER_CFG_DATA, pce);
}
scxmlRules.add(xp, new SetNextRule("setDatamodel"));
}
/**
* Add Digester rules for all <invoke> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of {@link CustomAction}s this digester
* instance will process, can be null or empty
* @param pr The PathResolver
* @param scxml The parent SCXML document (or null)
*/
private static void addInvokeRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final PathResolver pr, final SCXML scxml) {
scxmlRules.add(xp, new ObjectCreateRule(Invoke.class));
scxmlRules.add(xp, new SetPropertiesRule());
scxmlRules.add(xp, new SetCurrentNamespacesRule());
scxmlRules.add(xp, new SetPathResolverRule(pr));
scxmlRules.add(xp + XPF_PRM, new ObjectCreateRule(Param.class));
scxmlRules.add(xp + XPF_PRM, new SetPropertiesRule());
scxmlRules.add(xp + XPF_PRM, new SetCurrentNamespacesRule());
scxmlRules.add(xp + XPF_PRM, new SetNextRule("addParam"));
scxmlRules.add(xp + XPF_FIN, new ObjectCreateRule(Finalize.class));
scxmlRules.add(xp + XPF_FIN, new UpdateFinalizeRule());
addActionRules(xp + XPF_FIN, scxmlRules, pr, customActions);
scxmlRules.add(xp + XPF_FIN, new SetNextRule("setFinalize"));
scxmlRules.add(xp, new SetNextRule("setInvoke"));
}
/**
* Add Digester rules for all <initial> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param pr The PathResolver
* @param scxml The parent SCXML document (or null)
*/
private static void addInitialRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final PathResolver pr, final SCXML scxml) {
scxmlRules.add(xp, new ObjectCreateRule(Initial.class));
addPseudoStatePropertiesRules(xp, scxmlRules, customActions, pr,
scxml);
scxmlRules.add(xp, new UpdateModelRule(scxml));
addTransitionRules(xp + XPF_TR, scxmlRules, "setTransition",
pr, customActions);
scxmlRules.add(xp, new SetNextRule("setInitial"));
}
/**
* Add Digester rules for all <history> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param pr The PathResolver
* @param scxml The parent SCXML document (or null)
*/
private static void addHistoryRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final PathResolver pr, final SCXML scxml) {
scxmlRules.add(xp, new ObjectCreateRule(History.class));
addPseudoStatePropertiesRules(xp, scxmlRules, customActions, pr,
scxml);
scxmlRules.add(xp, new UpdateModelRule(scxml));
scxmlRules.add(xp, new SetPropertiesRule(new String[] {"type"},
new String[] {"type"}));
addTransitionRules(xp + XPF_TR, scxmlRules, "setTransition",
pr, customActions);
scxmlRules.add(xp, new SetNextRule("addHistory"));
}
/**
* Add Digester rules for all pseudo state (initial, history) element
* attributes.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param customActions The list of custom actions this digester needs
* to be able to process
* @param pr The PathResolver
* @param scxml The root document, if this one is src'ed in
*/
private static void addPseudoStatePropertiesRules(final String xp,
final ExtendedBaseRules scxmlRules, final List customActions,
final PathResolver pr, final SCXML scxml) {
scxmlRules.add(xp, new SetPropertiesRule(new String[] {"id"},
new String[] {"id"}));
scxmlRules.add(xp, new DigestSrcAttributeRule(scxml, customActions,
pr));
addParentRule(xp, scxmlRules, 1);
}
/**
* Add Digester rule for all setting parent state.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param parent The distance between this state and its parent
* state on the Digester stack
*/
private static void addParentRule(final String xp,
final ExtendedBaseRules scxmlRules, final int parent) {
if (parent < 1) {
return;
}
scxmlRules.add(xp, new Rule() {
// A generic version of setTopRule
public void body(final String namespace, final String name,
final String text) throws Exception {
TransitionTarget t = (TransitionTarget) getDigester().peek();
TransitionTarget p = (TransitionTarget) getDigester().peek(
parent);
// CHANGE - Moved parent property to TransitionTarget
t.setParent(p);
}
});
}
/**
* Add Digester rules for all <transition> elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param setNextMethod The method name for adding this transition
* to its parent (defined by the SCXML Java object model).
* @param pr The {@link PathResolver} for this document
* @param customActions The list of custom actions this digester needs
* to be able to process
*/
private static void addTransitionRules(final String xp,
final ExtendedBaseRules scxmlRules, final String setNextMethod,
final PathResolver pr, final List customActions) {
scxmlRules.add(xp, new ObjectCreateRule(Transition.class));
scxmlRules.add(xp, new SetPropertiesRule(
new String[] {"event", "cond", "target"},
new String[] {"event", "cond", "next"}));
scxmlRules.add(xp, new SetCurrentNamespacesRule());
scxmlRules.add(xp + XPF_TAR, new SetPropertiesRule());
addActionRules(xp, scxmlRules, pr, customActions);
scxmlRules.add(xp + XPF_EXT, new Rule() {
public void end(final String namespace, final String name) {
Transition t = (Transition) getDigester().peek(1);
State exitState = new State();
exitState.setFinal(true);
t.getTargets().add(exitState);
}
});
scxmlRules.add(xp, new SetNextRule(setNextMethod));
}
/**
* Add Digester rules for all <onentry> and <onexit>
* elements.
*
* @param xp The Digester style XPath expression of the parent
* XML element
* @param scxmlRules The rule set to be used for digestion
* @param pr The {@link PathResolver} for this document
* @param customActions The list of custom actions this digester needs
* to be able to process
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -