Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3f6d529ce1b6f758691155027499e4d884b18413 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.internal.core;

/**
 * Simple tokenizer class. Used to parse data.
 */
public class Tokenizer {
	protected char value[];
	protected int max;
	protected int cursor;

	public Tokenizer(String value) {
		this.value = value.toCharArray();
		max = this.value.length;
		cursor = 0;
	}

	private void skipWhiteSpace() {
		char[] val = value;
		int cur = cursor;

		for (; cur < max; cur++) {
			char c = val[cur];
			if ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')) {
				continue;
			}
			break;
		}
		cursor = cur;
	}

	public String getToken(String terminals) {
		skipWhiteSpace();
		char[] val = value;
		int cur = cursor;

		int begin = cur;
		for (; cur < max; cur++) {
			char c = val[cur];
			if ((terminals.indexOf(c) != -1)) {
				break;
			}
		}
		cursor = cur;
		int count = cur - begin;
		if (count > 0) {
			skipWhiteSpace();
			while (count > 0 && (val[begin + count - 1] == ' ' || val[begin + count - 1] == '\t'))
				count--;
			return (new String(val, begin, count));
		}
		return (null);
	}

	public String getString(String terminals) {
		skipWhiteSpace();
		char[] val = value;
		int cur = cursor;

		if (cur < max) {
			if (val[cur] == '\"') /* if a quoted string */
			{
				cur++; /* skip quote */
				char c = '\0';
				int begin = cur;
				for (; cur < max; cur++) {
					c = val[cur];
					if (c == '\"') {
						break;
					}
				}
				int count = cur - begin;
				if (c == '\"') {
					cur++;
				}
				cursor = cur;
				if (count > 0) {
					skipWhiteSpace();
					return (new String(val, begin, count));
				}
			} else /* not a quoted string; same as token */
			{
				int begin = cur;
				for (; cur < max; cur++) {
					char c = val[cur];
					if (c == '\"') {
						// but there could be a quoted string in the middle of the string
						cur = cur + skipQuotedString(val, cur);
					} else if ((terminals.indexOf(c) != -1)) {
						break;
					}
				}
				cursor = cur;
				int count = cur - begin;
				if (count > 0) {
					skipWhiteSpace();
					while (count > 0 && (val[begin + count - 1] == ' ' || val[begin + count - 1] == '\t'))
						count--;
					return (new String(val, begin, count));
				}
			}
		}
		return (null);
	}

	private int skipQuotedString(char[] val, int cur) {
		cur++; /* skip quote */
		char c = '\0';
		int begin = cur;
		for (; cur < max; cur++) {
			c = val[cur];
			if (c == '\"') {
				break;
			}
		}
		int count = cur - begin;
		if (c == '\"') {
			cur++;
		}
		cursor = cur;
		if (count > 0) {
			skipWhiteSpace();
		}
		return count;
	}

	public char getChar() {
		int cur = cursor;
		if (cur < max) {
			cursor = cur + 1;
			return (value[cur]);
		}
		return ('\0'); /* end of value */
	}

	public boolean hasMoreTokens() {
		if (cursor < max) {
			return true;
		}
		return false;
	}
}

Back to the top