You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ra...@apache.org on 2006/09/26 04:05:25 UTC

svn commit: r449882 - in /jakarta/commons/proper/digester/trunk/src: java/org/apache/commons/digester/ test/org/apache/commons/digester/

Author: rahul
Date: Mon Sep 25 19:05:24 2006
New Revision: 449882

URL: http://svn.apache.org/viewvc?view=rev&rev=449882
Log:
Provide ability to capture namespace snapshots. Useful if, for example, the document contains XPath expressions to be evaluated at some later point.

Reviewed by: Simon Kitching

DIGESTER-107

Added:
    jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java   (with props)
    jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java   (with props)
    jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml   (with props)
Modified:
    jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/Digester.java

Modified: jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/Digester.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/Digester.java?view=diff&rev=449882&r1=449881&r2=449882
==============================================================================
--- jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/Digester.java (original)
+++ jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/Digester.java Mon Sep 25 19:05:24 2006
@@ -1038,6 +1038,34 @@
     	return stackAction;
     }
 
+    /**
+     * Get the most current namespaces for all prefixes.
+     *
+     * @return Map A map with namespace prefixes as keys and most current
+     *             namespace URIs for the corresponding prefixes as values
+     *
+     * @since 1.8
+     */
+    public Map getCurrentNamespaces() {
+        if (!namespaceAware) {
+            log.warn("Digester is not namespace aware");
+        }
+        Map currentNamespaces = new HashMap();
+        Iterator nsIterator = namespaces.entrySet().iterator();
+        while (nsIterator.hasNext()) {
+            Map.Entry nsEntry = (Map.Entry) nsIterator.next();
+            try {
+                currentNamespaces.put(nsEntry.getKey(),
+                    ((ArrayStack) nsEntry.getValue()).peek());
+            } catch (RuntimeException e) {
+                // rethrow, after logging
+                log.error(e.getMessage(), e);
+                throw e;
+            }
+        }
+        return currentNamespaces;
+    }
+
     // ------------------------------------------------- ContentHandler Methods
 
 

Added: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java?view=auto&rev=449882
==============================================================================
--- jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java (added)
+++ jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java Mon Sep 25 19:05:24 2006
@@ -0,0 +1,126 @@
+/* $Id$
+ *
+ * Copyright 2006 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.digester;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.xml.sax.Attributes;
+
+
+/**
+ * Tests namespace snapshotting.
+ */
+
+public class NamespaceSnapshotTestCase extends TestCase {
+
+    /**
+     * A test case specific helper rule.
+     */
+    static class NamespaceSnapshotRule extends Rule {
+        /**
+         * @see Rule#begin(String, String, Attributes)
+         */
+        public final void begin(final String namespace, final String name,
+                final Attributes attributes) {
+            Digester d = getDigester();
+            Map namespaces = d.getCurrentNamespaces();
+            ((NamespacedBox) d.peek()).setNamespaces(namespaces);
+        }
+    }
+
+    /**
+     * Namespace snapshot test case.
+     */
+    public void testNamespaceSnapshots() throws Exception {
+
+        Digester digester = new Digester();
+        digester.setNamespaceAware(true);
+        digester.addObjectCreate("box", NamespacedBox.class);
+        digester.addSetProperties("box");
+        digester.addRule("box", new NamespaceSnapshotRule());
+        digester.addObjectCreate("box/subBox", NamespacedBox.class);
+        digester.addSetProperties("box/subBox");
+        digester.addRule("box/subBox", new NamespaceSnapshotRule());
+        digester.addSetNext("box/subBox", "addChild");
+
+        Object result = digester.parse(getInputStream("Test11.xml"));
+        assertNotNull(result);
+
+        NamespacedBox root = (NamespacedBox) result;
+        Map nsmap = root.getNamespaces();
+        assertEquals(3, nsmap.size());
+        assertEquals("", nsmap.get(""));
+        assertEquals("http://jakarta.apache.org/digester/Foo", nsmap.get("foo"));
+        assertEquals("http://jakarta.apache.org/digester/Bar", nsmap.get("bar"));
+
+        List children = root.getChildren();
+        assertEquals(3, children.size());
+
+        NamespacedBox child1 = (NamespacedBox) children.get(0);
+        nsmap = child1.getNamespaces();
+        assertEquals(3, nsmap.size());
+        assertEquals("", nsmap.get(""));
+        assertEquals("http://jakarta.apache.org/digester/Foo1", nsmap.get("foo"));
+        assertEquals("http://jakarta.apache.org/digester/Bar1", nsmap.get("bar"));
+
+        NamespacedBox child2 = (NamespacedBox) children.get(1);
+        nsmap = child2.getNamespaces();
+        assertEquals(5, nsmap.size());
+        assertEquals("", nsmap.get(""));
+        assertEquals("http://jakarta.apache.org/digester/Foo", nsmap.get("foo"));
+        assertEquals("http://jakarta.apache.org/digester/Bar", nsmap.get("bar"));
+        assertEquals("http://jakarta.apache.org/digester/Alpha", nsmap.get("alpha"));
+        assertEquals("http://jakarta.apache.org/digester/Beta", nsmap.get("beta"));
+
+        NamespacedBox child3 = (NamespacedBox) children.get(2);
+        nsmap = child3.getNamespaces();
+        assertEquals(4, nsmap.size());
+        assertEquals("", nsmap.get(""));
+        assertEquals("http://jakarta.apache.org/digester/Foo3", nsmap.get("foo"));
+        assertEquals("http://jakarta.apache.org/digester/Alpha", nsmap.get("alpha"));
+        assertEquals("http://jakarta.apache.org/digester/Bar", nsmap.get("bar"));
+
+    }
+
+    // ------------------------------------------------ Utility Support Methods
+
+
+    /**
+     * Return an appropriate InputStream for the specified test file (which
+     * must be inside our current package.
+     *
+     * @param name Name of the test file we want
+     *
+     * @exception IOException if an input/output error occurs
+     */
+    protected InputStream getInputStream(String name) throws IOException {
+
+        return (this.getClass().getResourceAsStream
+                ("/org/apache/commons/digester/" + name));
+
+    }
+
+}
+

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespaceSnapshotTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java?view=auto&rev=449882
==============================================================================
--- jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java (added)
+++ jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java Mon Sep 25 19:05:24 2006
@@ -0,0 +1,45 @@
+/* $Id$
+ *
+ * Copyright 2006 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.digester;
+
+import java.util.Map;
+
+/**
+ * Simple class for use in unit tests. A box with a namespaces property
+ * to store the current namespaces as a Map.
+ *
+ * Used by NamespaceSnapshotTestCase.
+ */
+public class NamespacedBox extends Box {
+
+    private Map namespaces;
+
+    public NamespacedBox() {
+        super();
+    }
+
+    public Map getNamespaces() {
+        return namespaces;
+    }
+
+    public void setNamespaces(Map namespaces) {
+        this.namespaces = namespaces;
+    }
+
+}
+

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NamespacedBox.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml?view=auto&rev=449882
==============================================================================
--- jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml (added)
+++ jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml Mon Sep 25 19:05:24 2006
@@ -0,0 +1,40 @@
+<?xml version="1.0"?>
+
+<!--
+ Copyright 2006 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.
+-->
+
+<!--
+    This file is used by NamespaceSnapshotTestCase.
+-->
+
+<box id="root" xmlns=""
+     xmlns:foo="http://jakarta.apache.org/digester/Foo"
+     xmlns:bar="http://jakarta.apache.org/digester/Bar">
+
+ <subBox id="child1"
+     xmlns:foo="http://jakarta.apache.org/digester/Foo1"
+     xmlns:bar="http://jakarta.apache.org/digester/Bar1"/>
+
+ <subBox id="child2"
+     xmlns:alpha="http://jakarta.apache.org/digester/Alpha"
+     xmlns:beta="http://jakarta.apache.org/digester/Beta"/>
+
+ <subBox id="child3"
+     xmlns:foo="http://jakarta.apache.org/digester/Foo3"
+     xmlns:alpha="http://jakarta.apache.org/digester/Alpha"/>
+
+</box>
+

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/digester/trunk/src/test/org/apache/commons/digester/Test11.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



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