1 // $Id: MarcXmlReader.java,v 1.4 2006/07/29 10:58:24 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 java.io.InputStream;
\r
26 import javax.xml.transform.Source;
\r
27 import javax.xml.transform.TransformerConfigurationException;
\r
28 import javax.xml.transform.TransformerFactory;
\r
29 import javax.xml.transform.sax.SAXTransformerFactory;
\r
30 import javax.xml.transform.sax.TransformerHandler;
\r
31 import javax.xml.transform.stream.StreamSource;
\r
33 import org.marc4j.marc.Record;
\r
34 import org.xml.sax.InputSource;
\r
37 * An iterator over a collection of MARC records in MARCXML format.
\r
43 * InputStream input = new FileInputStream("file.xml");
\r
44 * MarcReader reader = new MarcXmlReader(input);
\r
45 * while (reader.hasNext()) {
\r
46 * Record record = reader.next();
\r
51 * <p>Check the {@link org.marc4j.marc} package for examples about the use of
\r
52 * the {@link org.marc4j.marc.Record} object model.</p>
\r
55 * You can also pre-process the source to create MARC XML from a different format
\r
56 * using an XSLT stylesheet. The following example creates an iterator over a
\r
57 * collection of MARC records in MARC XML format from a MODS source and outputs
\r
58 * MARC records in MARC21 format:
\r
62 * InputStream in = new FileInputStream("modsfile.xml");
\r
64 * MarcStreamWriter writer = new MarcStreamWriter(System.out, Constants.MARC8);
\r
65 * MarcXmlReader reader = new MarcXmlReader(in, "http://www.loc.gov/standards/marcxml/xslt/MODS2MARC21slim.xsl");
\r
66 * while (reader.hasNext()) {
\r
67 * Record record = reader.next();
\r
68 * writer.write(record);
\r
73 * @author Bas Peters
\r
74 * @version $Revision: 1.4 $
\r
77 public class MarcXmlReader implements MarcReader {
\r
79 private RecordStack queue;
\r
82 * Constructs an instance with the specified input stream.
\r
87 public MarcXmlReader(InputStream input) {
\r
88 this(new InputSource(input));
\r
92 * Constructs an instance with the specified input source.
\r
97 public MarcXmlReader(InputSource input) {
\r
98 this.queue = new RecordStack();
\r
99 MarcXmlParserThread producer = new MarcXmlParserThread(queue, input);
\r
104 * Constructs an instance with the specified input stream and stylesheet
\r
107 * The stylesheet is used to transform the source file and should produce
\r
108 * valid MARC XML records. The result is then used to create
\r
109 * <code>Record</code> objects.
\r
113 * @param stylesheetUrl
\r
114 * the stylesheet location
\r
116 public MarcXmlReader(InputStream input, String stylesheetUrl) {
\r
117 this(new InputSource(input), new StreamSource(stylesheetUrl));
\r
121 * Constructs an instance with the specified input stream and stylesheet
\r
124 * The stylesheet is used to transform the source file and should produce
\r
125 * valid MARCXML records. The result is then used to create
\r
126 * <code>Record</code> objects.
\r
130 * @param stylesheet
\r
131 * the stylesheet source
\r
133 public MarcXmlReader(InputStream input, Source stylesheet) {
\r
134 this(new InputSource(input), stylesheet);
\r
138 * Constructs an instance with the specified input source and stylesheet
\r
141 * The stylesheet is used to transform the source file and should produce
\r
142 * valid MARCXML records. The result is then used to create
\r
143 * <code>Record</code> objects.
\r
147 * @param stylesheet
\r
148 * the stylesheet source
\r
150 public MarcXmlReader(InputSource input, Source stylesheet) {
\r
151 this.queue = new RecordStack();
\r
152 MarcXmlParserThread producer = new MarcXmlParserThread(queue, input);
\r
153 TransformerFactory factory = TransformerFactory.newInstance();
\r
154 SAXTransformerFactory stf = (SAXTransformerFactory) factory;
\r
155 TransformerHandler th = null;
\r
157 th = stf.newTransformerHandler(stylesheet);
\r
158 } catch (TransformerConfigurationException e) {
\r
159 throw new MarcException("Error creating TransformerHandler", e);
\r
161 producer.setTransformerHandler(th);
\r
166 * Constructs an instance with the specified input stream and transformer
\r
169 * The {@link javax.xml.transform.sax.TransformerHandler} is used to
\r
170 * transform the source file and should produce valid MARCXML records. The
\r
171 * result is then used to create <code>Record</code> objects. A
\r
172 * <code>TransformerHandler</code> can be obtained from a
\r
173 * <code>SAXTransformerFactory</code> with either a
\r
174 * {@link javax.xml.transform.Source} or
\r
175 * {@link javax.xml.transform.Templates} object.
\r
180 * the transformation content handler
\r
182 public MarcXmlReader(InputStream input, TransformerHandler th) {
\r
183 this(new InputSource(input), th);
\r
187 * Constructs an instance with the specified input source and transformer
\r
190 * The {@link javax.xml.transform.sax.TransformerHandler} is used to
\r
191 * transform the source file and should produce valid MARCXML records. The
\r
192 * result is then used to create <code>Record</code> objects. A
\r
193 * <code>TransformerHandler</code> can be obtained from a
\r
194 * <code>SAXTransformerFactory</code> with either a
\r
195 * {@link javax.xml.transform.Source} or
\r
196 * {@link javax.xml.transform.Templates} object.
\r
201 * the transformation content handler
\r
203 public MarcXmlReader(InputSource input, TransformerHandler th) {
\r
204 this.queue = new RecordStack();
\r
205 MarcXmlParserThread producer = new MarcXmlParserThread(queue, input);
\r
206 producer.setTransformerHandler(th);
\r
211 * Returns true if the iteration has more records, false otherwise.
\r
213 * @return boolean - true if the iteration has more records, false otherwise
\r
215 public boolean hasNext() {
\r
216 return queue.hasNext();
\r
220 * Returns the next record in the iteration.
\r
222 * @return Record - the record object
\r
224 public Record next() {
\r
225 return queue.pop();
\r