incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From devin...@apache.org
Subject svn commit: r1240007 - in /incubator/odf/trunk/odfdom/src: main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java test/java/org/odftoolkit/odfdom/dom/XPathTest.java
Date Fri, 03 Feb 2012 06:45:23 GMT
Author: devinhan
Date: Fri Feb  3 06:45:23 2012
New Revision: 1240007

URL: http://svn.apache.org/viewvc?rev=1240007&view=rev
Log:
#ODFTOOLKIT-302# getNamespaceURI() implementation in ODFDOM is not aware of duplicate namespace
prefixes.[Developer:Ashok Hariharan;Reviewer:Devin]

Modified:
    incubator/odf/trunk/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java
    incubator/odf/trunk/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java

Modified: incubator/odf/trunk/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java?rev=1240007&r1=1240006&r2=1240007&view=diff
==============================================================================
--- incubator/odf/trunk/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java (original)
+++ incubator/odf/trunk/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfFileDom.java Fri
Feb  3 06:45:23 2012
@@ -23,30 +23,27 @@ package org.odftoolkit.odfdom.pkg;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Iterator;
-import javax.xml.namespace.NamespaceContext;
 import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.HashSet;
-import org.apache.xerces.dom.DocumentImpl;
-import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
-import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeMasterStyles;
-import org.w3c.dom.DOMException;
-import org.w3c.dom.Node;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathFactory;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+
 import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.xerces.dom.DocumentImpl;
 import org.odftoolkit.odfdom.dom.OdfContentDom;
 import org.odftoolkit.odfdom.dom.OdfMetaDom;
 import org.odftoolkit.odfdom.dom.OdfSchemaDocument;
 import org.odftoolkit.odfdom.dom.OdfSettingsDom;
 import org.odftoolkit.odfdom.dom.OdfStylesDom;
-import org.odftoolkit.odfdom.dom.element.office.OfficeBodyElement;
-import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeStyles;
+import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.InputSource;
@@ -355,6 +352,19 @@ public class OdfFileDom extends Document
 		String nsURI = null;
 		nsURI = mUriByPrefix.get(prefix);
 		if (nsURI == null) {
+			// look in Duplicate URI prefixes
+			Set<String> urisWithDuplicatePrefixes = this.mDuplicatePrefixesByUri.keySet();
+			for (String aURI : urisWithDuplicatePrefixes) {
+				Set<String> prefixes = this.mDuplicatePrefixesByUri.get(aURI);
+				// check if requested prefix exists in hashset
+				if (prefixes.contains(prefix)) {
+					nsURI = aURI;
+					break;
+				}
+			}
+		}
+		// there is a possibility it still may be null - so we check
+		if (nsURI == null) {
 			nsURI = XMLConstants.NULL_NS_URI;
 		}
 		return nsURI;

Modified: incubator/odf/trunk/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java?rev=1240007&r1=1240006&r2=1240007&view=diff
==============================================================================
--- incubator/odf/trunk/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java (original)
+++ incubator/odf/trunk/odfdom/src/test/java/org/odftoolkit/odfdom/dom/XPathTest.java Fri
Feb  3 06:45:23 2012
@@ -30,6 +30,7 @@ import javax.xml.xpath.XPathConstants;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.odftoolkit.odfdom.doc.OdfDocument;
 import org.odftoolkit.odfdom.pkg.OdfAlienElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.odfdom.doc.OdfPresentationDocument;
@@ -42,7 +43,7 @@ public class XPathTest {
 	private static final Logger LOG = Logger.getLogger(XPathTest.class.getName());
 	private static final String SOURCE_FILE_1 = "XPathTest-foreignPrefix.odp";
 	private static final String SOURCE_FILE_2 = "XPathTest-foreignPrefix2.odp";
-
+        private static final String SOURCE_FILE_3 = "XPathTest-duplicate-prefix.odt";
 	/**
 	 * 1) The first test document "slideDeckWithTwoSlides.odp" uses the prefix "daisy" instead
of "office" for ODF XML elements.
 	   <daisy:document-content xmlns:daisy="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="ur...
@@ -179,4 +180,69 @@ public class XPathTest {
 			Assert.fail(e.toString());
 		}
 	}
+
+        /**
+         * This test checks if the XPath returned by OdfFileDom is correctly aware of namespacess.
+         * The NamespaceContext implementation in OdfFileDom is aware of duplicate prefixes,
but getNamespaceURI(prefix)
+         * in OdfFileDom does not implement it correctly
+         *
+         * With the patch applied in OdfFileDom these tests pass correctly
+         *
+         * sample metadata rdf generate from Openoffice 3.3 looks like this :
+         * <?xml version="1.0" encoding="utf-8"?>
+         *   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+         *     <rdf:Description rdf:about="../content.xml#id1366098766">
+         *       <ns1:BungeniActionEvent xmlns:ns1="http://editor.bungeni.org/1.0/anx/"></ns1:BungeniActionEvent>
+         *       <ns2:BungeniSectionID xmlns:ns2="http://editor.bungeni.org/1.0/anx/">Xda+5VC/SQKKG7Bk83a2JA</ns2:BungeniSectionID>
+         *       <ns3:BungeniSectionType xmlns:ns3="http://editor.bungeni.org/1.0/anx/">Conclusion</ns3:BungeniSectionType>
+         *       <ns4:hiddenBungeniMetaEditable xmlns:ns4="http://editor.bungeni.org/1.0/anx/">false</ns4:hiddenBungeniMetaEditable>
+         *     </rdf:Description>
+         *
+         * @throws Exception
+         */
+        @Test
+        public void testXPathDuplicatePrefixForForeignNamespace() throws Exception{
+           try {
+
+            OdfDocument odfDoc = OdfDocument.loadDocument(ResourceUtilities.getAbsolutePath(SOURCE_FILE_3));
+            OdfFileDom fileDom = odfDoc.getFileDom("meta/meta.rdf");
+            
+            // add additional duplicate NS prefixes to the DOM
+            fileDom.setNamespace("anx", "http://editor.bungeni.org/1.0/anx/");
+            fileDom.setNamespace("myrdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+
+            //get the XPath from the file dom and set its NS context to the OdfFileDom object
+            XPath xpathFileDom = fileDom.getXPath();
+            xpathFileDom.setNamespaceContext(fileDom);
+
+            //PASS - this works correctly
+            String rdfNsUri = fileDom.getNamespaceURI("rdf");
+            LOG.log(Level.INFO, "The value of the nsuri is {0} expected ns-uri is ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''",
rdfNsUri);
+            Assert.assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#", rdfNsUri);
+
+            //FAIL - for duplicate RDF ns prefix
+            String myrdfNsUri = fileDom.getNamespaceURI("myrdf");
+            LOG.log(Level.INFO, "The value of the nsuri is {0} expected ns-uri is ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''",
myrdfNsUri);
+            Assert.assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#", myrdfNsUri);
+
+            //PASS - for custom NS prefix in document, only the first one
+            String anxNsUri1 = fileDom.getNamespaceURI("ns1");
+            LOG.log(Level.INFO, "The value of the nsuri is {0} expected ns-uri is ''http://editor.bungeni.org/1.0/anx/''",
anxNsUri1);
+            Assert.assertEquals("http://editor.bungeni.org/1.0/anx/", anxNsUri1);
+
+            //FAIL - for custom NS prefix in document ns3, ns4, ns...
+            String anxNsUri2 = fileDom.getNamespaceURI("ns2");
+            LOG.log(Level.INFO, "The value of the ns-uri is {0} expected ns-uri is ''http://editor.bungeni.org/1.0/anx/''",
anxNsUri2);
+            Assert.assertEquals("http://editor.bungeni.org/1.0/anx/", anxNsUri2);
+
+            //FAIL - for custom duplicate NS prefix anx
+            String anxNsUriCustom = fileDom.getNamespaceURI("anx");
+            LOG.log(Level.INFO, "The value of the ns-uri is {0} expected ns-uri is ''http://editor.bungeni.org/1.0/anx/''",
anxNsUriCustom);
+            Assert.assertEquals("http://editor.bungeni.org/1.0/anx/", anxNsUriCustom);
+
+           } catch (Exception e) {
+			LOG.log(Level.SEVERE, e.getMessage(), e);
+			Assert.fail(e.toString());
+           }
+        }
 }



Mime
View raw message