svn commit: r730491 - in /xml/xindice/trunk/java: src/org/apache/xindice/xml/NamespaceMap.java tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java

[email protected]
Newsgroups gmane.text.xml.xindice.devel
Message-ID <[email protected]>
Author: natalia
Date: Wed Dec 31 16:05:24 2008
New Revision: 730491

URL: http://svn.apache.org/viewvc?rev=730491&view=rev
Log:
Workaround for XUpdate bug that stores default namespace prefix as null

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/xml/NamespaceMap.java
    xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java

Modified: xml/xindice/trunk/java/src/org/apache/xindice/xml/NamespaceMap.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/xml/NamespaceMap.java?rev=730491&r1=730490&r2=730491&view=diff
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/xml/NamespaceMap.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/xml/NamespaceMap.java Wed Dec 31 16:05:24 2008
@@ -25,14 +25,20 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
 
 /**
  * NamespaceMap is just a HashMap extension that provides some useful
  * Namespace related functionality.
  *
+ * <p>
+ * FIXME:
+ * XUpdate library has a bug that stores default namespace with null prefix
+ * istead of empty string. As a workaround, storing values with null key will
+ * store them with empty string key instead.
+ * If you are using Map interface methods on a NamespaceMap object, be aware
+ * of this.
+ * </p>
  * @version $Revision$, $Date$
  */
 public final class NamespaceMap extends HashMap {
@@ -44,10 +50,7 @@
     }
 
     public NamespaceMap(Map namespaces) {
-        for (Iterator i = namespaces.entrySet().iterator(); i.hasNext(); ) {
-            Map.Entry e = (Map.Entry) i.next();
-            setNamespace((String) e.getKey(), (String) e.getValue());
-        }
+        putAll(namespaces);
     }
 
     public Node getContextNode() {
@@ -60,7 +63,7 @@
                 Map.Entry entry = (Map.Entry) i.next();
                 String pfx = (String) entry.getKey();
                 String uri = (String) entry.getValue();
-                if (pfx == null) {
+                if ("".equals(pfx)) {
                     elem.setAttribute("xmlns", uri);
                 } else {
                     elem.setAttribute("xmlns:" + pfx, uri);
@@ -107,11 +110,27 @@
         Iterator newEntries = nsMap.entrySet().iterator();
         while (newEntries.hasNext()) {
             Map.Entry entry = (Map.Entry) newEntries.next();
-            if (!override && super.containsKey(entry.getKey())) {
+            if (!override && containsKey(entry.getKey())) {
                 continue;
             }
-            super.put(entry.getKey(), entry.getValue());
+            put(entry.getKey(), entry.getValue());
         }
         elem = null;
     }
+
+    /**
+     * This is a workaround.
+     * @see org.apache.xindice.xml.NamespaceMap
+     */
+    public Object put(Object o, Object o1) {
+        return o == null ? super.put("", o1) : super.put(o, o1);
+    }
+
+    /**
+     * This is a workaround.
+     * @see org.apache.xindice.xml.NamespaceMap
+     */
+    public boolean containsKey(Object o) {
+        return o == null ? super.containsKey(""): super.containsKey(o);
+    }
 }

Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java?rev=730491&r1=730490&r2=730491&view=diff
==============================================================================
--- xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java (original)
+++ xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/XUpdateQueryTest.java Wed Dec 31 16:05:24 2008
@@ -39,21 +39,24 @@
     public void setUp() throws Exception {
         super.setUp();
 
-        String document1 = "<?xml version=\"1.0\"?>" +
-                "<person status=\"single\">" +
+        String document1 = "<?xml version='1.0'?>" +
+                "<person status='single'>" +
                 "<first>John</first>" +
                 "<last>Smith</last>" +
                 "<phone type=\"work\">555-345-6789</phone>" +
                 "</person>";
-        String document2 = "<?xml version=\"1.0\"?>" +
-                "<person status=\"married\">" +
+        String document2 = "<?xml version='1.0'?>" +
+                "<person status='married'>" +
                 "<first>Sally</first>" +
                 "<last>Benton</last>" +
                 "<phone type=\"work\">555-345-6789</phone>" +
                 "</person>";
+        String document3 = "<?xml version='1.0' encoding='utf-8'?>" +
+                "<document/>";
 
         this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", document1);
         this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", document2);
+        this.client.insertDocument(TEST_COLLECTION_PATH, "doc3", document3);
     }
 
     public void tearDown() throws Exception {
@@ -88,6 +91,27 @@
         assertEquals(updatedDocument, doc);
     }
 
+    public void testXUpdateWithDefaultNamespace() throws Exception {
+        String query =  "<?xml version='1.0' encoding='UTF-8'?>\n" +
+                "<xupdate:modifications xmlns:xupdate='http://www.xmldb.org/xupdate' version='1.0'>\n" +
+                "   <xupdate:append select='/document'><element xmlns='http://xml.apache.org'/></xupdate:append>\n" +
+                "</xupdate:modifications>";
+
+        String updatedDocument = "<?xml version='1.0' encoding='utf-8'?>" +
+                "<document><element xmlns='http://xml.apache.org'/></document>";
+
+        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
+        XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
+
+        long count = service.updateResource("doc3", query);
+        assertEquals(1, count);
+
+        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc3");
+        System.out.println(doc);
+        assertNotNull(doc);
+        XMLAssert.assertXMLEqual(updatedDocument, doc);
+    }
+
     public void testUpdateCollection() throws Exception {
         String query =
                 "<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.