changeset 298:a5e6d1923c95

Made namespace resolution more compatible. artifacts/trunk@2383 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jul 2011 08:32:34 +0000
parents 694d818e99b2
children a8d62eb93cd4
files ChangeLog artifacts/pom.xml artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java
diffstat 3 files changed, 59 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Jul 20 12:30:57 2011 +0000
+++ b/ChangeLog	Thu Jul 21 08:32:34 2011 +0000
@@ -1,3 +1,13 @@
+2011-07-21	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java:
+	  Made it more compatible with mixed namespaces and mixtures of namespaces
+	  and no namespaces.
+
+	* artifacts/pom.xml: Set Java compatibility to 1.5. Why isn't this inherited
+	  from main pom.xml? The artifact-database module uses 1.5 features but does
+	  not need any extra configuration.
+
 2011-07-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/Artifact.java,
--- a/artifacts/pom.xml	Wed Jul 20 12:30:57 2011 +0000
+++ b/artifacts/pom.xml	Thu Jul 21 08:32:34 2011 +0000
@@ -18,6 +18,15 @@
   <build>
     <plugins>
       <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.0.2</version>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>buildnumber-maven-plugin</artifactId>
         <executions>
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java	Wed Jul 20 12:30:57 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java	Thu Jul 21 08:32:34 2011 +0000
@@ -9,6 +9,9 @@
 package de.intevation.artifacts;
 
 import java.util.Iterator;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.ArrayList;
 
 import javax.xml.XMLConstants;
 
@@ -39,10 +42,25 @@
     public static final ArtifactNamespaceContext INSTANCE =
         new ArtifactNamespaceContext();
 
+    protected Map<String, String> map;
+
     /**
      * The default constructor.
      */
     public ArtifactNamespaceContext() {
+        map = new HashMap<String, String>();
+        map.put(
+            XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
+        map.put(
+            XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.DEFAULT_NS_PREFIX);
+        map.put(
+            XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
+        map.put(
+            NAMESPACE_PREFIX, NAMESPACE_URI);
+    }
+
+    public void add(String prefix, String uri) {
+        map.put(prefix, uri);
     }
 
     /**
@@ -53,38 +71,45 @@
     public String getNamespaceURI(String prefix) {
 
         if (prefix == null) {
-            throw new NullPointerException("Null prefix");
+            throw new IllegalArgumentException("Null prefix");
         }
 
-        if (NAMESPACE_PREFIX.equals(prefix)) {
-            return NAMESPACE_URI;
-        }
+        String namespace = map.get(prefix);
 
-        if ("xml".equals(prefix)) {
-            return XMLConstants.XML_NS_URI;
-        }
-
-        return XMLConstants.NULL_NS_URI;
+        return namespace != null ? namespace : XMLConstants.NULL_NS_URI;
     }
 
     /**
      * @see javax.xml.namespace.NamespaceContext#getPrefix(String)
      * @param uri The URI
-     * @return nothing.
-     * @throws java.lang.UnsupportedOperationException
      */
     public String getPrefix(String uri) {
-        throw new UnsupportedOperationException();
+        
+        if (uri == null) {
+            throw new IllegalArgumentException("Null uri");
+        }
+
+        for (Map.Entry<String, String> entry: map.entrySet()) {
+            if (entry.getValue().equals(uri)) {
+                return entry.getKey();
+            }
+        }
+
+        return XMLConstants.DEFAULT_NS_PREFIX;
     }
 
     /**
      * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
      * @param uri The URI
-     * @return nothing
-     * @throws java.lang.UnsupportedOperationException
      */
     public Iterator getPrefixes(String uri) {
-        throw new UnsupportedOperationException();
+        ArrayList<String> results = new ArrayList<String>();
+        for (Map.Entry<String, String> entry: map.entrySet()) {
+            if (entry.getValue().equals(uri)) {
+                results.add(entry.getKey());
+            }
+        }
+        return results.iterator();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org