KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > lexer > TokenChange


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.lexer;
21
22 import org.netbeans.lib.lexer.LexerUtilsConstants;
23 import org.netbeans.lib.lexer.TokenList;
24 import org.netbeans.lib.lexer.inc.TokenChangeInfo;
25
26 /**
27  * Token change describes modification on one level of a token hierarchy.
28  * <br/>
29  * If there is only one token that was modified
30  * and there was a language embedding in that token then
31  * most of the embedded tokens can usually be retained.
32  * This defines an embedded change accessible by {@link #embeddedChange(int)}.
33  * <br/>
34  * There may possibly be multiple levels of the embedded changes.
35  *
36  * @author Miloslav Metelka
37  * @version 1.00
38  */

39
40 public final class TokenChange<T extends TokenId> {
41     
42     private final TokenChangeInfo<T> info;
43     
44     TokenChange(TokenChangeInfo<T> info) {
45         this.info = info;
46     }
47
48     /**
49      * Get number of embedded changes contained in this change.
50      *
51      * @return >=0 number of embedded changes.
52      */

53     public int embeddedChangeCount() {
54         return info.embeddedChanges().length;
55     }
56     
57     /**
58      * Get embedded change at the given index.
59      *
60      * @param index 0 &lt;= index &lt;= embeddedChangeCount() index of the embedded change.
61      * @return non-null embedded token change.
62      */

63     public TokenChange<? extends TokenId> embeddedChange(int index) {
64         return info.embeddedChanges()[index];
65     }
66
67     /**
68      * Get embedded token change of the given type.
69      *
70      * @return non-null token change or null if the embedded token change
71      * satisfies the condition <code>(embedded().language() == language)</code>.
72      * Null is returned otherwise.
73      */

74     public <T extends TokenId> TokenChange<T> embeddedChange(Language<T> language) {
75         TokenChange<? extends TokenId>[] ecs = info.embeddedChanges();
76         for (int i = ecs.length - 1; i >= 0; i--) {
77             TokenChange<? extends TokenId> c = ecs[i];
78             if (c.language() == language) {
79                 @SuppressWarnings JavaDoc("unchecked")
80                 TokenChange<T> ec = (TokenChange<T>)c;
81                 return ec;
82             }
83         }
84         return null;
85     }
86
87     /**
88      * Get the language describing token ids
89      * used by tokens contained in this token change.
90      */

91     public Language<T> language() {
92         return LexerUtilsConstants.mostEmbeddedLanguage(languagePath());
93     }
94     
95     /**
96      * Get the complete language path of the tokens contained
97      * in this token sequence (containing outer language levels as well).
98      */

99     public LanguagePath languagePath() {
100         return info.currentTokenList().languagePath();
101     }
102
103     /**
104      * Get index of the first token being modified.
105      */

106     public int index() {
107         return info.index();
108     }
109     
110     /**
111      * Get offset of the first token that was modified.
112      * <br/>
113      * If there were any added/removed tokens then this is a start offset
114      * of the first added/removed token.
115      */

116     public int offset() {
117         return info.offset();
118     }
119     
120     /**
121      * Get number of removed tokens contained in this token change.
122      */

123     public int removedTokenCount() {
124         TokenList<? extends TokenId> rtl = info.removedTokenList();
125         return (rtl != null) ? rtl.tokenCount() : 0;
126     }
127     
128     /**
129      * Create token sequence over the removed tokens.
130      *
131      * <p>
132      * There is no analogy of this method for the added tokens.
133      * The {@link #currentTokenSequence()} may be used for exploration
134      * of the current token sequence at this level.
135      * </p>
136      *
137      * @return token sequence over the removed tokens
138      * or null if there were no removed tokens.
139      */

140     public TokenSequence<T> removedTokenSequence() {
141         return new TokenSequence<T>(info.removedTokenList());
142     }
143  
144     /**
145      * Get number of the tokens added by this token change.
146      */

147     public int addedTokenCount() {
148         return info.addedTokenCount();
149     }
150     
151     /**
152      * Get the token sequence that corresponds to the current state
153      * of the token hierarchy.
154      * <br/>
155      * If this is an embedded token change then this method returns
156      * the token sequence at the corresponding embedded level.
157      */

158     public TokenSequence<T> currentTokenSequence() {
159         return new TokenSequence<T>(info.currentTokenList());
160     }
161
162     /**
163      * Used by package-private accessor.
164      */

165     TokenChangeInfo<T> info() {
166         return info;
167     }
168
169 }
Popular Tags