forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionString.java
More file actions
204 lines (173 loc) · 4.85 KB
/
QuestionString.java
File metadata and controls
204 lines (173 loc) · 4.85 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Question17_10;
import java.awt.List;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class QuestionString {
private Map<String, Byte> tagMap;
private static final Byte[] END = { 0, 1 };
private ArrayList<String> tokens;
private int currentTokenIndex;
public QuestionString(Map<String, Byte> tagMap) {this.tagMap = tagMap;}
public byte[] encode(char[] input) throws IOException {
// tokenize
tokenize(input);
currentTokenIndex = 0;
// parse
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
encodeTokens(outputStream);
return outputStream.toByteArray();
}
private void encodeTokens(ByteArrayOutputStream output)
throws IOException {
nextToken("<");
// read tag name
String tagName = nextToken();
output.write(getTagCode(tagName));
// read attributes
while (!hasNextToken(">") && !hasNextTokens("/", ">")) {
// read next attribute
String key = nextToken();
nextToken("=");
String value = nextToken();
output.write(getTagCode(key));
for (char c : value.toCharArray()) {
output.write(c);
}
output.write(END[0]);
output.write(END[1]);
}
// end of attributes
output.write(END[0]);
output.write(END[1]);
// finish this element
if (hasNextTokens("/", ">")) {
nextToken("/");
nextToken(">");
} else {
nextToken(">");
// while not the end tag
while (!hasNextTokens("<", "/")) {
// encode child
encodeTokens(output);
}
// ending tag
nextToken("<");
nextToken("/");
nextToken(tagName);
nextToken(">");
}
output.write(END[0]);
output.write(END[1]);
}
private String nextToken() throws IOException {
if (currentTokenIndex >= tokens.size()) {
throw new IOException("Unexpected end of input.");
}
String token = tokens.get(currentTokenIndex);
currentTokenIndex++;
return token;
}
private void nextToken(String expectedToken) throws IOException {
if (currentTokenIndex >= tokens.size()) {
throw new IOException("Unexpected end of input.");
}
String token = tokens.get(currentTokenIndex);
if (token.equals(expectedToken)) {
currentTokenIndex++;
} else {
throw new IOException("Unexpected input. Expected '"
+ expectedToken + "'; found '" + token + "'.");
}
}
private boolean hasNextToken(String expectedToken) {
if (currentTokenIndex < tokens.size()) {
return tokens.get(currentTokenIndex).equals(expectedToken);
} else {
return false;
}
}
private boolean hasNextTokens(String... expectedTokens) {
if (currentTokenIndex + expectedTokens.length >
tokens.size()) {
return false;
}
for (int i = 0; i < expectedTokens.length; i++) {
if (!tokens.get(currentTokenIndex + i)
.equals(expectedTokens[i])) return false;
}
return true;
}
private void tokenize(char[] input) {
tokens = new ArrayList<String>();
int i = 0;
while (i < input.length) {
i = setNextToken(input, i);
}
}
private int setNextToken(char[] input, int inputIndex) {
int i = inputIndex;
while (i < input.length && input[i] == ' ') i++;
if (i == input.length) return i;
// get 1 char token
char c = input[i];
if (c == '<' || c == '>' || c == '=' || c == '/') {
tokens.add(String.valueOf(c));
return i + 1;
}
// get multiple char token
StringBuilder string = new StringBuilder();
do {
string.append(c);
i++;
c = input[i];
if (c == '<' || c == '>' || c == '=' ||
c == '/' || c == ' ') {
break;
}
} while (i < input.length);
tokens.add(string.toString());
return i;
}
private byte getTagCode(String tag) throws IOException {
Byte tagCode = tagMap.get(tag);
if (tagCode == null) {
throw new IOException("Unknown tag: " + tag);
}
return tagCode;
}
public static void main(String args[]) {
try {
Map<String, Byte> tagMap = new HashMap<String, Byte>();
tagMap.put("a", (byte) 10);
tagMap.put("root", (byte) 11);
tagMap.put("href", (byte) 20);
tagMap.put("target", (byte) 21);
tagMap.put("name", (byte) 50);
tagMap.put("id", (byte) 51);
QuestionString encoder = new QuestionString(tagMap);
String input;
byte[] output;
input = "<root></root>";
output = encoder.encode(input.toCharArray());
print(output);
input = "<root id=a />";
output = encoder.encode(input.toCharArray());
print(output);
input = "<root><a href=abc id=xyz></a><a></a></root>";
output = encoder.encode(input.toCharArray());
print(output);
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void print(byte[] output) {
for (byte b : output) {
System.out.print(b);
System.out.print(" ");
}
System.out.println();
}
}