Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.xmlcalabash.extensions.fileutils;
 
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 Delete extends DefaultStep {
    private static final QName _href = new QName("href");
    private static final QName _recursive = new QName("recursive");
    private static final QName _fail_on_error = new QName("fail-on-error");
    private WritablePipe result = null;

    
Creates a new instance of UriInfo
    public Delete(XProcRuntime runtimeXAtomicStep step) {
        super(runtime,step);
    }
    public void setOutput(String portWritablePipe pipe) {
         = pipe;
    }
    public void reset() {
        .resetWriter();
    }
    public void run() throws SaxonApiException {
        super.run();
        if (.getSafeMode()) {
            throw XProcException.dynamicError(21);
        }
        boolean recursive = getOption(false);
        boolean fail_on_error = getOption(true);
        
        RuntimeValue href = getOption();
        URI uri = href.getBaseURI().resolve(href.getString());
        File file;
        if (!"file".equals(uri.getScheme())) {
            throw new XProcException(.getNode(), "Only file: scheme URIs are supported by the delete step.");
        } else {
            file = new File(uri.getPath());
        }
        if (!file.exists()) {
            if (fail_on_error) {
                throw new XProcException(.getNode(), "Cannot delete: file does not exist: " + file.getAbsolutePath());
            }
        } else {
            TreeWriter tree = new TreeWriter();
            tree.startDocument(.getNode().getBaseURI());
            tree.addStartElement(.);
            tree.startContent();
            tree.addText(file.toURI().toASCIIString());
            performDelete(filerecursivefail_on_error);
            tree.addEndElement();
            tree.endDocument();
            .write(tree.getResult());
        }
    }
    
    private void performDelete(File fileboolean recursiveboolean fail_on_error) {
        if (recursive && file.isDirectory()) {
            for (File f : file.listFiles()) {
                performDelete(frecursivefail_on_error);
            }
        }
        if (!file.delete() && fail_on_error) {
            throw new XProcException(.getNode(), "Delete failed for: " + file.getAbsolutePath());
        }
    }
New to GrepCode? Check out our FAQ X