KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > xml > RETokenizer


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.editor.xml;
18
19 import java.util.regex.Matcher JavaDoc;
20
21 /**
22  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Nov 16, 2004
23  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
24  */

25 public class RETokenizer {
26     protected StyleTokens types;
27     protected Matcher JavaDoc matcher;
28
29     public RETokenizer(StyleTokens types, String JavaDoc text) {
30         this.types = types;
31         matcher = types.getMatcher(text);
32     }
33
34  /* protected Token getToken(int position) {
35         for (int i = 0; i < types.getTokens().length; i++) {
36             StyleTokens.StyleToken styleToken = types.getTokens()[i];
37
38             String tokenMatch = matcher.group(i);
39             if (tokenMatch != null) {
40                 String type = styleToken.name;
41                 return new Token(tokenMatch, type, position);
42             }
43         }
44         return null;
45     }*/

46
47     protected Token getToken(int pos) {
48         int count = types.getTokens().length;
49         for (int i = 0; i < types.getTokens().length; i++) {
50             StyleTokens.StyleToken styleToken = types.getTokens()[i];
51             String JavaDoc token = matcher.group(i+1);
52             if (token != null) {
53                 String JavaDoc type = styleToken.name;
54                 return new Token(token, type, pos);
55             }
56         }
57         return null;
58     }
59
60     public Token nextToken() {
61         if (matcher.find()) {
62             return getToken(matcher.start());
63         }
64         return null;
65     }
66
67     public static class Token {
68         public String JavaDoc token;
69         public String JavaDoc type;
70         protected int position;
71
72         public Token(String JavaDoc token, String JavaDoc type, int getPosition) {
73             this.token = token;
74             this.type = type;
75             this.position = getPosition;
76         }
77
78         public String JavaDoc getText() {
79             return token;
80         }
81
82         public String JavaDoc getType() {
83             return type;
84         }
85
86         public int getPosition() {
87             return position;
88         }
89
90         public String JavaDoc toString() {
91             return type + "(" + token + ", " + position + ')';
92         }
93     }
94 }
95
Popular Tags