11/* Copyright 2013 Pascal Christoph.
22 * Licensed under the Eclipse Public License 1.0 */
33
4- package org .metafacture .biblio ;
4+ package org .metafacture .io ;
55
66import org .metafacture .framework .FluxCommand ;
77import org .metafacture .framework .MetafactureException ;
8- import org .metafacture .framework .ObjectReceiver ;
8+ import org .metafacture .framework .XmlReceiver ;
99import org .metafacture .framework .annotations .Description ;
1010import org .metafacture .framework .annotations .In ;
1111import org .metafacture .framework .annotations .Out ;
1212import 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 ;
1318
1419import java .io .IOException ;
1520import java .io .InputStream ;
16- import java .io .InputStreamReader ;
17- import java .io .Reader ;
1821import java .net .HttpURLConnection ;
1922import java .net .URL ;
23+ import javax .xml .parsers .ParserConfigurationException ;
24+ import javax .xml .parsers .SAXParserFactory ;
2025
2126/**
2227 * Opens an SRU (Search Retrieval by URL) stream and passes a reader to the receiver.
2631 */
2732@ 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." )
2833@ In (String .class )
29- @ Out (Reader .class )
34+ @ Out (XmlReceiver .class )
3035@ FluxCommand ("open-sru" )
31- public final class SruOpener extends DefaultObjectPipe <String , ObjectReceiver < Reader > > {
36+ public final class SruOpener extends DefaultObjectPipe <String , XmlReceiver > {
3237
3338 private static final String OPERATION = "searchRetrieve" ;
3439 private static final String RECORD_SCHEMA = "MARC21-xml" ;
@@ -38,6 +43,7 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
3843 private static final int CONNECTION_TIMEOUT = 11000 ;
3944 private static final int MAXIMUM_RECORDS = 10 ;
4045 private static final int START_RECORD = 1 ;
46+ private final XMLReader saxReader ;
4147
4248 private String operation = OPERATION ;
4349 private String query ;
@@ -56,6 +62,14 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
5662 * Creates an instance of {@link SruOpener}
5763 */
5864 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+ }
5973 }
6074
6175 /**
@@ -171,20 +185,24 @@ private void retrieve(StringBuilder srUrl, int startRecord) throws IOException {
171185 if (!userAgent .isEmpty ()) {
172186 connection .setRequestProperty ("User-Agent" , userAgent );
173187 }
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+ }
178198 System .out .println ("srUrl=" +srUrl );
179199 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
182202 stopRetrieving = true ;
183203 }
184-
185- getReceiver ().process (inputStreamReader );
204+ // getReceiver().process(saxReader);
186205 }
187- }
188206
189207 private InputStream getInputStream (final HttpURLConnection connection ) {
190208 try {
@@ -196,4 +214,19 @@ private InputStream getInputStream(final HttpURLConnection connection) {
196214 }
197215 }
198216
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+
199232}
0 commit comments