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 java.io.IOException;
22  import java.io.InputStream;
23  import java.io.StringReader;
24  import java.lang.reflect.UndeclaredThrowableException;
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  
32  import org.w3c.dom.Document;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.SAXException;
35  import org.xml.sax.XMLReader;
36  
37  public final class XmlUtils {
38  
39      public static final boolean isWellFormedXml(final String string) throws Exception {
40          return isWellFormedXml(new InputSource(new StringReader(string)));
41      }
42  
43      public static final XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
44      	final SAXParserFactory spf = SAXParserFactory.newInstance();
45      	spf.setValidating(false);
46      	spf.setNamespaceAware(true);
47      	return spf.newSAXParser().getXMLReader();
48      }
49  
50      public static final boolean isWellFormedXml(final InputSource isource) {
51      	try {
52      		newXMLReader().parse(isource);
53      		return true;
54      	} catch (SAXException e) {
55      		System.out.println(e);
56      		e.printStackTrace();
57      		return false;
58      	} catch (IOException e) {
59      		throw new UndeclaredThrowableException(e);
60      	} catch (ParserConfigurationException e) {
61      		throw new UndeclaredThrowableException(e);
62      	}
63      }
64  
65      public static final boolean isWellFormedXml(final InputStream in) throws Exception {
66      	return isWellFormedXml(new InputSource(in));
67      }
68      
69      public static final Document toDom(final InputStream in) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
70          final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
71          Document result;
72          result = builder.parse(in);
73          return result;
74      }
75  }