You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oz...@apache.org on 2005/02/12 08:27:49 UTC

svn commit: r153487 - in jakarta/commons/proper/digester/branches/digester2/src: java/org/apache/commons/digester2/DefaultRuleManager.java java/org/apache/commons/digester2/Path.java java/org/apache/commons/digester2/SupplementaryRuleManager.java test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java

Author: ozeigermann
Date: Fri Feb 11 23:27:46 2005
New Revision: 153487

URL: http://svn.apache.org/viewcvs?view=rev&rev=153487
Log:
Added early proposal for xmlio like rule manager and processing style

Added:
    jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java
    jakarta/commons/proper/digester/branches/digester2/src/test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java
Modified:
    jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/DefaultRuleManager.java
    jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Path.java

Modified: jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/DefaultRuleManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/DefaultRuleManager.java?view=diff&r1=153486&r2=153487
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/DefaultRuleManager.java (original)
+++ jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/DefaultRuleManager.java Fri Feb 11 23:27:46 2005
@@ -58,7 +58,7 @@
      * Map of namespace-prefix to namespace-uri, used only by the
      * addAction() method.
      */
-    private HashMap namespaces = new HashMap();
+    protected HashMap namespaces = new HashMap();
     
     /**
      * The list of all actions in the cache. This set allows us to
@@ -72,7 +72,7 @@
      * find the pattern that matches the current xml element, then
      * return the list of actions.
      */
-    private MultiHashMap rules = new MultiHashMap();
+    protected MultiHashMap rules = new MultiHashMap();
 
     // --------------------------------------------------------- 
     // Public Methods

Modified: jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Path.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Path.java?view=diff&r1=153486&r2=153487
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Path.java (original)
+++ jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/Path.java Fri Feb 11 23:27:46 2005
@@ -141,5 +141,36 @@
         namespaces.clear();
         localNames.clear();
     }
+    
+    public boolean matches(String pathToMatch) {
+        if (pathToMatch.charAt(0) == '/') {
+            // absolute
+            return getPath().equals(pathToMatch);
+        } else {
+            // relative
+            // XXX looks wrong but protects a match of 
+            // "a/b" against a path of "/gotcha/b", but
+            // still allows
+            // "a/b" to match against "/a/b"
+            return getPath().endsWith("/" + pathToMatch);
+        }
+    }
+    
+    /** 
+     * Checks if this path matches any of the paths given. This means we iterate through 
+     * <code>pathsToMatch</code> and match every entry to this path.
+     */
+    public boolean matchsAny(String[] pathsToMatch) {
+        for (int i = 0; i < pathsToMatch.length; i++) {
+            if (matches(pathsToMatch[i]))
+                return true;
+        }
+        return false;
+    }
+
+    public String toString() {
+        return getPath();
+    }
+
 }
 

Added: jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java?view=auto&rev=153487
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java (added)
+++ jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SupplementaryRuleManager.java Fri Feb 11 23:27:46 2005
@@ -0,0 +1,103 @@
+/* $Id: DefaultRuleManager.java 153050 2005-02-09 12:12:28Z skitching $
+ *
+ * Copyright 2001-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+
+package org.apache.commons.digester2;
+
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+
+
+/**
+ * @see DefaultRuleManager
+ */
+
+public class SupplementaryRuleManager extends DefaultRuleManager {
+
+    protected final Action supplementaryAction;
+    protected final Action fallbackAction;
+    
+    protected final List fallbackList = new ArrayList();
+    
+    public SupplementaryRuleManager(Action supplementaryAction) {
+        this(supplementaryAction, null);
+    }
+    
+    public SupplementaryRuleManager(Action supplementaryAction, Action fallbackAction) {
+        
+        if (fallbackAction == null && supplementaryAction == null) {
+            throw new IllegalArgumentException(
+                    "Both parameters set to null makes no sense. Use DefaultRuleManager instead.");
+        }
+
+        this.supplementaryAction = supplementaryAction;
+        this.fallbackAction = fallbackAction;
+
+        if (fallbackAction != null) {
+            fallbackList.add(fallbackAction);
+        }
+
+        if (supplementaryAction != null) {
+            fallbackList.add(supplementaryAction);
+        }
+    }
+    
+    public SupplementaryRuleManager(SupplementaryRuleManager manager) {
+        this(manager.supplementaryAction, manager.fallbackAction);
+        this.namespaces = (HashMap) manager.namespaces.clone();
+        this.actions = (ArrayList) manager.actions.clone();
+        this.rules = (MultiHashMap) manager.rules.clone();
+    }
+    
+    /**
+     * @see DefaultRuleManager#copy()
+     */
+    public RuleManager copy() {
+        return new SupplementaryRuleManager(this);
+    }
+    
+    /**
+     * @see DefaultRuleManager#getMatchingActions(String)
+     */
+    public List getMatchingActions(String path) {
+        List actionList = super.getMatchingActions(path);
+        if (actionList == Collections.EMPTY_LIST) {
+            return fallbackList;
+        }
+        if (supplementaryAction != null) {
+            actionList.add(supplementaryAction);
+        }
+        return actionList;
+    }
+
+    /**
+     * @return Returns the fallbackAction.
+     */
+    public Action getFallbackAction() {
+        return fallbackAction;
+    }
+
+    /**
+     * @return Returns the supplementaryAction.
+     */
+    public Action getSupplementaryAction() {
+        return supplementaryAction;
+    }
+}

Added: jakarta/commons/proper/digester/branches/digester2/src/test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java?view=auto&rev=153487
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java (added)
+++ jakarta/commons/proper/digester/branches/digester2/src/test/org/apache/commons/digester2/SupplementaryRuleManagerTestCase.java Fri Feb 11 23:27:46 2005
@@ -0,0 +1,132 @@
+/* $Id: DefaultRuleManagerTestCase.java 151644 2005-02-07 00:08:59Z skitching $
+ *
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.digester2;
+
+import java.io.StringReader;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
+/**
+ * <p>
+ * Test Case for the DefaultRuleManager class.
+ * </p>
+ */
+
+public class SupplementaryRuleManagerTestCase extends TestCase {
+
+    private static class XMLLikeAction extends AbstractAction {
+        
+        boolean rootFoundAbsolute = false;
+        boolean rootFoundRelative = false;
+        boolean wrongRelativeFound = false;
+        boolean longAbsoluteFound = false;
+    
+        public void begin(Context context, String namespace, String name, Attributes attrs) {
+
+            Path path = context.getCurrentPath();
+            if (path.matches("/root")) {
+                rootFoundAbsolute = true;
+            } 
+            
+            if (path.matches("root")) {
+                rootFoundRelative = true;
+            }
+
+            if (path.matches("/root/p/em")) {
+                longAbsoluteFound = true;
+            } 
+
+            if (path.matches("ot/p")) {
+                wrongRelativeFound = true;
+            }
+
+        }
+
+    };
+
+    // -----------------------------------------------------------
+    // Constructors
+    // -----------------------------------------------------------
+
+    /**
+     * Construct a new instance of this test case.
+     * 
+     * @param name
+     *            Name of the test case
+     */
+    public SupplementaryRuleManagerTestCase(String name) {
+        super(name);
+    }
+
+    // --------------------------------------------------
+    // Overall Test Methods
+    // --------------------------------------------------
+
+    /**
+     * Set up instance variables required by this test case.
+     */
+    public void setUp() {
+    }
+
+    /**
+     * Return the tests included in this test suite.
+     */
+    public static Test suite() {
+        return (new TestSuite(SupplementaryRuleManagerTestCase.class));
+    }
+
+    /**
+     * Tear down instance variables required by this test case.
+     */
+    public void tearDown() {
+    }
+
+    // ------------------------------------------------
+    // Individual Test Methods
+    // ------------------------------------------------
+
+    public void testGlobalCallBack() throws Exception {
+        String inputText = "<root><p>Hi <em>There</em></p></root>";
+        InputSource source = new InputSource(new StringReader(inputText));
+
+        Digester d = new Digester();
+
+        XMLReader reader = d.getXMLReader();
+
+        // xmlio-style digester
+        XMLLikeAction xmlioLikeHandler = new XMLLikeAction();
+
+        RuleManager manager = new SupplementaryRuleManager(xmlioLikeHandler);
+        d.setRuleManager(manager);
+
+        d.parse(source);
+
+        assertTrue("Root element was found absolute", xmlioLikeHandler.rootFoundAbsolute);
+        assertTrue("Root element was found relative", xmlioLikeHandler.rootFoundRelative);
+        assertTrue("Long absolute path was found", xmlioLikeHandler.longAbsoluteFound);
+        assertFalse("Incomplete relative was not found", xmlioLikeHandler.wrongRelativeFound);
+        
+    }
+
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org