View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.utils;
2   
3   /*
4    * Copyright 2008 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.UnsupportedEncodingException;
26  
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  import javax.xml.parsers.FactoryConfigurationError;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  import org.w3c.dom.Document;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.SAXException;
35  
36  
37  public class XMLUtils
38  {
39      private static XMLUtils xMLUtils = null;
40      
41   
42      
43      /**
44       * Creation forbidden...
45       */
46      private XMLUtils()
47      {
48          super();
49          
50      }
51      
52      public static XMLUtils getInstance()
53      {
54          if (xMLUtils == null){
55              xMLUtils = new XMLUtils();
56          }
57          return xMLUtils;
58      }
59      
60      /**
61       * @param xmlFilename
62       * @return
63       */
64      public Document getDocument(InputStream stream) throws Exception
65      {
66          Document doc = null;
67  
68          try
69          {
70              InputStreamReader inputStreamReader = new InputStreamReader(stream);
71              InputSource inputSource = new InputSource(inputStreamReader);
72  
73              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
74              doc = builder.parse(inputSource);
75          }
76          catch (FileNotFoundException e)
77          {
78              throw new Exception(e);
79          }
80          catch (UnsupportedEncodingException e)
81          {
82              throw new Exception(e);
83          }
84          catch (ParserConfigurationException e)
85          {
86              throw new Exception(e);
87          }
88          catch (FactoryConfigurationError e)
89          {
90              throw new Exception(e.getException());
91          }
92          catch (SAXException e)
93          {
94              throw new Exception(e);
95          }
96          catch (IOException e)
97          {
98              throw new Exception(e);
99          }
100 
101         return doc;
102     }
103     
104     public Document getDocument( File xmlFilename ) throws Exception
105     {
106         
107         FileInputStream fileInputStream = new FileInputStream( xmlFilename );
108         Document doc = getDocument( fileInputStream );
109         
110         return doc;
111     }
112 }