public static void isSameXML()

in SdmxSourceUtil/src/main/java/org/sdmxsource/sdmx/util/stax/StaxUtil.java [302:455]


    public static void isSameXML(InputStream xmlOne, InputStream xmlTwo, String ignoreAttrValue, String... ignoreNodes) throws Exception {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser1 = factory.createXMLStreamReader(xmlOne, "UTF-8");

        XMLStreamReader parser2 = factory.createXMLStreamReader(xmlTwo, "UTF-8");
        String nodeA = null;
        String nodeB = null;

        int element = 0;
        int a = -1;
        int b = -1;

        outer:
        while (parser1.hasNext()) {
            a = parser1.next();
            b = parser2.next();
            while (a == XMLStreamConstants.SPACE) {
                a = parser1.next();
            }
            while (b == XMLStreamConstants.SPACE) {
                b = parser2.next();
            }
            if (parser1.isCharacters()) {
                if (ObjectUtil.validString(parser1.getText())) {
                    if (!parser2.isCharacters()) {
                        throw new IllegalArgumentException("Text Differs on Node:" + nodeA);
                    }
                    if (!parser1.getText().equals(parser2.getText())) {
                        throw new IllegalArgumentException("Text Differs on Node:" + nodeA + "\nExpected:" + parser1.getText() + "\nActual:" + parser2.getText());
                    }
                } else {
                    a = parser1.next();
                    if (parser2.isCharacters() && !ObjectUtil.validString(parser2.getText())) {
                        b = parser2.next();
                    }
                }
            } else if (parser2.isCharacters()) {

                if (ObjectUtil.validString(parser2.getText())) {
                    throw new IllegalArgumentException("Text Differs on Node:" + nodeA + " : " + parser2.getText());
                }
                b = parser2.next();
            }

            if (a == XMLStreamConstants.END_ELEMENT) {
                if (b != a) {
                    nodeA = parser1.getLocalName();
                    throw new IllegalArgumentException("Input A is ending XML element: " + nodeA + " whilst input B is not");
                }
            } else if (a == XMLStreamConstants.START_ELEMENT) {
                element++;
                nodeA = parser1.getLocalName();
                if (b != XMLStreamConstants.START_ELEMENT) {
                    while (parser2.hasNext()) {
                        b = parser2.next();
                        if (b == XMLStreamConstants.START_ELEMENT) {
                            nodeB = parser2.getLocalName();
                            break;
                        }
                    }
                }
                if (b != XMLStreamConstants.START_ELEMENT) {
                    throw new IllegalArgumentException("Parser B end of document, Parser A on node: " + nodeA);
                }
                nodeB = parser2.getLocalName();


                if (!nodeA.equals(nodeB)) {
                    throw new IllegalArgumentException("XML NODES DIFFER: " + nodeA + "," + nodeB);
                }
                for (String ignoreNode : ignoreNodes) {
                    if (nodeA.equals(ignoreNode)) {
                        StaxUtil.skipNode(parser1);
                        StaxUtil.skipNode(parser2);
                        continue outer;
                    }
                }


                if (parser1.getNamespaceCount() != parser2.getNamespaceCount()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("Namespace count for parser 1 : " + parser1.getNamespaceCount() + "\n");
                    for (int i = 0; i < parser1.getNamespaceCount(); i++) {
                        sb.append("Parser 1 Namespace #" + (i + 1) + ":  " + parser1.getNamespaceURI(i) + "\n");
                    }
                    sb.append("Namespace count for parser 2 : " + parser2.getNamespaceCount() + "\n");
                    for (int i = 0; i < parser2.getNamespaceCount(); i++) {
                        sb.append("Parser 2 Namespace #" + (i + 1) + ":  " + parser2.getNamespaceURI(i) + "\n");
                    }
                    throw new IllegalArgumentException("Namespace count differs on node:" + nodeA + "\n" + sb.toString());
                }

                for (int i = 0; i < parser1.getNamespaceCount(); i++) {
                    boolean found = false;
                    String nameSpaceURI = parser1.getNamespaceURI(i);
                    for (int j = 0; j < parser2.getNamespaceCount(); j++) {
                        if (parser2.getNamespaceURI(j).equals(nameSpaceURI)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        throw new IllegalArgumentException("Namespace not found " + nameSpaceURI);
                    }
                }
                if (parser1.getAttributeCount() != parser2.getAttributeCount()) {
                    throw new IllegalArgumentException("Attribute count differs on node:" + nodeA);
                }

                for (int i = 0; i < parser1.getAttributeCount(); i++) {
                    boolean found = false;
                    String attributeLocalName = parser1.getAttributeLocalName(i);
                    String attributeNameSpace = parser1.getAttributeNamespace(i);
                    String attributeValue = parser1.getAttributeValue(i);
                    int j;
                    for (j = 0; j < parser2.getAttributeCount(); j++) {
                        if (parser2.getAttributeLocalName(j).equals(attributeLocalName)) {
                            if (attributeNameSpace == null && parser2.getAttributeNamespace(j) == null) {
                                //THIS IS OKAY
                            } else {
                                if (attributeNameSpace == null && parser2.getAttributeNamespace(j) != null) {
                                    throw new IllegalArgumentException("ATTRIBUTE NAMESPACE DIFFERS " + parser2.getAttributeNamespace(i) + "," + attributeNameSpace);
                                }
                                if (attributeNameSpace != null && parser2.getAttributeNamespace(j) == null) {
                                    throw new IllegalArgumentException("ATTRIBUTE NAMESPACE DIFFERS " + parser2.getAttributeNamespace(i) + "," + attributeNameSpace);
                                }
                                if (!parser2.getAttributeNamespace(j).equals(attributeNameSpace)) {
                                    throw new IllegalArgumentException("ATTRIBUTE NAMESPACE DIFFERS " + parser2.getAttributeNamespace(i) + "," + attributeNameSpace);
                                }
                            }


                            found = true;
                            break;
                        }

                    }
                    if (!found) {
                        throw new IllegalArgumentException("ATTRIBUTE NOT FOUND ON NODE '" + nodeA + "' : " + attributeLocalName);
                    }

                    // Ensure the attribute to be tested is not an ignore value then test it
                    if (!attributeValue.equals(ignoreAttrValue)) {
                        if (!parser2.getAttributeValue(j).equals(attributeValue)) {
                            String line_separator = System.getProperty("line.separator");
                            throw new IllegalArgumentException("ATTRIBUTE VALUE DIFFERS on: " + attributeLocalName + " - Node: " + element + line_separator
                                    + attributeValue + line_separator
                                    + parser2.getAttributeValue(i));
                        }
                    }
                }
            }
        }
    }