View Javadoc

1   /*
2    * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
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  /* $Id: MathMLParserSupport.java,v 2bab6eb875e8 2010/08/11 16:45:50 max $ */
18  
19  package net.sourceforge.jeuclid;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.StringReader;
26  
27  import javax.annotation.concurrent.ThreadSafe;
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.transform.stream.StreamSource;
31  
32  import net.sourceforge.jeuclid.parser.Parser;
33  
34  import org.w3c.dom.Document;
35  import org.xml.sax.SAXException;
36  
37  /**
38   * Utility class for the support parsing MathML and OpenDocument Formula (ODF)
39   * files.
40   * <p>
41   * This class supports parsing of files that are either MathML or OpenDocument
42   * Formula (ODF) files. It also supports parsing MathML from a given text
43   * string.
44   * 
45   * @version $Revision: 2bab6eb875e8 $
46   */
47  @ThreadSafe
48  public final class MathMLParserSupport {
49  
50      /**
51       * Logger for this class
52       */
53      // private static final Log LOGGER = LogFactory
54      // .getLog(MathMLParserSupport.class);
55      private MathMLParserSupport() {
56      }
57  
58      /**
59       * Creates a DocumentBuilder that can be used to parse MathML documents
60       * into a standard DOM model.
61       * 
62       * @return a DocumentBuilder instance that is configured for MathML
63       * @throws ParserConfigurationException
64       *             if the builder could not be configured properly.
65       */
66      public static DocumentBuilder createDocumentBuilder()
67              throws ParserConfigurationException {
68          final DocumentBuilder builder = Parser.getInstance()
69                  .getDocumentBuilder();
70          return builder;
71      }
72  
73      /**
74       * Parse an input stream in MathML XML format.
75       * 
76       * @param inStream
77       *            the stream to parse.
78       * @return the DOM Tree
79       * @throws SAXException
80       *             if a parse error occurs.
81       * @throws IOException
82       *             if a read I/O error occurs.
83       */
84      public static Document parseInputStreamXML(final InputStream inStream)
85              throws SAXException, IOException {
86          return Parser.getInstance().parseStreamSourceAsXml(
87                  new StreamSource(inStream));
88      }
89  
90      /**
91       * Parse an input stream in ODF format.
92       * 
93       * @param inStream
94       *            the stream to parse.
95       * @return the DOM Tree
96       * @throws SAXException
97       *             if a parse error occurs.
98       * @throws IOException
99       *             if a read I/O error occurs.
100      */
101     public static Document parseInputStreamODF(final InputStream inStream)
102             throws SAXException, IOException {
103         return Parser.getInstance().parseStreamSourceAsOdf(
104                 new StreamSource(inStream));
105     }
106 
107     /**
108      * Parse an input file and return the DOM tree.
109      * <p>
110      * This function will auto-detect if the given input is in MathML or ODF
111      * format.
112      * 
113      * @param inFile
114      *            the file to parse.
115      * @return the DOM Tree
116      * @throws SAXException
117      *             if a parse error occurs.
118      * @throws IOException
119      *             if a read I/O error occurs.
120      */
121     public static Document parseFile(final File inFile) throws SAXException,
122             IOException {
123         return Parser.getInstance().parseStreamSource(
124                 new StreamSource(new FileInputStream(inFile)));
125     }
126 
127     /**
128      * Create a DOM Document from a given string containing MathML content.
129      * This function uses a DOM Parser configured for MathML.
130      * 
131      * @param content
132      *            A String containing MathML.
133      * @return a DOM Document.
134      * @throws SAXException
135      *             a parsing error occurred.
136      * @throws ParserConfigurationException
137      *             a configuration error occurred.
138      * @throws IOException
139      *             for any other IO exception.
140      */
141     public static Document parseString(final String content)
142             throws SAXException, ParserConfigurationException, IOException {
143         return Parser.getInstance().parseStreamSourceAsXml(
144                 new StreamSource(new StringReader(content)));
145     }
146 
147 }