-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathTestStreamReader.java
221 lines (190 loc) · 9.26 KB
/
TestStreamReader.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package test;
import java.io.*;
import java.util.List;
import javax.xml.stream.*;
import org.codehaus.stax2.LocationInfo;
import org.codehaus.stax2.XMLInputFactory2;
import org.codehaus.stax2.XMLStreamReader2;
// import org.codehaus.stax2.io.Stax2ByteArraySource;
/**
* Simple helper test class for checking how stream reader handles xml
* documents.
*/
public class TestStreamReader
implements XMLStreamConstants
{
protected TestStreamReader() {
}
protected XMLInputFactory getFactory()
{
System.setProperty("javax.xml.stream.XMLInputFactory",
"com.fasterxml.aalto.stax.InputFactoryImpl");
XMLInputFactory f = XMLInputFactory.newInstance();
System.out.println("Factory instance: "+f.getClass());
//f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
f.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
//f.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
f.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
Boolean.FALSE
//Boolean.TRUE
);
f.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.TRUE);
//f.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
f.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
//f.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
return f;
}
@SuppressWarnings({ "unchecked", "resource" })
protected int test(File file)
throws Exception
{
XMLInputFactory f = getFactory();
System.out.print("Coalesce: "+f.getProperty(XMLInputFactory.IS_COALESCING));
System.out.println(", NS-aware: "+f.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE));
System.out.print("Entity-expanding: "+f.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES));
System.out.println(", validating: "+f.getProperty(XMLInputFactory.IS_VALIDATING));
//System.out.println(", name interning: "+f.getProperty(XMLInputFactory2.P_INTERN_NAMES));
int total = 0;
XMLStreamReader2 sr;
// Let's deal with gzipped files too?
/*
if (file.getName().endsWith(".gz")) {
System.out.println("[gzipped input file!]");
sr = f.createXMLStreamReader
(new InputStreamReader(new GZIPInputStream
(new FileInputStream(file)), "UTF-8"));
} else {
sr = f.createXMLStreamReader(new FileInputStream(file));
//sr = f.createXMLStreamReader(new FileReader(file));
}
*/
/*
{
byte[] data = readData(file);
sr = f.createXMLStreamReader(new Stax2ByteArraySource(data, 0, data.length));
System.out.println("File '"+file+"', read "+data.length+" bytes in.");
}
*/
sr = (XMLStreamReader2) f.createXMLStreamReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
//sr = f.createXMLStreamReader(new FileInputStream(file));
System.out.println("SR; name interning: "+sr.getProperty(XMLInputFactory2.P_INTERN_NAMES));
System.out.println("SR; URI interning: "+sr.getProperty(XMLInputFactory2.P_INTERN_NS_URIS));
int type = sr.getEventType();
System.out.println("START: version = '"+sr.getVersion()
+"', xml-encoding = '"+sr.getCharacterEncodingScheme()
+"', input encoding = '"+sr.getEncoding()+"'");
while (type != END_DOCUMENT) {
type = sr.next();
total += type; // so it won't be optimized out...
//boolean hasName = sr.hasName();
System.out.print("["+type+"]");
if (sr.hasText()) {
String text = sr.getText();
total += text.length(); // to prevent dead code elimination
if (type == CHARACTERS || type == CDATA || type == COMMENT) {
System.out.println(" Text("+text.length()+" = '"+text+"'.");
if (text.length() >= 1) {
System.out.println(" [first char code: 0x"+Integer.toHexString(text.charAt(0))+"]");
}
LocationInfo li = sr.getLocationInfo();
System.out.println(" [Loc, start: "+li.getStartLocation()+", end: "+li.getEndLocation()+"]");
} else if (type == SPACE) {
System.out.print(" Ws = '"+text+"'.");
char c = (text.length() == 0) ? ' ': text.charAt(text.length()-1);
if (c != '\r' && c != '\n') {
System.out.println();
}
} else if (type == DTD) {
System.out.println(" DTD;");
List<Object> entities = (List<Object>) sr.getProperty("javax.xml.stream.entities");
List<Object> notations = (List<Object>) sr.getProperty("javax.xml.stream.notations");
int entCount = (entities == null) ? -1 : entities.size();
int notCount = (notations == null) ? -1 : notations.size();
System.out.print(" ("+entCount+" entities, "+notCount
+" notations), sysid ");
System.out.print(", declaration = <<");
System.out.print(text);
System.out.println(">>");
} else if (type == ENTITY_REFERENCE) {
// entity ref
System.out.println(" Entity ref: &"+sr.getLocalName()+" -> '"+sr.getText()+"'.");
//hasName = false; // to suppress further output
} else { // PI?
;
}
}
if (type == PROCESSING_INSTRUCTION) {
System.out.println(" PI target = '"+sr.getPITarget()+"'.");
System.out.println(" PI data = '"+sr.getPIData()+"'.");
} else if (type == START_ELEMENT) {
String prefix = sr.getPrefix();
System.out.print('<');
if (prefix != null && prefix.length() > 0) {
System.out.print(prefix);
System.out.print(':');
}
System.out.print(sr.getLocalName());
//System.out.println("[first char 0x"+Integer.toHexString(sr.getLocalName().charAt(0))+"]");
System.out.print(" [QNameNS: "+sr.getName().getNamespaceURI()+"]");
System.out.print(" {ns '");
System.out.print(sr.getNamespaceURI());
System.out.print("'}> ");
int count = sr.getAttributeCount();
int nsCount = sr.getNamespaceCount();
System.out.println(" ["+nsCount+" ns, "+count+" attrs]");
// debugging:
for (int i = 0; i < nsCount; ++i) {
System.out.println(" ns#"+i+": '"+sr.getNamespacePrefix(i)
+"' -> '"+sr.getNamespaceURI(i)
+"'");
}
for (int i = 0; i < count; ++i) {
String val = sr.getAttributeValue(i);
System.out.print(" attr#"+i+": "+sr.getAttributePrefix(i)
+":"+sr.getAttributeLocalName(i)
+" ("+sr.getAttributeNamespace(i)
+") -> '"+val
+"' ["+(val.length())+"]");
System.out.println(sr.isAttributeSpecified(i) ?
"[specified]" : "[Default]");
}
} else if (type == END_ELEMENT) {
System.out.print("</");
String prefix = sr.getPrefix();
if (prefix != null && prefix.length() > 0) {
System.out.print(prefix);
System.out.print(':');
}
System.out.print(sr.getLocalName());
System.out.print(" {ns '");
System.out.print(sr.getNamespaceURI());
System.out.print("'}> ");
int nsCount = sr.getNamespaceCount();
System.out.println(" ["+nsCount+" ns unbound]");
} else if (type == START_DOCUMENT) { // only for multi-doc mode
System.out.print("XML-DECL: version = '"+sr.getVersion()
+"', xml-decl-encoding = '"+sr.getCharacterEncodingScheme()
+"', app-encoding = '"+sr.getEncoding()
+"', stand-alone set: "+sr.standaloneSet());
}
}
return total;
}
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
System.err.println("Usage: java ... "+TestStreamReader.class+" [file]");
System.exit(1);
}
try {
int total = new TestStreamReader().test(new File(args[0]));
System.out.println();
System.out.println("Total: "+total);
} catch (Throwable t) {
System.err.println("Error: "+t);
t.printStackTrace();
}
}
}