Java Shell Command runCommandPrompt(final String commandLine, final IPath path)

Here you can find the source of runCommandPrompt(final String commandLine, final IPath path)

Description

This executes the command line for running the command prompt.

License

Open Source License

Parameter

Parameter Description
commandLine command line to execute
path path to open

Exception

Parameter Description
IOException problem starting up the command line.

Declaration

public static void runCommandPrompt(final String commandLine, final IPath path) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import org.eclipse.core.runtime.IPath;

public class Main {
    /**/*www  .  ja v  a 2s.co m*/
     * This executes the command line for running the command prompt. It will
     * <b>not</b> append the path to the end of the string if the string "{}" is
     * not present in the command line.
     * 
     * @param commandLine
     *            command line to execute
     * @param path
     *            path to open
     * @throws IOException
     *             problem starting up the command line.
     */
    public static void runCommandPrompt(final String commandLine, final IPath path) throws IOException {
        final String parsedCommandLine = parse(commandLine, path, false);
        Runtime.getRuntime().exec(parsedCommandLine, null, getContainingFolder(path).toFile());
    }

    /**
     * This parses the command line. The parser is not too efficient, since it
     * goes through each character one at a time, but is smart enough to handle
     * path names with "%d" or any other marker.
     * 
     * The parser has the following states and transitions:
     * 
     * <ul>
     * <li>0 = starting state</li>
     * <li>0 -> 1 = if character is "%"</li>
     * <li>0 -> 2 = if character is "{"</li>
     * <li>1 -> 0 = after parsing the character</li>
     * <li>2 -> 0 = after parsing the character</li>
     * </ul>
     * 
     * @param commandLine
     *            command line to parse.
     * @param path
     *            path to parse.
     * @param autoAppend
     *            automatically append the path if there are no markers found.
     * @return the command line parsed
     */
    public static String parse(final String commandLine, final IPath path, final boolean autoAppend) {
        final IPath container = getContainingFolder(path);
        final String fileName = getFileName(path);

        if (hasMarkers(commandLine)) {
            final StringBuffer parsedCommandLine = new StringBuffer(commandLine.length());
            final char[] commandLineArray = commandLine.toCharArray();
            int state = 0;
            for (final char element : commandLineArray) {
                if (state == 0) {
                    if (element == '%') {
                        state = 1;
                    } else if (element == '{') {
                        state = 2;
                    } else {
                        parsedCommandLine.append(element);
                    }
                } else if (state == 1) {
                    if (element == '%') {
                        parsedCommandLine.append('%');
                    } else if (element == 'p') {
                        parsedCommandLine.append(path.toOSString());
                    } else if (element == 'd') {
                        parsedCommandLine.append(container.toOSString());
                    } else if (element == 'f') {
                        parsedCommandLine.append(fileName);
                    } else if (element == 'P') {
                        parsedCommandLine.append(path.toPortableString());
                    } else if (element == 'D') {
                        parsedCommandLine.append(container.toPortableString());
                    } else if (element == 'F') {
                        parsedCommandLine.append(fileName);
                    } else {
                        parsedCommandLine.append('%');
                        parsedCommandLine.append(element);
                    }
                    state = 0;
                } else if (state == 2) {
                    if (element == '}') {
                        parsedCommandLine.append(container.toOSString());
                    } else {
                        parsedCommandLine.append('{');
                        parsedCommandLine.append(element);
                    }
                    state = 0;
                }

            }
            return parsedCommandLine.toString();
        } else {
            if (autoAppend) {
                return commandLine.replaceAll("%%", "%") + ' ' + container.toOSString();
            } else {
                return commandLine.replaceAll("%%", "%");
            }
        }

    }

    /**
     * Returns the container of the path specified. If the path is already a
     * container, it will return itself.
     * 
     * @param path
     *            path to analyse
     * @return container
     */
    public static IPath getContainingFolder(final IPath path) {
        if (path.toFile().isFile()) {
            return path.removeLastSegments(1);
        } else {
            return path;
        }
    }

    /**
     * Returns the file portion of the path. It returns an empty string if the
     * path is a directory.
     * 
     * @param path
     *            path to evaluate
     * @return file portion of the path.
     */
    private static String getFileName(final IPath path) {
        if (path.toFile().isFile()) {
            return path.lastSegment();
        }
        return "";
    }

    /**
     * @param commandLine
     * @return
     */
    private static boolean hasMarkers(final String commandLine) {
        return commandLine.indexOf("{}") >= 0 || commandLine.indexOf("%p") >= 0 || commandLine.indexOf("%d") >= 0
                || commandLine.indexOf("%f") >= 0 || commandLine.indexOf("%P") >= 0
                || commandLine.indexOf("%D") >= 0 || commandLine.indexOf("%F") >= 0;
    }
}

Related

  1. runCommand(String[] command)
  2. runCommand(String[] command)
  3. runCommandAndWait(String command, String workingDir, String extraPath)
  4. runCommandArray(String[] command)
  5. runCommandGetOutput(String startFolder, String[] args, int[] cmdReturnValue)
  6. runCommandWithOutput(String cmd)
  7. runShell(File workspace, String... shellElements)
  8. runShell(String cmd)
  9. runShell(String shStr)