View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */ 
19  package org.apache.rat.report.xml;
20  
21  import org.w3c.dom.Document;
22  import org.xml.sax.InputSource;
23  import org.xml.sax.SAXException;
24  import org.xml.sax.XMLReader;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.FactoryConfigurationError;
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.parsers.SAXParserFactory;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.StringReader;
34  import java.lang.reflect.UndeclaredThrowableException;
35  
36  public final class XmlUtils {
37      /**
38       * Private constructor, to prevent accidental instantiation.
39       */
40      private XmlUtils() {
41          // Does nothing
42      }
43  
44      public static final boolean isWellFormedXml(final String string) throws Exception {
45          return isWellFormedXml(new InputSource(new StringReader(string)));
46      }
47  
48      public static final XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
49          final SAXParserFactory spf = SAXParserFactory.newInstance();
50          spf.setValidating(false);
51          spf.setNamespaceAware(true);
52          return spf.newSAXParser().getXMLReader();
53      }
54  
55      public static final boolean isWellFormedXml(final InputSource isource) {
56          try {
57              newXMLReader().parse(isource);
58              return true;
59          } catch (SAXException e) {
60              System.out.println(e);
61              e.printStackTrace();
62              return false;
63          } catch (IOException e) {
64              throw new UndeclaredThrowableException(e);
65          } catch (ParserConfigurationException e) {
66              throw new UndeclaredThrowableException(e);
67          }
68      }
69  
70      public static final boolean isWellFormedXml(final InputStream in) throws Exception {
71          return isWellFormedXml(new InputSource(in));
72      }
73      
74      public static final Document toDom(final InputStream in) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
75          final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
76          Document result;
77          result = builder.parse(in);
78          return result;
79      }
80  }