cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r835016 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/common/util/ common/common/src/main/java/org/apache/cxf/helpers/ common/common/src/main/java/org/apache/cxf/staxutils/ rt/bindings/jbi/src/main/java/org/apache/cxf/binding/...
Date Wed, 11 Nov 2009 19:09:41 GMT
Author: dkulp
Date: Wed Nov 11 19:09:41 2009
New Revision: 835016

URL: http://svn.apache.org/viewvc?rev=835016&view=rev
Log:
[CXF-2530] Move all the "create a prefix for me" stuff to a couple
places so we only need to optimize/debug one place.

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/StringUtils.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
    cxf/trunk/rt/bindings/jbi/src/main/java/org/apache/cxf/binding/jbi/interceptor/JBIWrapperOutInterceptor.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/WrappedOutInterceptor.java
    cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/util/NamespaceHelper.java
    cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/xml/stax/ElementWriter.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/StringUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/StringUtils.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/StringUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/StringUtils.java Wed
Nov 11 19:09:41 2009
@@ -69,10 +69,10 @@
     }
 
     public static boolean isEmpty(String str) {
-        if (str != null && str.trim().length() > 0) {
-            return false;
+        if (str == null || str.length() == 0) {
+            return true;
         }
-        return true;
+        return str.trim().length() == 0;
     }
     
     public static boolean isEmpty(List<String> list) {

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java Wed Nov 11
19:09:41 2009
@@ -549,7 +549,7 @@
             p = "ns" + i;
             i++;
         }
-        el.setAttribute(XMLNAMESPACE + ":" + p, ns);
+        addNamespacePrefix(el, ns, p);
         return p;
     }
 

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java
Wed Nov 11 19:09:41 2009
@@ -160,21 +160,9 @@
         } 
     }
     
-    private String creatPrefix() {
-        int count = 1;
-        String pfx = "ns" + count;
-        while (curContext.getNamespaceURI(pfx) != null) {
-            count++;
-            pfx = "ns" + count;
-        }
-        return pfx;
-    }
     public void writeAttribute(String uri, String name, String value) throws XMLStreamException
{
         if (!StringUtils.isEmpty(uri)) {
-            String pfx = this.getPrefix(uri);
-            if (pfx == null) {
-                pfx = creatPrefix();
-            }
+            String pfx = StaxUtils.getUniquePrefix(this, uri, false);
             addEvent(factory.createAttribute(pfx, uri, name, value));            
         } else {            
             addEvent(factory.createAttribute(name, value));            

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Wed Nov
11 19:09:41 2009
@@ -81,6 +81,11 @@
     private static final BlockingQueue<XMLOutputFactory> OUTPUT_FACTORY_POOL;
     
     private static final String XML_NS = "http://www.w3.org/2000/xmlns/";
+    private static final String DEF_PREFIXES[] = new String[] {
+        "ns1".intern(), "ns2".intern(), "ns3".intern(),
+        "ns4".intern(), "ns5".intern(), "ns6".intern(),
+        "ns7".intern(), "ns8".intern(), "ns9".intern()
+    };
     
     static {
         int i = 20;
@@ -212,6 +217,7 @@
         }
     }
 
+    
     public static void nextEvent(XMLStreamReader dr) {
         try {
             dr.next();
@@ -1027,7 +1033,6 @@
         return new QName(ns, localName, prefix);
     }
     
-
     /**
      * Create a unique namespace uri/prefix combination.
      * 
@@ -1049,18 +1054,29 @@
         }
         return prefix;
     }
-
+    public static String getUniquePrefix(XMLStreamWriter writer, String namespaceURI)
+        throws XMLStreamException {
+        return getUniquePrefix(writer, namespaceURI, false);
+    }
     public static String getUniquePrefix(XMLStreamWriter writer) {
-        int n = 1;
-        
         NamespaceContext nc = writer.getNamespaceContext();
+        if (nc == null) {
+            return DEF_PREFIXES[0];
+        }
+        for (String t : DEF_PREFIXES) {
+            String uri = nc.getNamespaceURI(t);
+            if (StringUtils.isEmpty(uri)) {
+                return t;
+            }
+        }
+
+        int n = 10;
         while (true) {
             String nsPrefix = "ns" + n;
-
-            if (nc == null || nc.getNamespaceURI(nsPrefix) == null) {
+            String uri = nc.getNamespaceURI(nsPrefix);
+            if (StringUtils.isEmpty(uri)) {
                 return nsPrefix;
             }
-
             n++;
         }
     }

Modified: cxf/trunk/rt/bindings/jbi/src/main/java/org/apache/cxf/binding/jbi/interceptor/JBIWrapperOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/bindings/jbi/src/main/java/org/apache/cxf/binding/jbi/interceptor/JBIWrapperOutInterceptor.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/rt/bindings/jbi/src/main/java/org/apache/cxf/binding/jbi/interceptor/JBIWrapperOutInterceptor.java
(original)
+++ cxf/trunk/rt/bindings/jbi/src/main/java/org/apache/cxf/binding/jbi/interceptor/JBIWrapperOutInterceptor.java
Wed Nov 11 19:09:41 2009
@@ -39,6 +39,7 @@
 import org.apache.cxf.service.model.BindingOperationInfo;
 import org.apache.cxf.service.model.MessageInfo;
 import org.apache.cxf.service.model.MessagePartInfo;
+import org.apache.cxf.staxutils.StaxUtils;
 
 public class JBIWrapperOutInterceptor extends AbstractOutDatabindingInterceptor {
 
@@ -129,14 +130,10 @@
             QName name = outPart.getConcreteName();
 
             try {
-                
-                int x = 1;
-                while (xmlWriter.getNamespaceContext().getNamespaceURI("ns" + x) != null)
{
-                    x++;
-                }
-                xmlWriter.setPrefix("ns" + x, name.getNamespaceURI());
-                xmlWriter.writeStartElement("ns" + x, name.getLocalPart(), name.getNamespaceURI());
-                xmlWriter.writeNamespace("ns" + x, name.getNamespaceURI());
+                String pfx = StaxUtils.getUniquePrefix(xmlWriter, name.getNamespaceURI());
+                xmlWriter.setPrefix(pfx, name.getNamespaceURI());
+                xmlWriter.writeStartElement(pfx, name.getLocalPart(), name.getNamespaceURI());
+                xmlWriter.writeNamespace(pfx, name.getNamespaceURI());
             } catch (XMLStreamException e) {
                 throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_WRITE_EXC",
BUNDLE), e);
             }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/WrappedOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/WrappedOutInterceptor.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/WrappedOutInterceptor.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/WrappedOutInterceptor.java
Wed Nov 11 19:09:41 2009
@@ -33,14 +33,10 @@
 import org.apache.cxf.service.model.BindingOperationInfo;
 import org.apache.cxf.service.model.MessageInfo;
 import org.apache.cxf.service.model.MessagePartInfo;
+import org.apache.cxf.staxutils.StaxUtils;
 
 public class WrappedOutInterceptor extends AbstractOutDatabindingInterceptor {
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(WrappedOutInterceptor.class);
-    private static final String DEF_PREFIXES[] = new String[] {
-        "ns1".intern(), "ns2".intern(), "ns3".intern(),
-        "ns4".intern(), "ns5".intern(), "ns6".intern(),
-        "ns7".intern(), "ns8".intern(), "ns9".intern()
-    };
 
     private WrappedOutEndingInterceptor ending = new WrappedOutEndingInterceptor();
     
@@ -75,21 +71,7 @@
                     pfx = service.getDataBinding().getDeclaredNamespaceMappings().get(name.getNamespaceURI());
                 }
                 if (pfx == null) {
-                    for (String t : DEF_PREFIXES) {
-                        if (!StringUtils.isEmpty(xmlWriter.getNamespaceContext()
-                                                     .getNamespaceURI(t))) {
-                            pfx = t;
-                            break;
-                        }
-                    }
-                    if (pfx == null) {    
-                        int x = 10;
-                        while (!StringUtils.isEmpty(xmlWriter.getNamespaceContext()
-                                                         .getNamespaceURI("ns" + x))) {
-                            x++;
-                        }
-                        pfx = "ns" + x;
-                    }
+                    pfx = StaxUtils.getUniquePrefix(xmlWriter, name.getNamespaceURI(), false);
                 }
                 xmlWriter.setPrefix(pfx, name.getNamespaceURI());
                 xmlWriter.writeStartElement(pfx, name.getLocalPart(), name.getNamespaceURI());

Modified: cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/util/NamespaceHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/util/NamespaceHelper.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/util/NamespaceHelper.java
(original)
+++ cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/util/NamespaceHelper.java
Wed Nov 11 19:09:41 2009
@@ -34,6 +34,7 @@
 import org.apache.cxf.aegis.DatabindingException;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.JavaUtils;
+import org.apache.cxf.staxutils.StaxUtils;
 
 /**
  * Namespace utilities.
@@ -52,14 +53,12 @@
      */
     public static String getUniquePrefix(Element element, String namespaceURI) {
         String prefix = getPrefix(element, namespaceURI);
-
         // it is OK to have both namespace URI and prefix be empty. 
         if (prefix == null) {
             if ("".equals(namespaceURI)) {
                 return "";
             }
-            prefix = getUniquePrefix(element);
-            DOMUtils.addNamespacePrefix(element, namespaceURI, prefix);
+            prefix = DOMUtils.createNamespace(element, namespaceURI);
         }
         return prefix;
     }
@@ -72,20 +71,6 @@
         DOMUtils.getPrefixesRecursive(element, namespaceURI, prefixes);
     }
 
-    public static String getUniquePrefix(Element el) {
-        int n = 1;
-
-        while (true) {
-            String nsPrefix = "ns" + n;
-
-            if (DOMUtils.getNamespace(el, nsPrefix) == null) {
-                return nsPrefix;
-            }
-
-            n++;
-        }
-    }
-
     /**
      * Create a unique namespace uri/prefix combination.
      * 
@@ -110,7 +95,9 @@
      * @throws XMLStreamException
      */
     public static String getUniquePrefix(XMLStreamWriter writer, 
-                                         String namespaceURI, String preferred, boolean declare)
+                                         String namespaceURI,
+                                         String preferred,
+                                         boolean declare)
         throws XMLStreamException {
         
         if (preferred != null) {
@@ -128,7 +115,7 @@
         }
         
         if (prefix == null) {
-            prefix = getUniquePrefix(writer);
+            prefix = StaxUtils.getUniquePrefix(writer);
         }
 
         if (declare) {
@@ -139,20 +126,6 @@
         return prefix;
     }
 
-    public static String getUniquePrefix(XMLStreamWriter writer) {
-        int n = 1;
-
-        while (true) {
-            String nsPrefix = "ns" + n;
-
-            if (writer.getNamespaceContext().getNamespaceURI(nsPrefix) == null) {
-                return nsPrefix;
-            }
-
-            n++;
-        }
-    }
-
     /**
      * Generates the name of a XML namespace from a given class name and
      * protocol. The returned namespace will take the form

Modified: cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/xml/stax/ElementWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/xml/stax/ElementWriter.java?rev=835016&r1=835015&r2=835016&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/xml/stax/ElementWriter.java
(original)
+++ cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/xml/stax/ElementWriter.java
Wed Nov 11 19:09:41 2009
@@ -26,10 +26,10 @@
 import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.cxf.aegis.DatabindingException;
-import org.apache.cxf.aegis.util.NamespaceHelper;
 import org.apache.cxf.aegis.xml.AbstractMessageWriter;
 import org.apache.cxf.aegis.xml.MessageWriter;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.staxutils.StaxUtils;
 
 public class ElementWriter extends AbstractMessageWriter implements MessageWriter {
     private XMLStreamWriter writer;
@@ -98,7 +98,7 @@
                
                 if (!StringUtils.isEmpty(namespace)) {
                     declare = true;
-                    prefix = NamespaceHelper.getUniquePrefix(writer);
+                    prefix = StaxUtils.getUniquePrefix(writer);
                 } else {
                     prefix = "";
                     if (!StringUtils.isEmpty(writer.getNamespaceContext().getNamespaceURI("")))
{
@@ -199,7 +199,7 @@
             String pfx = writer.getPrefix(ns);
 
             if (pfx == null) {
-                pfx = NamespaceHelper.getUniquePrefix(writer);
+                pfx = StaxUtils.getUniquePrefix(writer);
 
                 writer.setPrefix(pfx, ns);
                 writer.writeNamespace(pfx, ns);
@@ -227,7 +227,7 @@
                 } else if (contextPfx != null) {
                     pfx = contextPfx;
                 } else {
-                    pfx = NamespaceHelper.getUniquePrefix(writer);
+                    pfx = StaxUtils.getUniquePrefix(writer);
                 }
 
                 writer.setPrefix(pfx, ns);



Mime
View raw message