You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2005/08/15 20:20:06 UTC

svn commit: r232846 - in /cocoon/trunk/src: java/org/apache/cocoon/xml/NamespacesTable.java test/org/apache/cocoon/xml/NamespacesTableTestCase.java

Author: sylvain
Date: Mon Aug 15 11:20:00 2005
New Revision: 232846

URL: http://svn.apache.org/viewcvs?rev=232846&view=rev
Log:
Fix bug reported by Ugo when some xmlns declarations are on a jx:forEach

Modified:
    cocoon/trunk/src/java/org/apache/cocoon/xml/NamespacesTable.java
    cocoon/trunk/src/test/org/apache/cocoon/xml/NamespacesTableTestCase.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/xml/NamespacesTable.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/xml/NamespacesTable.java?rev=232846&r1=232845&r2=232846&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/xml/NamespacesTable.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/xml/NamespacesTable.java Mon Aug 15 11:20:00 2005
@@ -59,7 +59,7 @@
     /** The last namespace declaration. */
     private Entry lastEntry;
 
-    private boolean filterDuplicate = true;
+    private boolean usesScopes = false;
 
     /**
      * Construct a new <code>NamespacesTable</code> instance.
@@ -93,7 +93,7 @@
         }
 
         if (dup != null) {
-            if (filterDuplicate && dup.uri.equals(uri)) {
+            if (usesScopes && dup.uri.equals(uri)) {
                 return dup;
             }
             dup.overriden = true;
@@ -115,6 +115,11 @@
      * @return The removed <code>Declaration</code> or <b>null</b>.
      */
     public Declaration removeDeclaration(String prefix) {
+        if (usesScopes) {
+            // Automatically handled in leaveScope
+            return null; // or throw and IllegalStateException if enterScope(handler) was used?
+        }
+
         Entry current = this.lastEntry;
         Entry afterCurrent = null;
         while(current != null) {
@@ -159,6 +164,7 @@
      * @since 2.1.8
      */
     public void enterScope() {
+        this.usesScopes = true;
         this.lastEntry.closedScopes++;
     }
 
@@ -172,6 +178,7 @@
      * @since 2.1.8
      */
     public void enterScope(ContentHandler handler) throws SAXException {
+        this.usesScopes = true;
         Entry current = this.lastEntry;
         while (current != null && current.closedScopes == 0) {
             handler.startPrefixMapping(current.prefix, current.uri);
@@ -188,13 +195,13 @@
      * @param autoUndeclare if <code>true</code>, remove all mappings for the current scope.
      * @since 2.1.8
      */
-    public void leaveScope(boolean autoUndeclare) {
+    public void leaveScope() {
         Entry current = this.lastEntry;
         if (current.closedScopes <= 0) {
             throw new IllegalStateException("Misbalanced enter and leaving of scope.");
         }
         current.closedScopes--;
-        if (autoUndeclare) {
+        if (usesScopes) {
             while (current != null && current.closedScopes == 0) {
                 Entry overrides = current.overrides;
                 if (overrides != null) {
@@ -221,6 +228,7 @@
         if (current.closedScopes <= 0) {
             throw new IllegalStateException("Misbalanced enter and leaving of scope.");
         }
+        current.closedScopes--;
         while (current != null && current.closedScopes == 0) {
             handler.endPrefixMapping(current.prefix);
             Entry overrides = current.overrides;
@@ -231,7 +239,6 @@
             current = current.previous;
         }
 
-        current.closedScopes--;
         this.lastEntry = current;
     }
 

Modified: cocoon/trunk/src/test/org/apache/cocoon/xml/NamespacesTableTestCase.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/xml/NamespacesTableTestCase.java?rev=232846&r1=232845&r2=232846&view=diff
==============================================================================
--- cocoon/trunk/src/test/org/apache/cocoon/xml/NamespacesTableTestCase.java (original)
+++ cocoon/trunk/src/test/org/apache/cocoon/xml/NamespacesTableTestCase.java Mon Aug 15 11:20:00 2005
@@ -1,14 +1,15 @@
 package org.apache.cocoon.xml;
 
-import org.xml.sax.helpers.DefaultHandler;
-
 import junit.framework.TestCase;
 
+import org.xml.sax.ContentHandler;
+import org.xml.sax.helpers.DefaultHandler;
+
 public class NamespacesTableTestCase extends TestCase {
     public NamespacesTableTestCase(String name) {
         super(name);
     }
-        
+
     public void testSimple() {
         NamespacesTable ns = new NamespacesTable();
         
@@ -26,9 +27,10 @@
         assertNull(ns.getPrefix(ns.getPrefix("http://ns3")));
         assertNull(ns.getUri("ns3"));
         
-        ns.leaveScope(false);
-        assertNotNull(ns.removeDeclaration("ns1"));
-        assertNotNull(ns.removeDeclaration("ns2"));
+        ns.leaveScope();
+        // Declarations that occured before this scope are no more visible
+        assertNull(ns.removeDeclaration("ns1"));
+        assertNull(ns.removeDeclaration("ns2"));
         assertNull(ns.removeDeclaration("ns3"));
     }
     
@@ -44,7 +46,7 @@
         ns.addDeclaration("ns3", "http://ns3");
         
         try {
-            ns.leaveScope(false);
+            ns.leaveScope();
         } catch(IllegalStateException e) {
             return;
         }
@@ -65,13 +67,13 @@
         assertEquals("http://yetanotherns1", ns.getUri("ns1"));
         assertEquals(0, ns.getPrefixes("http://ns1").length);
         
-        ns.leaveScope(true);
-        ns.leaveScope(true);
+        ns.leaveScope();
+        ns.leaveScope();
         
         assertEquals("http://ns1", ns.getUri("ns1"));
         assertEquals(1, ns.getPrefixes("http://ns1").length);
         
-        ns.leaveScope(true);
+        ns.leaveScope();
         assertNull(ns.getUri("ns1"));
     }
     
@@ -106,7 +108,7 @@
         // Enter and leave a nested scope
         ns.addDeclaration("ns3", "http://ns3");
         ns.enterScope();
-        ns.leaveScope(true);
+        ns.leaveScope();
         
         ns.leaveScope(new DefaultHandler() {
             public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException {
@@ -114,4 +116,48 @@
             };
         });
     }
+    
+    /**
+     * A scenario that occurs in with jx:import where some prefixes are started but not used.
+     * @throws Exception
+     */
+    public void testJXImport() throws Exception {
+        NamespacesTable ns = new NamespacesTable();
+        ContentHandler handler = new DefaultHandler();
+        
+        ns.addDeclaration("ft", "http://apache.org/cocoon/forms/1.0#template");
+        ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
+        ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
+//        ns.enterScope(handler);
+          ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
+          ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
+          ns.addDeclaration("bu", "http://apache.org/cocoon/browser-update/1.0");
+          ns.removeDeclaration("jx");
+          ns.removeDeclaration("fi");
+          ns.removeDeclaration("bu");
+//        ns.leaveScope(handler);
+        assertEquals("ft", ns.getPrefix("http://apache.org/cocoon/forms/1.0#template"));
+        assertEquals("fi", ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
+        assertEquals("jx", ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
+        ns.removeDeclaration("ft");
+        ns.removeDeclaration("fi");
+        ns.removeDeclaration("jx");
+
+    }
+    
+    public void testDuplicate() throws Exception {
+        NamespacesTable ns = new NamespacesTable();
+        
+        ns.addDeclaration("ns1", "http://ns1");
+          ns.enterScope();
+            ns.addDeclaration("ns1", "http://ns1");
+              ns.enterScope();
+              ns.leaveScope();
+            ns.removeDeclaration("ns1");
+            assertEquals("http://ns1", ns.getUri("ns1"));
+          ns.leaveScope();
+        ns.removeDeclaration("ns1");
+        assertNull(ns.getUri("ns1"));
+    }
+
 }