1 // $Id: MarcXmlParser.java,v 1.2 2006/05/20 09:26:22 bpeters Exp $
\r
3 * Copyright (C) 2004 Bas Peters
\r
5 * This file is part of MARC4J
\r
7 * MARC4J is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU Lesser General Public
\r
9 * License as published by the Free Software Foundation; either
\r
10 * version 2.1 of the License, or (at your option) any later version.
\r
12 * MARC4J is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
15 * Lesser General Public License for more details.
\r
17 * You should have received a copy of the GNU Lesser General Public
\r
18 * License along with MARC4J; if not, write to the Free Software
\r
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
24 import javax.xml.parsers.SAXParserFactory;
\r
25 import javax.xml.transform.sax.SAXResult;
\r
26 import javax.xml.transform.sax.TransformerHandler;
\r
28 import org.xml.sax.ContentHandler;
\r
29 import org.xml.sax.InputSource;
\r
30 import org.xml.sax.XMLReader;
\r
33 * Creates <code>Record</code> objects from SAX events and pushes each item
\r
34 * onto the top of the <code>RecordStack</code>. Used by
\r
35 * <code>MarcXmlParserThread</code>.
\r
37 * This class requires a JAXP compliant XML parser and XSLT processor. The
\r
38 * underlying SAX2 parser should be namespace aware.
\r
40 * @author Bas Peters
\r
41 * @version $Revision: 1.2 $
\r
43 public class MarcXmlParser {
\r
45 private ContentHandler handler = null;
\r
48 * Default constructor.
\r
51 * the <code>MarcXmlHandler</code> object
\r
53 public MarcXmlParser(MarcXmlHandler handler) {
\r
54 this.handler = handler;
\r
63 public void parse(InputSource input) {
\r
64 parse(handler, input);
\r
68 * Calls the parser and tries to transform the source into MARCXML using the
\r
69 * given stylesheet source before creating <code>Record</code> objects.
\r
74 * the transformation content handler
\r
76 public void parse(InputSource input, TransformerHandler th) {
\r
77 SAXResult result = new SAXResult();
\r
78 result.setHandler(handler);
\r
79 th.setResult(result);
\r
84 private void parse(ContentHandler handler, InputSource input) {
\r
85 SAXParserFactory spf = SAXParserFactory.newInstance();
\r
86 XMLReader reader = null;
\r
88 reader = spf.newSAXParser().getXMLReader();
\r
89 reader.setFeature("http://xml.org/sax/features/namespaces", true);
\r
90 reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
\r
92 reader.setContentHandler(handler);
\r
93 reader.parse(input);
\r
94 } catch (Exception e) {
\r
95 throw new MarcException("Unable to parse input", e);
\r