Author: scheu
Date: Fri Jul 11 10:49:32 2008
New Revision: 676036

URL: http://svn.apache.org/viewvc?rev=676036&view=rev
Log:
WSCOMMONS-360
Contributor:Rich Scheuerle
Introduced a new property on the OMDataSourceExt (lossyPrefix).  If this 
property is set, then the OMSE must
use expansion to get the value of the prefix.

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java?rev=676036&r1=676035&r2=676036&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
 Fri Jul 11 10:49:32 2008
@@ -43,6 +43,13 @@
  * @see OMSourceElementImpl
  */
 public interface OMDataSourceExt extends OMDataSource {
+    
+    /* Property lossyPrefix
+     * Value null or Boolean.TRUE or Boolean.FALSE
+     * If Boolean.TRUE, this indicates that expansion is needed to 
+     * obtain the actual prefix name. 
+     */
+    public static final String LOSSY_PREFIX = "lossyPrefix";
 
     /**
      * Serializes element data directly to stream.

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=676036&r1=676035&r2=676036&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Fri Jul 11 10:49:32 2008
@@ -89,8 +89,18 @@
                                 OMDataSource source) {
         super(localName, null, factory);
         dataSource = source;
-        definedNamespace = ns;
         isExpanded = (dataSource == null);
+        if (!isExpanded) {
+            if (!isLossyPrefix(dataSource)) {
+                // Believe the prefix and create a normal OMNamespace
+                definedNamespace = ns;
+            } else {
+                // Create a deferred namespace that forces an expand to get 
the prefix
+                definedNamespace = new DeferredNamespace(ns.getNamespaceURI());
+            }
+        } else {
+            definedNamespace = ns;
+        }
     }
 
     /**
@@ -104,8 +114,18 @@
         //create a namespace
         super(qName.getLocalPart(), null, factory);
         dataSource = source;
-        definedNamespace = new OMNamespaceImpl(qName.getNamespaceURI(), 
qName.getPrefix());
         isExpanded = (dataSource == null);
+        if (!isExpanded) {
+            if (!isLossyPrefix(dataSource)) {
+                // Believe the prefix and create a normal OMNamespace
+                definedNamespace = new 
OMNamespaceImpl(qName.getNamespaceURI(), qName.getPrefix());
+            } else {
+                // Create a deferred namespace that forces an expand to get 
the prefix
+                definedNamespace = new 
DeferredNamespace(qName.getNamespaceURI());
+            }
+        } else {
+            definedNamespace = new OMNamespaceImpl(qName.getNamespaceURI(), 
qName.getPrefix());
+        }
     }
 
     public OMSourcedElementImpl(String localName, OMNamespace ns, OMContainer 
parent, OMFactory factory) {
@@ -137,6 +157,39 @@
             this.setNamespace(ns);
         }
     }
+    
+    
+    /**
+     * The namespace uri is immutable, but the OMDataSource may change
+     * the value of the prefix.  This method queries the OMDataSource to 
+     * see if the prefix is known.
+     * @param source
+     * @return true or false
+     */
+    private boolean isLossyPrefix(OMDataSource source) {
+        Object lossyPrefix = null;
+        if (source instanceof OMDataSourceExt) {
+            lossyPrefix = 
+                ((OMDataSourceExt) 
source).getProperty(OMDataSourceExt.LOSSY_PREFIX);
+                        
+        }
+        return lossyPrefix == Boolean.TRUE;
+    }
+    private void setDeferredNamespace(OMDataSource source, String uri, String 
prefix) {
+        Object lossyPrefix = null;
+        if (source instanceof OMDataSourceExt) {
+            lossyPrefix = 
+                ((OMDataSourceExt) 
source).getProperty(OMDataSourceExt.LOSSY_PREFIX);
+                        
+        }
+        if (lossyPrefix != Boolean.TRUE) {
+            // Believe the prefix and create a normal OMNamespace
+            definedNamespace = new OMNamespaceImpl(uri, prefix);
+        } else {
+            // Create a deferred namespace that forces an expand to get the 
prefix
+            definedNamespace = new DeferredNamespace(uri);
+        }
+    }
 
     /**
      * Generate element name for output.
@@ -226,7 +279,14 @@
             // Get the current prefix and the reader's prefix
             String readerPrefix = readerFromDS.getPrefix();
             readerPrefix = (readerPrefix == null) ? "" : readerPrefix;
-            String prefix = getNamespace().getPrefix();
+            String prefix = null;
+            
+            OMNamespace ns = getNamespace();
+            if (ns instanceof DeferredNamespace) {
+                // prefix is not available until after expansion
+            } else {
+                prefix = ns.getPrefix();
+            }
             
             // Set the builder for this element
             isExpanded = true;
@@ -239,7 +299,8 @@
             // Update the prefix if necessary.  This must be done after
             // isParserSet to avoid a recursive call
             if (!readerPrefix.equals(prefix) ||
-                    getNamespace() == null) {
+                 getNamespace() == null ||
+                 ns instanceof DeferredNamespace) {
                 if (log.isDebugEnabled()) {
                     log.debug(
                             "forceExpand: changing prefix from " + prefix + " 
to " + readerPrefix);
@@ -986,6 +1047,10 @@
             this.dataSource = dataSource;
             setComplete(false);
             isExpanded = false;
+            if (isLossyPrefix(dataSource)) {
+                // Create a deferred namespace that forces an expand to get 
the prefix
+                definedNamespace = new 
DeferredNamespace(definedNamespace.getNamespaceURI());
+            }
             return oldDS;
         }
     }
@@ -1020,4 +1085,53 @@
             readerFromDS = null;
         }
     }
+    
+    class DeferredNamespace implements OMNamespace {
+        
+        String uri;
+        
+        DeferredNamespace(String ns) {
+            this.uri = ns;
+        }
+
+        public boolean equals(String uri, String prefix) {
+            String thisPrefix = getPrefix();
+            return (this.uri.equals(uri) &&
+                    (thisPrefix == null ? prefix == null :
+                            thisPrefix.equals(prefix)));
+        }
+
+        public String getName() {
+            return uri;
+        }
+
+        public String getNamespaceURI() {
+            return uri;
+        }
+
+        public String getPrefix() {
+            if (!isExpanded()) {
+                forceExpand();
+            }
+            return getNamespace().getPrefix();
+        }
+        
+        public int hashCode() {
+            String thisPrefix = getPrefix();
+            return uri.hashCode() ^ (thisPrefix != null ? 
thisPrefix.hashCode() : 0);
+        }
+        
+        public boolean equals(Object obj) {
+            if (!(obj instanceof OMNamespace)) {
+                return false;
+            }
+            OMNamespace other = (OMNamespace)obj;
+            String otherPrefix = other.getPrefix();
+            String thisPrefix = getPrefix();
+            return (uri.equals(other.getNamespaceURI()) &&
+                    (thisPrefix == null ? otherPrefix == null :
+                            thisPrefix.equals(otherPrefix)));
+        }
+        
+    }
 }
\ No newline at end of file


Reply via email to