cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r735838 - in /cxf/branches/2.0.x-fixes: ./ common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
Date Mon, 19 Jan 2009 21:46:07 GMT
Author: dkulp
Date: Mon Jan 19 13:46:07 2009
New Revision: 735838

URL: http://svn.apache.org/viewvc?rev=735838&view=rev
Log:
Merged revisions 733915 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.1.x-fixes

........
  r733915 | dkulp | 2009-01-12 16:43:13 -0500 (Mon, 12 Jan 2009) | 2 lines
  
  Fix build failure
........

Modified:
    cxf/branches/2.0.x-fixes/   (props changed)
    cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java

Propchange: cxf/branches/2.0.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jan 19 13:46:07 2009
@@ -1,3 +1,3 @@
-/cxf/branches/2.1.x-fixes:673548,674485,674547,674551,674562,674601,674649,674764,674887,675644,675653,677048,677385,678004,678009,678559,678629,678808,678852,678891,678893,679248,679597,680435,681060,681165,681813,681816,682902,682951,683089,683290,683318,684099,684790-684793,684842,684862,684895-684918,685205,685253,686237,686283,686299,686333-686364,686765,686827,687097,687464-687465,689109,689112,689122,691316,691357,691491,691711,691715,691745,692162-692163,692468,692500,694466-694469,694472,694717,694748-694749,694870,695503,695509,695553,695555,695563,695875-695877,695940,695980,696436,696455,696721,697086,698129,701526,701634,702275,702443,702527,702582,702604,702610,702642-702643,702649,702760,702870,702873,702959,703193,703242,703523,704303,704587,704738,704998,705153,705280-705449,705455,709357,709641,709644,710177,710184,711736,712199,712225,712275,712600,712896,713083,713410,713413,713594,713599,713808,713901,714169-714171,718622,718929,719211,719221-719223,7192
 96,719300-719301,719303,719308,719332,719356,719363,719369-719383,719650,719695,720124,723545,724403-724404,724421,724448,724451,724486-724487,724714,725367,725371,725763,725774,726045,726048,726106,726123,726745-726746,726749,726754,726756-726758,726995,727794,727797-727798,727800,731676,731684,731686-731688,731690,733587,733873,733876,733884,733891,733893
+/cxf/branches/2.1.x-fixes:673548,674485,674547,674551,674562,674601,674649,674764,674887,675644,675653,677048,677385,678004,678009,678559,678629,678808,678852,678891,678893,679248,679597,680435,681060,681165,681813,681816,682902,682951,683089,683290,683318,684099,684790-684793,684842,684862,684895-684918,685205,685253,686237,686283,686299,686333-686364,686765,686827,687097,687464-687465,689109,689112,689122,691316,691357,691491,691711,691715,691745,692162-692163,692468,692500,694466-694469,694472,694717,694748-694749,694870,695503,695509,695553,695555,695563,695875-695877,695940,695980,696436,696455,696721,697086,698129,701526,701634,702275,702443,702527,702582,702604,702610,702642-702643,702649,702760,702870,702873,702959,703193,703242,703523,704303,704587,704738,704998,705153,705280-705449,705455,709357,709641,709644,710177,710184,711736,712199,712225,712275,712600,712896,713083,713410,713413,713594,713599,713808,713901,714169-714171,718622,718929,719211,719221-719223,7192
 96,719300-719301,719303,719308,719332,719356,719363,719369-719383,719650,719695,720124,723545,724403-724404,724421,724448,724451,724486-724487,724714,725367,725371,725763,725774,726045,726048,726106,726123,726745-726746,726749,726754,726756-726758,726995,727794,727797-727798,727800,731676,731684,731686-731688,731690,733587,733873,733876,733884,733891,733893,733915
 /cxf/trunk:651669-686342,686344-686363,686764,686820,687096,687387,687463,688086,688102,688735,691271,691355,691488,691602,691706,691728,692116,692157,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694869,695396,695484,695537,695552,695561,695619,695684,695835,695935,695977,696094,696433,696720,697085,698128,700261,700602,701783,701830,701862,702267,702580,702602,702609,702616,702656,702957,703191,703239,703501,704584,704997,705150,705274,705340,705446,708550,708554,709353-709354,709425,710150,712194,712198,712272,712312,712670,712893,713082,713584,713597,713804,713899,714167-714168,718281,718565,718620,718640,718665,719017,719210,719215-719218,719222,719273,719327-719354,719362,719368,719382,719649,719680,720119-720217,723338,723717-723791,724334-724371,724433-724438,724449,724481,724485,724668,724782,724795,725754,725773,725799,725839,726342,726524,726631,726637,726639,726692,726724,726992,727445,727692,727754,727792,730139,731598,731604,731615,73163
 1,731635,732320,732363,732411,732710,732827,733582
 /incubator/cxf/trunk:434594-651668

Propchange: cxf/branches/2.0.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java?rev=735838&r1=735837&r2=735838&view=diff
==============================================================================
--- cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
(original)
+++ cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java
Mon Jan 19 13:46:07 2009
@@ -22,10 +22,13 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Reader;
 import java.io.StringReader;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -38,6 +41,7 @@
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
@@ -49,7 +53,6 @@
 
 import org.apache.cxf.common.util.StringUtils;
 
-
 /**
  * Few simple utils to read DOM. This is originally from the Jakarta Commons
  * Modeler.
@@ -57,8 +60,8 @@
  * @author Costin Manolache
  */
 public final class DOMUtils {
-    static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();
-    static DocumentBuilder builder;
+    private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();
+    private static DocumentBuilder builder;
     private static final String XMLNAMESPACE = "xmlns";
 
     private DOMUtils() {
@@ -71,8 +74,24 @@
         }
         return builder;
     }
+    
+    /**
+     * This function is much like getAttribute, but returns null, not "", for a nonexistent
attribute.
+     * @param e
+     * @param attributeName
+     * @return
+     */
+    public static String getAttributeValueEmptyNull(Element e, String attributeName) {
+        Attr node = e.getAttributeNode(attributeName);
+        if (node == null) {
+            return null;
+        }
+        return node.getValue();
+    }
+    
+    
     /**
-     * Get the trimed text content of a node or null if there is no text
+     * Get the trimmed text content of a node or null if there is no text
      */
     public static String getContent(Node n) {
         if (n == null) {
@@ -248,11 +267,25 @@
         return null;
     }
     
+    /**
+     * Return the first element child with the specified qualified name.
+     * @param parent
+     * @param q
+     * @return
+     */
     public static Element getFirstChildWithName(Element parent, QName q) { 
         String ns = q.getNamespaceURI();
         String lp = q.getLocalPart();
         return getFirstChildWithName(parent, ns, lp);
     }
+    
+    /**
+     * Return the first element child with the specified qualified name.
+     * @param parent
+     * @param ns
+     * @param lp
+     * @return
+     */
     public static Element getFirstChildWithName(Element parent, String ns, String lp) { 
         for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
             if (n instanceof Element) {
@@ -263,10 +296,34 @@
                 }
             }
         }
-        return (Element)n;
+        return null;
     }
+
     /**
-     * Get the first direct child with a given type
+     * Return child elements with specified name.
+     * @param parent
+     * @param ns
+     * @param localName
+     * @return
+     */
+    public static List<Element> getChildrenWithName(Element parent, String ns, String
localName) {
+        List<Element> r = new ArrayList<Element>();
+        for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
+            if (n instanceof Element) {
+                Element e = (Element) n;
+                if (ns.equals(e.getNamespaceURI()) && localName.equals(e.getLocalName()))
{
+                    r.add(e);
+                }
+            }
+        }
+        return r;
+    }
+
+    /**
+     * Get the first child of the specified type.
+     * @param parent
+     * @param type
+     * @return
      */
     public static Node getChild(Node parent, int type) {
         Node n = parent.getFirstChild();
@@ -302,8 +359,7 @@
             if (type >= 0 && node.getNodeType() != type) {
                 continue;
             }
-            // System.out.println("getNode: " + name + " " +
-            // node.getNodeName());
+
             if (name == null) {
                 return node;
             }
@@ -342,6 +398,25 @@
 
         return db.parse(is);
     }
+    public static Document readXml(Reader is) throws SAXException, IOException,
+        ParserConfigurationException {
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+    
+        dbf.setValidating(false);
+        dbf.setIgnoringComments(false);
+        dbf.setIgnoringElementContentWhitespace(true);
+        dbf.setNamespaceAware(true);
+        // dbf.setCoalescing(true);
+        // dbf.setExpandEntityReferences(true);
+    
+        DocumentBuilder db = null;
+        db = dbf.newDocumentBuilder();
+        db.setEntityResolver(new NullResolver());
+    
+        // db.setErrorHandler( new MyErrorHandler());
+        InputSource ips = new InputSource(is);
+        return db.parse(ips);
+    }
     public static Document readXml(StreamSource is) throws SAXException, IOException,
         ParserConfigurationException {
         
@@ -382,6 +457,7 @@
             throw new RuntimeException("Couldn't find a DOM parser.", e);
         }
     }
+    
     public static Document createDocument() {
         try {
             return getBuilder().newDocument();
@@ -410,6 +486,38 @@
         }
         return null;
     }
+    
+    /**
+     * Get all prefixes defined, up to the root, for a namespace URI.
+     * @param element
+     * @param namespaceUri
+     * @param prefixes
+     */
+    public static void getPrefixesRecursive(Element element, String namespaceUri, List<String>
prefixes) {
+        getPrefixes(element, namespaceUri, prefixes);
+        Node parent = element.getParentNode();
+        if (parent instanceof Element) {
+            getPrefixesRecursive((Element)parent, namespaceUri, prefixes);
+        }
+    }
+    
+    /**
+     * Get all prefixes defined on this element for the specified namespace.
+     * @param element
+     * @param namespaceUri
+     * @param prefixes
+     */
+    public static void getPrefixes(Element element, String namespaceUri, List<String>
prefixes) {
+        NamedNodeMap atts = element.getAttributes();
+        for (int i = 0; i < atts.getLength(); i++) {
+            Node node = atts.item(i);
+            String name = node.getNodeName();
+            if (namespaceUri.equals(node.getNodeValue()) 
+                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE
+ ":")))) {
+                prefixes.add(node.getPrefix());
+            }
+        }
+    }
 
     public static String createNamespace(Element el, String ns) {
         String p = "ns1";
@@ -423,13 +531,20 @@
     }
 
     /**
-     * Searches the given element including it's parent elements
-     * for a matching namspace decleration.
-     * @param el element to search for namespace definitions
+     * Starting from a node, find the namespace declaration for a prefix.
+     * for a matching namespace declaration.
+     * @param node search up from here to search for namespace definitions
      * @param searchPrefix the prefix we are searching for
      * @return the namespace if found.
      */
-    public static String getNamespace(Element el, String searchPrefix) {
+    public static String getNamespace(Node node, String searchPrefix) {
+        
+        Element el;
+        while (!(node instanceof Element)) {
+            node = node.getParentNode();
+        }
+        el = (Element) node;
+        
         NamedNodeMap atts = el.getAttributes();
         for (int i = 0; i < atts.getLength(); i++) {
             Node currentAttribute = atts.item(i);
@@ -496,4 +611,36 @@
             elem = getNextElement(elem);
         }
     }
+    
+
+    /**
+     * Set a namespace/prefix on an element if it is not set already. First off, it
+     * searches for the element for the prefix associated with the specified
+     * namespace. If the prefix isn't null, then this is returned. Otherwise, it
+     * creates a new attribute using the namespace/prefix passed as parameters.
+     * 
+     * @param element
+     * @param namespace
+     * @param prefix
+     * @return the prefix associated with the set namespace
+     */
+    public static String setNamespace(Element element, String namespace,
+            String prefix) {
+        String pre = getPrefixRecursive(element, namespace);
+        if (pre != null) {
+            return pre;
+        }
+        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespace);
+        return prefix;
+    }
+    
+    /**
+     * Add a namespace prefix definition to an element.
+     * @param element
+     * @param namespaceUri
+     * @param prefix
+     */
+    public static void addNamespacePrefix(Element element, String namespaceUri, String prefix)
{
+        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
+    }
 }



Mime
View raw message