Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.util.HashSet;

import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

public class Main {
    public static boolean validProcess(String xml) {
        try {
            Set<String> classifiers = new HashSet<String>();

            InputStream is = new ByteArrayInputStream(xml.getBytes());
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document document = docBuilder.parse(is);

            // iterate over all operators
            NodeList nodes = document.getElementsByTagName("operator");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                String className = element.getAttribute("class");

                if (classifiers.contains(className)) {
                    return false;
                }
                classifiers.add(className);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}