Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.extensions.fileutils;
  
 
 import java.io.File;
 import java.net.URI;
Created by IntelliJ IDEA. User: ndw Date: May 24, 2009 Time: 3:17:23 PM To change this template use File | Settings | File Templates.
 
 public class Info extends DefaultStep {
     private static final QName _href = new QName("href");
     private static final QName _method = new QName("method");
     private static final QName _status_only = new QName("status-only");
     private static final QName _detailed = new QName("detailed");
     private static final QName _status = new QName("status");
     private static final QName _name = new QName("name");
     private static final QName _value = new QName("value");
     private static final QName _username = new QName("username");
     private static final QName _password = new QName("password");
     private static final QName _auth_method = new QName("auth_method");
     private static final QName _send_authorization = new QName("send-authorization");
     private static final QName _fail_on_error = new QName("fail-on-error");
     protected final static QName c_uri = new QName("c"."uri");
     protected final static QName c_directory = new QName("c"."directory");
     protected final static QName c_file = new QName("c"."file");
     protected final static QName c_other = new QName("c"."other");
     protected final static QName c_error = new QName("c"."error");
     protected final static QName err_fu01 = new QName("err"."FU01");
 
     private static final QName _uri = new QName("uri");
     private static final QName _readable = new QName("readable");
     private static final QName _writable = new QName("writable");
     private static final QName _exists = new QName("exists");
     private static final QName _hidden = new QName("hidden");
     private static final QName _last_modified = new QName("last-modified");
     private static final QName _size = new QName("size");
 
     private WritablePipe result = null;

    
Creates a new instance of UriInfo
 
     public Info(XProcRuntime runtimeXAtomicStep step) {
         super(runtime,step);
     }
 
     public void setOutput(String portWritablePipe pipe) {
          = pipe;
     }
 
     public void reset() {
         .resetWriter();
     }
 
     public void run() throws SaxonApiException {
         super.run();
 
         RuntimeValue href = getOption();
         URI uri = href.getBaseURI().resolve(href.getString());
 
         boolean failOnError = getOption(true);
 
         finest(.getNode(), "Checking info for " + uri);
 
         TreeWriter tree = new TreeWriter();
         tree.startDocument(.getNode().getBaseURI());
 
         if ("file".equals(uri.getScheme())) {
             File file = new File(uri.getPath());
 
             if (!file.exists()) {
                 if (failOnError) {
                     throw new XProcException();
                 } else {
                     tree.addStartElement();
                     tree.addText("File not found");
                    tree.addEndElement();
                    tree.endDocument();
                    .write(tree.getResult());
                    return;
                }
            }
            if (file.isDirectory()) {
                tree.addStartElement();
            } else if (file.isFile()) {
                tree.addStartElement();
            } else {
                tree.addStartElement();
            }
            tree.addAttribute(uri.toASCIIString());
            if (file.canRead())  { tree.addAttribute("true"); }
            if (file.canWrite()) { tree.addAttribute("true"); }
            if (file.isHidden()) { tree.addAttribute("true"); }
            tree.addAttribute("" + file.length());
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTimeInMillis(file.lastModified());
            TimeZone tz = TimeZone.getDefault();
            long gmt = file.lastModified() - tz.getRawOffset();
            if (tz.useDaylightTime() && tz.inDaylightTime(cal.getTime())) {
                gmt -= tz.getDSTSavings();
            }
            cal.setTimeInMillis(gmt);
            tree.addAttribute(, String.format("%1$04d-%2$02d-%3$02dT%4$02d:%5$02d:%6$02dZ",
                    cal.get(.), cal.get(.)+1, cal.get(.),
                    cal.get(.), cal.get(.), cal.get(.)));
            tree.startContent();
            tree.addEndElement();
        } else {
            tree.addStartElement();
            // Let's try HTTP
            HttpRequest httpReq = new HttpRequest();
            Pipe inputPipe = new Pipe();
            Pipe outputPipe = new Pipe();
            httpReq.setInput("source"inputPipe);
            httpReq.setOutput("result"outputPipe);
            TreeWriter req = new TreeWriter();
            req.startDocument(.getNode().getBaseURI());
            req.addStartElement(.);
            req.addAttribute("HEAD");
            req.addAttribute(uri.toASCIIString());
            req.addAttribute("true");
            req.addAttribute("true");
            for (QName name : new QName[] { } ) {
                RuntimeValue v = getOption(name);
                if (v != null) { req.addAttribute(namev.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());
            tree.addAttribute(href.getString());
            tree.addAttribute(""+status);
            tree.addAttribute(status >= 200 && status < 400 ? "true" : "false");
            tree.addAttribute(status >= 400 && status < 500 ? "false" : "true");
            tree.addAttribute(uri.toASCIIString());
            for (XdmNode node : new RelevantNodes(result.)) {
                if ("Last-Modified".equals(node.getAttributeValue())) {
                    String months[] = {"JAN""FEB""MAR""APR""MAY""JUN",
                                       "JUL""AUG""SEP""OCT""NOV""DEC" };
                    String dateStr = node.getAttributeValue();
                    // dateStr = Fri, 13 Mar 2009 12:12:07 GMT
                    //           00000000001111111111222222222
                    //           01234567890123456789012345678
                    //System.err.println("dateStr: " + dateStr);
                    String dayStr = dateStr.substring(5,7);
                    String monthStr = dateStr.substring(8,11).toUpperCase();
                    String yearStr = dateStr.substring(12,16);
                    String timeStr = dateStr.substring(17,25);
                    String tzStr = dateStr.substring(26,29);
                    int month = 0;
                    for (month = 0; month < 12; month++) {
                        if (months[month].equals(monthStr)) {
                            break;
                        }
                    }
                    tree.addAttribute(, String.format("%1$04d-%2$02d-%3$02dT%4$s%5$s",
                            Integer.parseInt(yearStr), month+1, Integer.parseInt(dayStr), timeStr,
                            "GMT".equals(tzStr) ? "Z" : ""));
                }
                if ("Content-Length".equals(node.getAttributeValue())) {
                    tree.addAttribute(node.getAttributeValue());
                }
            }
            tree.startContent();
            for (XdmNode node : new RelevantNodes(result.)) {
                tree.addSubtree(node);
            }
            tree.addEndElement();
        }
        tree.endDocument();
        .write(tree.getResult());
    }
New to GrepCode? Check out our FAQ X