Original 2.4
[marc4j.git] / src / org / marc4j / MarcXmlParserThread.java
1 // $Id: MarcXmlParserThread.java,v 1.3 2008/09/26 21:17:42 haschart 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 modify it under the\r
8  * terms of the GNU Lesser General Public License as published by the Free\r
9  * Software Foundation; either version 2.1 of the License, or (at your option)\r
10  * any later version.\r
11  * \r
12  * MARC4J is distributed in the hope that it will be useful, but WITHOUT ANY\r
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\r
14  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
15  * details.\r
16  * \r
17  * You should have received a copy of the GNU Lesser General Public License\r
18  * along with MARC4J; if not, write to the Free Software Foundation, Inc., 59\r
19  * 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.transform.sax.TransformerHandler;\r
25 \r
26 import org.xml.sax.InputSource;\r
27 \r
28 /**\r
29  * Extends <code>Thread</code> to produce <code>Record</code> objects from\r
30  * MARCXML data.\r
31  * \r
32  * @author Bas Peters\r
33  * @version $Revision: 1.3 $\r
34  */\r
35 public class MarcXmlParserThread extends Thread {\r
36 \r
37     private RecordStack queue;\r
38 \r
39     private InputSource input;\r
40 \r
41     private TransformerHandler th = null;\r
42 \r
43     /**\r
44      * Creates a new instance and registers the <code>RecordQueue</code>.\r
45      * \r
46      * @param queue\r
47      *            the record queue\r
48      */\r
49     public MarcXmlParserThread(RecordStack queue) {\r
50         this.queue = queue;\r
51     }\r
52 \r
53     /**\r
54      * Creates a new instance and registers the <code>RecordQueue</code> and\r
55      * the <code>InputStream</code>.\r
56      * \r
57      * @param queue\r
58      *            the record queue\r
59      * @param input\r
60      *            the input stream\r
61      */\r
62     public MarcXmlParserThread(RecordStack queue, InputSource input) {\r
63         this.queue = queue;\r
64         this.input = input;\r
65     }\r
66 \r
67     /**\r
68      * Returns the content handler to transform the source to MARCXML.\r
69      * \r
70      * @return TransformerHandler - the transformation content handler\r
71      */\r
72     public TransformerHandler getTransformerHandler() {\r
73         return th;\r
74     }\r
75 \r
76     /**\r
77      * Sets the content handler to transform the source to MARCXML.\r
78      * \r
79      * @param th -\r
80      *            the transformation content handler\r
81      */\r
82     public void setTransformerHandler(TransformerHandler th) {\r
83         this.th = th;\r
84     }\r
85 \r
86     /**\r
87      * Returns the input stream.\r
88      * \r
89      * @return InputSource - the input source\r
90      */\r
91     public InputSource getInputSource() {\r
92         return input;\r
93     }\r
94 \r
95     /**\r
96      * Sets the input stream.\r
97      * \r
98      * @param input\r
99      *            the input stream\r
100      */\r
101     public void setInputSource(InputSource input) {\r
102         this.input = input;\r
103     }\r
104 \r
105     /**\r
106      * Creates a new <code>MarcXmlHandler</code> instance, registers the\r
107      * <code>RecordQueue</code> and sends the <code>InputStream</code> to\r
108      * the <code>MarcXmlParser</code> parser.\r
109      */\r
110     public void run() {\r
111         try {\r
112             MarcXmlHandler handler = new MarcXmlHandler(queue);\r
113             MarcXmlParser parser = new MarcXmlParser(handler);\r
114             if (th == null)\r
115                 parser.parse(input);\r
116             else\r
117                 parser.parse(input, th);\r
118         } \r
119         catch (MarcException me)\r
120         {\r
121             queue.passException(me);\r
122         }\r
123         finally {\r
124             queue.end();\r
125         }\r
126     }\r
127 \r
128 }