001/*
002// $Id: //open/util/resgen/src/org/eigenbase/xom/wrappers/JaxpDOMParser.java#3 $
003// Package org.eigenbase.xom is an XML Object Mapper.
004// Copyright (C) 2005-2005 The Eigenbase Project
005// Copyright (C) 2005-2005 Disruptive Tech
006// Copyright (C) 2005-2005 LucidEra, Inc.
007// Portions Copyright (C) 2001-2005 Kana Software, Inc. and others.
008//
009// This library is free software; you can redistribute it and/or modify it
010// under the terms of the GNU Lesser General Public License as published by the
011// Free Software Foundation; either version 2 of the License, or (at your
012// option) any later version approved by The Eigenbase Project.
013//
014// This library is distributed in the hope that it will be useful, 
015// but WITHOUT ANY WARRANTY; without even the implied warranty of
016// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017// GNU Lesser General Public License for more details.
018// 
019// You should have received a copy of the GNU Lesser General Public License
020// along with this library; if not, write to the Free Software
021// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022*/
023
024package org.eigenbase.xom.wrappers;
025
026import org.eigenbase.xom.XOMException;
027import org.w3c.dom.Document;
028import org.xml.sax.InputSource;
029import org.xml.sax.SAXException;
030
031import javax.xml.parsers.DocumentBuilder;
032import javax.xml.parsers.DocumentBuilderFactory;
033import javax.xml.parsers.FactoryConfigurationError;
034import javax.xml.parsers.ParserConfigurationException;
035import java.io.IOException;
036
037/**
038 * A <code>JaxpDOMParser</code> implements {@link org.eigenbase.xom.Parser} using
039 * a {@link DocumentBuilder JAXP-compliant parser}.
040 *
041 * @author jhyde
042 * @since Aug 29, 2002
043 * @version $Id: //open/util/resgen/src/org/eigenbase/xom/wrappers/JaxpDOMParser.java#3 $
044 **/
045public class JaxpDOMParser extends GenericDOMParser {
046    private DocumentBuilder builder;
047
048    /** Creates a non-validating parser. **/
049    public JaxpDOMParser() throws XOMException {
050        this(false);
051    }
052
053    /** Creates a parser. **/
054    public JaxpDOMParser(boolean validating) throws XOMException {
055        try {
056            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
057            factory.setValidating(validating);
058            try {
059                factory.setAttribute(VALIDATION_FEATURE, new Boolean(validating));
060                factory.setAttribute(LOAD_EXTERNAL_DTD_FEATURE, new Boolean(validating));
061            } catch (IllegalArgumentException e) {
062                // Weblogic 6.1's parser complains 'No arguments are
063                // implemented'
064            }
065            builder = factory.newDocumentBuilder();
066        } catch (ParserConfigurationException e) {
067            throw new XOMException(e, "Error creating parser");
068        } catch (FactoryConfigurationError e) {
069            throw new XOMException(e, "Error creating parser");
070        }
071        builder.setErrorHandler(this);
072        document = builder.newDocument();
073    }
074
075    protected Document parseInputSource(InputSource in) throws XOMException {
076        prepareParse();
077        try {
078            Document document = builder.parse(in);
079            handleErrors();
080            return document;
081        } catch (SAXException e) {
082            // Display any pending errors
083            handleErrors();
084            throw new XOMException(e, "Document parse failed");
085        } catch (IOException e) {
086            // Display any pending errors
087            handleErrors();
088            throw new XOMException(e, "Document parse failed");
089        }
090    }
091}
092
093// End JaxpDOMParser.java