KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > ASTToken


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.languages;
21
22 import java.util.List JavaDoc;
23
24 /**
25  * Represents one token in AST.
26  */

27 public final class ASTToken extends ASTItem {
28     
29     private String JavaDoc identifier;
30     private String JavaDoc type;
31     
32     
33     /**
34      * Creates new token with given parameters.
35      *
36      * @param mimeType a mime type of token
37      * @param type a type of token
38      * @param identifier token identifier
39      * @param offset token offset
40      * @param length token length
41      * @param children a list of token children
42      * @return new ASTToken
43      */

44     public static ASTToken create (
45         String JavaDoc mimeType,
46         String JavaDoc type,
47         String JavaDoc identifier,
48         int offset,
49         int length,
50         List JavaDoc<? extends ASTItem> children
51     ) {
52         return new ASTToken (
53             mimeType,
54             type,
55             identifier,
56             offset,
57             length,
58             children
59         );
60     }
61     
62     
63     /**
64      * Creates new token with given parameters, no children and length
65      * derived from identifier.
66      *
67      * @param mimeType a mime type of token
68      * @param type a type of token
69      * @param identifier token identifier
70      * @param offset token offset
71      * @return new ASTToken
72      */

73     public static ASTToken create (
74         String JavaDoc mimeType,
75         String JavaDoc type,
76         String JavaDoc identifier,
77         int offset
78     ) {
79         return new ASTToken (
80             mimeType,
81             type,
82             identifier,
83             offset,
84             identifier == null ? 0 : identifier.length (),
85             null
86         );
87     }
88
89     
90     private ASTToken (
91         String JavaDoc mimeType,
92         String JavaDoc type,
93         String JavaDoc identifier,
94         int offset,
95         int length,
96         List JavaDoc<? extends ASTItem> children
97     ) {
98         super (mimeType, offset, length, children);
99         this.identifier = identifier;
100         this.type = type;
101     }
102
103     /**
104      * Retruns type of token.
105      *
106      * @return type of token
107      */

108     public String JavaDoc getType () {
109         return type;
110     }
111     
112
113     /**
114      * Retruns token identifier.
115      *
116      * @return token identifier
117      */

118     public String JavaDoc getIdentifier () {
119         return identifier;
120     }
121     
122     private String JavaDoc toString;
123
124     /**
125      * Retruns string representation of this token.
126      *
127      * @return string representation of this token
128      */

129     public String JavaDoc toString () {
130         if (toString == null) {
131             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
132             sb.append ('<').append (type);
133             if (identifier != null)
134                 sb.append (",'").
135                    append (e (identifier)).
136                    append ("'");
137             sb.append ('>');
138             toString = sb.toString ();
139         }
140         return toString;
141     }
142         
143     private static String JavaDoc e (CharSequence JavaDoc t) {
144         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
145         int i, k = t.length ();
146         for (i = 0; i < k; i++) {
147             if (t.charAt (i) == '\t')
148                 sb.append ("\\t");
149             else
150             if (t.charAt (i) == '\r')
151                 sb.append ("\\r");
152             else
153             if (t.charAt (i) == '\n')
154                 sb.append ("\\n");
155             else
156                 sb.append (t.charAt (i));
157         }
158         return sb.toString ();
159     }
160 }
Popular Tags