|
| 1 | +/* Copyright 2013 Pascal Christoph. |
| 2 | + * Licensed under the Eclipse Public License 1.0 */ |
| 3 | + |
| 4 | +package org.metafacture.biblio; |
| 5 | + |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.Reader; |
| 11 | + |
| 12 | +import javax.xml.parsers.ParserConfigurationException; |
| 13 | +import javax.xml.transform.TransformerException; |
| 14 | + |
| 15 | +import org.metafacture.framework.MetafactureException; |
| 16 | +import org.metafacture.framework.ObjectReceiver; |
| 17 | +import org.metafacture.framework.annotations.Description; |
| 18 | +import org.metafacture.framework.annotations.In; |
| 19 | +import org.metafacture.framework.annotations.Out; |
| 20 | +import org.metafacture.framework.helpers.DefaultObjectPipe; |
| 21 | +import org.xml.sax.SAXException; |
| 22 | + |
| 23 | +import ORG.oclc.oai.harvester2.app.RawWrite; |
| 24 | + |
| 25 | +/** |
| 26 | + * Opens an OAI-PMH stream and passes a reader to the receiver. |
| 27 | + * |
| 28 | + * @author Pascal Christoph (dr0i) |
| 29 | + * |
| 30 | + */ |
| 31 | +@Description("Opens an OAI-PMH stream and passes a reader to the receiver. Mandatory arguments are: BASE_URL, DATE_FROM, DATE_UNTIL, METADATA_PREFIX, SET_SPEC .") |
| 32 | +@In(String.class) |
| 33 | +@Out(java.io.Reader.class) |
| 34 | +public final class OaiPmhOpener extends |
| 35 | + DefaultObjectPipe<String, ObjectReceiver<Reader>> { |
| 36 | + |
| 37 | + private String encoding = "UTF-8"; |
| 38 | + |
| 39 | + final ByteArrayOutputStream OUTPUT_STREAM = new ByteArrayOutputStream(); |
| 40 | + |
| 41 | + private String dateFrom; |
| 42 | + |
| 43 | + private String dateUntil; |
| 44 | + |
| 45 | + private String setSpec; |
| 46 | + |
| 47 | + private String metadataPrefix; |
| 48 | + |
| 49 | + /** |
| 50 | + * Default constructor |
| 51 | + */ |
| 52 | + public OaiPmhOpener() { |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets the encoding to use. The default setting is UTF-8. |
| 58 | + * |
| 59 | + * @param encoding new default encoding |
| 60 | + */ |
| 61 | + public void setEncoding(final String encoding) { |
| 62 | + this.encoding = encoding; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets the beginning of the retrieving of updated data. The form is |
| 67 | + * YYYY-MM-DD . |
| 68 | + * |
| 69 | + * @param dateFrom The form is YYYY-MM-DD . |
| 70 | + */ |
| 71 | + public void setDateFrom(final String dateFrom) { |
| 72 | + this.dateFrom = dateFrom; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets the end of the retrieving of updated data. The form is YYYY-MM-DD . |
| 77 | + * |
| 78 | + * @param dateUntil The form is YYYY-MM-DD . |
| 79 | + */ |
| 80 | + public void setDateUntil(final String dateUntil) { |
| 81 | + this.dateUntil = dateUntil; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets the OAI-PM metadata prefix . |
| 86 | + * |
| 87 | + * @param metadataPrefix the OAI-PM metadata prefix |
| 88 | + */ |
| 89 | + public void setMetadataPrefix(final String metadataPrefix) { |
| 90 | + this.metadataPrefix = metadataPrefix; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the OAI-PM set specification . |
| 95 | + * |
| 96 | + * @param setSpec th OAI-PM set specification |
| 97 | + */ |
| 98 | + public void setSetSpec(final String setSpec) { |
| 99 | + this.setSpec = setSpec; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void process(final String baseUrl) { |
| 104 | + |
| 105 | + try { |
| 106 | + RawWrite.run(baseUrl, this.dateFrom, this.dateUntil, this.metadataPrefix, |
| 107 | + this.setSpec, OUTPUT_STREAM); |
| 108 | + } catch (IOException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } catch (ParserConfigurationException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } catch (SAXException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + } catch (TransformerException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } catch (NoSuchFieldException e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + try { |
| 120 | + getReceiver().process( |
| 121 | + new InputStreamReader(new ByteArrayInputStream(OUTPUT_STREAM |
| 122 | + .toByteArray()), encoding)); |
| 123 | + } catch (IOException e) { |
| 124 | + throw new MetafactureException(e); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments