Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * This file belongs to the BPELUnit utility and Eclipse plugin set. See enclosed
 * license file for more information.
 */

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Append XPath [expr] like /a/b[1] or /a/b[@name='x']
     * @param sb
     * @param n
     */
    private static void appendElementQualifier(StringBuffer sb, Element n) {
        if (n.getParentNode() == n.getOwnerDocument()) {
            return;
        }

        if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) {
            sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']");
        } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) {
            sb.append("[").append(getPosition(n)).append("]");
        }
    }

    private static int getChildrenCount(Element parentNode, String namespaceURI, String localName) {
        int counter = 0;

        List<Element> children = getChildElements(parentNode);
        for (Element c : children) {
            if (namespaceURI.equals(c.getNamespaceURI()) && localName.equals(c.getLocalName())) {
                counter++;
            }
        }

        return counter;
    }

    /**
     * Corresponds to the pos() xpath function
     * 
     * @param e
     * @return pos(), also the start index is 1 not 0
     */
    public static int getPosition(Element e) {
        Element parent = (Element) e.getParentNode();
        List<Element> children = getChildElements(parent);
        return children.indexOf(e) + 1;
    }

    public static List<Element> getChildElements(Element element) {
        NodeList childNodes = element.getChildNodes();

        List<Element> elements = new ArrayList<Element>();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n = childNodes.item(i);
            if (n instanceof Element) {
                elements.add((Element) n);
            }
        }
        return elements;
    }
}