Search in sources :

Example 1 with TraverserException

use of org.apache.axiom.truth.xml.spi.TraverserException 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);
    }
}
Also used : XML(org.apache.axiom.truth.xml.spi.XML) QName(javax.xml.namespace.QName) Traverser(org.apache.axiom.truth.xml.spi.Traverser) Event(org.apache.axiom.truth.xml.spi.Event) TraverserException(org.apache.axiom.truth.xml.spi.TraverserException)

Example 2 with TraverserException

use of org.apache.axiom.truth.xml.spi.TraverserException in project webservices-axiom by apache.

the class StAXTraverser method next.

@Override
public Event next() throws TraverserException {
    try {
        int event;
        if (depth == 0) {
            if (atStart) {
                atStart = false;
                event = XMLStreamReader.START_ELEMENT;
            } else {
                event = XMLStreamReader.END_DOCUMENT;
            }
        } else {
            event = reader.hasNext() ? reader.next() : XMLStreamReader.END_DOCUMENT;
        }
        switch(event) {
            case XMLStreamReader.DTD:
                return Event.DOCUMENT_TYPE;
            case XMLStreamReader.START_ELEMENT:
                if (depth != -1) {
                    depth++;
                }
                return Event.START_ELEMENT;
            case XMLStreamReader.END_ELEMENT:
                if (depth != -1) {
                    depth--;
                }
                return Event.END_ELEMENT;
            case XMLStreamReader.CHARACTERS:
                return Event.TEXT;
            case XMLStreamReader.SPACE:
                return Event.WHITESPACE;
            case XMLStreamReader.ENTITY_REFERENCE:
                return Event.ENTITY_REFERENCE;
            case XMLStreamReader.COMMENT:
                return Event.COMMENT;
            case XMLStreamReader.CDATA:
                return Event.CDATA_SECTION;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                return Event.PROCESSING_INSTRUCTION;
            case XMLStreamReader.END_DOCUMENT:
                return null;
            default:
                throw new IllegalStateException();
        }
    } catch (XMLStreamException ex) {
        throw new TraverserException(ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) TraverserException(org.apache.axiom.truth.xml.spi.TraverserException)

Example 3 with TraverserException

use of org.apache.axiom.truth.xml.spi.TraverserException 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;
}
Also used : Traverser(org.apache.axiom.truth.xml.spi.Traverser) Event(org.apache.axiom.truth.xml.spi.Event) TraverserException(org.apache.axiom.truth.xml.spi.TraverserException) HashSet(java.util.HashSet)

Aggregations

TraverserException (org.apache.axiom.truth.xml.spi.TraverserException)3 Event (org.apache.axiom.truth.xml.spi.Event)2 Traverser (org.apache.axiom.truth.xml.spi.Traverser)2 HashSet (java.util.HashSet)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XML (org.apache.axiom.truth.xml.spi.XML)1