14
14
import org .w3c .dom .Node ;
15
15
import org .xml .sax .SAXException ;
16
16
17
- import java .io .BufferedReader ;
18
17
import java .io .ByteArrayInputStream ;
19
18
import java .io .ByteArrayOutputStream ;
20
19
import java .io .IOException ;
21
20
import java .io .InputStream ;
22
21
import java .io .InputStreamReader ;
23
22
import java .io .Reader ;
23
+ import java .io .StringWriter ;
24
24
import java .net .HttpURLConnection ;
25
25
import java .net .URL ;
26
26
import javax .xml .parsers .DocumentBuilder ;
27
27
import javax .xml .parsers .DocumentBuilderFactory ;
28
28
import javax .xml .parsers .ParserConfigurationException ;
29
- import javax .xml .transform .Result ;
30
29
import javax .xml .transform .Transformer ;
31
30
import javax .xml .transform .TransformerException ;
32
31
import javax .xml .transform .TransformerFactory ;
33
32
import javax .xml .transform .dom .DOMSource ;
34
33
import javax .xml .transform .stream .StreamResult ;
35
34
36
35
/**
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.
38
37
*
39
38
* @author Pascal Christoph (dr0i)
40
39
*/
41
40
@ 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 " +
43
42
"to be retrieved from. Mandatory argument is: QUERY.\n " +
44
43
"The output is an XML document holding the user defined \" maximumRecords\" as documents. If there are" +
45
44
"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 ." )
47
46
@ In (String .class )
48
47
@ Out (java .io .Reader .class )
49
48
@ FluxCommand ("open-sru" )
@@ -162,9 +161,9 @@ public void process(final String baseUrl) {
162
161
StringBuilder srUrl = new StringBuilder (baseUrl );
163
162
if (query != null ) {
164
163
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 {
168
167
throw new IllegalArgumentException ("Missing mandatory parameter 'query'" );
169
168
}
170
169
@@ -182,53 +181,32 @@ private InputStream getXmlDocsViaSru(final StringBuilder srUrl) {
182
181
DocumentBuilder docBuilder = factory .newDocumentBuilder ();
183
182
Document xmldoc = docBuilder .parse (byteArrayInputStream );
184
183
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 ));
204
187
205
- ByteArrayOutputStream os = new ByteArrayOutputStream ();
188
+ numberOfRecords = getIntegerValueFromElement (xmldoc ,"numberOfRecords" );
189
+ int recordPosition = getIntegerValueFromElement (xmldoc ,"recordPosition" );
190
+ int nextRecordPosition = getIntegerValueFromElement (xmldoc ,"nextRecordPosition" );
206
191
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
224
194
195
+ return new ByteArrayInputStream (stringWriter .toString ().getBytes ());
225
196
}
226
- catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
227
- stopRetrieving = true ;
197
+ catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
228
198
throw new MetafactureException (e );
229
199
}
230
200
}
231
201
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
+
232
210
private ByteArrayInputStream retrieve (StringBuilder srUrl , int startRecord , int maximumRecords ) throws IOException {
233
211
final URL urlToOpen =
234
212
new URL (srUrl .toString () + "&maximumRecords=" + maximumRecords + "&startRecord=" + startRecord );
0 commit comments