Java List Print prettyPrintArgv(List argv)

Here you can find the source of prettyPrintArgv(List argv)

Description

Given an argv array such as might be passed to execve(2), returns a string that can be copied and pasted into a Bourne shell for a similar effect.

License

Open Source License

Declaration

public static String prettyPrintArgv(List<String> argv) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.List;

public class Main {
    /**/*from  www  .j ava 2 s.  c  o  m*/
     * Characters that have no special meaning to the shell.
     */
    private static final String SAFE_PUNCTUATION = "@%-_+:,./";

    /**
     * Given an argv array such as might be passed to execve(2), returns a string
     * that can be copied and pasted into a Bourne shell for a similar effect.
     */
    public static String prettyPrintArgv(List<String> argv) {
        StringBuilder buf = new StringBuilder();
        for (String arg : argv) {
            if (buf.length() > 0) {
                buf.append(' ');
            }
            buf.append(shellEscape(arg));
        }
        return buf.toString();
    }

    /**
     * Quotes a word so that it can be used, without further quoting,
     * as an argument (or part of an argument) in a shell command.
     */
    public static String shellEscape(String word) {
        int len = word.length();
        if (len == 0) {
            // Empty string is a special case: needs to be quoted to ensure that it gets
            // treated as a separate argument.
            return "''";
        }
        for (int ii = 0; ii < len; ii++) {
            char c = word.charAt(ii);
            // We do this positively so as to be sure we don't inadvertently forget
            // any unsafe characters.
            if (!Character.isLetterOrDigit(c)
                    && SAFE_PUNCTUATION.indexOf(c) == -1) {
                // replace() actually means "replace all".
                return "'" + word.replace("'", "'\\''") + "'";
            }
        }
        return word;
    }
}

Related

  1. limitLengthOfPrintedList(Collection list)
  2. prettyList(final List list)
  3. prettyList(List list)
  4. prettyPrint(List userPermissions)
  5. prettyPrint(List rows)
  6. prettyPrintList(Collection objects, String separator)
  7. prettyPrintList(List list)
  8. prettyPrintWarningMessage(final List results, final String message)
  9. prettyRoleOutput(List roles, final String delimiter)