3333import java .io .InputStreamReader ;
3434import java .io .Reader ;
3535import java .io .StringWriter ;
36- import java .io .UnsupportedEncodingException ;
3736import java .net .HttpURLConnection ;
3837import java .net .URL ;
3938import java .net .URLEncoder ;
@@ -77,18 +76,13 @@ public final class SruOpener extends DefaultObjectPipe<String, ObjectReceiver<Re
7776 private int maximumRecords = MAXIMUM_RECORDS ;
7877 private int startRecord = START_RECORD ;
7978 private int totalRecords = Integer .MAX_VALUE ;
80- private int numberOfRecords = Integer .MAX_VALUE ;
81- private int recordsRetrieved ;
8279
8380 private String operation = OPERATION ;
8481 private String query ;
8582 private String recordSchema = RECORD_SCHEMA ;
8683 private String userAgent = USER_AGENT ;
8784 private String version = VERSION ;
8885
89- private String xmlDeclarationTemplate = "<?xml version=\" %s\" encoding=\" %s\" ?>" ;
90- private String xmlDeclaration ;
91-
9286 /**
9387 * Default constructor
9488 */
@@ -98,10 +92,10 @@ public SruOpener() {
9892 /**
9993 * Sets the User Agent to use. <strong>Default value: {@value USER_AGENT}</strong>.
10094 *
101- * @param userAgent a user agent to be used when opening a URL
95+ * @param useragent a user agent to be used when opening a URL
10296 */
103- public void setUserAgent (final String userAgent ) {
104- this . userAgent = userAgent ;
97+ public void setUserAgent (final String useragent ) {
98+ userAgent = useragent ;
10599 }
106100
107101 /**
@@ -112,12 +106,7 @@ public void setUserAgent(final String userAgent) {
112106 */
113107
114108 public void setQuery (final String query ) {
115- try {
116- this .query = URLEncoder .encode (query , StandardCharsets .UTF_8 .toString ());
117- }
118- catch (final UnsupportedEncodingException e ) {
119- throw new MetafactureException (e );
120- }
109+ this .query = URLEncoder .encode (query , StandardCharsets .UTF_8 );
121110 }
122111
123112 /**
@@ -127,26 +116,26 @@ public void setQuery(final String query) {
127116 * @param totalrecords total number of records to be retrieved
128117 */
129118 public void setTotal (final int totalrecords ) {
130- this . totalRecords = totalrecords ;
119+ totalRecords = totalrecords ;
131120 }
132121
133122 /**
134123 * Sets the maximum of records returned in one lookup. <strong>Default value: {@value MAXIMUM_RECORDS}</strong>.
135124 * The lookup is repeated as long as {@link #maximumRecords} is less than {@link #totalRecords}.
136125 *
137- * @param maximumRecords maximum of records returned in one lookup
126+ * @param maximumrecords maximum of records returned in one lookup
138127 */
139- public void setMaximumRecords (final int maximumRecords ) {
140- this . maximumRecords = maximumRecords ;
128+ public void setMaximumRecords (final int maximumrecords ) {
129+ maximumRecords = maximumrecords ;
141130 }
142131
143132 /**
144133 * Sets where to start when retrieving records. <strong>Default value: {@value START_RECORD}</strong>.
145134 *
146- * @param startRecord where to start when retrieving records
135+ * @param startrecord where to start when retrieving records
147136 */
148- public void setStartRecord (final int startRecord ) {
149- this . startRecord = startRecord ;
137+ public void setStartRecord (final int startrecord ) {
138+ startRecord = startrecord ;
150139 }
151140
152141 /**
@@ -187,41 +176,36 @@ public void process(final String baseUrl) {
187176 else {
188177 throw new IllegalArgumentException ("Missing mandatory parameter 'query'" );
189178 }
190-
179+ int recordsRetrieved = 0 ;
180+ int numberOfRecords = Integer .MAX_VALUE ;
191181 while (!stopRetrieving && recordsRetrieved < totalRecords && startRecord < numberOfRecords ) {
192- try (final InputStream inputStream = getXmlDocsViaSru (srUrl )) {
193- getReceiver ().process (new InputStreamReader (inputStream ));
194- } catch (Exception e ) {
195- throw new MetafactureException (e );
196- }
197- }
198-
199- }
200-
201- private InputStream getXmlDocsViaSru (final StringBuilder srUrl ) {
202- try {
203- final InputStream inputStreamOfURl = retrieveUrl (srUrl , startRecord , maximumRecords );
204- final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
205- final DocumentBuilder docBuilder = factory .newDocumentBuilder ();
206- final Document xmldoc = docBuilder .parse (inputStreamOfURl );
207182
208- final Transformer t = TransformerFactory .newInstance ().newTransformer ();
209- final StringWriter stringWriter = new StringWriter ();
210- t .transform (new DOMSource (xmldoc ), new StreamResult (stringWriter ));
183+ try {
184+ final InputStream inputStreamOfURl = retrieveUrl (srUrl );
185+ final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
186+ final DocumentBuilder docBuilder = factory .newDocumentBuilder ();
187+ final Document xmldoc = docBuilder .parse (inputStreamOfURl );
211188
212- numberOfRecords = getIntegerValueFromElement ( xmldoc , "numberOfRecords" , 0 );
213- final int recordPosition = getIntegerValueFromElement ( xmldoc , "recordPosition" , 0 );
214- final int nextRecordPosition = getIntegerValueFromElement (xmldoc , "nextRecordPosition" , totalRecords );
189+ final Transformer t = TransformerFactory . newInstance (). newTransformer ( );
190+ final StringWriter stringWriter = new StringWriter ( );
191+ t . transform ( new DOMSource (xmldoc ), new StreamResult ( stringWriter ) );
215192
216- recordsRetrieved = recordsRetrieved + nextRecordPosition - recordPosition ;
217- startRecord = nextRecordPosition ; // grenzwert : wenn maximumRcords > als in echt
193+ numberOfRecords = getIntegerValueFromElement (xmldoc , "numberOfRecords" , 0 );
194+ final int recordPosition = getIntegerValueFromElement (xmldoc , "recordPosition" , 0 );
195+ final int nextRecordPosition = getIntegerValueFromElement (xmldoc , "nextRecordPosition" , totalRecords );
218196
219- return new ByteArrayInputStream (stringWriter .toString ().getBytes ());
197+ recordsRetrieved = recordsRetrieved + nextRecordPosition - recordPosition ;
198+ startRecord = nextRecordPosition ;
220199
200+ try (InputStream inputStream = new ByteArrayInputStream (stringWriter .toString ().getBytes ())) {
201+ getReceiver ().process (new InputStreamReader (inputStream ));
202+ }
203+ }
204+ catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
205+ throw new MetafactureException (e );
206+ }
221207 }
222- catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
223- throw new MetafactureException (e );
224- }
208+
225209 }
226210
227211 private int getIntegerValueFromElement (final Document xmlDoc , final String tagName , final int fallback ) {
@@ -232,18 +216,16 @@ private int getIntegerValueFromElement(final Document xmlDoc, final String tagNa
232216 return fallback ;
233217 }
234218
235- private InputStream retrieveUrl (final StringBuilder srUrl , final int startrecord , final int maximumrecords ) throws IOException {
219+ private InputStream retrieveUrl (final StringBuilder srUrl ) throws IOException {
236220 final URL urlToOpen =
237- new URL (srUrl .toString () + "&maximumRecords=" + maximumrecords + "&startRecord=" + startrecord );
221+ new URL (srUrl .toString () + "&maximumRecords=" + maximumRecords + "&startRecord=" + startRecord );
238222 final HttpURLConnection connection = (HttpURLConnection ) urlToOpen .openConnection ();
239223
240224 connection .setConnectTimeout (CONNECTION_TIMEOUT );
241225 if (!userAgent .isEmpty ()) {
242226 connection .setRequestProperty ("User-Agent" , userAgent );
243227 }
244- final InputStream inputStream = getInputStream (connection );
245-
246- return inputStream ;
228+ return getInputStream (connection );
247229 }
248230
249231 private InputStream getInputStream (final HttpURLConnection connection ) {
0 commit comments