KickJava   Java API By Example, From Geeks To Geeks.

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


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-15
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec.tokens;
14
15 import jfun.parsec.Tokenizer;
16
17 /**
18  * represents a string that is quoted by a open and close string.
19  * Use this token if the value of open quote and close quote matters to the syntax.
20  * @author Ben Yu
21  *
22  * 2004-11-15
23  */

24 public class TokenQuoted implements java.io.Serializable JavaDoc{
25   private final String JavaDoc open;
26   private final String JavaDoc close;
27   private final String JavaDoc quoted;
28   
29   /**
30    * @param open the open quote
31    * @param close the close quote
32    * @param quoted the quoted string
33    */

34   
35   TokenQuoted(final String JavaDoc open, final String JavaDoc close, final String JavaDoc quoted) {
36     this.open = open;
37     this.close = close;
38     this.quoted = quoted;
39   }
40   
41   public boolean equals(Object JavaDoc obj) {
42     if(obj instanceof TokenQuoted){
43       final TokenQuoted tq2 = (TokenQuoted)obj;
44       return open.equals(tq2.open) && close.equals(tq2.close)
45       && quoted.equals(tq2.quoted);
46     }
47     else return false;
48   }
49   public int hashCode() {
50     return open.hashCode() + quoted.hashCode() + close.hashCode();
51   }
52   public String JavaDoc toString(){
53     return open+quoted+close;
54   }
55   /**
56    * creates a Tokenizer instance that can parse a string quoted by open and close.
57    * @param open the open quote
58    * @param close the close quote
59    * @return the tokenizer.
60    * @deprecated Use {@link Tokenizers#forQuotedString(char, char)} instead.
61    */

62   public static Tokenizer getTokenizer(final char open, final char close){
63     return getTokenizer(""+open, ""+close);
64   }
65   /**
66    * creates a Tokenizer instance that can parse a string quoted by open and close.
67    * @param open the opening quote
68    * @param close the closeing quote
69    * @return the tokenizer.
70    * @deprecated Use {@link Tokenizers#forQuotedString(String, String)} instead.
71    */

72   public static Tokenizer getTokenizer(final String JavaDoc open, final String JavaDoc close){
73     return new Tokenizer(){
74       public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
75         final int start = from + open.length();
76         final int end = from + len - close.length();
77         return new TokenQuoted(open, close, cs.subSequence(start, end).toString());
78       }
79     };
80   }
81   /**
82    * Returns the closing quote.
83    * @return the closing quote
84    */

85   public final String JavaDoc getClose() {
86     return close;
87   }
88   /**
89    * Returns the quoted text.
90    * @return the quoted text
91    */

92   public final String JavaDoc getQuoted() {
93     return quoted;
94   }
95   /**
96    * Returns the opening quote.
97    * @return the opening quote
98    */

99   public final String JavaDoc getOpen() {
100     return open;
101   }
102 }
103
Popular Tags