use of javax.xml.stream.XMLInputFactory in project camel by apache.
the class StaxConverter method createXMLInputFactory.
public static XMLInputFactory createXMLInputFactory(boolean nsAware) {
XMLInputFactory factory = XMLInputFactory.newInstance();
setProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, nsAware);
setProperty(factory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
setProperty(factory, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
setProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setXMLResolver(new XMLResolver() {
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
throw new XMLStreamException("Reading external entities is disabled");
}
});
if (isWoodstox(factory)) {
// just log a debug as we are good then
LOG.debug("Created Woodstox XMLInputFactory: {}", factory);
} else {
// log a hint that woodstock may be a better factory to use
LOG.info("Created XMLInputFactory: {}. DOMSource/DOMResult may have issues with {}. We suggest using Woodstox.", factory, factory);
}
return factory;
}
use of javax.xml.stream.XMLInputFactory 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.XMLInputFactory in project hbase by apache.
the class RemoteAdmin method getInputStream.
/**
* Convert the REST server's response to an XML reader.
*
* @param response The REST server's response.
* @return A reader over the parsed XML document.
* @throws IOException If the document fails to parse
*/
private XMLStreamReader getInputStream(Response response) throws IOException {
try {
// Prevent the parser from reading XMl with external entities defined
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
return xif.createXMLStreamReader(new ByteArrayInputStream(response.getBody()));
} catch (XMLStreamException e) {
throw new IOException("Failed to parse XML", e);
}
}
use of javax.xml.stream.XMLInputFactory 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.XMLInputFactory in project OpenRefine by OpenRefine.
the class XmlImporter method createXMLStreamReader.
private static final XMLStreamReader createXMLStreamReader(InputStream inputStream) throws XMLStreamException, IOException {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_COALESCING, true);
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
return factory.createXMLStreamReader(wrapPrefixRemovingInputStream(inputStream));
}
Aggregations