use of javax.xml.stream.XMLEventReader in project camel by apache.
the class StaxConverterTest method testEncodingXmlEventReader.
public void testEncodingXmlEventReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLEventReader reader = null;
XMLEventWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
while (reader.hasNext()) {
writer.add(reader.nextEvent());
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
// normalize the auotation mark
if (result.indexOf('\'') > 0) {
result = result.replace('\'', '"');
}
boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
assertTrue("Should match header", equals);
}
use of javax.xml.stream.XMLEventReader in project hadoop by apache.
the class NodeInfo method parseConf.
private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException {
QName configuration = new QName("configuration");
QName property = new QName("property");
List<NodeInfo> nodes = new ArrayList<NodeInfo>();
Stack<NodeInfo> parsed = new Stack<NodeInfo>();
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(in);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement currentElement = event.asStartElement();
NodeInfo currentNode = new NodeInfo(currentElement);
if (parsed.isEmpty()) {
if (!currentElement.getName().equals(configuration)) {
return null;
}
} else {
NodeInfo parentNode = parsed.peek();
QName parentName = parentNode.getStartElement().getName();
if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) {
@SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes();
while (it.hasNext()) {
currentNode.addAttribute(it.next());
}
} else if (parentName.equals(property)) {
parentNode.addElement(currentElement);
}
}
parsed.push(currentNode);
} else if (event.isEndElement()) {
NodeInfo node = parsed.pop();
if (parsed.size() == 1) {
nodes.add(node);
}
} else if (event.isCharacters()) {
if (2 < parsed.size()) {
NodeInfo parentNode = parsed.pop();
StartElement parentElement = parentNode.getStartElement();
NodeInfo grandparentNode = parsed.peek();
if (grandparentNode.getElement(parentElement) == null) {
grandparentNode.setElement(parentElement, event.asCharacters());
}
parsed.push(parentNode);
}
}
}
return nodes;
}
use of javax.xml.stream.XMLEventReader in project GCViewer by chewiebug.
the class DataReaderIBM_J9_R28 method read.
@Override
public GCModel read() throws IOException {
GCModel model = new GCModel();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(in);
GCEvent currentGcEvent = null;
String eventNameStart = null;
while (eventReader.hasNext()) {
try {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
switch(startElement.getName().getLocalPart()) {
case VERBOSEGC:
handleVerboseGC(startElement);
break;
case INITIALIZED:
handleInitialized(eventReader);
break;
case EXCLUSIVE_START:
currentGcEvent = handleExclusiveStart(startElement);
break;
case SYS_START:
assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
eventNameStart = handleSysStart(eventReader, startElement);
break;
case AF_START:
assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
eventNameStart = handleAfStart(eventReader, startElement);
break;
case GC_START:
handleGcStart(eventReader, startElement, currentGcEvent, eventNameStart);
break;
case GC_END:
if (currentGcEvent.getTypeAsString() != null) {
handleGcEnd(eventReader, currentGcEvent);
}
break;
case EXCLUSIVE_END:
handleExclusiveEnd(startElement, currentGcEvent);
if (currentGcEvent.getExtendedType() == null) {
if (getLogger().isLoggable(Level.FINE))
getLogger().fine("event at " + in.getLineNumber() + " doesn't contain any information, the parser can handle");
} else {
model.add(currentGcEvent);
}
currentGcEvent = null;
eventNameStart = null;
break;
}
}
} catch (Exception e) {
if (e instanceof XMLStreamException) {
throw e;
}
if (getLogger().isLoggable(Level.WARNING))
getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
if (getLogger().isLoggable(Level.FINE))
getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
}
}
} catch (XMLStreamException e) {
if (getLogger().isLoggable(Level.WARNING))
getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
if (getLogger().isLoggable(Level.FINE))
getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
} finally {
if (getLogger().isLoggable(Level.INFO))
getLogger().info("Done reading.");
}
return model;
}
use of javax.xml.stream.XMLEventReader in project buck by facebook.
the class FatJar method load.
/**
* @return the {@link FatJar} object deserialized from the resource name via {@code loader}.
*/
public static FatJar load(ClassLoader loader) throws XMLStreamException, JAXBException, IOException {
InputStream inputStream = loader.getResourceAsStream(FAT_JAR_INFO_RESOURCE);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
try {
XMLEventReader xmlEventReader = XMLInputFactory.newFactory().createXMLEventReader(bufferedInputStream);
JAXBContext context = JAXBContext.newInstance(FatJar.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<FatJar> jaxbElementA = unmarshaller.unmarshal(xmlEventReader, FatJar.class);
return jaxbElementA.getValue();
} finally {
bufferedInputStream.close();
}
} finally {
inputStream.close();
}
}
use of javax.xml.stream.XMLEventReader in project hibernate-orm by hibernate.
the class XmlParserHelper method getJaxbRoot.
public <T> T getJaxbRoot(InputStream stream, Class<T> clazz, Schema schema) throws XmlParsingException {
XMLEventReader staxEventReader;
try {
staxEventReader = createXmlEventReader(stream);
} catch (XMLStreamException e) {
throw new XmlParsingException("Unable to create stax reader", e);
}
ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
try {
staxEventReader = new JpaNamespaceTransformingEventReader(staxEventReader);
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(handler);
return clazz.cast(unmarshaller.unmarshal(staxEventReader));
} catch (JAXBException e) {
StringBuilder builder = new StringBuilder();
builder.append("Unable to perform unmarshalling at line number ");
builder.append(handler.getLineNumber());
builder.append(" and column ");
builder.append(handler.getColumnNumber());
builder.append(". Message: ");
builder.append(handler.getMessage());
throw new XmlParsingException(builder.toString(), e);
}
}
Aggregations