Skip to content
Browse files

Recursively search upwards from an element's location for namespace.

  • Loading branch information...
1 parent 48b1c83 commit 4a5862562ce662210e19d86d5a65f8b38f23e77c @kjw kjw committed Apr 24, 2012
Showing with 48 additions and 13 deletions.
  1. +0 −1 .classpath
  2. +30 −4 src/org/crossref/pdfmark/XmlUtils.java
  3. +18 −8 src/org/crossref/pdfmark/XmpUtils.java
View
1 .classpath
@@ -2,7 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
- <classpathentry kind="lib" path="lib/iText-2.1.7.jar"/>
<classpathentry kind="lib" path="lib/jargs.jar"/>
<classpathentry kind="lib" path="lib/httpclient-4.0.jar"/>
<classpathentry kind="lib" path="lib/httpmime-4.0.jar"/>
View
34 src/org/crossref/pdfmark/XmlUtils.java
@@ -17,6 +17,8 @@
*/
package org.crossref.pdfmark;
+import java.util.UUID;
+
import javax.xml.XMLConstants;
import org.w3c.dom.Attr;
@@ -32,19 +34,43 @@ private XmlUtils() {
* @return A String[] of length two, [prefix, URI].
*/
public static String[] getNamespaceDeclaration(Element ele) {
- String[] ns = new String[2]; // prefix, URI
+ String prefixHint = null;
+ String[] parts = ele.getNodeName().split(":");
+ if (parts.length == 2) {
+ prefixHint = parts[0];
+ }
+ return getNamespaceDeclaration(ele, prefixHint);
+ }
+
+ /**
+ * @return A String[] of length two, [prefix, URI].
+ */
+ public static String[] getNamespaceDeclaration(Element ele, String prefixHint) {
+ String[] ns = new String[2]; // prefix, URI
NamedNodeMap attribs = ele.getAttributes();
for (int i=0; i<attribs.getLength(); i++) {
Attr attr = (Attr) attribs.item(i);
if (attr.getName().startsWith("xmlns")) {
- ns[0] = attr.getLocalName(); // prefix
- ns[1] = attr.getTextContent(); // URI
+ if ((prefixHint != null && attr.getName().endsWith(prefixHint))
+ || attr.getName().equals("xmlns")) {
+ ns[0] = attr.getLocalName(); // prefix
+ ns[1] = attr.getTextContent(); // URI
+
+ // catch default namespace change
+ if (ns[0] == "xmlns") {
+ ns[0] = UUID.randomUUID().toString();
+ }
+ }
}
}
- return ns;
+ if (ns[1] == null) {
+ return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint);
+ } else {
+ return ns;
+ }
}
public static String getNamespaceUriDeclaration(Element ele) {
View
26 src/org/crossref/pdfmark/XmpUtils.java
@@ -20,6 +20,10 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import javax.sql.rowset.spi.XmlWriter;
import javax.xml.XMLConstants;
@@ -58,7 +62,7 @@ private XmpUtils() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
- doc = builder.parse(new ByteArrayInputStream(xmpData));
+ doc = builder.parse(new ByteArrayInputStream(xmlData));
} catch (IOException e) {
throw new XmpException(e);
} catch (SAXException e) {
@@ -68,28 +72,34 @@ private XmpUtils() {
}
NodeList descriptionNodes = doc.getElementsByTagName("rdf:Description");
- XmpSchema[] schemata = new XmpSchema[descriptionNodes.getLength()];
+ Map<String, XmpSchema> schemata = new HashMap<String, XmpSchema>();
for (int i=0; i<descriptionNodes.getLength(); i++) {
Element description = (Element) descriptionNodes.item(i);
NodeList children = description.getChildNodes();
- String[] ns = XmlUtils.getNamespaceDeclaration(description);
- schemata[i] = new AnyXmpSchema(ns[0], ns[1]);
-
for (int j=0; j<children.getLength(); j++) {
Node n = children.item(j);
if (n instanceof Element) {
- parseRdfElement(schemata[i], (Element) n);
+ parseRdfElement(schemata, (Element) n);
}
}
}
- return schemata;
+ return schemata.values().toArray(new XmpSchema[0]);
}
- private static void parseRdfElement(XmpSchema schema, Element ele) {
+ private static void parseRdfElement(Map<String, XmpSchema> schemata, Element ele) {
String propertyName = ele.getNodeName();
+ String[] ns = XmlUtils.getNamespaceDeclaration(ele);
+ XmpSchema schema = null;
+
+ if (schemata.containsKey(ns[1])) {
+ schema = schemata.get(ns[1]);
+ } else {
+ schema = new AnyXmpSchema(ns[0], ns[1]);
+ schemata.put(ns[1], schema);
+ }
/* Should have either Text or a single <rdf:Bag/Alt/Seq>. */
boolean hasElementChildren = false;

0 comments on commit 4a58625

Please sign in to comment.
Something went wrong with that request. Please try again.