use of javax.xml.stream.events.XMLEvent in project structr by structr.
the class XMLStructureAnalyzer method getStructure.
public Map<String, Object> getStructure(final int threshold) {
int level = 0;
int exceptionCount = 0;
while (reader.hasNext() && analysisCount < threshold && exceptionCount < XML_MAX_EXCEPTIONS) {
try {
final XMLEvent event = reader.nextEvent();
final String tagName = getTagName(event);
switch(event.getEventType()) {
case XMLEvent.START_ELEMENT:
current = new Element(current, tagName, level++);
current.attributes.addAll(getAttributes(event.asStartElement()));
break;
case XMLEvent.END_ELEMENT:
// one level up
current = current.parent;
level--;
// start analysis for elements with more than one occurrence
if (current != null && current.level == 0) {
analyze(current, structure);
analysisCount++;
}
break;
}
} catch (XMLStreamException strex) {
logger.warn(strex.getMessage());
exceptionCount++;
}
}
if (exceptionCount == XML_MAX_EXCEPTIONS) {
logger.info("Stopping XML processing at error threshold ({})", XML_MAX_EXCEPTIONS);
}
return structure;
}
use of javax.xml.stream.events.XMLEvent in project sis by apache.
the class TransformingReader method nextTag.
/**
* Forwards the call and keep trace of the XML elements opened up to this point.
*/
@Override
public XMLEvent nextTag() throws XMLStreamException {
final XMLEvent event = in.nextTag();
final XMLEvent next = nextEvent;
if (next != null) {
nextEvent = null;
switch(event.getEventType()) {
case START_ELEMENT:
case END_ELEMENT:
{
assert isWrapper(event, next) : event;
return event;
}
}
}
return convert(event);
}
use of javax.xml.stream.events.XMLEvent in project rpki-validator-3 by RIPE-NCC.
the class RrdpParser method delta.
public Delta delta(final InputStream inputStream) {
final Map<String, DeltaElement> objects = new HashMap<>();
try {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLEventReader eventReader = factory.createXMLEventReader(inputStream);
String sessionId = null;
BigInteger serial = null;
String uri = null;
String hash = null;
StringBuilder base64 = new StringBuilder();
boolean inPublishElement = false;
final Base64.Decoder decoder = Base64.getDecoder();
while (eventReader.hasNext()) {
final XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
final StartElement startElement = event.asStartElement();
final String qName = startElement.getName().getLocalPart();
switch(qName) {
case "publish":
uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
hash = getAttr(startElement, "hash");
inPublishElement = true;
break;
case "withdraw":
uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
hash = getAttr(startElement, "hash", "Hash is not present in 'withdraw' element");
break;
case "delta":
serial = new BigInteger(getAttr(startElement, "serial", "Notification serial is not present"));
sessionId = getAttr(startElement, "session_id", "Session id is not present");
break;
}
break;
case XMLStreamConstants.CHARACTERS:
final Characters characters = event.asCharacters();
if (inPublishElement) {
final String thisBase64 = characters.getData();
base64.append(thisBase64.replaceAll("\\s", ""));
}
break;
case XMLStreamConstants.END_ELEMENT:
final EndElement endElement = event.asEndElement();
final String qqName = endElement.getName().getLocalPart();
switch(qqName) {
case "publish":
final byte[] decoded = decoder.decode(base64.toString());
objects.put(uri, new DeltaPublish(decoded, uri, Hex.parse(hash)));
base64 = new StringBuilder();
break;
case "withdraw":
objects.put(uri, new DeltaWithdraw(uri, Hex.parse(hash)));
break;
}
break;
}
}
return new Delta(objects, sessionId, serial);
} catch (XMLStreamException e) {
throw new RrdpException("Couldn't parse snapshot: ", e);
}
}
use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.
the class XML2HTTP method slurpCharacterData.
private static String slurpCharacterData(XMLEventReader parser, StartElement sevent) throws XMLStreamException, XML2HTTPException {
String name = sevent.getName().getLocalPart();
StringBuilder value = new StringBuilder();
while (parser.hasNext()) {
XMLEvent event = parser.nextEvent();
if (event.isCharacters()) {
value.append(event.asCharacters().getData());
} else if (event.isEndElement()) {
break;
} else {
throw new XML2HTTPException("XML-HTTP doc <" + name + "> element contains non-character data.");
}
}
return value.toString();
}
use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.
the class TextUtil method isValidXMLSnippet.
/**
* Checks whether s is a valid (well-formed and balanced) XML snippet.
*
* Note that attributes escaped by single quotes are accepted (which is illegal by spec).
*/
public static boolean isValidXMLSnippet(String s) {
try {
XMLEventReader parser;
synchronized (xmlInputFactory) {
parser = xmlInputFactory.createXMLEventReader(new StringReader("<a>" + s + "</a>"));
}
XMLEvent event = null;
while (parser.hasNext()) {
event = (XMLEvent) parser.next();
}
return event != null && event.isEndDocument();
} catch (Exception e) {
log.error("", e);
return false;
}
}
Aggregations