Original 2.4
[marc4j.git] / src / org / marc4j / MarcXmlParser.java
1 // $Id: MarcXmlParser.java,v 1.2 2006/05/20 09:26:22 bpeters Exp $\r
2 /**\r
3  * Copyright (C) 2004 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\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
11  *\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
16  *\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
20  *\r
21  */\r
22 package org.marc4j;\r
23 \r
24 import javax.xml.parsers.SAXParserFactory;\r
25 import javax.xml.transform.sax.SAXResult;\r
26 import javax.xml.transform.sax.TransformerHandler;\r
27 \r
28 import org.xml.sax.ContentHandler;\r
29 import org.xml.sax.InputSource;\r
30 import org.xml.sax.XMLReader;\r
31 \r
32 /**\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
36  * \r
37  * This class requires a JAXP compliant XML parser and XSLT processor. The\r
38  * underlying SAX2 parser should be namespace aware.\r
39  * \r
40  * @author Bas Peters\r
41  * @version $Revision: 1.2 $\r
42  */\r
43 public class MarcXmlParser {\r
44 \r
45     private ContentHandler handler = null;\r
46 \r
47     /**\r
48      * Default constructor.\r
49      * \r
50      * @param handler\r
51      *            the <code>MarcXmlHandler</code> object\r
52      */\r
53     public MarcXmlParser(MarcXmlHandler handler) {\r
54         this.handler = handler;\r
55     }\r
56 \r
57     /**\r
58      * Calls the parser.\r
59      * \r
60      * @param input\r
61      *            the input source\r
62      */\r
63     public void parse(InputSource input) {\r
64         parse(handler, input);\r
65     }\r
66 \r
67     /**\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
70      * \r
71      * @param input\r
72      *            the input source\r
73      * @param th\r
74      *            the transformation content handler\r
75      */\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
80         parse(th, input);\r
81 \r
82     }\r
83 \r
84     private void parse(ContentHandler handler, InputSource input) {\r
85         SAXParserFactory spf = SAXParserFactory.newInstance();\r
86         XMLReader reader = null;\r
87         try {\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
91                     true);\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
96         }\r
97     }\r
98 \r
99 }