首页

关于apache的fediz-core源码包中XMLUtils工具类对于不同xml数据源进行解析处理

标签:XMLUtils,xml解析工具类,apache-cxf,DOMUtils     发布时间:2018-04-25   

一、前言

关于apachefediz-core源码包中的org.apache.cxf.fediz.core.util.XMLUtils工具类,对XML不同数据源类型(File/InputStream/String/byte[])读取解析parse处理、写出writeTo生成XML文件及转为字符串处理等。

二、源码说明

1.XMLUtils工具类

package org.apache.cxf.fediz.core.util;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.PrintStream;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@import java.util.Collections;@b@import java.util.LinkedHashSet;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Properties;@b@import java.util.Set;@b@import java.util.StringTokenizer;@b@import java.util.WeakHashMap;@b@import javax.xml.namespace.QName;@b@import javax.xml.parsers.DocumentBuilder;@b@import javax.xml.parsers.DocumentBuilderFactory;@b@import javax.xml.parsers.ParserConfigurationException;@b@import javax.xml.transform.Source;@b@import javax.xml.transform.Transformer;@b@import javax.xml.transform.TransformerConfigurationException;@b@import javax.xml.transform.TransformerException;@b@import javax.xml.transform.TransformerFactory;@b@import javax.xml.transform.dom.DOMResult;@b@import javax.xml.transform.dom.DOMSource;@b@import javax.xml.transform.stream.StreamResult;@b@import org.w3c.dom.Attr;@b@import org.w3c.dom.DOMImplementation;@b@import org.w3c.dom.Document;@b@import org.w3c.dom.Element;@b@import org.w3c.dom.NamedNodeMap;@b@import org.w3c.dom.Node;@b@import org.w3c.dom.Text;@b@import org.w3c.dom.bootstrap.DOMImplementationRegistry;@b@import org.w3c.dom.ls.DOMImplementationLS;@b@import org.w3c.dom.ls.LSOutput;@b@import org.w3c.dom.ls.LSSerializer;@b@import org.xml.sax.InputSource;@b@import org.xml.sax.SAXException;@b@@b@public final class XMLUtils@b@{@b@  private static final Map<ClassLoader, DocumentBuilderFactory> DOCUMENT_BUILDER_FACTORIES = Collections.synchronizedMap(new WeakHashMap());@b@  private static final Map<ClassLoader, TransformerFactory> TRANSFORMER_FACTORIES = Collections.synchronizedMap(new WeakHashMap());@b@@b@  private static TransformerFactory getTransformerFactory()@b@  {@b@    ClassLoader loader = Thread.currentThread().getContextClassLoader();@b@    if (loader == null)@b@      loader = XMLUtils.class.getClassLoader();@b@@b@    if (loader == null)@b@      return TransformerFactory.newInstance();@b@@b@    TransformerFactory factory = (TransformerFactory)TRANSFORMER_FACTORIES.get(loader);@b@    if (factory == null) {@b@      factory = TransformerFactory.newInstance();@b@      TRANSFORMER_FACTORIES.put(loader, factory);@b@    }@b@    return factory;@b@  }@b@@b@  private static DocumentBuilderFactory getDocumentBuilderFactory() {@b@    ClassLoader loader = Thread.currentThread().getContextClassLoader();@b@    if (loader == null)@b@      loader = XMLUtils.class.getClassLoader();@b@@b@    if (loader == null)@b@      return DocumentBuilderFactory.newInstance();@b@@b@    DocumentBuilderFactory factory = (DocumentBuilderFactory)DOCUMENT_BUILDER_FACTORIES.get(loader);@b@    if (factory == null) {@b@      factory = DocumentBuilderFactory.newInstance();@b@      factory.setNamespaceAware(true);@b@      DOCUMENT_BUILDER_FACTORIES.put(loader, factory);@b@    }@b@    return factory;@b@  }@b@@b@  public static Transformer newTransformer() throws TransformerConfigurationException {@b@    return getTransformerFactory().newTransformer();@b@  }@b@@b@  public static Transformer newTransformer(int indent) throws TransformerConfigurationException {@b@    if (indent > 0) {@b@      TransformerFactory f = TransformerFactory.newInstance();@b@      try@b@      {@b@        f.setAttribute("indent-number", Integer.toString(indent));@b@      }@b@      catch (Throwable t) {@b@      }@b@      return f.newTransformer();@b@    }@b@    return getTransformerFactory().newTransformer();@b@  }@b@@b@  public static DocumentBuilder getParser() throws ParserConfigurationException {@b@    return getDocumentBuilderFactory().newDocumentBuilder();@b@  }@b@@b@  public static Document parse(InputSource is) throws ParserConfigurationException, SAXException, IOException@b@  {@b@    return getParser().parse(is);@b@  }@b@@b@  public static Document parse(File is) throws ParserConfigurationException, SAXException, IOException {@b@    return getParser().parse(is);@b@  }@b@@b@  public static Document parse(InputStream in) throws ParserConfigurationException, SAXException, IOException@b@  {@b@    return getParser().parse(in);@b@  }@b@@b@  public static Document parse(String in) throws ParserConfigurationException, SAXException, IOException {@b@    return parse(in.getBytes());@b@  }@b@@b@  public static Document parse(byte[] in) throws ParserConfigurationException, SAXException, IOException {@b@    if (in == null)@b@      return null;@b@@b@    return getParser().parse(new ByteArrayInputStream(in));@b@  }@b@@b@  public static Document newDocument() throws ParserConfigurationException {@b@    return getParser().newDocument();@b@  }@b@@b@  public static void writeTo(Node node, OutputStream os) {@b@    writeTo(new DOMSource(node), os);@b@  }@b@@b@  public static void writeTo(Node node, OutputStream os, int indent) {@b@    writeTo(new DOMSource(node), os, indent);@b@  }@b@@b@  public static void writeTo(Source src, OutputStream os) {@b@    writeTo(src, os, -1);@b@  }@b@@b@  public static void writeTo(Node node, Writer os) {@b@    writeTo(new DOMSource(node), os);@b@  }@b@@b@  public static void writeTo(Node node, Writer os, int indent) {@b@    writeTo(new DOMSource(node), os, indent);@b@  }@b@@b@  public static void writeTo(Source src, Writer os) {@b@    writeTo(src, os, -1);@b@  }@b@@b@  public static void writeTo(Source src, OutputStream os, int indent) {@b@    String enc = null;@b@    if ((src instanceof DOMSource) && (((DOMSource)src).getNode() instanceof Document))@b@      try {@b@        enc = ((Document)((DOMSource)src).getNode()).getXmlEncoding();@b@      }@b@      catch (Exception ex)@b@      {@b@      }@b@    writeTo(src, os, indent, enc, "no");@b@  }@b@@b@  public static void writeTo(Source src, Writer os, int indent) {@b@    String enc = null;@b@    if ((src instanceof DOMSource) && (((DOMSource)src).getNode() instanceof Document))@b@      try {@b@        enc = ((Document)((DOMSource)src).getNode()).getXmlEncoding();@b@      }@b@      catch (Exception ex)@b@      {@b@      }@b@    writeTo(src, os, indent, enc, "no");@b@  }@b@@b@  public static void writeTo(Source src, OutputStream os, int indent, String charset, String omitXmlDecl)@b@  {@b@    try {@b@      if (StringUtils.isEmpty(charset)) {@b@        charset = "utf-8";@b@      }@b@@b@      Transformer it = newTransformer(indent);@b@      it.setOutputProperty("method", "xml");@b@      if (indent > -1) {@b@        it.setOutputProperty("indent", "yes");@b@        it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));@b@      }@b@      it.setOutputProperty("omit-xml-declaration", omitXmlDecl);@b@      it.setOutputProperty("encoding", charset);@b@      it.transform(src, new StreamResult(os));@b@    } catch (TransformerException e) {@b@      throw new RuntimeException("Failed to configure TRaX", e);@b@    }@b@  }@b@@b@  public static void writeTo(Source src, Writer os, int indent, String charset, String omitXmlDecl)@b@  {@b@    try {@b@      if (StringUtils.isEmpty(charset)) {@b@        charset = "utf-8";@b@      }@b@@b@      Transformer it = newTransformer(indent);@b@      it.setOutputProperty("method", "xml");@b@      if (indent > -1) {@b@        it.setOutputProperty("indent", "yes");@b@        it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));@b@      }@b@      it.setOutputProperty("omit-xml-declaration", omitXmlDecl);@b@      it.setOutputProperty("encoding", charset);@b@      it.transform(src, new StreamResult(os));@b@    } catch (TransformerException e) {@b@      throw new RuntimeException("Failed to configure TRaX", e);@b@    }@b@  }@b@@b@  public static String toString(Source source) throws TransformerException, IOException {@b@    return toString(source, null);@b@  }@b@@b@  public static String toString(Source source, Properties props) throws TransformerException, IOException {@b@    StringWriter bos = new StringWriter();@b@    StreamResult sr = new StreamResult(bos);@b@    Transformer trans = newTransformer();@b@    if (props == null) {@b@      props = new Properties();@b@      props.put("omit-xml-declaration", "yes");@b@    }@b@    trans.setOutputProperties(props);@b@    trans.transform(source, sr);@b@    bos.close();@b@    return bos.toString();@b@  }@b@@b@  public static String toString(Node node, int indent) {@b@    ByteArrayOutputStream out = new ByteArrayOutputStream();@b@    writeTo(node, out, indent);@b@    return out.toString();@b@  }@b@@b@  public static String toString(Node node) {@b@    ByteArrayOutputStream out = new ByteArrayOutputStream();@b@    writeTo(node, out);@b@    return out.toString();@b@  }@b@@b@  public static void printDOM(Node node) {@b@    printDOM("", node);@b@  }@b@@b@  public static void printDOM(String words, Node node) {@b@    System.out.println(words);@b@    System.out.println(toString(node));@b@  }@b@@b@  public static Attr getAttribute(Element el, String attrName) {@b@    return el.getAttributeNode(attrName);@b@  }@b@@b@  public static void replaceAttribute(Element element, String attr, String value) {@b@    if (element.hasAttribute(attr))@b@      element.removeAttribute(attr);@b@@b@    element.setAttribute(attr, value);@b@  }@b@@b@  public static boolean hasAttribute(Element element, String value) {@b@    NamedNodeMap attributes = element.getAttributes();@b@    for (int i = 0; i < attributes.getLength(); ++i) {@b@      Node node = attributes.item(i);@b@      if (value.equals(node.getNodeValue()))@b@        return true;@b@    }@b@@b@    return false;@b@  }@b@@b@  public static void printAttributes(Element element) {@b@    NamedNodeMap attributes = element.getAttributes();@b@    for (int i = 0; i < attributes.getLength(); ++i) {@b@      Node node = attributes.item(i);@b@      System.err.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value=" + node.getNodeValue());@b@    }@b@  }@b@@b@  public static QName getNamespace(Map<String, String> namespaces, String str, String defaultNamespace)@b@  {@b@    String prefix = null;@b@    String localName = null;@b@@b@    StringTokenizer tokenizer = new StringTokenizer(str, ":");@b@    if (tokenizer.countTokens() == 2) {@b@      prefix = tokenizer.nextToken();@b@      localName = tokenizer.nextToken();@b@    } else if (tokenizer.countTokens() == 1) {@b@      localName = tokenizer.nextToken();@b@    }@b@@b@    String namespceURI = defaultNamespace;@b@    if (prefix != null)@b@      namespceURI = (String)namespaces.get(prefix);@b@@b@    return new QName(namespceURI, localName); }@b@@b@  public static void generateXMLFile(Element element, Writer writer) {@b@    Transformer it;@b@    try {@b@      it = newTransformer();@b@@b@      it.setOutputProperty("method", "xml");@b@      it.setOutputProperty("indent", "yes");@b@      it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");@b@      it.setOutputProperty("encoding", "UTF-8");@b@      it.transform(new DOMSource(element), new StreamResult(writer));@b@    } catch (Exception e) {@b@      e.printStackTrace();@b@    }@b@  }@b@@b@  public static Element createElementNS(Node node, QName name) {@b@    return createElementNS(node.getOwnerDocument(), name.getNamespaceURI(), name.getLocalPart());@b@  }@b@@b@  public static Element createElementNS(Document root, QName name) {@b@    return createElementNS(root, name.getNamespaceURI(), name.getLocalPart());@b@  }@b@@b@  public static Element createElementNS(Document root, String namespaceURI, String qualifiedName) {@b@    return root.createElementNS(namespaceURI, qualifiedName);@b@  }@b@@b@  public static Text createTextNode(Document root, String data) {@b@    return root.createTextNode(data);@b@  }@b@@b@  public static Text createTextNode(Node node, String data) {@b@    return createTextNode(node.getOwnerDocument(), data);@b@  }@b@@b@  public static void removeContents(Node parent) {@b@    Node node = parent.getFirstChild();@b@    while (node != null) {@b@      parent.removeChild(node);@b@      node = node.getNextSibling();@b@    }@b@  }@b@@b@  public static InputStream getInputStream(Document doc) throws Exception {@b@    DOMImplementationLS impl = null;@b@    DOMImplementation docImpl = doc.getImplementation();@b@@b@    if ((docImpl != null) && (docImpl.hasFeature("LS", "3.0"))) {@b@      impl = (DOMImplementationLS)docImpl.getFeature("LS", "3.0");@b@    } else {@b@      DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();@b@      impl = (DOMImplementationLS)registry.getDOMImplementation("LS");@b@      if (impl == null) {@b@        System.setProperty("org.w3c.dom.DOMImplementationSourceList", "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");@b@@b@        registry = DOMImplementationRegistry.newInstance();@b@        impl = (DOMImplementationLS)registry.getDOMImplementation("LS");@b@      }@b@    }@b@    LSOutput output = impl.createLSOutput();@b@    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();@b@    output.setByteStream(byteArrayOutputStream);@b@    LSSerializer writer = impl.createLSSerializer();@b@    writer.write(doc, output);@b@    byte[] buf = byteArrayOutputStream.toByteArray();@b@    return new ByteArrayInputStream(buf);@b@  }@b@@b@  public static Element fetchElementByNameAttribute(Element parent, String targetName, String nameValue)@b@  {@b@    List elemList = DOMUtils.findAllElementsByTagName(parent, targetName);@b@    for (Element elem : elemList)@b@      if (elem.getAttribute("name").equals(nameValue))@b@        return elem;@b@@b@@b@    return null;@b@  }@b@@b@  public static QName getQName(String value, Node node) {@b@    if (value == null) {@b@      return null;@b@    }@b@@b@    int index = value.indexOf(":");@b@@b@    if (index == -1) {@b@      return new QName(value);@b@    }@b@@b@    String prefix = value.substring(0, index);@b@    String localName = value.substring(index + 1);@b@    String ns = node.lookupNamespaceURI(prefix);@b@@b@    if ((ns == null) || (localName == null)) {@b@      throw new RuntimeException("Invalid QName in mapping: " + value);@b@    }@b@@b@    return new QName(ns, localName, prefix);@b@  }@b@@b@  public static Node fromSource(Source src) throws Exception@b@  {@b@    Transformer trans = TransformerFactory.newInstance().newTransformer();@b@    DOMResult res = new DOMResult();@b@    trans.transform(src, res);@b@    return res.getNode();@b@  }@b@@b@  public static QName convertStringToQName(String expandedQName) {@b@    return convertStringToQName(expandedQName, "");@b@  }@b@@b@  public static QName convertStringToQName(String expandedQName, String prefix) {@b@    int ind1 = expandedQName.indexOf(123);@b@    if (ind1 != 0) {@b@      return new QName(expandedQName);@b@    }@b@@b@    int ind2 = expandedQName.indexOf(125);@b@    if ((ind2 <= ind1 + 1) || (ind2 >= expandedQName.length() - 1))@b@      return null;@b@@b@    String ns = expandedQName.substring(ind1 + 1, ind2);@b@    String localName = expandedQName.substring(ind2 + 1);@b@    return new QName(ns, localName, prefix);@b@  }@b@@b@  public static Set<QName> convertStringsToQNames(List<String> expandedQNames) {@b@    Set dropElements = Collections.emptySet();@b@    if (expandedQNames != null) {@b@      dropElements = new LinkedHashSet(expandedQNames.size());@b@      for (String val : expandedQNames)@b@        dropElements.add(convertStringToQName(val));@b@    }@b@@b@    return dropElements;@b@  }@b@}

2.DOMUtils类

package org.apache.cxf.fediz.core.util;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.Reader;@b@import java.io.StringReader;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.WeakHashMap;@b@import javax.xml.namespace.QName;@b@import javax.xml.parsers.DocumentBuilder;@b@import javax.xml.parsers.DocumentBuilderFactory;@b@import javax.xml.parsers.ParserConfigurationException;@b@import javax.xml.transform.Transformer;@b@import javax.xml.transform.TransformerException;@b@import javax.xml.transform.TransformerFactory;@b@import javax.xml.transform.dom.DOMSource;@b@import javax.xml.transform.stream.StreamResult;@b@import javax.xml.transform.stream.StreamSource;@b@import org.w3c.dom.Attr;@b@import org.w3c.dom.Document;@b@import org.w3c.dom.Element;@b@import org.w3c.dom.NamedNodeMap;@b@import org.w3c.dom.Node;@b@import org.w3c.dom.Text;@b@import org.xml.sax.EntityResolver;@b@import org.xml.sax.InputSource;@b@import org.xml.sax.SAXException;@b@@b@public final class DOMUtils@b@{@b@  private static final String XMLNAMESPACE = "xmlns";@b@  private static final Map<ClassLoader, DocumentBuilder> DOCUMENT_BUILDERS = Collections.synchronizedMap(new WeakHashMap());@b@@b@  private static DocumentBuilder getBuilder()@b@    throws ParserConfigurationException@b@  {@b@    ClassLoader loader = Thread.currentThread().getContextClassLoader();@b@    if (loader == null)@b@      loader = DOMUtils.class.getClassLoader();@b@@b@    if (loader == null)@b@      return XMLUtils.getParser();@b@@b@    DocumentBuilder builder = (DocumentBuilder)DOCUMENT_BUILDERS.get(loader);@b@    if (builder == null) {@b@      builder = XMLUtils.getParser();@b@      DOCUMENT_BUILDERS.put(loader, builder);@b@    }@b@    return builder;@b@  }@b@@b@  public static String getAttributeValueEmptyNull(Element e, String attributeName)@b@  {@b@    Attr node = e.getAttributeNode(attributeName);@b@    if (node == null)@b@      return null;@b@@b@    return node.getValue();@b@  }@b@@b@  public static String getContent(Node n)@b@  {@b@    String s = getRawContent(n);@b@    if (s != null)@b@      s = s.trim();@b@@b@    return s;@b@  }@b@@b@  public static String getRawContent(Node n)@b@  {@b@    if (n == null)@b@      return null;@b@@b@    StringBuilder b = null;@b@    String s = null;@b@    Node n1 = n.getFirstChild();@b@    while (n1 != null) {@b@      if (n1.getNodeType() == 3)@b@        if (b != null) {@b@          b.append(((Text)n1).getNodeValue());@b@        } else if (s == null) {@b@          s = ((Text)n1).getNodeValue();@b@        } else {@b@          b = new StringBuilder(s).append(((Text)n1).getNodeValue());@b@          s = null;@b@        }@b@@b@      n1 = n1.getNextSibling();@b@    }@b@    if (b != null)@b@      return b.toString();@b@@b@    return s;@b@  }@b@@b@  public static Node getChild(Node parent, String name)@b@  {@b@    if (parent == null) {@b@      return null;@b@    }@b@@b@    Node first = parent.getFirstChild();@b@    if (first == null) {@b@      return null;@b@    }@b@@b@    for (Node node = first; node != null; node = node.getNextSibling())@b@    {@b@      if (node.getNodeType() != 1)@b@        break label63:@b@@b@      if ((name != null) && (name.equals(node.getNodeName())))@b@        return node;@b@@b@      if (name == null)@b@        return node;@b@    }@b@@b@    label63: return null;@b@  }@b@@b@  public static String getAttribute(Node element, String attName) {@b@    NamedNodeMap attrs = element.getAttributes();@b@    if (attrs == null)@b@      return null;@b@@b@    Node attN = attrs.getNamedItem(attName);@b@    if (attN == null)@b@      return null;@b@@b@    return attN.getNodeValue();@b@  }@b@@b@  public static String getAttribute(Element element, QName attName)@b@  {@b@    Attr attr;@b@    if (StringUtils.isEmpty(attName.getNamespaceURI()))@b@      attr = element.getAttributeNode(attName.getLocalPart());@b@    else@b@      attr = element.getAttributeNodeNS(attName.getNamespaceURI(), attName.getLocalPart());@b@@b@    return ((attr == null) ? null : attr.getValue());@b@  }@b@@b@  public static void setAttribute(Node node, String attName, String val) {@b@    NamedNodeMap attributes = node.getAttributes();@b@    Node attNode = node.getOwnerDocument().createAttributeNS(null, attName);@b@    attNode.setNodeValue(val);@b@    attributes.setNamedItem(attNode);@b@  }@b@@b@  public static void removeAttribute(Node node, String attName) {@b@    NamedNodeMap attributes = node.getAttributes();@b@    attributes.removeNamedItem(attName);@b@  }@b@@b@  public static void setText(Node node, String val)@b@  {@b@    Node chld = getChild(node, 3);@b@    if (chld == null) {@b@      Node textN = node.getOwnerDocument().createTextNode(val);@b@      node.appendChild(textN);@b@      return;@b@    }@b@@b@    chld.setNodeValue(val);@b@  }@b@@b@  public static Node findChildWithAtt(Node parent, String elemName, String attName, String attVal)@b@  {@b@    Node child = getChild(parent, 1);@b@    if (attVal == null) while (true) {@b@        if ((child == null) || ((elemName != null) && (!(elemName.equals(child.getNodeName())))) || (getAttribute(child, attName) == null))@b@          break label103;@b@        child = getNext(child, elemName, 1);@b@      }@b@@b@    while ((child != null) && (((elemName == null) || (elemName.equals(child.getNodeName())))) && (!(attVal.equals(getAttribute(child, attName)))))@b@    {@b@      child = getNext(child, elemName, 1);@b@    }@b@@b@    label103: return child;@b@  }@b@@b@  public static String getChildContent(Node parent, String name)@b@  {@b@    Node first = parent.getFirstChild();@b@    if (first == null)@b@      return null;@b@@b@    for (Node node = first; node != null; node = node.getNextSibling())@b@    {@b@      if (name.equals(node.getNodeName()))@b@        return getRawContent(node);@b@    }@b@@b@    return null;@b@  }@b@@b@  public static QName getElementQName(Element el) {@b@    return new QName(el.getNamespaceURI(), el.getLocalName());@b@  }@b@@b@  public static Element getFirstElement(Node parent)@b@  {@b@    Node n = parent.getFirstChild();@b@    while ((n != null) && (1 != n.getNodeType()))@b@      n = n.getNextSibling();@b@@b@    if (n == null)@b@      return null;@b@@b@    return ((Element)n);@b@  }@b@@b@  public static Element getNextElement(Element el) {@b@    Node nd = el.getNextSibling();@b@    while (nd != null) {@b@      if (nd.getNodeType() == 1)@b@        return ((Element)nd);@b@@b@      nd = nd.getNextSibling();@b@    }@b@    return null;@b@  }@b@@b@  public static Element getFirstChildWithName(Element parent, QName q)@b@  {@b@    String ns = q.getNamespaceURI();@b@    String lp = q.getLocalPart();@b@    return getFirstChildWithName(parent, ns, lp);@b@  }@b@@b@  public static Element getFirstChildWithName(Element parent, String ns, String lp)@b@  {@b@    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling())@b@      if (n instanceof Element) {@b@        Element e = (Element)n;@b@        String ens = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();@b@        if ((ns.equals(ens)) && (lp.equals(e.getLocalName())))@b@          return e;@b@      }@b@@b@@b@    return null;@b@  }@b@@b@  public static List<Element> getChildrenWithName(Element parent, String ns, String localName)@b@  {@b@    List r = new ArrayList();@b@    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling())@b@      if (n instanceof Element) {@b@        Element e = (Element)n;@b@        String eNs = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();@b@        if ((ns.equals(eNs)) && (localName.equals(e.getLocalName())))@b@          r.add(e);@b@      }@b@@b@@b@    return r;@b@  }@b@@b@  public static List<Element> getChildrenWithNamespace(Element parent, String ns)@b@  {@b@    List r = new ArrayList();@b@    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling())@b@      if (n instanceof Element) {@b@        Element e = (Element)n;@b@        String eNs = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();@b@        if (ns.equals(eNs))@b@          r.add(e);@b@      }@b@@b@@b@    return r;@b@  }@b@@b@  public static Node getChild(Node parent, int type)@b@  {@b@    Node n = parent.getFirstChild();@b@    while ((n != null) && (type != n.getNodeType()))@b@      n = n.getNextSibling();@b@@b@    if (n == null)@b@      return null;@b@@b@    return n;@b@  }@b@@b@  public static Node getNext(Node current)@b@  {@b@    String name = current.getNodeName();@b@    int type = current.getNodeType();@b@    return getNext(current, name, type);@b@  }@b@@b@  public static Node getNext(Node current, String name, int type)@b@  {@b@    Node first = current.getNextSibling();@b@    if (first == null) {@b@      return null;@b@    }@b@@b@    for (Node node = first; node != null; node = node.getNextSibling())@b@    {@b@      if ((type >= 0) && (node.getNodeType() != type)) {@b@        break label63:@b@      }@b@@b@      if (name == null)@b@        return node;@b@@b@      if (name.equals(node.getNodeName()))@b@        return node;@b@    }@b@@b@    label63: return null;@b@  }@b@@b@  public static Document readXml(InputStream is)@b@    throws SAXException, IOException, ParserConfigurationException@b@  {@b@    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();@b@@b@    dbf.setValidating(false);@b@    dbf.setIgnoringComments(false);@b@    dbf.setIgnoringElementContentWhitespace(true);@b@    dbf.setNamespaceAware(true);@b@@b@    DocumentBuilder db = null;@b@    db = dbf.newDocumentBuilder();@b@    db.setEntityResolver(new NullResolver());@b@@b@    return db.parse(is);@b@  }@b@@b@  public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException {@b@    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();@b@@b@    dbf.setValidating(false);@b@    dbf.setIgnoringComments(false);@b@    dbf.setIgnoringElementContentWhitespace(true);@b@    dbf.setNamespaceAware(true);@b@@b@    DocumentBuilder db = null;@b@    db = dbf.newDocumentBuilder();@b@    db.setEntityResolver(new NullResolver());@b@@b@    InputSource ips = new InputSource(is);@b@    return db.parse(ips);@b@  }@b@@b@  public static Document readXml(StreamSource is)@b@    throws SAXException, IOException, ParserConfigurationException@b@  {@b@    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();@b@@b@    dbf.setValidating(false);@b@    dbf.setIgnoringComments(false);@b@    dbf.setIgnoringElementContentWhitespace(true);@b@    dbf.setNamespaceAware(true);@b@@b@    DocumentBuilder db = null;@b@    db = dbf.newDocumentBuilder();@b@    db.setEntityResolver(new NullResolver());@b@@b@    InputSource is2 = new InputSource();@b@    is2.setSystemId(is.getSystemId());@b@    is2.setByteStream(is.getInputStream());@b@    is2.setCharacterStream(is.getReader());@b@@b@    return db.parse(is2);@b@  }@b@@b@  public static void writeXml(Node n, OutputStream os) throws TransformerException {@b@    TransformerFactory tf = TransformerFactory.newInstance();@b@@b@    Transformer t = tf.newTransformer();@b@    t.setOutputProperty("indent", "yes");@b@    t.transform(new DOMSource(n), new StreamResult(os));@b@  }@b@@b@  public static DocumentBuilder createDocumentBuilder() {@b@    try {@b@      return getBuilder();@b@    } catch (ParserConfigurationException e) {@b@      throw new RuntimeException("Couldn't find a DOM parser.", e);@b@    }@b@  }@b@@b@  public static Document createDocument() {@b@    try {@b@      return getBuilder().newDocument();@b@    } catch (ParserConfigurationException e) {@b@      throw new RuntimeException("Couldn't find a DOM parser.", e);@b@    }@b@  }@b@@b@  public static String getPrefixRecursive(Element el, String ns) {@b@    String prefix = getPrefix(el, ns);@b@    if ((prefix == null) && (el.getParentNode() instanceof Element))@b@      prefix = getPrefixRecursive((Element)el.getParentNode(), ns);@b@@b@    return prefix;@b@  }@b@@b@  public static String getPrefix(Element el, String ns) {@b@    NamedNodeMap atts = el.getAttributes();@b@    for (int i = 0; i < atts.getLength(); ++i) {@b@      Node node = atts.item(i);@b@      String name = node.getNodeName();@b@      if ((ns.equals(node.getNodeValue())) && (name != null) && ((("xmlns".equals(name)) || (name.startsWith("xmlns:")))))@b@      {@b@        return node.getLocalName();@b@      }@b@    }@b@    return null;@b@  }@b@@b@  public static void getPrefixesRecursive(Element element, String namespaceUri, List<String> prefixes)@b@  {@b@    getPrefixes(element, namespaceUri, prefixes);@b@    Node parent = element.getParentNode();@b@    if (parent instanceof Element)@b@      getPrefixesRecursive((Element)parent, namespaceUri, prefixes);@b@  }@b@@b@  public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes)@b@  {@b@    NamedNodeMap atts = element.getAttributes();@b@    for (int i = 0; i < atts.getLength(); ++i) {@b@      Node node = atts.item(i);@b@      String name = node.getNodeName();@b@      if ((namespaceUri.equals(node.getNodeValue())) && (name != null) && ((("xmlns".equals(name)) || (name.startsWith("xmlns:")))))@b@      {@b@        prefixes.add(node.getPrefix());@b@      }@b@    }@b@  }@b@@b@  public static String createNamespace(Element el, String ns) {@b@    String p = "ns1";@b@    int i = 1;@b@    while (getPrefix(el, ns) != null) {@b@      p = new StringBuilder().append("ns").append(i).toString();@b@      ++i;@b@    }@b@    addNamespacePrefix(el, ns, p);@b@    return p;@b@  }@b@@b@  public static String getNamespace(Node node, String searchPrefix)@b@  {@b@    while (!(node instanceof Element))@b@      node = node.getParentNode();@b@@b@    Element el = (Element)node;@b@@b@    NamedNodeMap atts = el.getAttributes();@b@    for (int i = 0; i < atts.getLength(); ++i) {@b@      Node currentAttribute = atts.item(i);@b@      String currentLocalName = currentAttribute.getLocalName();@b@      String currentPrefix = currentAttribute.getPrefix();@b@      if ((searchPrefix.equals(currentLocalName)) && ("xmlns".equals(currentPrefix)))@b@        return currentAttribute.getNodeValue();@b@      if ((StringUtils.isEmpty(searchPrefix)) && ("xmlns".equals(currentLocalName)) && (StringUtils.isEmpty(currentPrefix)))@b@      {@b@        return currentAttribute.getNodeValue();@b@      }@b@    }@b@@b@    Node parent = el.getParentNode();@b@    if (parent instanceof Element) {@b@      return getNamespace((Element)parent, searchPrefix);@b@    }@b@@b@    return null;@b@  }@b@@b@  public static List<Element> findAllElementsByTagNameNS(Element elem, String nameSpaceURI, String localName) {@b@    List ret = new LinkedList();@b@    findAllElementsByTagNameNS(elem, nameSpaceURI, localName, ret);@b@    return ret;@b@  }@b@@b@  private static void findAllElementsByTagNameNS(Element el, String nameSpaceURI, String localName, List<Element> elementList)@b@  {@b@    if ((localName.equals(el.getLocalName())) && (nameSpaceURI.contains(el.getNamespaceURI())))@b@      elementList.add(el);@b@@b@    Element elem = getFirstElement(el);@b@    while (elem != null) {@b@      findAllElementsByTagNameNS(elem, nameSpaceURI, localName, elementList);@b@      elem = getNextElement(elem);@b@    }@b@  }@b@@b@  public static List<Element> findAllElementsByTagName(Element elem, String tagName) {@b@    List ret = new LinkedList();@b@    findAllElementsByTagName(elem, tagName, ret);@b@    return ret;@b@  }@b@@b@  private static void findAllElementsByTagName(Element el, String tagName, List<Element> elementList)@b@  {@b@    if (tagName.equals(el.getTagName()))@b@      elementList.add(el);@b@@b@    Element elem = getFirstElement(el);@b@    while (elem != null) {@b@      findAllElementsByTagName(elem, tagName, elementList);@b@      elem = getNextElement(elem);@b@    }@b@  }@b@@b@  public static boolean hasElementInNS(Element el, String namespace)@b@  {@b@    if (namespace.equals(el.getNamespaceURI()))@b@      return true;@b@@b@    Element elem = getFirstElement(el);@b@    while (elem != null) {@b@      if (hasElementInNS(elem, namespace))@b@        return true;@b@@b@      elem = getNextElement(elem);@b@    }@b@    return false;@b@  }@b@@b@  public static String setNamespace(Element element, String namespace, String prefix)@b@  {@b@    String pre = getPrefixRecursive(element, namespace);@b@    if (pre != null)@b@      return pre;@b@@b@    element.setAttributeNS("http://www.w3.org/2000/xmlns/", new StringBuilder().append("xmlns:").append(prefix).toString(), namespace);@b@    return prefix;@b@  }@b@@b@  public static void addNamespacePrefix(Element element, String namespaceUri, String prefix)@b@  {@b@    element.setAttributeNS("http://www.w3.org/2000/xmlns/", new StringBuilder().append("xmlns:").append(prefix).toString(), namespaceUri);@b@  }@b@@b@  public static class NullResolver@b@    implements EntityResolver@b@  {@b@    public InputSource resolveEntity(String publicId, String systemId)@b@      throws SAXException, IOException@b@    {@b@      return new InputSource(new StringReader(""));@b@    }@b@  }@b@}