RuntimeValue href = getOption(_href);
URI uri = href.getBaseURI().resolve(href.getString());
logger.trace(MessageFormatter.nodeMessage(step.getNode(), "Checking uri-info for " + uri));
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(c_uriinfo);
if (uri.getScheme().equals("file")) {
String fn = href.getString();
if (fn.startsWith("file:")) {
fn = fn.substring(5);
if (fn.startsWith("///")) {
fn = fn.substring(2);
}
}
File f = new File(fn);
tree.addAttribute(_href, href.getString());
tree.addAttribute(_exists, f.exists() ? "true" : "false");
tree.addAttribute(_readable, f.canRead() ? "true" : "false");
if (f.exists()) {
tree.addAttribute(_writable, f.canWrite() ? "true" : "false");
tree.addAttribute(_size, "" + f.length());
tree.addAttribute(_absolute, f.isAbsolute() ? "true" : "false");
tree.addAttribute(_directory, f.isDirectory() ? "true" : "false");
tree.addAttribute(_hidden, f.isHidden() ? "true" : "false");
tree.addAttribute(_file, f.isFile() ? "true" : "false");
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(f.lastModified());
TimeZone tz = TimeZone.getDefault();
long gmt = f.lastModified() - tz.getRawOffset();
if (tz.useDaylightTime() && tz.inDaylightTime(cal.getTime())) {
gmt -= tz.getDSTSavings();
}
cal.setTimeInMillis(gmt);
tree.addAttribute(_last_modified, String.format("%1$04d-%2$02d-%3$02dT%4$02d:%5$02d:%6$02dZ",
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)));
} else {
String path = f.getAbsolutePath();
int slash = path.lastIndexOf("/");
path = path.substring(0,slash);
File parent = new File(path);
tree.addAttribute(_writable, parent.canWrite() ? "true" : "false");
}
tree.addAttribute(_absolute_path, f.getAbsolutePath());
tree.addAttribute(_uri, f.toURI().toASCIIString());
try {
String cp = f.getCanonicalPath();
tree.addAttribute(_canonical_path, cp);
} catch (Exception e) {
// nevermind
}
tree.startContent();
tree.addEndElement();
} else {
// Let's try HTTP
HttpRequest httpReq = new HttpRequest(runtime, step);
Pipe inputPipe = new Pipe(runtime);
Pipe outputPipe = new Pipe(runtime);
httpReq.setInput("source", inputPipe);
httpReq.setOutput("result", outputPipe);
TreeWriter req = new TreeWriter(runtime);
req.startDocument(step.getNode().getBaseURI());
req.addStartElement(XProcConstants.c_request);
req.addAttribute(_method, "HEAD");
req.addAttribute(_href, uri.toASCIIString());
req.addAttribute(_status_only, "true");
req.addAttribute(_detailed, "true");
for (QName name : new QName[] {_username, _password, _auth_method, _send_authorization } ) {
RuntimeValue v = getOption(name);
if (v != null) { req.addAttribute(name, v.getString()); }
}
req.startContent();
req.addEndElement();
req.endDocument();
inputPipe.write(req.getResult());
httpReq.run();
XdmNode result = S9apiUtils.getDocumentElement(outputPipe.read());
int status = Integer.parseInt(result.getAttributeValue(_status));