KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > tokens > TokenStringLiteral


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on 2004-11-7
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec.tokens;
14
15 import jfun.parsec.Tokenizer;
16
17 /**
18  * This Token represents a string literal.
19  * @author Ben Yu
20  *
21  * 2004-11-7
22  */

23 @Deprecated JavaDoc
24 public class TokenStringLiteral implements java.io.Serializable JavaDoc{
25   /**
26    * creates a Tokenizer instance() that parsers a double quoted string literal that is possiblly escaped by '\'
27    * @return the tokenizer instance.
28    */

29   public static Tokenizer getDoubleQuoteTokenizer(){return sTokenizer;}
30   /**
31    * creates a Tokenizer instance() that parsers a single quoted string literal.
32    * two single quotes represents a single quote.
33    * @return the tokenizer instance.
34    */

35   public static Tokenizer getSqlTokenizer(){return sqlTokenizer;}
36   private TokenStringLiteral(){}
37   private static final char escapedChar(final char c){
38     switch(c){
39       case 'r':
40         return '\r';
41       case 'n':
42         return '\n';
43       case 't':
44         return '\t';
45       default:
46         return c;
47       
48     }
49   }
50   static String JavaDoc tokenizeDoubleQuote(final CharSequence JavaDoc cs,
51       final int from, final int len){
52     final int end = from + len -1;
53     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54     for(int i=from+1; i<end;i++){
55       final char c = cs.charAt(i);
56       if(c!='\\'){
57         buf.append(c);
58       }
59       else{
60         final char c1 = cs.charAt(++i);
61         buf.append(escapedChar(c1));
62       }
63     }
64     return buf.toString();
65   }
66   private static final Tokenizer sTokenizer = new Tokenizer(){
67     public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
68       return tokenizeDoubleQuote(cs, from, len);
69     }
70   };
71
72   private static final Tokenizer sqlTokenizer = new Tokenizer(){
73     public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
74       final int end = from+len-1;
75       final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
76       for(int i=from+1; i<end; i++){
77         final char c = cs.charAt(i);
78         if(c!='\'')
79           buf.append(c);
80         else{
81           buf.append('\'');
82           i++;
83         }
84       }
85       return buf.toString();
86     }
87   };
88 }
89
Popular Tags