Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0972cf23caa4f43944e20ce611c4cd8ed7135722 (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 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.equinox.internal.p2.publisher;

import java.io.*;
import java.util.Enumeration;
import java.util.NoSuchElementException;

/**
 * Tokenzier which supports quoting using '"'
 * The resulting tokens will not contain the quote character '"' unless it was escaped '\"'
 */
public class QuotedTokenizer implements Enumeration<String> {
	private StreamTokenizer tokenizer = null;

	/**
	 * Default delimiter is whitespace characters
	 * @param str - String to be tokenized
	 */
	public QuotedTokenizer(String str) {
		this(str, null);
	}

	/**
	 * Tokenize based on the given delimiters.  The quote character '"' can not be
	 * used as a delimiter.
	 * @param str - String to be tokenized
	 * @param delim - delimiter characters
	 * @throws IllegalArgumentException if delim contains the quote character '"'
	 */
	public QuotedTokenizer(String str, String delim) {
		if (delim != null && delim.indexOf('"') > -1)
			throw new IllegalArgumentException();

		StringReader reader = new StringReader(str);
		tokenizer = new StreamTokenizer(reader);

		tokenizer.resetSyntax();
		if (delim == null)
			tokenizer.ordinaryChars(0, 0x20);
		else
			tokenizer.wordChars(0, 0x20);
		tokenizer.wordChars(0x21, 0xFF); //characters > 0xFF are also word chars
		tokenizer.quoteChar('"');

		if (delim != null) {
			for (int i = 0; i < delim.length(); i++) {
				tokenizer.ordinaryChar(delim.charAt(i));
			}
		}
	}

	/**
	 * Test to see if more tokens are available
	 * @return true if there is another token
	 */
	public boolean hasMoreTokens() {
		return (token(null) != StreamTokenizer.TT_EOF);
	}

	/**
	 * Return the next token,
	 * @return the next token
	 * @throws NoSuchElementException if there are no more tokens
	 */
	public String nextToken() {
		StringBuffer buffer = new StringBuffer(10);
		int tokenType = token(buffer);

		if (tokenType == StreamTokenizer.TT_EOF)
			throw new NoSuchElementException();

		return buffer.toString();
	}

	/**
	 * Get the next token, or check that there is a next token
	 * @param buffer to hold the token, or null if we just want to know if there is one 
	 */
	private int token(StringBuffer buffer) {
		int tokenType = 0;
		int next = 0;

		get_token: while (true) {
			try {
				tokenType = tokenizer.nextToken();
			} catch (IOException e) {
				tokenType = StreamTokenizer.TT_EOF;
			}
			switch (tokenType) {
				case StreamTokenizer.TT_WORD :
				case '"' :
					if (buffer == null) {
						//we just wanted to know if there was something coming
						tokenizer.pushBack();
						return tokenType;
					}
					buffer.append(tokenizer.sval);

					// peek at the next token, 
					try {
						next = tokenizer.nextToken();
						tokenizer.pushBack();
					} catch (IOException e) {
						next = StreamTokenizer.TT_EOF;
					}

					//if the next token is a quote, it is still this token, otherwise we are done
					if (next == '"')
						continue;
					break get_token;
				case StreamTokenizer.TT_EOF :
					break get_token;
				default :
					//ordinary char from delim, if we have something we are done, otherwise keep looking for a token
					if (buffer != null && buffer.length() > 0)
						break get_token;
					continue;
			}
		}
		return tokenType;
	}

	@Override
	public boolean hasMoreElements() {
		return hasMoreTokens();
	}

	@Override
	public String nextElement() {
		return nextToken();
	}
}

Back to the top