List<Action> suffixMatch(ElementPath elementPath) { int max = 0; ElementSelector longestMatchingElementSelector = null; for (ElementSelector selector : rules.keySet()) { if (isSuffixPattern(selector)) { int r = selector.getTailMatchLength(elementPath); if (r > max) { max = r; longestMatchingElementSelector = selector; } } } if (longestMatchingElementSelector != null) { return rules.get(longestMatchingElementSelector); } else { return null; } }
List<Action> prefixMatch(ElementPath elementPath) { int max = 0; ElementSelector longestMatchingElementSelector = null; for (ElementSelector selector : rules.keySet()) { String last = selector.peekLast(); if (isKleeneStar(last)) { int r = selector.getPrefixMatchLength(elementPath); // to qualify the match length must equal p's size omitting the '*' if ((r == selector.size() - 1) && (r > max)) { max = r; longestMatchingElementSelector = selector; } } } if (longestMatchingElementSelector != null) { return rules.get(longestMatchingElementSelector); } else { return null; } }
private void startElement(String namespaceURI, String localName, String qName, Attributes atts) { String tagName = getTagName(localName, qName); elementPath.push(tagName); if (skip != null) { // every startElement pushes an action list pushEmptyActionList(); return; } List<Action> applicableActionList = getApplicableActionList(elementPath, atts); if (applicableActionList != null) { actionListStack.add(applicableActionList); callBeginAction(applicableActionList, tagName, atts); } else { // every startElement pushes an action list pushEmptyActionList(); String errMsg = "no applicable action for [" + tagName + "], current ElementPath is [" + elementPath + "]"; cai.addError(errMsg); } }
private void endElement(String namespaceURI, String localName, String qName) { // given that an action list is always pushed for every startElement, we // need // to always pop for every endElement List<Action> applicableActionList = (List<Action>) actionListStack.pop(); if (skip != null) { if (skip.equals(elementPath)) { skip = null; } } else if (applicableActionList != EMPTY_LIST) { callEndAction(applicableActionList, getTagName(localName, qName)); } // given that we always push, we must also pop the pattern elementPath.pop(); }
/** * Check if any implicit actions are applicable. As soon as an applicable * action is found, it is returned. Thus, the returned list will have at most * one element. */ List<Action> lookupImplicitAction(ElementPath elementPath, Attributes attributes, InterpretationContext ec) { int len = implicitActions.size(); for (int i = 0; i < len; i++) { ImplicitAction ia = (ImplicitAction) implicitActions.get(i); if (ia.isApplicable(elementPath, attributes, ec)) { List<Action> actionList = new ArrayList<Action>(1); actionList.add(ia); return actionList; } } return null; }
private void callBodyAction(List<Action> applicableActionList, String body) { if (applicableActionList == null) { return; } Iterator<Action> i = applicableActionList.iterator(); while (i.hasNext()) { Action action = i.next(); try { action.body(interpretationContext, body); } catch (ActionException ae) { cai .addError("Exception in end() methd for action [" + action + "]", ae); } } }
private void callEndAction(List<Action> applicableActionList, String tagName) { if (applicableActionList == null) { return; } // logger.debug("About to call end actions on node: [" + localName + "]"); Iterator<Action> i = applicableActionList.iterator(); while (i.hasNext()) { Action action = i.next(); // now let us invoke the end method of the action. We catch and report // any eventual exceptions try { action.end(interpretationContext, tagName); } catch (ActionException ae) { // at this point endAction, there is no point in skipping children as // they have been already processed cai.addError("ActionException in Action for tag [" + tagName + "]", ae); } catch (RuntimeException e) { // no point in setting skip cai.addError("RuntimeException in Action for tag [" + tagName + "]", e); } } }
@Test public void smoke() throws Exception { srs.addRule(new ElementSelector("a/b"), new XAction()); // test for all possible case combinations of "a/b" for (String s : cc.combinations("a/b")) { System.out.println("s="+s); List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void smokeII() throws Exception { srs.addRule(new ElementSelector("a/b"), new XAction()); srs.addRule(new ElementSelector("a/b"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(2, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } if (!(r.get(1) instanceof YAction)) { fail("Wrong type"); } } }
@Test public void testSlashSuffix() throws Exception { ElementSelector pa = new ElementSelector("a/"); srs.addRule(pa, new XAction()); for (String s : cc.combinations("a")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testTail2() throws Exception { SimpleRuleStore srs = new SimpleRuleStore(new ContextBase()); srs.addRule(new ElementSelector("*/c"), new XAction()); for (String s : cc.combinations("a/b/c")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testTail4() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); srs.addRule(new ElementSelector("*/a/b"), new YAction()); srs.addRule(new ElementSelector("a/b"), new ZAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof ZAction)) { fail("Wrong type"); } } }
@Test public void testPrefixSuffixInteraction1() throws Exception { srs.addRule(new ElementSelector("a"), new ZAction()); srs.addRule(new ElementSelector("a/*"), new YAction()); srs.addRule(new ElementSelector("*/a/b"), new XAction(3)); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); assertTrue(r.get(0) instanceof XAction); XAction xaction = (XAction) r.get(0); assertEquals(3, xaction.id); } }
public List<FruitShell> doFirstPart(String filename) throws Exception { try { HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); rulesMap.put(new ElementSelector("group/fruitShell"), new FruitShellAction()); rulesMap.put(new ElementSelector("group/fruitShell/fruit"), new FruitFactoryAction()); rulesMap.put(new ElementSelector("group/fruitShell/fruit/*"), new NOPAction()); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap); simpleConfigurator.setContext(fruitContext); simpleConfigurator.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/replay/" + filename); return fruitContext.getFruitShellList(); } catch (Exception je) { StatusPrinter.print(fruitContext); throw je; } }
public static void main(String[] args) throws Exception { Context context = new ContextBase(); Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>(); // we start with the rule for the top-most (root) element ruleMap.put(new ElementSelector("*/foo"), new NOPAction()); // Add an implicit action. List<ImplicitAction> iaList = new ArrayList<ImplicitAction>(); iaList.add(new PrintMeImplicitAction()); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap, iaList); // link the configurator with its context simpleConfigurator.setContext(context); simpleConfigurator.doConfigure(args[0]); StatusPrinter.printInCaseOfErrorsOrWarnings(context); }
public static void main(String[] args) throws Exception { Context context = new ContextBase(); Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>(); // we start with the rule for the top-most (root) element ruleMap.put(new ElementSelector("*/computation"), new ComputationAction1()); // Associate "/new-rule" pattern with NewRuleAction from the // org.apache.joran.action package. // // We will let the XML file to teach the Joran interpreter about new rules ruleMap.put(new ElementSelector("/computation/newRule"), new NewRuleAction()); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap); // link the configurator with its context simpleConfigurator.setContext(context); simpleConfigurator.doConfigure(args[0]); // Print any errors that might have occured. StatusPrinter.printInCaseOfErrorsOrWarnings(context); }
public static void main(String[] args) throws Exception { Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>(); // Note the wild card character '*', in the paterns, signifying any level // of nesting. ruleMap.put(new ElementSelector("*/computation"), new ComputationAction2()); ruleMap.put(new ElementSelector("*/computation/literal"), new LiteralAction()); ruleMap.put(new ElementSelector("*/computation/add"), new AddAction()); ruleMap.put(new ElementSelector("*/computation/multiply"), new MultiplyAction()); Context context = new ContextBase(); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap); // link the configurator with its context simpleConfigurator.setContext(context); try { simpleConfigurator.doConfigure(args[0]); } catch (JoranException e) { // Print any errors that might have occured. StatusPrinter.print(context); } }
public static void main(String[] args) throws Exception { Context context = new ContextBase(); Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>(); // Associate "/computation" pattern with ComputationAction1 ruleMap.put(new ElementSelector("/computation"), new ComputationAction1()); // Other associations ruleMap.put(new ElementSelector("/computation/literal"), new LiteralAction()); ruleMap.put(new ElementSelector("/computation/add"), new AddAction()); ruleMap.put(new ElementSelector("/computation/multiply"), new MultiplyAction()); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap); // link the configurator with its context simpleConfigurator.setContext(context); simpleConfigurator.doConfigure(args[0]); // Print any errors that might have occured. StatusPrinter.print(context); }
/** * Add a new rule, i.e. a pattern, action pair to the rule store. <p> Note * that the added action's LoggerRepository will be set in the process. */ public void addRule(ElementSelector elementSelector, Action action) { action.setContext(context); List<Action> a4p = rules.get(elementSelector); if (a4p == null) { a4p = new ArrayList<Action>(); rules.put(elementSelector, a4p); } a4p.add(action); }
public List<Action> matchActions(ElementPath elementPath) { List<Action> actionList; if ((actionList = fullPathMatch(elementPath)) != null) { return actionList; } else if ((actionList = suffixMatch(elementPath)) != null) { return actionList; } else if ((actionList = prefixMatch(elementPath)) != null) { return actionList; } else if ((actionList = middleMatch(elementPath)) != null) { return actionList; } else { return null; } }
List<Action> fullPathMatch(ElementPath elementPath) { for (ElementSelector selector : rules.keySet()) { if(selector.fullPathMatch(elementPath)) return rules.get(selector); } return null; }
List<Action> middleMatch(ElementPath path) { int max = 0; ElementSelector longestMatchingElementSelector = null; for (ElementSelector selector : rules.keySet()) { String last = selector.peekLast(); String first = null; if(selector.size() > 1) { first = selector.get(0); } if (isKleeneStar(last) && isKleeneStar(first)) { List<String> copyOfPartList = selector.getCopyOfPartList(); if(copyOfPartList.size() > 2) { copyOfPartList.remove(0); copyOfPartList.remove(copyOfPartList.size()-1); } int r = 0; ElementSelector clone = new ElementSelector(copyOfPartList); if(clone.isContainedIn(path)) { r = clone.size(); } if (r > max) { max = r; longestMatchingElementSelector = selector; } } } if (longestMatchingElementSelector != null) { return rules.get(longestMatchingElementSelector); } else { return null; } }
public Interpreter(Context context, RuleStore rs, ElementPath initialElementPath) { this.cai = new CAI_WithLocatorSupport(context, this); ruleStore = rs; interpretationContext = new InterpretationContext(context, this); implicitActions = new ArrayList<ImplicitAction>(3); this.elementPath = initialElementPath; actionListStack = new Stack<List<Action>>(); eventPlayer = new EventPlayer(this); }
public void characters(BodyEvent be) { setDocumentLocator(be.locator); String body = be.getText(); List<Action> applicableActionList = actionListStack.peek(); if (body != null) { body = body.trim(); if (body.length() > 0) { // System.out.println("calling body method with ["+body+ "]"); callBodyAction(applicableActionList, body); } } }
/** * Return the list of applicable patterns for this */ List<Action> getApplicableActionList(ElementPath elementPath, Attributes attributes) { List<Action> applicableActionList = ruleStore.matchActions(elementPath); // logger.debug("set of applicable patterns: " + applicableActionList); if (applicableActionList == null) { applicableActionList = lookupImplicitAction(elementPath, attributes, interpretationContext); } return applicableActionList; }
@Override protected void addInstanceRules(RuleStore rs) { for(ElementSelector elementSelector : rulesMap.keySet()) { Action action = rulesMap.get(elementSelector); rs.addRule(elementSelector, action); } }
@Test public void testTail1() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testTail3() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); srs.addRule(new ElementSelector("*/a/b"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof YAction)) { fail("Wrong type"); } } }
@Test public void testSuffix() throws Exception { srs.addRule(new ElementSelector("a"), new XAction()); srs.addRule(new ElementSelector("a/*"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); assertTrue(r.get(0) instanceof YAction); } }
@Test public void testDeepSuffix() throws Exception { srs.addRule(new ElementSelector("a"), new XAction(1)); srs.addRule(new ElementSelector("a/b/*"), new XAction(2)); for (String s : cc.combinations("a/other")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNull(r); } }
@Test public void testPrefixSuffixInteraction2() throws Exception { srs.addRule(new ElementSelector("tG"), new XAction()); srs.addRule(new ElementSelector("tG/tS"), new YAction()); srs.addRule(new ElementSelector("tG/tS/test"), new ZAction()); srs.addRule(new ElementSelector("tG/tS/test/*"), new XAction(9)); for (String s : cc.combinations("tG/tS/toto")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNull(r); } }
@Before public void setUp() throws Exception { HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); rulesMap.put(new ElementSelector("x"), new NOPAction()); rulesMap.put(new ElementSelector("x/stack"), stackAction); rulesMap.put(new ElementSelector("x/property"), new PropertyAction()); rulesMap.put(new ElementSelector("*/if"), new IfAction()); rulesMap.put(new ElementSelector("*/if/then"), new ThenAction()); rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction()); rulesMap.put(new ElementSelector("*/if/else"), new ElseAction()); rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction()); tc = new TrivialConfigurator(rulesMap); tc.setContext(context); }
@Before public void setUp() throws Exception { HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); rulesMap.put(new ElementSelector("x"), new NOPAction()); rulesMap.put(new ElementSelector("x/stack"), stackAction); rulesMap.put(new ElementSelector("*/if"), new IfAction()); rulesMap.put(new ElementSelector("*/if/then"), new ThenAction()); rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction()); rulesMap.put(new ElementSelector("*/if/else"), new ElseAction()); rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction()); rulesMap.put(new ElementSelector("x/include"), new IncludeAction()); tc = new TrivialConfigurator(rulesMap); tc.setContext(context); }
@Before public void setUp() throws Exception { fruitContext.setName("fruits"); HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); rulesMap.put(new ElementSelector("/context/"), new FruitContextAction()); simpleConfigurator = new SimpleConfigurator(rulesMap); simpleConfigurator.setContext(fruitContext); }
public static void main(String[] args) throws Exception { Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>(); // Associate "hello-world" pattern with HelloWorldAction ruleMap.put(new ElementSelector("hello-world"), new HelloWorldAction()); // Joran needs to work within a context. Context context = new ContextBase(); SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap); // link the configurator with its context simpleConfigurator.setContext(context); simpleConfigurator.doConfigure(args[0]); StatusPrinter.print(context); }
@Override protected void addInstanceRules(RuleStore rs) { for (ElementSelector elementSelector : ruleMap.keySet()) { Action action = ruleMap.get(elementSelector); rs.addRule(elementSelector, action); } }
@Override protected void addInstanceRules(RuleStore rs) { for (Pattern pattern : ruleMap.keySet()) { Action action = ruleMap.get(pattern); rs.addRule(pattern, action); } }
public TrivialConfigurator(HashMap<ElementSelector, Action> rules) { this.rulesMap = rules; }
public SimpleConfigurator(HashMap<ElementSelector, Action> rules) { this.rulesMap = rules; }