boolean failOnError = getOption(_fail_on_error, true);
final int maxCount = getOption(_count, 10);
final int negMaxCount = -maxCount;
RuntimeValue href = getOption(_href);
final TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(XProcConstants.c_result);
tree.startContent();
try {
DataStore store = runtime.getDataStore();
store.readEntry(href.getString(), href.getBaseURI().toASCIIString(), "text/*, */*", null, new DataReader() {
public void load(URI id, String media, InputStream content, long len)
throws IOException {
Reader rdr = new InputStreamReader(content);
BufferedReader brdr = new BufferedReader(rdr);
try {
String line = null;
int count = 0;
if (maxCount >= 0) {
line = brdr.readLine();
while (line != null && count < maxCount) {
tree.addStartElement(c_line);
tree.startContent();
tree.addText(line);
tree.addEndElement();
tree.addText("\n");
count++;
line = brdr.readLine();
}
} else {
line = "not null";
while (line != null && count < negMaxCount) {
count++;
line = brdr.readLine();
}
line = brdr.readLine();
while (line != null) {
tree.addStartElement(c_line);
tree.startContent();
tree.addText(line);
tree.addEndElement();
tree.addText("\n");
line = brdr.readLine();
}
}
} finally {
brdr.close();
// BufferedReader.close() also closes the underlying
// reader, so this second call is unnecessary.
// rdr.close();
}
}
});
} catch (FileNotFoundException fnfe) {
URI uri = href.getBaseURI().resolve(href.getString());
throw new XProcException(step.getNode(), "Cannot read: file does not exist: " + uri.toASCIIString());
} catch (IOException ioe) {
throw new XProcException(ioe);
}