test.TestStreamReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aalto-xml Show documentation
Show all versions of aalto-xml Show documentation
Ultra-high performance non-blocking XML processor (Stax/Stax2, SAX/SAX2)
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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy