Java Vector from String parseArgumentVector(String argStr)

Here you can find the source of parseArgumentVector(String argStr)

Description

parse Argument Vector

License

Open Source License

Declaration

public static Vector parseArgumentVector(String argStr) 

Method Source Code


//package com.java2s;
/*/*ww  w  . ja v  a  2 s  .com*/
** Tim Endres' utilities package.
** Copyright (c) 1997 by Tim Endres
** 
** This program is free software.
** 
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included   with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE. 
** 
*/

import java.util.*;

public class Main {
    public static Vector parseArgumentVector(String argStr) {
        Vector result = new Vector();
        StringBuffer argBuf = new StringBuffer();

        boolean backSlash = false;
        boolean matchSglQuote = false;
        boolean matchDblQuote = false;

        for (int cIdx = 0; cIdx < argStr.length(); ++cIdx) {
            char ch = argStr.charAt(cIdx);

            switch (ch) {
            //
            // W H I T E S P A C E
            //
            case ' ':
            case '\t':
            case '\n':
            case '\r':
                if (backSlash) {
                    argBuf.append(ch);
                    backSlash = false;
                } else if (matchSglQuote || matchDblQuote) {
                    argBuf.append(ch);
                } else if (argBuf.length() > 0) {
                    result.addElement(argBuf.toString());
                    argBuf.setLength(0);
                }
                break;

            case '\\':
                if (backSlash) {
                    argBuf.append("\\");
                }
                backSlash = !backSlash;
                break;

            case '\'':
                if (backSlash) {
                    argBuf.append("'");
                    backSlash = false;
                } else if (matchSglQuote) {
                    result.addElement(argBuf.toString());
                    argBuf.setLength(0);
                    matchSglQuote = false;
                } else if (!matchDblQuote) {
                    matchSglQuote = true;
                }
                break;

            case '"':
                if (backSlash) {
                    argBuf.append("\"");
                    backSlash = false;
                } else if (matchDblQuote) {
                    result.addElement(argBuf.toString());
                    argBuf.setLength(0);
                    matchDblQuote = false;
                } else if (!matchSglQuote) {
                    matchDblQuote = true;
                }
                break;

            default:
                if (backSlash) {
                    switch (ch) {
                    case 'b':
                        argBuf.append('\b');
                        break;
                    case 'f':
                        argBuf.append('\f');
                        break;
                    case 'n':
                        argBuf.append('\n');
                        break;
                    case 'r':
                        argBuf.append('\r');
                        break;
                    case 't':
                        argBuf.append('\t');
                        break;

                    default:
                        char ch2 = argStr.charAt(cIdx + 1);
                        char ch3 = argStr.charAt(cIdx + 2);
                        if ((ch >= '0' && ch <= '7') && (ch2 >= '0' && ch2 <= '7') && (ch3 >= '0' && ch3 <= '7')) {
                            int octal = (((ch - '0') * 64) + ((ch2 - '0') * 8) + (ch3 - '0'));
                            argBuf.append((char) octal);
                            cIdx += 2;
                        } else if (ch == '0') {
                            argBuf.append('\0');
                        } else {
                            argBuf.append(ch);
                        }
                        break;
                    }
                } else {
                    argBuf.append(ch);
                }

                backSlash = false;
                break;
            }
        }

        if (argBuf.length() > 0) {
            result.addElement(argBuf.toString());
        }

        return result;
    }
}

Related

  1. getposString(Vector vect_valores, String valor)
  2. getStringFromTokens(Vector vector, String delimiter)
  3. implode(Vector handler, String separator)
  4. implode(Vector strings, char delim)
  5. orderedStringInsert(String key, Vector into)
  6. reverseVector(final Vector src)
  7. reverseVector(Vector rc)
  8. string2Vector(String s, String delimiter)
  9. stringArrayToVector(String[] array)