alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.vcscore.cmdline.exec;

import java.io.File;
import java.util.Hashtable;
import org.netbeans.modules.vcscore.Variables;

/**
 * The representation of structured execution property.
 *
 * @author  Martin Entlicher
 */
public class StructuredExec extends Object {
    
    private File working;
    private String executable;
    private Argument[] args;
    
    /** Creates a new instance of StructuredExec.
     * @param working The working directory or null to use the current
     *                working directory.
     * @param executable The executable
     * @param args The list of arguments passed to the executable
     */
    public StructuredExec(File working, String executable, Argument[] args) {
        this.working = working;
        this.executable = executable;
        this.args = args;
    }
    
    /**
     * Get the working directory.
     * @return The working directory or null to use the current
     *         working directory.
     */
    public File getWorking() {
        return working;
    }
    
    /**
     * Set the working directory.
     * @param working The working directory or null to use the current
     *                working directory.
     */
    public void setWorking(File working) {
        this.working = working;
    }
    
    public String getExecutable() {
        return executable;
    }
    
    public void setExecutable(String executable) {
        this.executable = executable;
    }
    
    public Argument[] getArguments() {
        return args;
    }
    
    public void setArguments(Argument[] args) {
        this.args = args;
    }
    
    public void setArgument(int pos, Argument arg) {
        if (pos < 0 || pos >= args.length) {
            throw new IllegalArgumentException("Illegal argument position: "+pos);
        }
        args[pos] = arg;
    }
    
    public void addArgument(Argument arg) {
        addArgument(args.length, arg);
    }
    
    public void addArgument(int pos, Argument arg) {
        if (pos < 0) pos = 0;
        Argument[] newArgs = new Argument[args.length + 1];
        if (pos < args.length) {
            if (pos > 0) {
                System.arraycopy(args, 0, newArgs, 0, pos);
            }
            System.arraycopy(args, pos, newArgs, pos + 1, args.length - pos);
            newArgs[pos] = arg;
        } else {
            System.arraycopy(args, 0, newArgs, 0, args.length);
            newArgs[args.length] = arg;
        }
        args = newArgs;
    }
    
    public void removeArgument(int pos) {
        if (pos < 0 || pos >= args.length) {
            throw new IllegalArgumentException("Illegal argument position: "+pos);
        }
        Argument[] newArgs = new Argument[args.length - 1];
        if (pos > 0) {
            System.arraycopy(args, 0, newArgs, 0, pos);
        }
        if (pos < (args.length - 1)) {
            System.arraycopy(args, pos + 1, newArgs, pos, args.length - pos - 1);
        }
        args = newArgs;
    }
    
    public StructuredExec getExpanded(Hashtable vars, boolean warnUndefVars) {
        String ew = null;
        if (getWorking() != null) {
            ew = getWorking().getPath();
            ew = Variables.expand(vars, ew, warnUndefVars);
        }
        String ee = Variables.expand(vars, getExecutable(), warnUndefVars);
        Argument[] eargs = new Argument[args.length];
        for (int i = 0; i < args.length; i++) {
            eargs[i] = new Argument(Variables.expand(vars, args[i].getArgument(), warnUndefVars), args[i].isLine());
        }
        return new StructuredExec((ew != null) ? new File(ew) : null, ee, eargs);
    }
    
    /**
     * Representation of an argument.
* The argument can be either a single String argument, or a sequence of * space-separated arguments in a single String. This is distinguished * by the line property. */ public static class Argument extends Object { private String argument; private boolean line; /** * Create a new argument. * @param argument The String value of the argument. * @param line When false, the argument * represents a single String argument of the executable, * if false, the argument represents * a sequence of space-separated arguments. */ public Argument(String argument, boolean line) { this.argument = argument; this.line = line; } public String getArgument() { return argument; } public void setArgument(String argument) { this.argument = argument; } public boolean isLine() { return line; } public void setLine(boolean line) { this.line = line; } } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.