View Javadoc

1   /*
2    * Copyright 2009 - 2010 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 $ */
18  
19  package net.sourceforge.jeuclid.biparser;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  
24  import javax.xml.parsers.ParserConfigurationException;
25  import javax.xml.parsers.SAXParser;
26  import javax.xml.parsers.SAXParserFactory;
27  
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  import org.xml.sax.SAXParseException;
31  import org.xml.sax.helpers.DefaultHandler;
32  
33  /**
34   * this class is creates a SAXParser as singleton.
35   * 
36   * @version $Revision: 0b66106c7ff7 $
37   */
38  public final class SAXBiParser {
39  
40      /** hide constructor. */
41      private SAXBiParser() {
42      }
43  
44      /** make a new SAXBiParser object as singleton. */
45      private static final class SingletonHolder {
46  
47          /** make only one instance of SAXBiParser. */
48          private static final SAXBiParser INSTANCE = new SAXBiParser();
49  
50          /** hide constructor. */
51          private SingletonHolder() {
52          }
53      }
54  
55      /**
56       * get the instance of the SAXParser.
57       * 
58       * @return the singleton instance of the SAXParser
59       */
60      public static SAXBiParser getInstance() {
61          return SAXBiParser.SingletonHolder.INSTANCE;
62      }
63  
64      /**
65       * parse a text with SAXParser.
66       * 
67       * @param text
68       *            inputtext to parse
69       * @return result BiTree of parsed inputtext
70       * @throws NonIncrementalElementException
71       *             if the subtree contains an element which cannot be
72       *             incrementally updated.
73       */
74      public BiTree parse(final String text)
75              throws NonIncrementalElementException {
76          BiTree tree;
77          DefaultHandler handler;
78          SAXParserFactory factory;
79          SAXParser saxParser;
80          StringReader inStream;
81          InputSource inSource;
82  
83          tree = new BiTree();
84          handler = new JEuclidSAXHandler(text, tree);
85          factory = SAXParserFactory.newInstance();
86          inStream = new StringReader(text);
87          inSource = new InputSource(inStream);
88  
89          try {
90              factory.setNamespaceAware(true);
91              factory.setValidating(false);
92  
93              saxParser = factory.newSAXParser();
94              saxParser.parse(inSource, handler);
95          } catch (final NonIncrementalElementException e) {
96              throw e;
97          } catch (final SAXParseException e) {
98              tree = null;
99          } catch (final ParserConfigurationException e) {
100             e.printStackTrace();
101             tree = null;
102         } catch (final SAXException e) {
103             e.printStackTrace();
104             tree = null;
105         } catch (final IOException e) {
106             e.printStackTrace();
107             tree = null;
108         }
109         return tree;
110     }
111 }