View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.internal.utils;
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  import java.util.LinkedHashSet;
25  import java.util.Set;
26  
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  import javax.xml.parsers.ParserConfigurationException;
30  
31  import org.junit.Assert;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Node;
34  import org.xml.sax.InputSource;
35  import org.xml.sax.SAXException;
36  
37  /**
38   * XmlUtil.
39   * @noinspection ClassOnlyUsedInOnePackage
40   */
41  public final class XmlUtil {
42  
43      private XmlUtil() {
44      }
45  
46      public static Document getRawXml(String fileName, String code, String unserializedSource)
47              throws ParserConfigurationException {
48          Document rawXml = null;
49          try {
50              final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51              factory.setValidating(false);
52              factory.setNamespaceAware(true);
53  
54              final DocumentBuilder builder = factory.newDocumentBuilder();
55  
56              rawXml = builder.parse(new InputSource(new StringReader(code)));
57          }
58          catch (IOException | SAXException ex) {
59              Assert.fail(fileName + " has invalid xml (" + ex.getMessage() + "): "
60                      + unserializedSource);
61          }
62  
63          return rawXml;
64      }
65  
66      public static Set<Node> getChildrenElements(Node node) {
67          final Set<Node> result = new LinkedHashSet<>();
68  
69          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
70              if (child.getNodeType() != Node.TEXT_NODE) {
71                  result.add(child);
72              }
73          }
74  
75          return result;
76      }
77  
78      public static Node getFirstChildElement(Node node) {
79          Node firstChildElement = null;
80          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
81              if (child.getNodeType() != Node.TEXT_NODE) {
82                  firstChildElement = child;
83                  break;
84              }
85          }
86  
87          return firstChildElement;
88      }
89  
90      public static Set<Node> findChildElementsByTag(Node node, String tag) {
91          final Set<Node> result = new LinkedHashSet<>();
92  
93          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
94              if (tag.equals(child.getNodeName())) {
95                  result.add(child);
96              }
97              else if (child.hasChildNodes()) {
98                  result.addAll(findChildElementsByTag(child, tag));
99              }
100         }
101 
102         return result;
103     }
104 
105 }