6363@ FluxCommand ("open-sru" )
6464public final class SruOpener extends DefaultObjectPipe <String , ObjectReceiver <Reader >> {
6565
66+ private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory .newInstance ();
6667 private static final String OPERATION = "searchRetrieve" ;
6768 private static final String RECORD_SCHEMA = "MARC21-xml" ;
6869 private static final String USER_AGENT = "metafacture-core" ;
6970 private static final String VERSION = "2.0" ;
70-
7171 private static final int CONNECTION_TIMEOUT = 11000 ;
7272 private static final int MAXIMUM_RECORDS = 10 ;
7373 private static final int START_RECORD = 1 ;
74+ private static final Transformer TRANSFORMER ;
75+
76+ static {
77+ try {
78+ TRANSFORMER = TransformerFactory .newInstance ().newTransformer ();
79+ }
80+ catch (final TransformerConfigurationException e ) {
81+ throw new MetafactureException (e );
82+ }
83+ }
7484
85+ private DocumentBuilder docBuilder ;
7586 private boolean stopRetrieving ;
7687
7788 private int maximumRecords = MAXIMUM_RECORDS ;
@@ -179,23 +190,21 @@ public void process(final String baseUrl) {
179190
180191 int recordsRetrieved = 0 ;
181192 int numberOfRecords = Integer .MAX_VALUE ;
182- final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
183- final Transformer transformer ;
184193 try {
185- transformer = TransformerFactory . newInstance (). newTransformer ();
194+ docBuilder = DOCUMENT_BUILDER_FACTORY . newDocumentBuilder ();
186195 }
187- catch (final TransformerConfigurationException e ) {
196+ catch (final ParserConfigurationException e ) {
188197 throw new MetafactureException (e );
189198 }
190199 while (!stopRetrieving && recordsRetrieved < totalRecords && startRecord < numberOfRecords ) {
191200
192201 try {
193202 final InputStream inputStreamOfURl = retrieveUrl (srUrl );
194- final DocumentBuilder docBuilder = factory . newDocumentBuilder ();
203+
195204 final Document xmldoc = docBuilder .parse (inputStreamOfURl );
196205
197206 final StringWriter stringWriter = new StringWriter ();
198- transformer .transform (new DOMSource (xmldoc ), new StreamResult (stringWriter ));
207+ TRANSFORMER .transform (new DOMSource (xmldoc ), new StreamResult (stringWriter ));
199208
200209 numberOfRecords = getIntegerValueFromElement (xmldoc , "numberOfRecords" , 0 );
201210 final int recordPosition = getIntegerValueFromElement (xmldoc , "recordPosition" , 0 );
@@ -208,7 +217,7 @@ public void process(final String baseUrl) {
208217 getReceiver ().process (new InputStreamReader (inputStream ));
209218 }
210219 }
211- catch (final IOException | TransformerException | SAXException | ParserConfigurationException e ) {
220+ catch (final IOException | TransformerException | SAXException e ) {
212221 throw new MetafactureException (e );
213222 }
214223 }
0 commit comments