use of javax.xml.stream.XMLStreamException in project midpoint by Evolveum.
the class Validator method readFromStreamAndValidate.
private EventResult readFromStreamAndValidate(XMLStreamReader stream, OperationResult objectResult, Map<String, String> rootNamespaceDeclarations, OperationResult validatorResult) {
objectResult.addContext(START_LINE_NUMBER, stream.getLocation().getLineNumber());
Document objectDoc;
try {
// Parse the object from stream to DOM
objectDoc = domConverter.buildDocument(stream);
} catch (XMLStreamException ex) {
validatorResult.recordFatalError("XML parsing error: " + ex.getMessage(), ex);
if (handler != null) {
handler.handleGlobalError(validatorResult);
}
objectResult.recordFatalError(ex);
return EventResult.skipObject(ex.getMessage());
}
objectResult.addContext(END_LINE_NUMBER, stream.getLocation().getLineNumber());
// This element may not have complete namespace definitions for a
// stand-alone
// processing, therefore copy namespace definitions from the root
// element
Element objectElement = DOMUtil.getFirstChildElement(objectDoc);
DOMUtil.setNamespaceDeclarations(objectElement, rootNamespaceDeclarations);
return validateObjectInternal(objectElement, objectResult, validatorResult);
}
use of javax.xml.stream.XMLStreamException in project midpoint by Evolveum.
the class MidpointFunctionsImpl method parseXmlToMap.
@Override
public Map<String, String> parseXmlToMap(String xml) {
Map<String, String> resultingMap = new HashMap<String, String>();
if (xml != null && !xml.isEmpty()) {
XMLInputFactory factory = XMLInputFactory.newInstance();
String value = "";
String startName = "";
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
boolean isRootElement = true;
try {
XMLEventReader eventReader = factory.createXMLEventReader(stream);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
Integer code = event.getEventType();
if (code == XMLStreamConstants.START_ELEMENT) {
StartElement startElement = event.asStartElement();
startName = startElement.getName().getLocalPart();
if (!isRootElement) {
resultingMap.put(startName, null);
} else {
isRootElement = false;
}
} else if (code == XMLStreamConstants.CHARACTERS) {
Characters characters = event.asCharacters();
if (!characters.isWhiteSpace()) {
StringBuilder valueBuilder;
if (value != null) {
valueBuilder = new StringBuilder(value).append(" ").append(characters.getData().toString());
} else {
valueBuilder = new StringBuilder(characters.getData().toString());
}
value = valueBuilder.toString();
}
} else if (code == XMLStreamConstants.END_ELEMENT) {
EndElement endElement = event.asEndElement();
String endName = endElement.getName().getLocalPart();
if (endName.equals(startName)) {
if (value != null) {
resultingMap.put(endName, value);
value = null;
}
} else {
LOGGER.trace("No value between xml tags, tag name : {0}", endName);
}
} else if (code == XMLStreamConstants.END_DOCUMENT) {
isRootElement = true;
}
}
} catch (XMLStreamException e) {
StringBuilder error = new StringBuilder("Xml stream exception wile parsing xml string").append(e.getLocalizedMessage());
throw new SystemException(error.toString());
}
} else {
LOGGER.trace("Input xml string null or empty.");
}
return resultingMap;
}
use of javax.xml.stream.XMLStreamException in project jdk8u_jdk by JetBrains.
the class SkipDTDTest method test.
@Test
public void test() {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
for (int i = 0; i < 3; i++) {
runReader(factory, i);
}
} catch (XMLStreamException xe) {
xe.printStackTrace();
Assert.fail(xe.getMessage());
}
}
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;
}
Aggregations