001    /*
002     * Copyright 2009 - 2010 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id $ */
018    
019    package net.sourceforge.jeuclid.biparser;
020    
021    import java.io.IOException;
022    import java.io.StringReader;
023    
024    import javax.xml.parsers.ParserConfigurationException;
025    import javax.xml.parsers.SAXParser;
026    import javax.xml.parsers.SAXParserFactory;
027    
028    import org.xml.sax.InputSource;
029    import org.xml.sax.SAXException;
030    import org.xml.sax.SAXParseException;
031    import org.xml.sax.helpers.DefaultHandler;
032    
033    /**
034     * this class is creates a SAXParser as singleton.
035     * 
036     * @version $Revision: 0b66106c7ff7 $
037     */
038    public final class SAXBiParser {
039    
040        /** hide constructor. */
041        private SAXBiParser() {
042        }
043    
044        /** make a new SAXBiParser object as singleton. */
045        private static final class SingletonHolder {
046    
047            /** make only one instance of SAXBiParser. */
048            private static final SAXBiParser INSTANCE = new SAXBiParser();
049    
050            /** hide constructor. */
051            private SingletonHolder() {
052            }
053        }
054    
055        /**
056         * get the instance of the SAXParser.
057         * 
058         * @return the singleton instance of the SAXParser
059         */
060        public static SAXBiParser getInstance() {
061            return SAXBiParser.SingletonHolder.INSTANCE;
062        }
063    
064        /**
065         * parse a text with SAXParser.
066         * 
067         * @param text
068         *            inputtext to parse
069         * @return result BiTree of parsed inputtext
070         * @throws NonIncrementalElementException
071         *             if the subtree contains an element which cannot be
072         *             incrementally updated.
073         */
074        public BiTree parse(final String text)
075                throws NonIncrementalElementException {
076            BiTree tree;
077            DefaultHandler handler;
078            SAXParserFactory factory;
079            SAXParser saxParser;
080            StringReader inStream;
081            InputSource inSource;
082    
083            tree = new BiTree();
084            handler = new JEuclidSAXHandler(text, tree);
085            factory = SAXParserFactory.newInstance();
086            inStream = new StringReader(text);
087            inSource = new InputSource(inStream);
088    
089            try {
090                factory.setNamespaceAware(true);
091                factory.setValidating(false);
092    
093                saxParser = factory.newSAXParser();
094                saxParser.parse(inSource, handler);
095            } catch (final NonIncrementalElementException e) {
096                throw e;
097            } catch (final SAXParseException e) {
098                tree = null;
099            } 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    }