1
1
/* Copyright 2013 Pascal Christoph.
2
2
* Licensed under the Eclipse Public License 1.0 */
3
3
4
- package org .metafacture .biblio ;
4
+ package org .metafacture .io ;
5
5
6
6
import org .metafacture .framework .FluxCommand ;
7
7
import org .metafacture .framework .MetafactureException ;
8
- import org .metafacture .framework .ObjectReceiver ;
8
+ import org .metafacture .framework .XmlReceiver ;
9
9
import org .metafacture .framework .annotations .Description ;
10
10
import org .metafacture .framework .annotations .In ;
11
11
import org .metafacture .framework .annotations .Out ;
12
12
import org .metafacture .framework .helpers .DefaultObjectPipe ;
13
+ import org .xml .sax .InputSource ;
14
+ import org .xml .sax .SAXException ;
15
+ import org .xml .sax .SAXNotRecognizedException ;
16
+ import org .xml .sax .SAXNotSupportedException ;
17
+ import org .xml .sax .XMLReader ;
13
18
14
19
import java .io .IOException ;
15
20
import java .io .InputStream ;
16
- import java .io .InputStreamReader ;
17
- import java .io .Reader ;
18
21
import java .net .HttpURLConnection ;
19
22
import java .net .URL ;
23
+ import javax .xml .parsers .ParserConfigurationException ;
24
+ import javax .xml .parsers .SAXParserFactory ;
20
25
21
26
/**
22
27
* Opens an SRU (Search Retrieval by URL) stream and passes a reader to the receiver.
26
31
*/
27
32
@ Description ("Opens a SRU stream and passes a reader to the receiver. The input should be the base URL of the SRU service to be retrieved from. Mandatory argument is: QUERY." )
28
33
@ In (String .class )
29
- @ Out (Reader .class )
34
+ @ Out (XmlReceiver .class )
30
35
@ FluxCommand ("open-sru" )
31
- public final class SruOpener extends DefaultObjectPipe <String , ObjectReceiver < Reader > > {
36
+ public final class SruOpener extends DefaultObjectPipe <String , XmlReceiver > {
32
37
33
38
private static final String OPERATION = "searchRetrieve" ;
34
39
private static final String RECORD_SCHEMA = "MARC21-xml" ;
@@ -38,6 +43,7 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
38
43
private static final int CONNECTION_TIMEOUT = 11000 ;
39
44
private static final int MAXIMUM_RECORDS = 10 ;
40
45
private static final int START_RECORD = 1 ;
46
+ private final XMLReader saxReader ;
41
47
42
48
private String operation = OPERATION ;
43
49
private String query ;
@@ -56,6 +62,14 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
56
62
* Creates an instance of {@link SruOpener}
57
63
*/
58
64
public SruOpener () {
65
+ try {
66
+ final SAXParserFactory parserFactory = SAXParserFactory .newInstance ();
67
+ parserFactory .setNamespaceAware (true );
68
+ saxReader = parserFactory .newSAXParser ().getXMLReader ();
69
+ }
70
+ catch (final ParserConfigurationException | SAXException e ) {
71
+ throw new MetafactureException (e );
72
+ }
59
73
}
60
74
61
75
/**
@@ -171,20 +185,24 @@ private void retrieve(StringBuilder srUrl, int startRecord) throws IOException {
171
185
if (!userAgent .isEmpty ()) {
172
186
connection .setRequestProperty ("User-Agent" , userAgent );
173
187
}
174
- InputStream istream = getInputStream (connection );
175
- try (
176
- InputStreamReader inputStreamReader = new InputStreamReader (istream );
177
- ) {
188
+ InputStream inputStream = getInputStream (connection );
189
+ try {
190
+ InputSource inputSource = new InputSource (inputStream );
191
+ saxReader .parse (inputSource );
192
+ // String sr = saxReader.getProperty("huhu").toString();
193
+ // System.out.println(sr);
194
+ }
195
+ catch (final IOException | SAXException e ) {
196
+ throw new MetafactureException (e );
197
+ }
178
198
System .out .println ("srUrl=" +srUrl );
179
199
System .out .println ("startRecord=" +startRecord );
180
- System .out .println ("istream.length=" +istream .available ());
181
- if (istream .available () < 768 ){ // we take it that this is a result without a record
200
+ System .out .println ("istream.length=" +inputStream .available ());
201
+ if (inputStream .available () < 768 ){ // we take it that this is a result without a record
182
202
stopRetrieving = true ;
183
203
}
184
-
185
- getReceiver ().process (inputStreamReader );
204
+ // getReceiver().process(saxReader);
186
205
}
187
- }
188
206
189
207
private InputStream getInputStream (final HttpURLConnection connection ) {
190
208
try {
@@ -196,4 +214,19 @@ private InputStream getInputStream(final HttpURLConnection connection) {
196
214
}
197
215
}
198
216
217
+ private static final String SAX_PROPERTY_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler" ;
218
+ @ Override
219
+ protected void onSetReceiver () {
220
+ saxReader .setContentHandler (getReceiver ());
221
+ saxReader .setDTDHandler (getReceiver ());
222
+ saxReader .setEntityResolver (getReceiver ());
223
+ saxReader .setErrorHandler (getReceiver ());
224
+ try {
225
+ saxReader .setProperty (SAX_PROPERTY_LEXICAL_HANDLER , getReceiver ());
226
+ }
227
+ catch (final SAXNotRecognizedException | SAXNotSupportedException e ) {
228
+ throw new MetafactureException (e );
229
+ }
230
+ }
231
+
199
232
}
0 commit comments