-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIHarvester.java
405 lines (375 loc) · 14.6 KB
/
APIHarvester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.conaltuohy.apiharvester;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.namespace.NamespaceContext;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class APIHarvester {
public static void main(String[] args) {
try {
APIHarvester h = new APIHarvester(args);
h.run();
} catch (Exception e) {
System.err.println("Harvest failed:");
System.err.println(e.getMessage());
System.out.println();
printUsage();
}
}
private static void printUsage() {
System.out.println("APIHarvester is a tool to harvest XML records from a web API. APIHarvester will:");
System.out.println();
System.out.println(" • download a response from a given URL, if necessary retrying in the event of failure");
System.out.println(" • split the response into multiple records which match an XPath expression");
System.out.println(" • save each record under a filename specified using another XPath expression");
System.out.println(" • continue harvesting from additional URLs, extracted from the response using another XPath expression");
System.out.println();
System.out.println("APIHarvester is controlled using XPath 1 expressions; see https://www.w3.org/TR/xpath/ for details.");
System.out.println();
System.out.println("Usage:");
System.out.println();
System.out.println("java -jar apiharvester.jar [parameter list]");
System.out.println();
System.out.println("Parameters are specified as [key=value]. Values containing spaces, ampersands, etc should be enclosed in quotes.");
System.out.println("XML namespace prefixes can be bound to namespace URIs using 'xmlns:' parameters.");
System.out.println();
System.out.println("Parameters:");
System.out.println();
System.out.println(" • xmlns:foo");
System.out.println(" Binds the 'foo' namespace prefix to a namespace URI, for use in the XPath expressions.");
System.out.println(" • directory");
System.out.println(" Location of output files. If not specified, the current directory is used.");
System.out.println(" • url");
System.out.println(" Initial URL to harvest from - required.");
System.out.println(" • records-xpath");
System.out.println(" XPath identifying the individual records within a response. If not specified, the entire response is saved as a single record.");
System.out.println(" • id-xpath");
System.out.println(" XPath of unique id for each record, evaluated within the context of each record - required.");
System.out.println(" • discard-xpath");
System.out.println(" XPath of elements or text which should be discarded, evaluated within the context of each record.");
System.out.println(" • resume-when-xpath");
System.out.println(" XPath determining whether to resume from a harvest page or not - default = \"true()\"");
System.out.println(" • resumption-xpath");
System.out.println(" XPath of URL or URLs for subsequent pages of data - if not specified only the initial URL will be harvested)");
System.out.println(" • url-suffix");
System.out.println(" Specifies a common suffix for URLs; useful for specifying an 'API key' for some APIs.");
System.out.println(" • retries");
System.out.println(" Specifies a number of times to retry in the event of any error; default is 3");
System.out.println(" • delay");
System.out.println(" Specifies a number of seconds to wait between requests; default is 0.");
System.out.println(" • indent");
System.out.println(" Specifies whether to indent the XML or not. Valid values are \"yes\" or \"no\". If unspecified, the value is \"no\".");
System.out.println();
System.out.println("Example:");
System.out.println();
System.out.println("java -jar apiharvester.jar retries=4 xmlns:foo=\"http://example.com/ns/foo\" url=\"http://example.com/api?foo=bar\" records-xpath=\"/foo:response/foo:result\" id-xpath=\"concat('record-', @id)\" discard-xpath=\"*[not(normalize-space())]\" resumption-xpath=\"concat('/api?foo=bar&page=', /foo:response/@page-number + 1)\" url-suffix=\"&api_key=asdkfjasd\" indent=yes delay=10");
}
private HashMap<String, String> arguments;
private APIHarvester(String[] args) {
// parse the parameters, specified as key=value pairs, into a map
arguments = new HashMap(args.length);
for (String argument: args) {
int delimiterPosition = argument.indexOf("=");
if (delimiterPosition == -1) {
throw new IllegalArgumentException("The '=' symbol is missing: " + argument);
}
String key = argument.substring(0, delimiterPosition);
if (key.length() == 0) {
throw new IllegalArgumentException("The parameter name is missing: " + argument);
}
String value = argument.substring(delimiterPosition + 1);
if (value.length() == 0) {
throw new IllegalArgumentException("The parameter value is missing: " + argument);
}
arguments.put(key, value);
}
if (!arguments.isEmpty()) {
System.out.println("Harvest will run with the following parameters:");
System.out.println();
for (Map.Entry<String, String> argument : arguments.entrySet()) {
System.out.println(argument.getKey() + "=" + argument.getValue());
}
System.out.println();
}
}
private void checkArgument(String name, String message) {
if (!arguments.containsKey(name)) {
throw new IllegalArgumentException(message);
}
}
private Stack<URL> urls = new Stack<URL>();
private int harvested = 0;
XPathExpression recordsXPath = null;
XPathExpression idXPath = null;
XPathExpression resumptionXPath = null;
XPathExpression resumeWhenXPath = null;
XPathExpression discardXPath = null;
private Transformer transformer = null;
private void run() throws Exception {
if (arguments.isEmpty()) {
printUsage();
} else {
// check required parameters are present
checkArgument("url", "The 'url' argument is required");
checkArgument("id-xpath", "The 'id-xpath' argument is required");
// compile XPath expressions
XMLNamespaceContext namespaces = new XMLNamespaceContext();
for (String key : arguments.keySet()) {
if (key.startsWith("xmlns:")) {
// argument specifies an XML namespace binding
String prefix = key.substring(6); // after "xmlns:"
String namespaceURI = arguments.get(key);
namespaces.bind(prefix, namespaceURI);
}
}
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(namespaces);
recordsXPath = xpath.compile(getRecordsXPath());
idXPath = xpath.compile(arguments.get("id-xpath"));
if (arguments.containsKey("resumption-xpath")) {
resumptionXPath = xpath.compile(arguments.get("resumption-xpath"));
}
resumeWhenXPath = xpath.compile(getArgument("resume-when-xpath", "true()"));
if (arguments.containsKey("discard-xpath")) {
discardXPath = xpath.compile(arguments.get("discard-xpath"));
}
// prepare transformer for serializing records
TransformerFactory tf = TransformerFactory.newInstance();
transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, getIndent());
// create output directory if needed
String outputDirectory = arguments.get("directory");
if (outputDirectory != null) {
new File(outputDirectory).mkdirs();
}
URL url = new URL(arguments.get("url") + getArgument("url-suffix", ""));
harvest(url);
while (! urls.isEmpty()) {
// one or more "resumption" URLs remain to be harvested
Thread.sleep(getDelay());
url = urls.pop();
harvest(url);
};
System.out.println();
System.out.println(
String.valueOf(harvested) +
((harvested == 1) ? " record was harvested." : " records were harvested.")
);
}
}
private int getRetries() {
return Integer.valueOf(getArgument("retries", "3"));
}
private long getDelay() {
return Long.valueOf(getArgument("delay", "0")) * 1000;
}
private void harvest(URL url) throws Exception {
// harvests from a URL, queues up additional URLs to harvest from
System.out.println("harvesting from " + url + " ...");
// load the XML document
Document doc = load(url);
// parse the XML into separate records
NodeList records = getRecords(doc);
for (int i = records.getLength() - 1; i >= 0; i--) {
Node record = records.item(i);
// discard any unwanted parts of the record
if (discardXPath != null) {
NodeList discards = (NodeList) discardXPath.evaluate(record, XPathConstants.NODESET);
for (int j = discards.getLength() - 1; j >= 0; j--) {
Node discard = discards.item(j);
switch (discard.getNodeType()) {
case Node.ATTRIBUTE_NODE:
System.out.println("Cannot discard attribute nodes");
break;
case Node.DOCUMENT_NODE:
System.out.println("Cannot discard document node");
break;
default:
if (discard.isSameNode(record)) {
System.out.println("Cannot discard entire record");
} else {
Node parent = discard.getParentNode();
if (parent != null) {
parent.removeChild(discard);
}
}
}
}
}
//System.out.println("Saving record " + Integer.valueOf(i) + " ...");
save(record);
}
// remember any resumption URLs
Stack<String> resumptionURLs = getResumptionURLs(doc);
//NodeList resumptionURLs = getResumptionURLs(doc);
for (String relativeResumptionURL : resumptionURLs) {
// resolve the resumption URL relative to the current URL
URL resumptionURL = new URL(url, relativeResumptionURL + getArgument("url-suffix", ""));
urls.push(resumptionURL);
}
}
private Stack<String> getResumptionURLs(Document doc) throws XPathExpressionException {
Stack<String> resumptionURLs = new Stack<String>();
if (resumptionXPath != null) {
try {
// first check whether we should resume
Boolean resume = (Boolean) resumeWhenXPath.evaluate(
doc,
XPathConstants.BOOLEAN
);
if (resume) {
// generate a list of resumption URLs
NodeList resumptionURLNodes = null;
resumptionURLNodes = (NodeList) resumptionXPath.evaluate(
doc,
XPathConstants.NODESET
);
for (int i = resumptionURLNodes.getLength() - 1; i >= 0; i--) {
String relativeResumptionURL = resumptionURLNodes.item(i).getTextContent();
resumptionURLs.push(relativeResumptionURL);
}
}
} catch (Exception e) {
// accept a single resumption URL as a String
String relativeResumptionURL = (String) resumptionXPath.evaluate(
doc,
XPathConstants.STRING
);
if (!"".equals(relativeResumptionURL)) {
resumptionURLs.push(relativeResumptionURL);
}
}
}
return resumptionURLs;
}
private String getIndent() {
return getArgument("indent", "no");
}
private void save(Node record)
throws
FileNotFoundException,
TransformerConfigurationException,
TransformerException,
XPathExpressionException,
UnsupportedEncodingException
{
// serialize record to file
File file = new File(
arguments.get("directory"),
getFilename(record)
);
DOMSource source = new DOMSource(record);
OutputStream out = new FileOutputStream(file);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
harvested++;
}
private String getFilename(Node record)
throws
XPathExpressionException,
UnsupportedEncodingException
{
XPath xpath = XPathFactory.newInstance().newXPath();
String id = (String) idXPath.evaluate(
record,
XPathConstants.STRING
);
return URLEncoder.encode(id, "UTF-8") + ".xml";
}
private String getArgument(String name, String defaultValue) {
if (arguments.containsKey(name)) {
return arguments.get(name);
} else {
return defaultValue;
}
}
private String getRecordsXPath() {
return getArgument("records-xpath", "/*");
}
private NodeList getRecords(Document doc) throws XPathExpressionException {
return (NodeList) recordsXPath.evaluate(
doc,
XPathConstants.NODESET
);
}
private Document load(URL url) throws Exception {
return load(url, 0);
}
private Document load(URL url, int numberOfRetries) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
try {
return factory.newDocumentBuilder().parse(url.openStream());
} catch (Exception e) {
if (numberOfRetries < getRetries()) {
int secondsToWait = 5 * (int) Math.pow(2, numberOfRetries);
System.out.println(
"Error reading XML. Retrying after " + String.valueOf(secondsToWait) + "s. " +
"Retry " + String.valueOf(numberOfRetries + 1) + " of " + String.valueOf(getRetries()) + "."
);
Thread.sleep(secondsToWait * 1000);
return load(url, numberOfRetries + 1);
} else {
throw e;
}
}
}
private class XMLNamespaceContext implements NamespaceContext {
private HashMap<String, String> bindings = new HashMap<String, String>();
private void bind(String prefix, String namespaceURI) {
bindings.put(prefix, namespaceURI);
}
// NamespaceContext implementation
public String getNamespaceURI(String prefix) {
return bindings.get(prefix);
}
public String getPrefix(String namespaceURI) throws IllegalArgumentException {
if (namespaceURI == null) {
throw new IllegalArgumentException();
};
for (Map.Entry<String, String> binding : bindings.entrySet()) {
if (namespaceURI.equals(binding.getValue())) {
return binding.getKey();
}
}
return null;
}
public Iterator<String> getPrefixes(String namespaceURI) throws IllegalArgumentException {
if (namespaceURI == null) {
throw new IllegalArgumentException();
};
Vector<String> prefixes = new Vector<String>();
for (Map.Entry<String, String> binding : bindings.entrySet()) {
if (namespaceURI.equals(binding.getValue())) {
prefixes.add(binding.getKey());
}
}
return prefixes.iterator();
}
}
}