Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

import java.util.ArrayList;

public class Main {
    /**
     * Returns an array of strings representing all values for provided
     * attribute prefixed by 2 levels of parents, all nodes (e.g.:
     * parenetNodeAtrValue.childNodeAtrValue.childNodeAtrValue)
     * @param xsdDoc
     * @param atrName
     * @return
     */
    public static ArrayList<String> getAllValuesWithParentForAttribute(Document xsdDoc, String atrName) {
        try {
            NodeList allNodes = xsdDoc.getElementsByTagName("*");
            return getValuesWithParentForAttribute(atrName, allNodes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Returns an array of strings representing values for provided attribute
     * prefixed by 2 levels of parents, extracted from a provided list of nodes
     * (e.g.: parenetNodeAtrValue.childNodeAtrValue.childNodeAtrValue)
     * @param atrName
     * @param nodes
     * @return
     */
    private static ArrayList<String> getValuesWithParentForAttribute(String atrName, NodeList nodes) {
        ArrayList<String> filteredNodesV = new ArrayList<String>();
        for (int j = 0; j < nodes.getLength(); j++) {
            Element childE = (Element) nodes.item(j);
            if (childE.hasAttribute(atrName)) {
                filteredNodesV.add(getXsdParentsAtrValue(childE, atrName) + childE.getAttribute(atrName));
            }
        }
        return filteredNodesV;
    }

    /**
     * Returns parent attribute value for a specific element (2 levels of
     * parents)
     * @param element
     * @param attr
     * @return
     */
    protected static String getXsdParentsAtrValue(Element element, String attr) {
        Node tmpElement = null;
        String tmpRes = "";
        try {
            for (int i = 1; i < 3; i++) {
                if (i == 1) {
                    tmpElement = element.getParentNode();
                } else {
                    tmpElement = tmpElement.getParentNode();
                }
                if (tmpElement.getNodeName().equals("xs:schema")) {
                    if (i == 1) {
                        tmpRes = "..";
                    } else {
                        tmpRes = "." + tmpRes;
                    }
                    break;
                }
                while ((tmpElement.getNodeType() != Node.ELEMENT_NODE)
                        || ((tmpElement.getNodeType() != Node.DOCUMENT_NODE)
                                && !((Element) tmpElement).hasAttribute(attr))) {
                    if (tmpElement.getNodeName().equals("xs:schema")) {
                        break;
                    }
                    tmpElement = tmpElement.getParentNode();
                }
                tmpRes = ((Element) tmpElement).getAttribute(attr) + "." + tmpRes;
            }
            return tmpRes;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}