-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLReader.java
172 lines (157 loc) · 4.29 KB
/
XMLReader.java
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
/* Synth Patch Conversion
* Copyright (C) 2003-4, Kenneth L. Martinez (kmartin@users.sourceforge.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package PatchConversion;
/**
* Read one element at a time from input string containing XML
*
* @author Kenneth L. Martinez
*/
//import java.util.*;
public class XMLReader {
private String xml;
private int indx;
XMLReader(String pXml, boolean convertEscapedChars) {
if (convertEscapedChars) {
xml = convertInputEscapedChars(pXml);
} else {
xml = pXml;
}
indx = 0;
}
XMLReader(String pXml) {
this(pXml, true);
}
/**
* Returns a string array containing element name, value, and then attribute
*
* @return String[3]
*/
String[] getNextTag() {
int start, endBlank, end, valueStart, valueEnd;
String tag[] = new String[3];
start = xml.indexOf("<", indx);
if (start == -1) {
// System.out.println("begin tag \"<\" not found");
return null;
}
end = xml.indexOf(">", start);
if (end == -1) {
// System.out.println("begin tag \">\" not found");
return null;
}
endBlank = xml.indexOf(" ", start);
if (endBlank != -1 && endBlank < end) {
// attribute found after element name
tag[0] = xml.substring(start + 1, endBlank);
tag[2] = xml.substring(endBlank + 1, end);
} else {
tag[0] = xml.substring(start + 1, end);
tag[2] = null;
}
valueStart = end + 1;
valueEnd = xml.indexOf("</" + tag[0] + ">", valueStart);
if (valueEnd == -1) {
// System.out.println("end tag </" + tag[0] + "> not found");
return null;
}
tag[1] = xml.substring(valueStart, valueEnd);
indx = valueEnd + tag[0].length() + 3;
return tag;
}
static String getTagValue(String xml, String name) {
int start, end, valueStart, valueEnd;
start = xml.indexOf("<" + name);
if (start == -1) {
// System.out.println("begin tag <" + name + "> not found");
return null;
}
valueStart = start + name.length() + 2;
end = xml.indexOf("</" + name + ">", valueStart);
if (end == -1) {
// System.out.println("end tag </" + name + "> not found");
return null;
}
return xml.substring(valueStart, end);
}
/**
* Convert "&" to "&", etc
*/
static String convertInputEscapedChars(String xml) {
String s;
StringBuffer sb = new StringBuffer();
int i = 0, j;
while (i < xml.length()) {
if ((j = xml.indexOf("&", i)) != -1) {
s = xml.substring(j);
if (s.length() >= 5 && s.substring(0, 5).equals("&")) {
sb.append(xml.substring(i, j));
sb.append("&");
i = j + 5;
continue;
}
if (s.length() >= 4 && s.substring(0, 4).equals("<")) {
sb.append(xml.substring(i, j));
sb.append("<");
i = j + 4;
continue;
}
if (s.length() >= 4 && s.substring(0, 4).equals(">")) {
sb.append(xml.substring(i, j));
sb.append(">");
i = j + 4;
continue;
}
// FIXME failing silently, because when this is called to initially write out XML,
// conversion hasn't yet occurred... is this a problem?
// System.out.println("convertInputEscapedChars: unexpected ampersand in '" + xml + "'");
}
sb.append(xml.substring(i));
break;
}
return sb.toString();
}
/**
* Convert "&" to "&", etc
*/
static String convertOutputEscapedChars(String xml) {
StringBuffer sb = new StringBuffer();
int i = 0, j;
char ch;
for (j = 0; j < xml.length(); j++) {
ch = xml.charAt(j);
if (ch == '&') {
sb.append("&");
i = j + 1;
continue;
}
if (ch == '<') {
sb.append("<");
i = j + 1;
continue;
}
if (ch == '>') {
sb.append(">");
i = j + 1;
continue;
}
sb.append(ch);
}
return sb.toString();
}
}