svn commit: r708 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/joran/spi test/java/ch/qos/logback/core test/java/ch/qos/logback/core/joran test/java/ch/qos/logback/core/joran/action

Author: ceki Date: Wed Oct 18 12:33:40 2006 New Revision: 708 Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialcConfiguratorTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/HelloAction.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/TouchAction.java Log: Interpreter no longer extends DefaultHandler. This change ensures that project as a whole stops accessing Interpreter directly and always goes through a Configurator which goes about its business of configuring in two steps, a event registration step and a replay (and interpret) step. Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java ============================================================================== --- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java (original) +++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java Wed Oct 18 12:33:40 2006 @@ -18,9 +18,6 @@ import org.xml.sax.Attributes; import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.helpers.DefaultHandler; import ch.qos.logback.core.joran.action.Action; import ch.qos.logback.core.joran.action.ImplicitAction; @@ -58,7 +55,7 @@ * @author Ceki Gülcuü * */ -public class Interpreter extends DefaultHandler { +public class Interpreter { private static List EMPTY_LIST = new Vector(0); private RuleStore ruleStore; private ExecutionContext ec; @@ -102,7 +99,7 @@ startElement(se.namespaceURI, se.localName, se.qName, se.attributes); } - public void startElement(String namespaceURI, String localName, String qName, + private void startElement(String namespaceURI, String localName, String qName, Attributes atts) { String tagName = getTagName(localName, qName); @@ -146,7 +143,7 @@ endElement(endEvent.namespaceURI, endEvent.localName, endEvent.qName); } - public void endElement(String namespaceURI, String localName, String qName) { + private void endElement(String namespaceURI, String localName, String qName) { List applicableActionList = (List) actionListStack.pop(); // System.out.println("endElement ["+getTagName(localName, qName)+"]"); @@ -270,7 +267,7 @@ } } - void callBodyAction(List applicableActionList, String body) { + private void callBodyAction(List applicableActionList, String body) { if (applicableActionList == null) { return; } @@ -287,7 +284,7 @@ } } - void callEndAction(List applicableActionList, String tagName) { + private void callEndAction(List applicableActionList, String tagName) { if (applicableActionList == null) { return; } @@ -329,95 +326,5 @@ this.ruleStore = ruleStore; } - // /** - // * Call the finish methods for all actions. Unfortunately, the endDocument - // * method is not called in case of errors in the XML document, which - // * makes endDocument() pretty damn useless. - // */ - // public void endDocument() { - // Set arrayListSet = ruleStore.getActionSet(); - // Iterator iterator = arrayListSet.iterator(); - // while(iterator.hasNext()) { - // ArrayList al = (ArrayList) iterator.next(); - // for(int i = 0; i < al.size(); i++) { - // Action a = (Action) al.get(i); - // a.endDocument(ec); - // } - // } - // } - public void error(SAXParseException spe) throws SAXException { - ec.addError("Parsing error", this, spe); - ec.addError("Parsing problem on line " + spe.getLineNumber() - + " and column " + spe.getColumnNumber(), this, spe); - } - - public void fatalError(SAXParseException spe) throws SAXException { - ec.addError("Parsing fatal error", this, spe); - ec.addError("Parsing problem on line " + spe.getLineNumber() - + " and column " + spe.getColumnNumber(), this, spe); - } - - public void warning(SAXParseException spe) throws SAXException { - ec.addWarn("Parsing warning", this, spe); - ec.addWarn("Parsing problem on line " + spe.getLineNumber() - + " and column " + spe.getColumnNumber(), this, spe); - } - - public void endPrefixMapping(java.lang.String prefix) { - } - - public void ignorableWhitespace(char[] ch, int start, int length) { - } - - public void processingInstruction(java.lang.String target, - java.lang.String data) { - } - - public void skippedEntity(java.lang.String name) { - } - - public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) { - } - - // public EntityResolver getEntityResolver() { - // return entityResolver; - // } - // - // public void setEntityResolver(EntityResolver entityResolver) { - // this.entityResolver = entityResolver; - // } - - /** - * If a specific entityResolver is set for this Interpreter instance, then we - * use it to resolve entities. Otherwise, we use the default implementation - * offered by the super class. - * - * <p> - * Due to inexplicable voodoo, the original resolveEntity method in - * org.xml.sax.helpers.DefaultHandler declares throwing an IOException, - * whereas the org.xml.sax.helpers.DefaultHandler class included in JDK 1.4 - * masks this exception. - * - * <p> - * In order to compile under JDK 1.4, we are forced to mask the IOException as - * well. Since its signatures varies, we cannot call our super class' - * resolveEntity method. We are forced to implement the default behavior - * ourselves, which in this case, is just returning null. - * - */ - // public InputSource resolveEntity(String publicId, String systemId) throws - // SAXException { - // if(entityResolver == null) { - // // the default implementation is to return null - // return null; - // } else { - // try { - // return entityResolver.resolveEntity(publicId, systemId); - // } catch(IOException ioe) { - // // fall back to the default "implementation" - // return null; - // } - // } - // } } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java Wed Oct 18 12:33:40 2006 @@ -1,14 +1,12 @@ package ch.qos.logback.core; - - import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class AllTest extends TestCase { - public static Test suite() { + public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(ch.qos.logback.core.util.PackageTest.suite()); suite.addTest(ch.qos.logback.core.pattern.AllTest.suite()); Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java Wed Oct 18 12:33:40 2006 @@ -9,22 +9,22 @@ */ package ch.qos.logback.core.joran; +import java.util.HashMap; + import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import ch.qos.logback.core.Context; import ch.qos.logback.core.ContextBase; +import ch.qos.logback.core.joran.action.Action; import ch.qos.logback.core.joran.action.BadBeginAction; import ch.qos.logback.core.joran.action.BadEndAction; import ch.qos.logback.core.joran.action.HelloAction; import ch.qos.logback.core.joran.action.TouchAction; -import ch.qos.logback.core.joran.spi.ExecutionContext; -import ch.qos.logback.core.joran.spi.Interpreter; import ch.qos.logback.core.joran.spi.Pattern; -import ch.qos.logback.core.joran.spi.RuleStore; -import ch.qos.logback.core.joran.spi.SimpleRuleStore; import ch.qos.logback.core.util.Constants; @@ -36,8 +36,12 @@ */ public class SkippingInInterpreterTest extends TestCase { + HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>(); + Context context = new ContextBase(); + public SkippingInInterpreterTest(String name) { super(name); + } protected void setUp() throws Exception { @@ -63,22 +67,21 @@ * @throws Exception */ public void testChildrenSkipping() throws Exception { - RuleStore rs = new SimpleRuleStore(new ContextBase()); - rs.addRule(new Pattern("test"), new NOPAction()); - rs.addRule(new Pattern("test/badBegin"), new BadBeginAction()); - rs.addRule(new Pattern("test/badBegin/touch"), new TouchAction()); - rs.addRule(new Pattern("test/hello"), new HelloAction()); - - Interpreter jp = new Interpreter(rs); - ExecutionContext ec = jp.getExecutionContext(); - ec.setContext(new ContextBase()); - - SAXParser saxParser = createParser(); - saxParser.parse("file:" + Constants.TEST_DIR_PREFIX + "input/joran/exception1.xml", jp); - String str = (String) ec.getObjectMap().get("hello"); + + rulesMap.put(new Pattern("test"), new NOPAction()); + rulesMap.put(new Pattern("test/badBegin"), new BadBeginAction()); + rulesMap.put(new Pattern("test/badBegin/touch"), new TouchAction()); + rulesMap.put(new Pattern("test/hello"), new HelloAction()); + + TrivialConfigurator tc = new TrivialConfigurator(rulesMap); + tc.setContext(context); + + tc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/exception1.xml"); + + String str = context.getProperty("hello"); assertEquals("Hello John Doe.", str); - Object i = ec.getObjectMap().get(TouchAction.KEY); + Object i = (String) context.getObject(TouchAction.KEY); assertNull(i); } @@ -90,43 +93,38 @@ */ public void testSkipSiblings() throws Exception { - RuleStore rs = new SimpleRuleStore(new ContextBase()); - rs.addRule(new Pattern("test"), new NOPAction()); - rs.addRule(new Pattern("test/badEnd"), new BadEndAction()); - rs.addRule(new Pattern("test/badEnd/touch"), new TouchAction()); - rs.addRule(new Pattern("test/hello"), new HelloAction()); - - Interpreter jp = new Interpreter(rs); - ExecutionContext ec = jp.getExecutionContext(); - ec.setContext(new ContextBase()); + rulesMap.put(new Pattern("test"), new NOPAction()); + rulesMap.put(new Pattern("test/badEnd"), new BadEndAction()); + rulesMap.put(new Pattern("test/badEnd/touch"), new TouchAction()); + rulesMap.put(new Pattern("test/hello"), new HelloAction()); + + TrivialConfigurator tc = new TrivialConfigurator(rulesMap); + tc.setContext(context); - SAXParser saxParser = createParser(); - saxParser.parse("file:" + Constants.TEST_DIR_PREFIX + "input/joran/badEnd1.xml", jp); + tc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/badEnd1.xml"); + - String str = (String) ec.getObjectMap().get("hello"); + String str = context.getProperty("hello"); assertNull(str); - Integer i = (Integer) ec.getObjectMap().get(TouchAction.KEY); + Integer i = (Integer) context.getObject(TouchAction.KEY); assertEquals(2, i.intValue()); } public void testSkipSiblings2() throws Exception { - RuleStore rs = new SimpleRuleStore(new ContextBase()); - rs.addRule(new Pattern("test"), new NOPAction()); - rs.addRule(new Pattern("test/isolate/badEnd"), new BadEndAction()); - rs.addRule(new Pattern("*/touch"), new TouchAction()); - rs.addRule(new Pattern("test/hello"), new HelloAction()); - - Interpreter jp = new Interpreter(rs); - ExecutionContext ec = jp.getExecutionContext(); - ec.setContext(new ContextBase()); + rulesMap.put(new Pattern("test"), new NOPAction()); + rulesMap.put(new Pattern("test/isolate/badEnd"), new BadEndAction()); + rulesMap.put(new Pattern("*/touch"), new TouchAction()); + rulesMap.put(new Pattern("test/hello"), new HelloAction()); + + TrivialConfigurator tc = new TrivialConfigurator(rulesMap); + tc.setContext(context); - SAXParser saxParser = createParser(); - saxParser.parse("file:" + Constants.TEST_DIR_PREFIX + "input/joran/badEnd2.xml", jp); + tc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/badEnd2.xml"); - String str = (String) ec.getObjectMap().get("hello"); + String str = context.getProperty("hello"); assertEquals("Hello John Doe.", str); - Integer i = (Integer) ec.getObjectMap().get(TouchAction.KEY); + Integer i = (Integer) context.getObject(TouchAction.KEY); assertEquals(1, i.intValue()); } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java Wed Oct 18 12:33:40 2006 @@ -1,20 +1,30 @@ package ch.qos.logback.core.joran; -import ch.qos.logback.core.joran.action.IncAction; +import java.util.HashMap; + +import ch.qos.logback.core.joran.action.Action; import ch.qos.logback.core.joran.spi.Interpreter; import ch.qos.logback.core.joran.spi.Pattern; import ch.qos.logback.core.joran.spi.RuleStore; public class TrivialConfigurator extends GenericConfigurator { + HashMap<Pattern, Action> rulesMap; + + TrivialConfigurator(HashMap<Pattern, Action> rules) { + this.rulesMap = rules; + } + @Override protected void addImplicitRules(Interpreter interpreter) { } @Override protected void addInstanceRules(RuleStore rs) { - rs.addRule(new Pattern("x/inc"), new IncAction()); - + for(Pattern pattern : rulesMap.keySet()) { + Action action = rulesMap.get(pattern); + rs.addRule(pattern, action); + } } } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialcConfiguratorTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialcConfiguratorTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialcConfiguratorTest.java Wed Oct 18 12:33:40 2006 @@ -1,9 +1,13 @@ package ch.qos.logback.core.joran; +import java.util.HashMap; + import junit.framework.TestCase; import ch.qos.logback.core.Context; import ch.qos.logback.core.ContextBase; +import ch.qos.logback.core.joran.action.Action; import ch.qos.logback.core.joran.action.IncAction; +import ch.qos.logback.core.joran.spi.Pattern; import ch.qos.logback.core.util.Constants; public class TrivialcConfiguratorTest extends TestCase { @@ -24,7 +28,11 @@ public void doTest(String filename) throws Exception { - TrivialConfigurator gc = new TrivialConfigurator(); + HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>(); + rulesMap.put(new Pattern("x/inc"), new IncAction()); + + TrivialConfigurator gc = new TrivialConfigurator(rulesMap); + gc.setContext(context); gc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/"+ filename); Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/HelloAction.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/HelloAction.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/HelloAction.java Wed Oct 18 12:33:40 2006 @@ -27,7 +27,7 @@ */ public void begin(ExecutionContext ec, String name, Attributes attributes) { String str = "Hello "+attributes.getValue("name")+"."; - ec.getObjectMap().put("hello", str); + ec.getContext().setProperty("hello", str); } /** Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/TouchAction.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/TouchAction.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/TouchAction.java Wed Oct 18 12:33:40 2006 @@ -29,11 +29,11 @@ * */ public void begin(ExecutionContext ec, String name, Attributes attributes) { - Integer i = (Integer) ec.getObjectMap().get(KEY); + Integer i = (Integer) ec.getContext().getObject(KEY); if(i == null) { - ec.getObjectMap().put(KEY, new Integer(1)); + ec.getContext().putObject(KEY, new Integer(1)); } else { - ec.getObjectMap().put(KEY, new Integer(i.intValue()+1)); + ec.getContext().putObject(KEY, new Integer(i.intValue()+1)); } }
participants (1)
-
noreply.ceki@qos.ch