Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.extensions;
  
 
 import java.net.URI;
 import java.io.File;
Created by IntelliJ IDEA. User: ndw Date: Mar 12, 2009 Time: 9:41:54 PM To change this template use File | Settings | File Templates.
 
 public class UriInfo 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");
     protected final static QName c_uriinfo = new QName("c"."uri-info");
 
     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 _absolute = new QName("absolute");
     private static final QName _directory = new QName("directory");
     private static final QName _hidden = new QName("hidden");
     private static final QName _file = new QName("file");
     private static final QName _last_modified = new QName("last-modified");
     private static final QName _size = new QName("size");
     private static final QName _absolute_path = new QName("absolute-path");
     private static final QName _canonical_path = new QName("canonical-path");
 
     private WritablePipe result = null;

    
Creates a new instance of UriInfo
 
     public UriInfo(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());
 
         finest(.getNode(), "Checking uri-info for " + uri);
 
         TreeWriter tree = new TreeWriter();
         tree.startDocument(.getNode().getBaseURI());
         tree.addStartElement();
 
         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.getString());
            tree.addAttribute(f.exists() ? "true" : "false");
            tree.addAttribute(f.canRead() ? "true" : "false");
            if (f.exists()) {
                tree.addAttribute(f.canWrite() ? "true" : "false");
                tree.addAttribute("" + f.length());
                tree.addAttribute(f.isAbsolute() ? "true" : "false");
                tree.addAttribute(f.isDirectory() ? "true" : "false");
                tree.addAttribute(f.isHidden() ? "true" : "false");
                tree.addAttribute(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(, 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(.)));
            } else {
                String path = f.getAbsolutePath();
                int slash = path.lastIndexOf("/");
                path = path.substring(0,slash);
                File parent = new File(path);
                tree.addAttribute(parent.canWrite() ? "true" : "false");
            }
            tree.addAttribute(f.getAbsolutePath());
            tree.addAttribute(f.toURI().toASCIIString());
            try {
                String cp = f.getCanonicalPath();
                tree.addAttribute(cp);
            } catch (Exception e) {
                // nevermind
            }
            tree.startContent();
            tree.addEndElement();
        } else {
            // 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