use of org.apache.axiom.truth.xml.spi.Event in project webservices-axiom by apache.
the class XMLSubject method hasSameContentAs.
/**
* Fails unless the subject represents the same XML as the given object.
*
* @param other
* the object to compare with
*/
public void hasSameContentAs(Object other) {
try {
Traverser actual = createTraverser(xml);
XML expectedXML = XMLTruth.xml(other);
Traverser expected = createTraverser(expectedXML);
while (true) {
Event actualEvent = actual.next();
Event expectedEvent = expected.next();
if (expectedEvent == Event.WHITESPACE || expectedEvent == Event.TEXT) {
if (!xml.isReportingElementContentWhitespace()) {
assertThat(actualEvent).isEqualTo(Event.TEXT);
} else if (treatWhitespaceAsText || !expectedXML.isReportingElementContentWhitespace()) {
assertThat(actualEvent).isAnyOf(Event.WHITESPACE, Event.TEXT);
} else {
assertThat(actualEvent).isEqualTo(expectedEvent);
}
} else {
assertThat(actualEvent).isEqualTo(expectedEvent);
}
if (expectedEvent == null) {
break;
}
switch(expectedEvent) {
case DOCUMENT_TYPE:
assertThat(actual.getRootName()).isEqualTo(expected.getRootName());
assertThat(actual.getPublicId()).isEqualTo(expected.getPublicId());
assertThat(actual.getSystemId()).isEqualTo(expected.getSystemId());
break;
case START_ELEMENT:
QName actualQName = actual.getQName();
Map<QName, String> actualAttributes = actual.getAttributes();
QName expectedQName = expected.getQName();
Map<QName, String> expectedAttributes = expected.getAttributes();
assertThat(actualQName).isEqualTo(expectedQName);
assertThat(actualAttributes).isEqualTo(expectedAttributes);
if (!ignoreNamespacePrefixes) {
assertThat(actualQName.getPrefix()).isEqualTo(expectedQName.getPrefix());
if (expectedAttributes != null) {
assertThat(extractPrefixes(actualAttributes.keySet())).isEqualTo(extractPrefixes(expectedAttributes.keySet()));
}
}
if (!ignoreNamespaceDeclarations) {
assertThat(actual.getNamespaces()).isEqualTo(expected.getNamespaces());
}
break;
case END_ELEMENT:
break;
case TEXT:
case WHITESPACE:
case COMMENT:
case CDATA_SECTION:
assertThat(actual.getText()).isEqualTo(expected.getText());
break;
case ENTITY_REFERENCE:
if (expandEntityReferences) {
throw new IllegalStateException();
}
assertThat(actual.getEntityName()).isEqualTo(expected.getEntityName());
break;
case PROCESSING_INSTRUCTION:
assertThat(actual.getPITarget()).isEqualTo(expected.getPITarget());
assertThat(actual.getPIData()).isEqualTo(expected.getPIData());
break;
default:
throw new IllegalStateException();
}
}
} catch (TraverserException ex) {
// TODO: check how to fail properly
throw new RuntimeException(ex);
}
}
use of org.apache.axiom.truth.xml.spi.Event in project webservices-axiom by apache.
the class RedundantNamespaceDeclarationFilter method next.
@Override
public Event next() throws TraverserException {
Event event = super.next();
if (event == Event.START_ELEMENT) {
Map<String, String> namespaces = super.getNamespaces();
if (namespaces != null) {
for (Iterator<Map.Entry<String, String>> it = namespaces.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, String> namespace = it.next();
if (namespace.getValue().equals(lookupNamespaceURI(namespace.getKey()))) {
it.remove();
}
}
if (namespaces.isEmpty()) {
namespaces = null;
}
}
stack.add(namespaces);
} else if (event == Event.END_ELEMENT) {
stack.remove(stack.size() - 1);
}
return event;
}
use of org.apache.axiom.truth.xml.spi.Event in project webservices-axiom by apache.
the class XMLSubject method createTraverser.
private Traverser createTraverser(XML xml) throws TraverserException {
Traverser traverser = xml.createTraverser(expandEntityReferences);
if (ignoreWhitespaceInPrologAndEpilog || ignorePrologAndEpilog) {
final boolean onlyWhitespace = !ignorePrologAndEpilog;
traverser = new Filter(traverser) {
private int depth;
@Override
public Event next() throws TraverserException {
Event event;
while ((event = super.next()) != null) {
switch(event) {
case START_ELEMENT:
depth++;
break;
case END_ELEMENT:
depth--;
break;
default:
if (onlyWhitespace) {
break;
}
// Fall through
case WHITESPACE:
if (depth == 0) {
continue;
}
}
break;
}
return event;
}
};
}
final Set<Event> ignoredEvents = new HashSet<>();
if (ignoreComments) {
ignoredEvents.add(Event.COMMENT);
}
if (ignoreWhitespace || ignoreElementContentWhitespace) {
ignoredEvents.add(Event.WHITESPACE);
}
if (!ignoredEvents.isEmpty()) {
traverser = new Filter(traverser) {
@Override
public Event next() throws TraverserException {
Event event;
while (ignoredEvents.contains(event = super.next())) {
// loop
}
return event;
}
};
}
traverser = new CoalescingFilter(traverser);
if (ignoreWhitespace) {
traverser = new Filter(traverser) {
@Override
public Event next() throws TraverserException {
Event event = super.next();
if (event == Event.TEXT) {
String text = getText();
for (int i = 0; i < text.length(); i++) {
if (" \r\n\t".indexOf(text.charAt(i)) == -1) {
return Event.TEXT;
}
}
return super.next();
} else {
return event;
}
}
};
}
if (ignoreRedundantNamespaceDeclarations && !ignoreNamespaceDeclarations) {
traverser = new RedundantNamespaceDeclarationFilter(traverser);
}
return traverser;
}
use of org.apache.axiom.truth.xml.spi.Event in project webservices-axiom by apache.
the class CoalescingFilter method next.
@Override
public Event next() throws TraverserException {
savedText = null;
if (savedEvent != null) {
Event event = savedEvent;
savedEvent = null;
return event;
} else {
Event event = super.next();
if (event == Event.TEXT || event == Event.WHITESPACE) {
String text = super.getText();
StringBuilder buffer = null;
Event nextEvent;
while ((nextEvent = super.next()) == event) {
if (buffer == null) {
buffer = new StringBuilder(text);
}
buffer.append(super.getText());
}
savedEvent = nextEvent;
savedText = buffer == null ? text : buffer.toString();
}
return event;
}
}
Aggregations