Debian Codegraph @

bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/QuotedTokenizer.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2017 IBM Corporation and others.
3 * All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License 2.0 which accompanies this distribution,
6 t https://www.eclipse.org/legal/epl-2.0/
7 t
8 t SPDX-License-Identifier: EPL-2.0
9 *
10 * Contributors: IBM Corporation - initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.equinox.internal.p2.publisher;
14
15 import java.io.*;
16 import java.util.Enumeration;
17 import java.util.NoSuchElementException;
18
19 /**
20 * Tokenzier which supports quoting using '"'
21 * The resulting tokens will not contain the quote character '"' unless it was escaped '\"'
22 */
23 public class QuotedTokenizer implements Enumeration<String> {
24 private StreamTokenizer tokenizer = null;
25
26 /**
27 * Default delimiter is whitespace characters
28 * @param str - String to be tokenized
29 */
30 public QuotedTokenizer(String str) {
31 this(str, null);
32 }
33
34 /**
35 * Tokenize based on the given delimiters. The quote character '"' can not be
36 * used as a delimiter.
37 * @param str - String to be tokenized
38 * @param delim - delimiter characters
39 * @throws IllegalArgumentException if delim contains the quote character '"'
40 */
41 public QuotedTokenizer(String str, String delim) {
42 if (delim != null && delim.indexOf('"') > -1)
43 throw new IllegalArgumentException();
44
45 StringReader reader = new StringReader(str);
46 tokenizer = new StreamTokenizer(reader);
47
48 tokenizer.resetSyntax();
49 if (delim == null)
50 tokenizer.ordinaryChars(0, 0x20);
51 else
52 tokenizer.wordChars(0, 0x20);
53 tokenizer.wordChars(0x21, 0xFF); //characters > 0xFF are also word chars
54 tokenizer.quoteChar('"');
55
56 if (delim != null) {
57 for (int i = 0; i < delim.length(); i++) {
58 tokenizer.ordinaryChar(delim.charAt(i));
59 }
60 }
61 }
62
63 /**
64 * Test to see if more tokens are available
65 * @return true if there is another token
66 */
67 public boolean hasMoreTokens() {
68 return (token(null) != StreamTokenizer.TT_EOF);
69 }
70
71 /**
72 * Return the next token,
73 * @return the next token
74 * @throws NoSuchElementException if there are no more tokens
75 */
76 public String nextToken() {
77 StringBuffer buffer = new StringBuffer(10);
78 int tokenType = token(buffer);
79
80 if (tokenType == StreamTokenizer.TT_EOF)
81 throw new NoSuchElementException();
82
83 return buffer.toString();
84 }
85
86 /**
87 * Get the next token, or check that there is a next token
88 * @param buffer to hold the token, or null if we just want to know if there is one
89 */
90 private int token(StringBuffer buffer) {
91 int tokenType = 0;
92 int next = 0;
93
94 get_token: while (true) {
95 try {
96 tokenType = tokenizer.nextToken();
97 } catch (IOException e) {
98 tokenType = StreamTokenizer.TT_EOF;
99 }
100 switch (tokenType) {
101 case StreamTokenizer.TT_WORD :
102 case '"' :
103 if (buffer == null) {
104 //we just wanted to know if there was something coming
105 tokenizer.pushBack();
106 return tokenType;
107 }
108 buffer.append(tokenizer.sval);
109
110 // peek at the next token,
111 try {
112 next = tokenizer.nextToken();
113 tokenizer.pushBack();
114 } catch (IOException e) {
115 next = StreamTokenizer.TT_EOF;
116 }
117
118 //if the next token is a quote, it is still this token, otherwise we are done
119 if (next == '"')
120 continue;
121 break get_token;
122 case StreamTokenizer.TT_EOF :
123 break get_token;
124 default :
125 //ordinary char from delim, if we have something we are done, otherwise keep looking for a token
126 if (buffer != null && buffer.length() > 0)
127 break get_token;
128 continue;
129 }
130 }
131 return tokenType;
132 }
133
134 @Override
135 public boolean hasMoreElements() {
136 return hasMoreTokens();
137 }
138
139 @Override
140 public String nextElement() {
141 return nextToken();
142 }
143 }