1414import org .w3c .dom .Node ;
1515import org .xml .sax .SAXException ;
1616
17- import java .io .BufferedReader ;
1817import java .io .ByteArrayInputStream ;
1918import java .io .ByteArrayOutputStream ;
2019import java .io .IOException ;
2120import java .io .InputStream ;
2221import java .io .InputStreamReader ;
2322import java .io .Reader ;
23+ import java .io .StringWriter ;
2424import java .net .HttpURLConnection ;
2525import java .net .URL ;
2626import javax .xml .parsers .DocumentBuilder ;
2727import javax .xml .parsers .DocumentBuilderFactory ;
2828import javax .xml .parsers .ParserConfigurationException ;
29- import javax .xml .transform .Result ;
3029import javax .xml .transform .Transformer ;
3130import javax .xml .transform .TransformerException ;
3231import javax .xml .transform .TransformerFactory ;
3332import javax .xml .transform .dom .DOMSource ;
3433import javax .xml .transform .stream .StreamResult ;
3534
3635/**
37- * Opens an SRU (Search Retrieval by URL) stream and passes a reader to the receiver.
36+ * Opens an SRU (Search Retrieval by URL) stream and passes a reader to the receiver. Pages through the SRU.
3837 *
3938 * @author Pascal Christoph (dr0i)
4039 */
4140@ Description (
42- "Opens a SRU stream and passes a reader to the receiver. The input is be the base URL of the SRU service " +
41+ "Opens a SRU stream and passes a reader to the receiver. The input is the base URL of the SRU service " +
4342 "to be retrieved from. Mandatory argument is: QUERY.\n " +
4443 "The output is an XML document holding the user defined \" maximumRecords\" as documents. If there are" +
4544 "more documents than defined by MAXIMUM_RECORDS and there are more documents wanted (defined by " +
46- "\" totalRecords\" ) there will be consecutive XML documents output." )
45+ "\" totalRecords\" ) there will be consecutive XML documents output as it pages through the SRU ." )
4746@ In (String .class )
4847@ Out (java .io .Reader .class )
4948@ FluxCommand ("open-sru" )
@@ -162,9 +161,9 @@ public void process(final String baseUrl) {
162161 StringBuilder srUrl = new StringBuilder (baseUrl );
163162 if (query != null ) {
164163 srUrl .append ("?query=" ).append (query ).append ("&operation=" ).append (operation ).append ("&recordSchema=" )
165- .append (recordSchema ).append ("&version=" ).append (version );
166- } else {
167- stopRetrieving = true ;
164+ .append (recordSchema ).append ("&version=" ).append (version );
165+ }
166+ else {
168167 throw new IllegalArgumentException ("Missing mandatory parameter 'query'" );
169168 }
170169
@@ -182,53 +181,32 @@ private InputStream getXmlDocsViaSru(final StringBuilder srUrl) {
182181 DocumentBuilder docBuilder = factory .newDocumentBuilder ();
183182 Document xmldoc = docBuilder .parse (byteArrayInputStream );
184183
185- Node node = xmldoc .getElementsByTagName ("numberOfRecords" ).item (0 );
186- if (node != null ) {
187- numberOfRecords = Integer .parseInt (node .getTextContent ());
188- }
189-
190- int recordPosition =0 ;
191- node = xmldoc .getElementsByTagName ("recordPosition" ).item (0 );
192- if (node != null ) {
193- recordPosition = Integer .parseInt (node .getTextContent ());
194- }
195- int nextRecordPosition =recordPosition +1 ;
196- node = xmldoc .getElementsByTagName ("nextRecordPosition" ).item (0 );
197- if (node != null ) {
198- nextRecordPosition = Integer .parseInt (node .getTextContent ());
199- }
200- String xmlEncoding = xmldoc .getXmlEncoding ();
201- String xmlVersion = xmldoc .getXmlVersion ();
202- xmlDeclaration = String .format (xmlDeclarationTemplate , xmldoc .getXmlVersion (), xmldoc .getXmlEncoding ());
203- recordsRetrieved = recordsRetrieved + nextRecordPosition - recordPosition ;
184+ Transformer t = TransformerFactory .newInstance ().newTransformer ();
185+ StringWriter stringWriter = new StringWriter ();
186+ t .transform (new DOMSource (xmldoc ), new StreamResult (stringWriter ));
204187
205- ByteArrayOutputStream os = new ByteArrayOutputStream ();
188+ numberOfRecords = getIntegerValueFromElement (xmldoc ,"numberOfRecords" );
189+ int recordPosition = getIntegerValueFromElement (xmldoc ,"recordPosition" );
190+ int nextRecordPosition = getIntegerValueFromElement (xmldoc ,"nextRecordPosition" );
206191
207- Result result = new StreamResult (os );
208- Transformer t = TransformerFactory .newInstance ().newTransformer ();
209- t .setOutputProperty ("omit-xml-declaration" , "yes" );
210- t .transform (new DOMSource (xmldoc ), result );
211-
212- ByteArrayInputStream inputStream = new ByteArrayInputStream (os .toByteArray ());
213- startRecord = startRecord + maximumRecords ;
214-
215- //get searchRetrieveResponse and add XML declaration
216- BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream ));
217- String line ;
218- StringBuilder stringBuilder = new StringBuilder (1024 * 1024 );
219- stringBuilder .append (xmlDeclaration + "\n " );
220- while ((line = bufferedReader .readLine ()) != null ) {
221- stringBuilder .append (line + "\n " );
222- }
223- return new ByteArrayInputStream (stringBuilder .toString ().getBytes ());
192+ recordsRetrieved = recordsRetrieved + nextRecordPosition - recordPosition ;
193+ startRecord = nextRecordPosition ; // grenzwert : wenn maximumRcords > als in echt
224194
195+ return new ByteArrayInputStream (stringWriter .toString ().getBytes ());
225196 }
226- catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
227- stopRetrieving = true ;
197+ catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
228198 throw new MetafactureException (e );
229199 }
230200 }
231201
202+ private int getIntegerValueFromElement (final Document xmlDoc , final String tagName ) {
203+ Node node = xmlDoc .getElementsByTagName (tagName ).item (0 );
204+ if (node != null ) {
205+ return Integer .parseInt (node .getTextContent ());
206+ }
207+ return 0 ;
208+ }
209+
232210 private ByteArrayInputStream retrieve (StringBuilder srUrl , int startRecord , int maximumRecords ) throws IOException {
233211 final URL urlToOpen =
234212 new URL (srUrl .toString () + "&maximumRecords=" + maximumRecords + "&startRecord=" + startRecord );
0 commit comments