1010import org .metafacture .framework .annotations .In ;
1111import org .metafacture .framework .annotations .Out ;
1212import org .metafacture .framework .helpers .DefaultObjectPipe ;
13+
1314import org .w3c .dom .Document ;
1415import org .w3c .dom .Node ;
1516import org .xml .sax .SAXException ;
@@ -55,20 +56,21 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
5556 private static final int CONNECTION_TIMEOUT = 11000 ;
5657 private static final int MAXIMUM_RECORDS = 10 ;
5758 private static final int START_RECORD = 1 ;
58- private String operation = OPERATION ;
59- private String query ;
60- private String recordSchema = RECORD_SCHEMA ;
61- private String userAgent = USER_AGENT ;
62- private String version = VERSION ;
59+
60+ private boolean stopRetrieving ;
6361
6462 private int maximumRecords = MAXIMUM_RECORDS ;
6563 private int startRecord = START_RECORD ;
6664 private int totalRecords = Integer .MAX_VALUE ;
67- int numberOfRecords = Integer .MAX_VALUE ;
68-
69- private boolean stopRetrieving ;
65+ private int numberOfRecords = Integer .MAX_VALUE ;
7066 private int recordsRetrieved ;
7167
68+ private String operation = OPERATION ;
69+ private String query ;
70+ private String recordSchema = RECORD_SCHEMA ;
71+ private String userAgent = USER_AGENT ;
72+ private String version = VERSION ;
73+
7274 private String xmlDeclarationTemplate = "<?xml version=\" %s\" encoding=\" %s\" ?>" ;
7375 private String xmlDeclaration ;
7476
@@ -102,10 +104,10 @@ public void setQuery(final String query) {
102104 * Sets total number of records to be retrieved. <strong>Default value: indefinite (as in "all")
103105 * </strong>.
104106 *
105- * @param totalRecords total number of records to be retrieved
107+ * @param totalrecords total number of records to be retrieved
106108 */
107- public void setTotal (final String totalRecords ) {
108- this .totalRecords = Integer .parseInt (totalRecords );
109+ public void setTotal (final String totalrecords ) {
110+ this .totalRecords = Integer .parseInt (totalrecords );
109111 }
110112
111113 /**
@@ -157,7 +159,7 @@ public void setVersion(final String version) {
157159 @ Override
158160 public void process (final String baseUrl ) {
159161
160- StringBuilder srUrl = new StringBuilder (baseUrl );
162+ final StringBuilder srUrl = new StringBuilder (baseUrl );
161163 if (query != null ) {
162164 srUrl .append ("?query=" ).append (query ).append ("&operation=" ).append (operation ).append ("&recordSchema=" )
163165 .append (recordSchema ).append ("&version=" ).append (version );
@@ -166,27 +168,27 @@ public void process(final String baseUrl) {
166168 throw new IllegalArgumentException ("Missing mandatory parameter 'query'" );
167169 }
168170
169- while (!stopRetrieving && recordsRetrieved < totalRecords && ( startRecord < numberOfRecords ) ) {
170- InputStream inputStream = getXmlDocsViaSru (srUrl );
171+ while (!stopRetrieving && recordsRetrieved < totalRecords && startRecord < numberOfRecords ) {
172+ final InputStream inputStream = getXmlDocsViaSru (srUrl );
171173 getReceiver ().process (new InputStreamReader (inputStream ));
172174 }
173175
174176 }
175177
176178 private InputStream getXmlDocsViaSru (final StringBuilder srUrl ) {
177179 try {
178- InputStream inputStreamOfURl = retrieveUrl (srUrl , startRecord , maximumRecords );
179- DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
180- DocumentBuilder docBuilder = factory .newDocumentBuilder ();
181- Document xmldoc = docBuilder .parse (inputStreamOfURl );
180+ final InputStream inputStreamOfURl = retrieveUrl (srUrl , startRecord , maximumRecords );
181+ final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
182+ final DocumentBuilder docBuilder = factory .newDocumentBuilder ();
183+ final Document xmldoc = docBuilder .parse (inputStreamOfURl );
182184
183- Transformer t = TransformerFactory .newInstance ().newTransformer ();
184- StringWriter stringWriter = new StringWriter ();
185+ final Transformer t = TransformerFactory .newInstance ().newTransformer ();
186+ final StringWriter stringWriter = new StringWriter ();
185187 t .transform (new DOMSource (xmldoc ), new StreamResult (stringWriter ));
186188
187- numberOfRecords = getIntegerValueFromElement (xmldoc ,"numberOfRecords" , 0 );
188- int recordPosition = getIntegerValueFromElement (xmldoc ,"recordPosition" , 0 );
189- int nextRecordPosition = getIntegerValueFromElement (xmldoc ,"nextRecordPosition" , totalRecords );
189+ numberOfRecords = getIntegerValueFromElement (xmldoc , "numberOfRecords" , 0 );
190+ final int recordPosition = getIntegerValueFromElement (xmldoc , "recordPosition" , 0 );
191+ final int nextRecordPosition = getIntegerValueFromElement (xmldoc , "nextRecordPosition" , totalRecords );
190192
191193 recordsRetrieved = recordsRetrieved + nextRecordPosition - recordPosition ;
192194 startRecord = nextRecordPosition ; // grenzwert : wenn maximumRcords > als in echt
@@ -200,23 +202,23 @@ private InputStream getXmlDocsViaSru(final StringBuilder srUrl) {
200202 }
201203
202204 private int getIntegerValueFromElement (final Document xmlDoc , final String tagName , final int fallback ) {
203- Node node = xmlDoc .getElementsByTagName (tagName ).item (0 );
205+ final Node node = xmlDoc .getElementsByTagName (tagName ).item (0 );
204206 if (node != null ) {
205207 return Integer .parseInt (node .getTextContent ());
206208 }
207209 return fallback ;
208210 }
209211
210- private InputStream retrieveUrl (StringBuilder srUrl , int startRecord , int maximumRecords ) throws IOException {
212+ private InputStream retrieveUrl (final StringBuilder srUrl , final int startrecord , final int maximumrecords ) throws IOException {
211213 final URL urlToOpen =
212- new URL (srUrl .toString () + "&maximumRecords=" + maximumRecords + "&startRecord=" + startRecord );
214+ new URL (srUrl .toString () + "&maximumRecords=" + maximumrecords + "&startRecord=" + startrecord );
213215 final HttpURLConnection connection = (HttpURLConnection ) urlToOpen .openConnection ();
214216
215217 connection .setConnectTimeout (CONNECTION_TIMEOUT );
216218 if (!userAgent .isEmpty ()) {
217219 connection .setRequestProperty ("User-Agent" , userAgent );
218220 }
219- InputStream inputStream = getInputStream (connection );
221+ final InputStream inputStream = getInputStream (connection );
220222
221223 return inputStream ;
222224 }
0 commit comments