View Javadoc

1   package com.explosion.utilities;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.io.IOException;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.ParserConfigurationException;
28  
29  import org.w3c.dom.Document;
30  import org.xml.sax.SAXException;
31  
32  /***
33   * @author Stephen Cowx Date created:@31-Jan-2003
34   */
35  public class XMLUtils
36  {
37  
38      public static Document readFileIntoDOM(String filePath, boolean validate, boolean nameSpaceAware)
39      {
40  
41          Document document = null;
42          FileSystemUtils.checkGivenPathValid(filePath);
43  
44          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45  
46          factory.setValidating(validate);
47          factory.setNamespaceAware(nameSpaceAware);
48  
49          try
50          {
51              DocumentBuilder builder = factory.newDocumentBuilder();
52              document = builder.parse(filePath);
53          } catch (SAXException sxe)
54          {
55              // Error generated during parsing)
56              Exception x = sxe;
57              if (sxe.getException() != null) x = sxe.getException();
58  
59              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(x, null);
60          } catch (ParserConfigurationException pce)
61          {
62              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(pce, null);
63          } catch (IOException ioe)
64          {
65              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ioe, null);
66          }
67  
68          return document;
69      }
70  }