use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventFactoryTest method testCreateCData.
@Test
public void testCreateCData() throws XMLStreamException {
Characters event = factory.createCData("foobar");
verify(event, XMLStreamConstants.CDATA, "<![CDATA[foobar]]>");
}
use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventFactoryTest method testCreateIgnorableSpace.
@Test
public void testCreateIgnorableSpace() throws XMLStreamException {
Characters event = factory.createIgnorableSpace(" ");
verify(event, XMLStreamConstants.SPACE, "");
Assert.assertTrue(event.isWhiteSpace());
Assert.assertTrue(event.isIgnorableWhiteSpace());
}
use of javax.xml.stream.events.Characters in project wso2-synapse by wso2.
the class SimpleXMLEventWriter method add.
@Override
public void add(XMLEvent event) throws XMLStreamException {
switch(event.getEventType()) {
case XMLStreamConstants.ATTRIBUTE:
Attribute attribute = (Attribute) event;
QName attrName = attribute.getName();
delegate.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attribute.getValue());
break;
case XMLStreamConstants.END_DOCUMENT:
delegate.writeEndDocument();
break;
case XMLStreamConstants.END_ELEMENT:
delegate.writeEndElement();
break;
case XMLStreamConstants.NAMESPACE:
Namespace namespace = (Namespace) event;
delegate.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
break;
case XMLStreamConstants.START_DOCUMENT:
StartDocument startDocument = (StartDocument) event;
if (startDocument.encodingSet()) {
// encoding defined?
delegate.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
} else {
delegate.writeStartDocument(startDocument.getVersion());
}
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
QName elemName = startElement.getName();
delegate.writeStartElement(elemName.getPrefix(), elemName.getLocalPart(), elemName.getNamespaceURI());
Iterator<?> namespaces = startElement.getNamespaces();
while (namespaces.hasNext()) {
add((Namespace) namespaces.next());
}
Iterator<?> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
add((Attribute) attributes.next());
}
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
Characters characters = event.asCharacters();
if (characters.isCData()) {
delegate.writeCData(characters.getData());
} else {
delegate.writeCharacters(characters.getData());
}
break;
case XMLStreamConstants.COMMENT:
delegate.writeComment(((Comment) event).getText());
break;
case XMLStreamConstants.DTD:
delegate.writeDTD(((DTD) event).getDocumentTypeDeclaration());
break;
case XMLStreamConstants.ENTITY_REFERENCE:
delegate.writeEntityRef(((EntityReference) event).getName());
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
delegate.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
break;
case XMLStreamConstants.SPACE:
break;
default:
throw new XMLStreamException("Cannot write event " + event);
}
}
use of javax.xml.stream.events.Characters in project tomee by apache.
the class StaxCompare method compare.
public static boolean compare(final XMLEventReader a, final XMLEventReader b, final StringBuilder message) {
XMLEvent eventA;
XMLEvent eventB;
int eventType;
try {
while ((eventA = nextInterestingEvent(a)) != null & (eventB = nextInterestingEvent(b)) != null) {
if ((eventType = eventA.getEventType()) != eventB.getEventType()) {
message.append("events of different types: ").append(eventA).append(", ").append(eventB);
return false;
}
if (eventType == XMLStreamConstants.START_ELEMENT) {
final StartElement startA = eventA.asStartElement();
final StartElement startB = eventB.asStartElement();
if (!startA.getName().getLocalPart().equals(startB.getName().getLocalPart())) {
message.append("Different elements ").append(startA.getName()).append(", ").append(startB.getName()).append(" at location ").append(eventA.getLocation());
return false;
}
} else if (eventType == XMLStreamConstants.END_ELEMENT) {
final EndElement endA = eventA.asEndElement();
final EndElement endB = eventB.asEndElement();
if (!endA.getName().getLocalPart().equals(endB.getName().getLocalPart())) {
message.append("Different elements ").append(endA.getName()).append(", ").append(endB.getName()).append(" at location ").append(eventA.getLocation());
return false;
}
} else if (eventType == XMLStreamConstants.CHARACTERS) {
final Characters endA = eventA.asCharacters();
final Characters endB = eventB.asCharacters();
if (!endA.getData().equals(endB.getData())) {
message.append("Different content ").append(endA.getData()).append(", ").append(endB.getData()).append(" at location ").append(eventA.getLocation());
return false;
}
}
}
if (eventA != null) {
message.append("A is longer: ").append(eventA.getLocation());
return false;
}
if (eventB != null) {
message.append("B is longer: ").append(eventB.getLocation());
return false;
}
} catch (final XMLStreamException e) {
message.append("Exception processing ").append(e.getMessage());
return false;
}
return true;
}
use of javax.xml.stream.events.Characters in project rpki-validator-3 by RIPE-NCC.
the class RrdpParser method snapshot.
public Snapshot snapshot(final InputStream inputStream) {
final Map<String, SnapshotObject> objects = new HashMap<>();
try {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLEventReader eventReader = factory.createXMLEventReader(inputStream);
String sessionId = null;
BigInteger serial = null;
String uri = 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().toLowerCase(Locale.ROOT);
switch(qName) {
case "publish":
uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
inPublishElement = true;
break;
case "snapshot":
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().toLowerCase(Locale.ROOT);
if ("publish".equals(qqName)) {
final byte[] decoded = decoder.decode(base64.toString());
objects.put(uri, new SnapshotObject(decoded, uri));
inPublishElement = false;
base64 = new StringBuilder();
}
break;
}
}
return new Snapshot(objects, sessionId, serial);
} catch (XMLStreamException e) {
throw new RrdpException("Couldn't parse snapshot: ", e);
}
}
Aggregations