src/share/jaxws_classes/com/sun/tools/internal/xjc/util/DOMUtils.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 /*
    27  * DOMUtils.java
    28  *
    29  * Created on May 7th 2002
    30  */
    32 package com.sun.tools.internal.xjc.util;
    34 import java.util.ArrayList;
    36 import javax.xml.namespace.QName;
    38 import org.w3c.dom.DOMException;
    39 import org.w3c.dom.Document;
    40 import org.w3c.dom.Element;
    41 import org.w3c.dom.Node;
    42 import org.w3c.dom.NodeList;
    45 /**
    46  *
    47  * @author  Vivek Pandey
    48  * @version 1.0
    49  *
    50  */
    51 public class DOMUtils {
    52     /** Gets the fist child of the given name, or null. */
    53     public static Element getFirstChildElement( Element parent, String nsUri, String localPart ) {
    54         NodeList children = parent.getChildNodes();
    55         for( int i=0; i<children.getLength(); i++ ) {
    56             Node item = children.item(i);
    57             if(!(item instanceof Element ))     continue;
    59             if(nsUri.equals(item.getNamespaceURI())
    60             && localPart.equals(item.getLocalName()) )
    61                 return (Element)item;
    62         }
    63         return null;
    64     }
    66     /** Gets the child elements of the given name. */
    67     public static Element[] getChildElements(Element parent, String nsUri, String localPart ) {
    68         ArrayList a = new ArrayList();
    69         NodeList children = parent.getChildNodes();
    70         for( int i=0; i<children.getLength(); i++ ) {
    71             Node item = children.item(i);
    72             if(!(item instanceof Element ))     continue;
    74             if(nsUri.equals(item.getNamespaceURI())
    75             && localPart.equals(item.getLocalName()) )
    76                 a.add(item);
    77         }
    78         return (Element[]) a.toArray(new Element[a.size()]);
    79     }
    81     /** Gets all the child elements. */
    82     public static Element[] getChildElements( Element parent ) {
    83         ArrayList a = new ArrayList();
    84         NodeList children = parent.getChildNodes();
    85         for( int i=0; i<children.getLength(); i++ ) {
    86             Node item = children.item(i);
    87             if(!(item instanceof Element ))     continue;
    89             a.add(item);
    90         }
    91         return (Element[]) a.toArray(new Element[a.size()]);
    92     }
    95   public static String getElementText(Element element) throws DOMException{
    96     for (Node child = element.getFirstChild(); child != null;
    97      child = child.getNextSibling()) {
    98       if(child.getNodeType() == Node.TEXT_NODE)
    99     return child.getNodeValue();
   100     }
   101     return element.getNodeValue();
   102   }
   104   public static Element getElement(Document parent, String name){
   105     NodeList children = parent.getElementsByTagName(name);
   106     if(children.getLength() >= 1)
   107       return (Element)children.item(0);
   108     return null;
   109   }
   111   public static Element getElement(Document parent, QName qname){
   112     NodeList children = parent.getElementsByTagNameNS(qname.getNamespaceURI(), qname.getLocalPart());
   113     if(children.getLength() >= 1)
   114       return (Element)children.item(0);
   115     return null;
   116   }
   118   public static Element getElement(Document parent, String namespaceURI,
   119                        String localName) {
   120     NodeList children = parent.getElementsByTagNameNS(namespaceURI, localName);
   121     if(children.getLength() >= 1)
   122       return (Element)children.item(0);
   123     return null;
   124   }
   126     public static Element[] getElements(NodeList children) {
   127         Element[] elements = null;
   128         int len = 0;
   129         for (int i = 0; i < children.getLength(); ++i) {
   130             if (elements == null)
   131                 elements = new Element[1];
   132             if (elements.length == len) {
   133                 Element[] buf = new Element[elements.length + 1];
   134                 System.arraycopy(elements, 0, buf, 0, elements.length);
   135                 elements = buf;
   136             }
   137             elements[len++] = (Element)children.item(i);
   138         }
   139         return elements;
   140     }
   141 }

mercurial