Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
 
 /* ------------------------------------------------------------ */
StringTokenizer with Quoting support. This class is a copy of the java.util.StringTokenizer API and the behaviour is the same, except that single and double quoted string values are recognised. Delimiters within quotes are not considered delimiters. Quotes can be escaped with '\'.

 
 public class QuotedStringTokenizer
     extends StringTokenizer
 {
     private final static String __delim="\t\n\r";
     private String _string;
     private String _delim = ;
     private boolean _returnQuotes=false;
     private boolean _returnDelimiters=false;
     private StringBuffer _token;
     private boolean _hasToken=false;
     private int _i=0;
     private int _lastStart=0;
     private boolean _double=true;
     private boolean _single=true;
 
     /* ------------------------------------------------------------ */
     public QuotedStringTokenizer(String str,
                                  String delim,
                                  boolean returnDelimiters,
                                  boolean returnQuotes)
     {
         super("");
         =str;
         if (delim!=null)
             =delim;
         =returnDelimiters;
         =returnQuotes;
 
         if (.indexOf('\'')>=0 ||
             .indexOf('"')>=0)
             throw new Error("Can't use quotes as delimiters: "+);
 
         =new StringBuffer(.length()>1024?512:.length()/2);
     }
 
     /* ------------------------------------------------------------ */
     public QuotedStringTokenizer(String str,
                                  String delim,
                                  boolean returnDelimiters)
     {
         this(str,delim,returnDelimiters,false);
     }
 
     /* ------------------------------------------------------------ */
     public QuotedStringTokenizer(String str,
                                  String delim)
     {
         this(str,delim,false,false);
     }
 
     /* ------------------------------------------------------------ */
     public QuotedStringTokenizer(String str)
     {
         this(str,null,false,false);
     }
 
     /* ------------------------------------------------------------ */
     @Override
     public boolean hasMoreTokens()
     {
         // Already found a token
         if ()
            return true;
        =;
        int state=0;
        boolean escape=false;
        while (<.length())
        {
            char c=.charAt(++);
            switch (state)
            {
              case 0: // Start
                  if(.indexOf(c)>=0)
                  {
                      if ()
                      {
                          .append(c);
                          return =true;
                      }
                  }
                  else if (c=='\'' && )
                  {
                      if ()
                          .append(c);
                      state=2;
                  }
                  else if (c=='\"' && )
                  {
                      if ()
                          .append(c);
                      state=3;
                  }
                  else
                  {
                      .append(c);
                      =true;
                      state=1;
                  }
                  break;
              case 1: // Token
                  =true;
                  if(.indexOf(c)>=0)
                  {
                      if ()
                          --;
                      return ;
                  }
                  else if (c=='\'' && )
                  {
                      if ()
                          .append(c);
                      state=2;
                  }
                  else if (c=='\"' && )
                  {
                      if ()
                          .append(c);
                      state=3;
                  }
                  else
                  {
                      .append(c);
                  }
                  break;
              case 2: // Single Quote
                  =true;
                  if (escape)
                  {
                      escape=false;
                      .append(c);
                  }
                  else if (c=='\'')
                  {
                      if ()
                          .append(c);
                      state=1;
                  }
                  else if (c=='\\')
                  {
                      if ()
                          .append(c);
                      escape=true;
                  }
                  else
                  {
                      .append(c);
                  }
                  break;
              case 3: // Double Quote
                  =true;
                  if (escape)
                  {
                      escape=false;
                      .append(c);
                  }
                  else if (c=='\"')
                  {
                      if ()
                          .append(c);
                      state=1;
                  }
                  else if (c=='\\')
                  {
                      if ()
                          .append(c);
                      escape=true;
                  }
                  else
                  {
                      .append(c);
                  }
                  break;
            }
        }
        return ;
    }
    /* ------------------------------------------------------------ */
    @Override
    public String nextToken()
        throws NoSuchElementException
    {
        if (!hasMoreTokens() || ==null)
            throw new NoSuchElementException();
        String t=.toString();
        .setLength(0);
        =false;
        return t;
    }
    /* ------------------------------------------------------------ */
    @Override
    public String nextToken(String delim)
        throws NoSuchElementException
    {
        =delim;
        =;
        .setLength(0);
        =false;
        return nextToken();
    }
    /* ------------------------------------------------------------ */
    @Override
    public boolean hasMoreElements()
    {
        return hasMoreTokens();
    }
    /* ------------------------------------------------------------ */
    @Override
    public Object nextElement()
        throws NoSuchElementException
    {
        return nextToken();
    }
    /* ------------------------------------------------------------ */
    
Not implemented.
    @Override
    public int countTokens()
    {
        return -1;
    }
    /* ------------------------------------------------------------ */
    
Quote a string. The string is quoted only if quoting is required due to embedded delimiters, quote characters or the empty string.

Parameters:
s The string to quote.
delim the delimiter to use to quote the string
Returns:
quoted string
    public static String quoteIfNeeded(String sString delim)
    {
        if (s==null)
            return null;
        if (s.length()==0)
            return "\"\"";
        for (int i=0;i<s.length();i++)
        {
            char c = s.charAt(i);
            if (c=='\\' || c=='"' || c=='\'' || Character.isWhitespace(c) || delim.indexOf(c)>=0)
            {
                StringBuffer b=new StringBuffer(s.length()+8);
                quote(b,s);
                return b.toString();
            }
        }
        return s;
    }
    /* ------------------------------------------------------------ */
    
Quote a string. The string is quoted only if quoting is required due to embeded delimiters, quote characters or the empty string.

Parameters:
s The string to quote.
Returns:
quoted string
    public static String quote(String s)
    {
        if (s==null)
            return null;
        if (s.length()==0)
            return "\"\"";
        StringBuffer b=new StringBuffer(s.length()+8);
        quote(b,s);
        return b.toString();
    }
    private static final char[] escapes = new char[32];
    static
    {
        Arrays.fill(, (char)0xFFFF);
        ['\b'] = 'b';
        ['\t'] = 't';
        ['\n'] = 'n';
        ['\f'] = 'f';
        ['\r'] = 'r';
    }
    /* ------------------------------------------------------------ */
    
Quote a string into an Appendable. The characters ", \, \n, \r, \t, \f and \b are escaped

Parameters:
buffer The Appendable
input The String to quote.
    public static void quote(Appendable bufferString input)
    {
        try
        {
            buffer.append('"');
            for (int i = 0; i < input.length(); ++i)
            {
                char c = input.charAt(i);
                if (c >= 32)
                {
                    if (c == '"' || c == '\\')
                        buffer.append('\\');
                    buffer.append(c);
                }
                else
                {
                    char escape = [c];
                    if (escape == 0xFFFF)
                    {
                        // Unicode escape
                        buffer.append('\\').append('u').append('0').append('0');
                        if (c < 0x10)
                            buffer.append('0');
                        buffer.append(Integer.toString(c, 16));
                    }
                    else
                    {
                        buffer.append('\\').append(escape);
                    }
                }
            }
            buffer.append('"');
        }
        catch (IOException x)
        {
            throw new RuntimeException(x);
        }
    }
    /* ------------------------------------------------------------ */
    
Quote a string into a StringBuffer only if needed. Quotes are forced if any delim characters are present.

Parameters:
buf The StringBuffer
s The String to quote.
delim String of characters that must be quoted.
Returns:
true if quoted;
    public static boolean quoteIfNeeded(Appendable bufString s,String delim)
    {
        for (int i=0;i<s.length();i++)
        {
            char c = s.charAt(i);
            if (delim.indexOf(c)>=0)
            {
            	quote(buf,s);
            	return true;
            }
        }
        try
        {
            buf.append(s);
            return false;
        }
        catch(IOException e)
        {
            throw new RuntimeException(e);
        }
    }
    
    
    /* ------------------------------------------------------------ */
    public static String unquoteOnly(String s)
    {
        return unquoteOnly(sfalse);
    }
    
    
    /* ------------------------------------------------------------ */
    
Unquote a string, NOT converting unicode sequences

Parameters:
s The string to unquote.
lenient if true, will leave in backslashes that aren't valid escapes
Returns:
quoted string
    public static String unquoteOnly(String sboolean lenient)
    {
        if (s==null)
            return null;
        if (s.length()<2)
            return s;
        char first=s.charAt(0);
        char last=s.charAt(s.length()-1);
        if (first!=last || (first!='"' && first!='\''))
            return s;
        StringBuilder b = new StringBuilder(s.length() - 2);
        boolean escape=false;
        for (int i=1;i<s.length()-1;i++)
        {
            char c = s.charAt(i);
            if (escape)
            {
                escape=false;
                if (lenient && !isValidEscaping(c))
                {
                    b.append('\\');
                }
                b.append(c);
            }
            else if (c=='\\')
            {
                escape=true;
            }
            else
            {
                b.append(c);
            }
        }
        return b.toString(); 
    }
    
    /* ------------------------------------------------------------ */
    public static String unquote(String s)
    {
        return unquote(s,false);
    }
    
    /* ------------------------------------------------------------ */
    
Unquote a string.

Parameters:
s The string to unquote.
Returns:
quoted string
    public static String unquote(String sboolean lenient)
    {
        if (s==null)
            return null;
        if (s.length()<2)
            return s;
        char first=s.charAt(0);
        char last=s.charAt(s.length()-1);
        if (first!=last || (first!='"' && first!='\''))
            return s;
        StringBuilder b = new StringBuilder(s.length() - 2);
        boolean escape=false;
        for (int i=1;i<s.length()-1;i++)
        {
            char c = s.charAt(i);
            if (escape)
            {
                escape=false;
                switch (c)
                {
                    case 'n':
                        b.append('\n');
                        break;
                    case 'r':
                        b.append('\r');
                        break;
                    case 't':
                        b.append('\t');
                        break;
                    case 'f':
                        b.append('\f');
                        break;
                    case 'b':
                        b.append('\b');
                        break;
                    case '\\':
                        b.append('\\');
                        break;
                    case '/':
                        b.append('/');
                        break;
                    case '"':
                        b.append('"');
                        break;
                    case 'u':
                        b.append((char)(
                                (TypeUtil.convertHexDigit((byte)s.charAt(i++))<<24)+
                                (TypeUtil.convertHexDigit((byte)s.charAt(i++))<<16)+
                                (TypeUtil.convertHexDigit((byte)s.charAt(i++))<<8)+
                                (TypeUtil.convertHexDigit((byte)s.charAt(i++)))
                                )
                        );
                        break;
                    default:
                        if (lenient && !isValidEscaping(c))
                        {
                            b.append('\\');
                        }
                        b.append(c);
                }
            }
            else if (c=='\\')
            {
                escape=true;
            }
            else
            {
                b.append(c);
            }
        }
        return b.toString();
    }
    
    
    /* ------------------------------------------------------------ */
    
Check that char c (which is preceded by a backslash) is a valid escape sequence.

Parameters:
c
Returns:
    private static boolean isValidEscaping(char c)
    {
        return ((c == 'n') || (c == 'r') || (c == 't') || 
                 (c == 'f') || (c == 'b') || (c == '\\') || 
                 (c == '/') || (c == '"') || (c == 'u'));
    }
    /* ------------------------------------------------------------ */
    

Returns:
handle double quotes if true
    public boolean getDouble()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Parameters:
d handle double quotes if true
    public void setDouble(boolean d)
    {
        =d;
    }
    /* ------------------------------------------------------------ */
    

Returns:
handle single quotes if true
    public boolean getSingle()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Parameters:
single handle single quotes if true
    public void setSingle(boolean single)
    {
        =single;
    }
New to GrepCode? Check out our FAQ X