View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17    Copyright (C) 2004-2007 University Corporation for Advanced Internet Development, Inc.
18    Copyright (C) 2004-2007 The University Of Chicago
19  
20    Licensed under the Apache License, Version 2.0 (the "License");
21    you may not use this file except in compliance with the License.
22    You may obtain a copy of the License at
23  
24      http://www.apache.org/licenses/LICENSE-2.0
25  
26    Unless required by applicable law or agreed to in writing, software
27    distributed under the License is distributed on an "AS IS" BASIS,
28    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29    See the License for the specific language governing permissions and
30    limitations under the License.
31  */
32  
33  package edu.internet2.middleware.grouper.xml;
34  import java.io.ByteArrayInputStream;
35  import java.io.File;
36  import java.io.IOException;
37  import java.net.URL;
38  
39  import javax.xml.parsers.DocumentBuilder;
40  import javax.xml.parsers.DocumentBuilderFactory;
41  import javax.xml.parsers.ParserConfigurationException;
42  
43  import org.w3c.dom.Document;
44  import org.xml.sax.SAXException;
45  
46  import edu.internet2.middleware.grouper.exception.GrouperException;
47  
48  /**
49   * Read XML representation of the Groups Registry.
50   * <p/>
51   * @author  blair christensen.
52   * @version $Id: XmlReader.java,v 1.2 2008-09-29 03:38:30 mchyzer Exp $
53   * @since   1.1.0
54   */
55  public class XmlReader {
56  
57    // PUBLIC CLASS METHODS //
58  
59    /**
60     * Read <tt>Document</tt> from file.
61     * <pre class="eg">
62     * try {
63     *   Document doc = XmlReader.getDocumentFromFile(filename);
64     * }
65     * catch (GrouperException eG) {
66     *   // unable to retrieve document
67     * }
68     * </pre>
69     * @param   filename  Read <tt>Document</tt> from this file.
70     * @throws  GrouperException
71     * @since   1.1.0
72     */
73    public static Document getDocumentFromFile(String filename) 
74      throws  GrouperException
75    {
76      try {
77        return _getDocumentBuilder().parse( new File(filename) );
78      }
79      catch (IOException eIO)   {
80        throw new GrouperException(eIO.getMessage(), eIO);
81      }
82      catch (SAXException eSAX) {
83        throw new GrouperException(eSAX.getMessage(), eSAX);
84      }
85    } // public static Document getDocumentFromFile(filename)
86  
87    /**
88     * Read <tt>Document</tt> from <tt>String</tt>.
89     * <pre class="eg">
90     * try {
91     *   Document doc = XmlReader.getDocumentFromString(s);
92     * }
93     * catch (GrouperException eG) {
94     *   // unable to retrieve document
95     * }
96     * </pre>
97     * @param   s   Read document from this <tt>String</tt>.
98     * @throws  GrouperException
99     * @since   1.1.0
100    */
101   public static Document getDocumentFromString(String s) 
102     throws  GrouperException  
103   {
104     try {
105       return _getDocumentBuilder().parse( new ByteArrayInputStream( s.getBytes() ));
106     }
107     catch (IOException eIO)   {
108       throw new GrouperException(eIO.getMessage(), eIO);
109     }
110     catch (SAXException eSAX) {
111       throw new GrouperException(eSAX.getMessage(), eSAX);
112     }
113   } // public static Document getDocumentFromString(s)
114 
115   /**
116    * Read <tt>Document</tt> from <tt>URL</tt>.
117    * <pre class="eg">
118    * try {
119    * }
120    * catch (GrouperException eG) {
121    * }
122    * </pre>
123    * @param   url   Read <tt>Document</tt> from this <tt>URL</tt>.
124    * @throws  GrouperException
125    * @since   1.1.0
126    */
127   public static Document getDocumentFromURL(URL url) 
128     throws  GrouperException
129   {
130     try {
131       return _getDocumentBuilder().parse( url.openStream() );
132     }
133     catch (IOException eIO)   {
134       throw new GrouperException(eIO.getMessage(), eIO);
135     }
136     catch (SAXException eSAX) {
137       throw new GrouperException(eSAX.getMessage(), eSAX);
138     }
139   } // private static Document _getDocument(url)
140 
141 
142   // PRIVATE CLASS METHODS //
143 
144   // @since   1.1.0
145   private static DocumentBuilder _getDocumentBuilder() 
146     throws  GrouperException
147   {
148     try {
149       return DocumentBuilderFactory.newInstance().newDocumentBuilder();
150     }
151     catch (ParserConfigurationException ePC)  {
152       throw new GrouperException(ePC.getMessage(), ePC);
153     }
154   } // private static DocumentBuilder _getDocumentBuilder()
155 
156 } // public class XmlReader
157