|
| 1 | +/** |
| 2 | + * Copyright 2006 OCLC, Online Computer Library Center Licensed under the Apache License, Version 2.0 (the |
| 3 | + * "License"); you may not use this file except in compliance with the License. You may obtain a copy of the |
| 4 | + * License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 9 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +package org.oclc.oai.harvester2.app; |
| 13 | + |
| 14 | +import java.io.*; |
| 15 | +import java.lang.NoSuchFieldException; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Date; |
| 18 | +import java.util.List; |
| 19 | +import java.util.HashMap; |
| 20 | +import javax.xml.parsers.ParserConfigurationException; |
| 21 | +import javax.xml.xpath.XPathException; |
| 22 | +import javax.xml.xpath.XPathExpressionException; |
| 23 | +import org.oclc.oai.harvester2.verb.*; |
| 24 | +import org.w3c.dom.Node; |
| 25 | +import org.w3c.dom.NodeList; |
| 26 | +import org.xml.sax.SAXException; |
| 27 | + |
| 28 | +public class RawWrite { |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + try { |
| 32 | + System.out.println(new Date()); |
| 33 | + |
| 34 | + HashMap options = getOptions(args); |
| 35 | + List rootArgs = (List) options.get("rootArgs"); |
| 36 | + String baseURL = null; |
| 37 | + if (rootArgs.size() > 0) { |
| 38 | + baseURL = (String) rootArgs.get(0); |
| 39 | + } else { |
| 40 | + throw new IllegalArgumentException(); |
| 41 | + } |
| 42 | + |
| 43 | + OutputStream out = System.out; |
| 44 | + String outFileName = (String) options.get("-out"); |
| 45 | + String from = (String) options.get("-from"); |
| 46 | + String until = (String) options.get("-until"); |
| 47 | + String metadataPrefix = (String) options.get("-metadataPrefix"); |
| 48 | + if (metadataPrefix == null) metadataPrefix = "oai_dc"; |
| 49 | + String resumptionToken = (String) options.get("-resumptionToken"); |
| 50 | + String setSpec = (String) options.get("-setSpec"); |
| 51 | + |
| 52 | + if (resumptionToken != null) { |
| 53 | + if (outFileName != null) |
| 54 | + out = new FileOutputStream(outFileName, true); |
| 55 | + run(baseURL, resumptionToken, out); |
| 56 | + } else { |
| 57 | + if (outFileName != null) |
| 58 | + out = new FileOutputStream(outFileName); |
| 59 | + run(baseURL, from, until, metadataPrefix, setSpec, out); |
| 60 | + } |
| 61 | + |
| 62 | + if (out != System.out) out.close(); |
| 63 | + System.out.println(new Date()); |
| 64 | + } catch (IllegalArgumentException e) { |
| 65 | + System.err.println("RawWrite <-from date> <-until date> <-metadataPrefix prefix> <-setSpec setName> <-resumptionToken token> <-out fileName> baseURL"); |
| 66 | + } catch (Exception e) { |
| 67 | + e.printStackTrace(); |
| 68 | + System.exit(-1); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static void run(String baseURL, String resumptionToken, |
| 73 | + OutputStream out) |
| 74 | + throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, |
| 75 | + NoSuchFieldException { |
| 76 | + ListRecords listRecords = new ListRecords(baseURL, resumptionToken); |
| 77 | + while (listRecords != null) { |
| 78 | + NodeList errors = listRecords.getErrors(); |
| 79 | + if (errors != null && errors.getLength() > 0) { |
| 80 | + System.out.println("Found errors"); |
| 81 | + int length = errors.getLength(); |
| 82 | + for (int i = 0; i < length; ++i) { |
| 83 | + Node item = errors.item(i); |
| 84 | + System.out.println(item); |
| 85 | + } |
| 86 | + System.out.println("Error record: " + listRecords.toString()); |
| 87 | + break; |
| 88 | + } |
| 89 | +// System.out.println(listRecords); |
| 90 | + out.write(listRecords.toString().getBytes("UTF-8")); |
| 91 | + out.write("\n".getBytes("UTF-8")); |
| 92 | + resumptionToken = listRecords.getResumptionToken(); |
| 93 | + System.out.println("resumptionToken: " + resumptionToken); |
| 94 | + if (resumptionToken == null || resumptionToken.length() == 0) { |
| 95 | + listRecords = null; |
| 96 | + } else { |
| 97 | + listRecords = new ListRecords(baseURL, resumptionToken); |
| 98 | + } |
| 99 | + } |
| 100 | + out.write("</harvest>\n".getBytes("UTF-8")); |
| 101 | + } |
| 102 | + |
| 103 | + public static void run(String baseURL, String from, String until, |
| 104 | + String metadataPrefix, String setSpec, |
| 105 | + OutputStream out) |
| 106 | + throws IOException, ParserConfigurationException, SAXException, XPathException, |
| 107 | + NoSuchFieldException { |
| 108 | + out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8")); |
| 109 | + out.write("<harvest>\n".getBytes("UTF-8")); |
| 110 | + out.write(new Identify(baseURL).toString().getBytes("UTF-8")); |
| 111 | + out.write("\n".getBytes("UTF-8")); |
| 112 | + out.write(new ListMetadataFormats(baseURL).toString().getBytes("UTF-8")); |
| 113 | + out.write("\n".getBytes("UTF-8")); |
| 114 | + ListSets listSets = new ListSets(baseURL); |
| 115 | + while (listSets != null) { |
| 116 | + out.write(listSets.toString().getBytes("UTF-8")); |
| 117 | + out.write("\n".getBytes("UTF-8")); |
| 118 | + String resumptionToken = listSets.getResumptionToken(); |
| 119 | + System.out.println("resumptionToken: " + resumptionToken); |
| 120 | + if (resumptionToken == null || resumptionToken.length() == 0) { |
| 121 | + listSets = null; |
| 122 | + } else { |
| 123 | + listSets = new ListSets(baseURL, resumptionToken); |
| 124 | + } |
| 125 | + } |
| 126 | + ListRecords listRecords = new ListRecords(baseURL, from, until, setSpec, |
| 127 | + metadataPrefix); |
| 128 | + while (listRecords != null) { |
| 129 | + NodeList errors = listRecords.getErrors(); |
| 130 | + if (errors != null && errors.getLength() > 0) { |
| 131 | + System.out.println("Found errors"); |
| 132 | + int length = errors.getLength(); |
| 133 | + for (int i = 0; i < length; ++i) { |
| 134 | + Node item = errors.item(i); |
| 135 | + System.out.println(item); |
| 136 | + } |
| 137 | + System.out.println("Error record: " + listRecords.toString()); |
| 138 | + break; |
| 139 | + } |
| 140 | +// System.out.println(listRecords); |
| 141 | + out.write(listRecords.toString().getBytes("UTF-8")); |
| 142 | + out.write("\n".getBytes("UTF-8")); |
| 143 | + String resumptionToken = listRecords.getResumptionToken(); |
| 144 | + System.out.println("resumptionToken: " + resumptionToken); |
| 145 | + if (resumptionToken == null || resumptionToken.length() == 0) { |
| 146 | + listRecords = null; |
| 147 | + } else { |
| 148 | + listRecords = new ListRecords(baseURL, resumptionToken); |
| 149 | + } |
| 150 | + } |
| 151 | + out.write("</harvest>\n".getBytes("UTF-8")); |
| 152 | + } |
| 153 | + |
| 154 | + private static HashMap getOptions(String[] args) { |
| 155 | + HashMap options = new HashMap(); |
| 156 | + ArrayList rootArgs = new ArrayList(); |
| 157 | + options.put("rootArgs", rootArgs); |
| 158 | + |
| 159 | + for (int i = 0; i < args.length; ++i) { |
| 160 | + if (args[i].charAt(0) != '-') { |
| 161 | + rootArgs.add(args[i]); |
| 162 | + } else if (i + 1 < args.length) { |
| 163 | + options.put(args[i], args[++i]); |
| 164 | + } else { |
| 165 | + throw new IllegalArgumentException(); |
| 166 | + } |
| 167 | + } |
| 168 | + return options; |
| 169 | + } |
| 170 | +} |
0 commit comments