validate XML Element with required element, attributes - Android XML

Android examples for XML:XML Attribute

Description

validate XML Element with required element, attributes

Demo Code

/**/*from   ww w .j av a  2  s .co  m*/
 * RAMPART - Robust Automatic MultiPle AssembleR Toolkit
 * Copyright (C) 2013  Daniel Mapleson - TGAC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    public static boolean validate(Element ele,
            String[] requiredAttributes, String[] optionalAttributes,
            String[] requiredElements, String[] optionalElements) {

        boolean[] foundAttributes = new boolean[requiredAttributes.length];

        for (int i = 0; i < foundAttributes.length; i++) {
            foundAttributes[i] = false;
        }

        for (int i = 0; i < ele.getAttributes().getLength(); i++) {
            Node n = ele.getAttributes().item(i);

            boolean found = false;

            for (int j = 0; j < requiredAttributes.length; j++) {
                if (requiredAttributes[j].equalsIgnoreCase(n.getNodeName())) {
                    foundAttributes[j] = true;
                    found = true;
                    break;
                }
            }

            if (!found) {
                for (String c : optionalAttributes) {
                    if (n.getNodeName().equalsIgnoreCase(c)) {
                        found = true;
                        break;
                    }
                }
            }

            // If we've got this far and nothings been found then there must have been an unexpected attribute
            if (!found) {
                throw new IllegalArgumentException(
                        "Error validating XML Element: "
                                + ele.getNodeName()
                                + "; Found unexpected attribute: "
                                + n.getNodeName());
            }
        }

        // Check all required attributes were found
        for (int i = 0; i < foundAttributes.length; i++) {
            if (foundAttributes[i] == false) {
                throw new IllegalArgumentException(
                        "Error validating XML Element: "
                                + ele.getNodeName()
                                + "; Failed to find expected attribute or child element named: "
                                + requiredAttributes[i]);
            }
        }

        boolean[] foundElements = new boolean[requiredElements.length];

        for (int i = 0; i < foundElements.length; i++) {
            foundElements[i] = false;
        }

        for (int i = 0; i < ele.getChildNodes().getLength(); i++) {
            Node n = ele.getChildNodes().item(i);

            boolean found = false;

            for (int j = 0; j < requiredElements.length; j++) {
                if (requiredElements[j].equalsIgnoreCase(n.getNodeName())) {
                    foundElements[j] = true;
                    found = true;
                    break;
                }
            }

            if (!found) {
                for (String c : optionalElements) {
                    if (n.getNodeName().equalsIgnoreCase(c)) {
                        found = true;
                        break;
                    }
                }
            }

            // If we've got this far and nothings been found then there must have been an unexpected element
            if (!found) {

                if (n.getNodeName().startsWith("#")
                        || n.getNodeName().startsWith("<!--")) {
                    // Then we just ignore these elements
                } else {
                    throw new IllegalArgumentException(
                            "Error validating XML Element: "
                                    + ele.getNodeName()
                                    + "; Found unexpected child node: "
                                    + n.getNodeName());
                }
            }
        }

        // Check all required child elements were found
        for (int i = 0; i < foundElements.length; i++) {
            if (foundElements[i] == false) {
                throw new IllegalArgumentException(
                        "Error validating XML Element: "
                                + ele.getNodeName()
                                + "; Failed to find expected attribute or child element named: "
                                + requiredElements[i]);
            }
        }

        return true;
    }
}

Related Tutorials