View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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      private XmlUtil() {
43      }
44  
45      public static Document getRawXml(String fileName, String code, String unserializedSource)
46              throws ParserConfigurationException {
47          Document rawXml = null;
48          try {
49              final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
50              factory.setValidating(false);
51              factory.setNamespaceAware(true);
52  
53              final DocumentBuilder builder = factory.newDocumentBuilder();
54  
55              rawXml = builder.parse(new InputSource(new StringReader(code)));
56          }
57          catch (IOException | SAXException ex) {
58              Assert.fail(fileName + " has invalid xml (" + ex.getMessage() + "): "
59                      + unserializedSource);
60          }
61  
62          return rawXml;
63      }
64  
65      public static Set<Node> getChildrenElements(Node node) {
66          final Set<Node> result = new LinkedHashSet<>();
67  
68          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
69              if (child.getNodeType() != Node.TEXT_NODE) {
70                  result.add(child);
71              }
72          }
73  
74          return result;
75      }
76  
77      public static Node getFirstChildElement(Node node) {
78          Node firstChildElement = null;
79          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
80              if (child.getNodeType() != Node.TEXT_NODE) {
81                  firstChildElement = child;
82                  break;
83              }
84          }
85  
86          return firstChildElement;
87      }
88  
89      public static Set<Node> findChildElementsByTag(Node node, String tag) {
90          final Set<Node> result = new LinkedHashSet<>();
91  
92          for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
93              if (tag.equals(child.getNodeName())) {
94                  result.add(child);
95              }
96              else if (child.hasChildNodes()) {
97                  result.addAll(findChildElementsByTag(child, tag));
98              }
99          }
100 
101         return result;
102     }
103 }