1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/util/DOMUtils.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,141 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * DOMUtils.java 1.31 + * 1.32 + * Created on May 7th 2002 1.33 + */ 1.34 + 1.35 +package com.sun.tools.internal.xjc.util; 1.36 + 1.37 +import java.util.ArrayList; 1.38 + 1.39 +import javax.xml.namespace.QName; 1.40 + 1.41 +import org.w3c.dom.DOMException; 1.42 +import org.w3c.dom.Document; 1.43 +import org.w3c.dom.Element; 1.44 +import org.w3c.dom.Node; 1.45 +import org.w3c.dom.NodeList; 1.46 + 1.47 + 1.48 +/** 1.49 + * 1.50 + * @author Vivek Pandey 1.51 + * @version 1.0 1.52 + * 1.53 + */ 1.54 +public class DOMUtils { 1.55 + /** Gets the fist child of the given name, or null. */ 1.56 + public static Element getFirstChildElement( Element parent, String nsUri, String localPart ) { 1.57 + NodeList children = parent.getChildNodes(); 1.58 + for( int i=0; i<children.getLength(); i++ ) { 1.59 + Node item = children.item(i); 1.60 + if(!(item instanceof Element )) continue; 1.61 + 1.62 + if(nsUri.equals(item.getNamespaceURI()) 1.63 + && localPart.equals(item.getLocalName()) ) 1.64 + return (Element)item; 1.65 + } 1.66 + return null; 1.67 + } 1.68 + 1.69 + /** Gets the child elements of the given name. */ 1.70 + public static Element[] getChildElements(Element parent, String nsUri, String localPart ) { 1.71 + ArrayList a = new ArrayList(); 1.72 + NodeList children = parent.getChildNodes(); 1.73 + for( int i=0; i<children.getLength(); i++ ) { 1.74 + Node item = children.item(i); 1.75 + if(!(item instanceof Element )) continue; 1.76 + 1.77 + if(nsUri.equals(item.getNamespaceURI()) 1.78 + && localPart.equals(item.getLocalName()) ) 1.79 + a.add(item); 1.80 + } 1.81 + return (Element[]) a.toArray(new Element[a.size()]); 1.82 + } 1.83 + 1.84 + /** Gets all the child elements. */ 1.85 + public static Element[] getChildElements( Element parent ) { 1.86 + ArrayList a = new ArrayList(); 1.87 + NodeList children = parent.getChildNodes(); 1.88 + for( int i=0; i<children.getLength(); i++ ) { 1.89 + Node item = children.item(i); 1.90 + if(!(item instanceof Element )) continue; 1.91 + 1.92 + a.add(item); 1.93 + } 1.94 + return (Element[]) a.toArray(new Element[a.size()]); 1.95 + } 1.96 + 1.97 + 1.98 + public static String getElementText(Element element) throws DOMException{ 1.99 + for (Node child = element.getFirstChild(); child != null; 1.100 + child = child.getNextSibling()) { 1.101 + if(child.getNodeType() == Node.TEXT_NODE) 1.102 + return child.getNodeValue(); 1.103 + } 1.104 + return element.getNodeValue(); 1.105 + } 1.106 + 1.107 + public static Element getElement(Document parent, String name){ 1.108 + NodeList children = parent.getElementsByTagName(name); 1.109 + if(children.getLength() >= 1) 1.110 + return (Element)children.item(0); 1.111 + return null; 1.112 + } 1.113 + 1.114 + public static Element getElement(Document parent, QName qname){ 1.115 + NodeList children = parent.getElementsByTagNameNS(qname.getNamespaceURI(), qname.getLocalPart()); 1.116 + if(children.getLength() >= 1) 1.117 + return (Element)children.item(0); 1.118 + return null; 1.119 + } 1.120 + 1.121 + public static Element getElement(Document parent, String namespaceURI, 1.122 + String localName) { 1.123 + NodeList children = parent.getElementsByTagNameNS(namespaceURI, localName); 1.124 + if(children.getLength() >= 1) 1.125 + return (Element)children.item(0); 1.126 + return null; 1.127 + } 1.128 + 1.129 + public static Element[] getElements(NodeList children) { 1.130 + Element[] elements = null; 1.131 + int len = 0; 1.132 + for (int i = 0; i < children.getLength(); ++i) { 1.133 + if (elements == null) 1.134 + elements = new Element[1]; 1.135 + if (elements.length == len) { 1.136 + Element[] buf = new Element[elements.length + 1]; 1.137 + System.arraycopy(elements, 0, buf, 0, elements.length); 1.138 + elements = buf; 1.139 + } 1.140 + elements[len++] = (Element)children.item(i); 1.141 + } 1.142 + return elements; 1.143 + } 1.144 +}