KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > MementoTokenizer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import org.eclipse.jdt.internal.core.JavaElement;
14
15 public class MementoTokenizer {
16     private static final String JavaDoc COUNT = Character.toString(JavaElement.JEM_COUNT);
17     private static final String JavaDoc JAVAPROJECT = Character.toString(JavaElement.JEM_JAVAPROJECT);
18     private static final String JavaDoc PACKAGEFRAGMENTROOT = Character.toString(JavaElement.JEM_PACKAGEFRAGMENTROOT);
19     private static final String JavaDoc PACKAGEFRAGMENT = Character.toString(JavaElement.JEM_PACKAGEFRAGMENT);
20     private static final String JavaDoc FIELD = Character.toString(JavaElement.JEM_FIELD);
21     private static final String JavaDoc METHOD = Character.toString(JavaElement.JEM_METHOD);
22     private static final String JavaDoc INITIALIZER = Character.toString(JavaElement.JEM_INITIALIZER);
23     private static final String JavaDoc COMPILATIONUNIT = Character.toString(JavaElement.JEM_COMPILATIONUNIT);
24     private static final String JavaDoc CLASSFILE = Character.toString(JavaElement.JEM_CLASSFILE);
25     private static final String JavaDoc TYPE = Character.toString(JavaElement.JEM_TYPE);
26     private static final String JavaDoc PACKAGEDECLARATION = Character.toString(JavaElement.JEM_PACKAGEDECLARATION);
27     private static final String JavaDoc IMPORTDECLARATION = Character.toString(JavaElement.JEM_IMPORTDECLARATION);
28     private static final String JavaDoc LOCALVARIABLE = Character.toString(JavaElement.JEM_LOCALVARIABLE);
29     private static final String JavaDoc TYPE_PARAMETER = Character.toString(JavaElement.JEM_TYPE_PARAMETER);
30
31     private final char[] memento;
32     private final int length;
33     private int index = 0;
34     
35     public MementoTokenizer(String JavaDoc memento) {
36         this.memento = memento.toCharArray();
37         this.length = this.memento.length;
38     }
39     
40     public boolean hasMoreTokens() {
41         return this.index < this.length;
42     }
43     
44     public String JavaDoc nextToken() {
45         int start = this.index;
46         StringBuffer JavaDoc buffer = null;
47         switch (this.memento[this.index++]) {
48             case JavaElement.JEM_ESCAPE:
49                 buffer = new StringBuffer JavaDoc();
50                 buffer.append(this.memento[this.index]);
51                 start = ++this.index;
52                 break;
53             case JavaElement.JEM_COUNT:
54                 return COUNT;
55             case JavaElement.JEM_JAVAPROJECT:
56                 return JAVAPROJECT;
57             case JavaElement.JEM_PACKAGEFRAGMENTROOT:
58                 return PACKAGEFRAGMENTROOT;
59             case JavaElement.JEM_PACKAGEFRAGMENT:
60                 return PACKAGEFRAGMENT;
61             case JavaElement.JEM_FIELD:
62                 return FIELD;
63             case JavaElement.JEM_METHOD:
64                 return METHOD;
65             case JavaElement.JEM_INITIALIZER:
66                 return INITIALIZER;
67             case JavaElement.JEM_COMPILATIONUNIT:
68                 return COMPILATIONUNIT;
69             case JavaElement.JEM_CLASSFILE:
70                 return CLASSFILE;
71             case JavaElement.JEM_TYPE:
72                 return TYPE;
73             case JavaElement.JEM_PACKAGEDECLARATION:
74                 return PACKAGEDECLARATION;
75             case JavaElement.JEM_IMPORTDECLARATION:
76                 return IMPORTDECLARATION;
77             case JavaElement.JEM_LOCALVARIABLE:
78                 return LOCALVARIABLE;
79             case JavaElement.JEM_TYPE_PARAMETER:
80                 return TYPE_PARAMETER;
81         }
82         loop: while (this.index < this.length) {
83             switch (this.memento[this.index]) {
84                 case JavaElement.JEM_ESCAPE:
85                     if (buffer == null) buffer = new StringBuffer JavaDoc();
86                     buffer.append(this.memento, start, this.index - start);
87                     start = ++this.index;
88                     break;
89                 case JavaElement.JEM_COUNT:
90                 case JavaElement.JEM_JAVAPROJECT:
91                 case JavaElement.JEM_PACKAGEFRAGMENTROOT:
92                 case JavaElement.JEM_PACKAGEFRAGMENT:
93                 case JavaElement.JEM_FIELD:
94                 case JavaElement.JEM_METHOD:
95                 case JavaElement.JEM_INITIALIZER:
96                 case JavaElement.JEM_COMPILATIONUNIT:
97                 case JavaElement.JEM_CLASSFILE:
98                 case JavaElement.JEM_TYPE:
99                 case JavaElement.JEM_PACKAGEDECLARATION:
100                 case JavaElement.JEM_IMPORTDECLARATION:
101                 case JavaElement.JEM_LOCALVARIABLE:
102                 case JavaElement.JEM_TYPE_PARAMETER:
103                     break loop;
104             }
105             this.index++;
106         }
107         if (buffer != null) {
108             buffer.append(this.memento, start, this.index - start);
109             return buffer.toString();
110         } else {
111             return new String JavaDoc(this.memento, start, this.index - start);
112         }
113     }
114     
115 }
116
Popular Tags