KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > save > TokenUtilities


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 package org.netbeans.modules.java.source.save;
20
21 import org.netbeans.api.java.lexer.JavaTokenId;
22 import org.netbeans.api.lexer.TokenSequence;
23
24 /**
25  * todo (#pf): Should be merged with PositionEstimator utilities methods.
26  * Utilities method for tokens.
27  *
28  * @author Pavel Flaska
29  */

30 class TokenUtilities {
31
32     /**
33      * Moves to previous java relevant token. In other words, it goes from
34      * token at <tt>pos</tt> to previous token important for java-compiler.
35      * Strictly speaking, it ignores whitespaces and comments.
36      *
37      * @param tokenSequence traverse sequence
38      * @param pos offset (not token id!)
39      * @return offset (not token id!) of newly selected token
40      */

41     static int movePrevious(TokenSequence<JavaTokenId> tokenSequence, final int pos) {
42         // remove also package keyword
43
tokenSequence.move(pos);
44         tokenSequence.movePrevious();
45         JavaTokenId type = tokenSequence.token().id();
46         while (JavaTokenId.WHITESPACE.equals(type) ||
47                JavaTokenId.BLOCK_COMMENT.equals(type) ||
48                JavaTokenId.LINE_COMMENT.equals(type)
49         ) {
50             if (tokenSequence.movePrevious() == false)
51                 break;
52             type = tokenSequence.token().id();
53         }
54         return tokenSequence.offset();
55     }
56     
57     /**
58      * Moves to next java relevant token. In other words, it goes from
59      * token at <tt>pos</tt> to next token important for java-compiler.
60      * Strictly speaking, it ignores whitespaces and comments.
61      *
62      * @param tokenSequence traverse sequence
63      * @param pos offset (not token id!)
64      * @return offset (not token id!) of newly selected token
65      */

66     static int moveNext(TokenSequence<JavaTokenId> tokenSequence, final int pos) {
67         tokenSequence.move(pos);
68         tokenSequence.moveNext(); // Assumes the pos is located within input bounds
69
JavaTokenId type = tokenSequence.token().id();
70         while (JavaTokenId.WHITESPACE.equals(type) ||
71                JavaTokenId.BLOCK_COMMENT.equals(type) ||
72                JavaTokenId.LINE_COMMENT.equals(type)
73         ) {
74             if (tokenSequence.moveNext() == false)
75                 break;
76             type = tokenSequence.token().id();
77         }
78         return tokenSequence.offset();
79     }
80     
81     static int moveFwdToToken(TokenSequence<JavaTokenId> tokenSequence,
82             final int pos,
83             JavaTokenId id)
84     {
85         tokenSequence.move(pos);
86         tokenSequence.moveNext(); // Assumes the pos is located within input bounds
87
while (!id.equals(tokenSequence.token().id())) {
88             if (!tokenSequence.moveNext())
89                 return -1;
90         }
91         return tokenSequence.offset();
92     }
93     
94     static int moveBackToToken(TokenSequence<JavaTokenId> tokenSequence,
95             final int pos,
96             JavaTokenId id)
97     {
98         tokenSequence.move(pos);
99         tokenSequence.moveNext(); // Assumes the pos is located within input bounds
100
while (!id.equals(tokenSequence.token().id())) {
101             if (!tokenSequence.movePrevious())
102                 return -1;
103         }
104         return tokenSequence.offset();
105     }
106     
107     static int moveFwdToToken(TokenSequence<JavaTokenId> tokenSequence,
108             final int pos,
109             String JavaDoc text)
110     {
111         tokenSequence.move(pos);
112         tokenSequence.moveNext(); // Assumes the pos is located within input bounds
113
while (!text.equals(tokenSequence.token().text())) {
114             if (!tokenSequence.moveNext())
115                 return -1;
116         }
117         return tokenSequence.offset();
118     }
119     
120     static int moveBackToToken(TokenSequence<JavaTokenId> tokenSequence,
121             final int pos,
122             String JavaDoc text)
123     {
124         tokenSequence.move(pos);
125         tokenSequence.moveNext(); // Assumes the pos is located within input bounds
126
while (!text.equals(tokenSequence.token().text())) {
127             if (!tokenSequence.movePrevious())
128                 return -1;
129         }
130         return tokenSequence.offset();
131     }
132 }
133
Popular Tags