use of javax.xml.stream.XMLStreamException 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.XMLStreamException in project elasticsearch by elastic.
the class Ec2DiscoveryClusterFormationTests method startHttpd.
/**
* Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call
*/
@BeforeClass
public static void startHttpd() throws Exception {
logDir = createTempDir();
httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
httpServer.createContext("/", (s) -> {
Headers headers = s.getResponseHeaders();
headers.add("Content-Type", "text/xml; charset=UTF-8");
String action = null;
for (NameValuePair parse : URLEncodedUtils.parse(IOUtils.toString(s.getRequestBody()), StandardCharsets.UTF_8)) {
if ("Action".equals(parse.getName())) {
action = parse.getValue();
break;
}
}
assertThat(action, equalTo("DescribeInstances"));
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
StringWriter out = new StringWriter();
XMLStreamWriter sw;
try {
sw = xmlOutputFactory.createXMLStreamWriter(out);
sw.writeStartDocument();
String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/";
sw.setDefaultNamespace(namespace);
sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace);
{
sw.writeStartElement("requestId");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("reservationSet");
{
Path[] files = FileSystemUtils.files(logDir);
for (int i = 0; i < files.length; i++) {
Path resolve = files[i].resolve("transport.ports");
if (Files.exists(resolve)) {
List<String> addresses = Files.readAllLines(resolve);
Collections.shuffle(addresses, random());
sw.writeStartElement("item");
{
sw.writeStartElement("reservationId");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("instancesSet");
{
sw.writeStartElement("item");
{
sw.writeStartElement("instanceId");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("imageId");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("instanceState");
{
sw.writeStartElement("code");
sw.writeCharacters("16");
sw.writeEndElement();
sw.writeStartElement("name");
sw.writeCharacters("running");
sw.writeEndElement();
}
sw.writeEndElement();
sw.writeStartElement("privateDnsName");
sw.writeCharacters(addresses.get(0));
sw.writeEndElement();
sw.writeStartElement("dnsName");
sw.writeCharacters(addresses.get(0));
sw.writeEndElement();
sw.writeStartElement("instanceType");
sw.writeCharacters("m1.medium");
sw.writeEndElement();
sw.writeStartElement("placement");
{
sw.writeStartElement("availabilityZone");
sw.writeCharacters("use-east-1e");
sw.writeEndElement();
sw.writeEmptyElement("groupName");
sw.writeStartElement("tenancy");
sw.writeCharacters("default");
sw.writeEndElement();
}
sw.writeEndElement();
sw.writeStartElement("privateIpAddress");
sw.writeCharacters(addresses.get(0));
sw.writeEndElement();
sw.writeStartElement("ipAddress");
sw.writeCharacters(addresses.get(0));
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
}
}
sw.writeEndElement();
}
sw.writeEndElement();
sw.writeEndDocument();
sw.flush();
final byte[] responseAsBytes = out.toString().getBytes(StandardCharsets.UTF_8);
s.sendResponseHeaders(200, responseAsBytes.length);
OutputStream responseBody = s.getResponseBody();
responseBody.write(responseAsBytes);
responseBody.close();
} catch (XMLStreamException e) {
Loggers.getLogger(Ec2DiscoveryClusterFormationTests.class).error("Failed serializing XML", e);
throw new RuntimeException(e);
}
});
httpServer.start();
}
use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.
the class XmlImporter method descendElement.
private static final JSONObject descendElement(XMLStreamReader parser, PreviewParsingState state) {
JSONObject result = new JSONObject();
{
String name = parser.getLocalName();
JSONUtilities.safePut(result, "n", name);
String prefix = parser.getPrefix();
if (prefix != null) {
JSONUtilities.safePut(result, "p", prefix);
}
String nsUri = parser.getNamespaceURI();
if (nsUri != null) {
JSONUtilities.safePut(result, "uri", nsUri);
}
}
int namespaceCount = parser.getNamespaceCount();
if (namespaceCount > 0) {
JSONArray namespaces = new JSONArray();
JSONUtilities.safePut(result, "ns", namespaces);
for (int i = 0; i < namespaceCount; i++) {
JSONObject namespace = new JSONObject();
JSONUtilities.append(namespaces, namespace);
JSONUtilities.safePut(namespace, "p", parser.getNamespacePrefix(i));
JSONUtilities.safePut(namespace, "uri", parser.getNamespaceURI(i));
}
}
int attributeCount = parser.getAttributeCount();
if (attributeCount > 0) {
JSONArray attributes = new JSONArray();
JSONUtilities.safePut(result, "a", attributes);
for (int i = 0; i < attributeCount; i++) {
JSONObject attribute = new JSONObject();
JSONUtilities.append(attributes, attribute);
JSONUtilities.safePut(attribute, "n", parser.getAttributeLocalName(i));
JSONUtilities.safePut(attribute, "v", parser.getAttributeValue(i));
String prefix = parser.getAttributePrefix(i);
if (prefix != null) {
JSONUtilities.safePut(attribute, "p", prefix);
}
}
}
JSONArray children = new JSONArray();
try {
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
int tokenType = parser.next();
state.tokenCount++;
if (tokenType == XMLStreamConstants.END_ELEMENT) {
break;
} else if (tokenType == XMLStreamConstants.START_ELEMENT) {
JSONObject childElement = descendElement(parser, state);
if (childElement != null) {
JSONUtilities.append(children, childElement);
}
} else if (tokenType == XMLStreamConstants.CHARACTERS || tokenType == XMLStreamConstants.CDATA || tokenType == XMLStreamConstants.SPACE) {
JSONObject childElement = new JSONObject();
JSONUtilities.safePut(childElement, "t", parser.getText());
JSONUtilities.append(children, childElement);
} else {
// ignore everything else
}
}
} catch (XMLStreamException e) {
logger.error("Error generating parser UI initialization data for XML file", e);
}
if (children.length() > 0) {
JSONUtilities.safePut(result, "c", children);
}
return result;
}
use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.
the class XmlImporter method wrapPrefixRemovingInputStream.
private static final InputStream wrapPrefixRemovingInputStream(InputStream inputStream) throws XMLStreamException, IOException {
PushbackInputStream pis = new PushbackInputStream(inputStream);
int b;
int count = 0;
while (count < 100 && (b = pis.read()) >= 0) {
if (++count > 100) {
throw new XMLStreamException("File starts with too much non-XML content to skip over");
} else if (b == '<') {
pis.unread(b);
break;
}
}
return pis;
}
use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.
the class XmlImporter method createParserUIInitializationData.
@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
try {
if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
InputStream is = new FileInputStream(file);
try {
XMLStreamReader parser = createXMLStreamReader(is);
PreviewParsingState state = new PreviewParsingState();
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
int tokenType = parser.next();
state.tokenCount++;
if (tokenType == XMLStreamConstants.START_ELEMENT) {
JSONObject rootElement = descendElement(parser, state);
if (rootElement != null) {
JSONUtilities.safePut(options, "dom", rootElement);
break;
}
} else {
// ignore everything else
}
}
} catch (XMLStreamException e) {
logger.warn("Error generating parser UI initialization data for XML file", e);
} finally {
is.close();
}
}
} catch (IOException e) {
logger.error("Error generating parser UI initialization data for XML file", e);
}
return options;
}
Aggregations