Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    public static String getpathToRoot(Node n) {
        // log.severe("getpathToRoot called");
        String path = "";
        Node Parent = null;
        int y = 0;
        if (n != null) {

            while (n.getParentNode() != null) {

                Parent = n.getParentNode();
                if (y > 0) {
                    path = ":" + path;
                }
                y++;
                int i = getCurrentPosition(n);
                i = i - 1;
                // log.finest("Current position of Node = " + i);
                // Element elem = (Element) n;
                // log.finest("Node = " + elem.getLocalName() +
                // " AttribName = "+
                // elem.getAttribute("name"));
                // Check to see it has children as otherwise the htmltree has a
                // problem with expanding leaves.
                if (n.hasChildNodes()) {
                    path = i + path;
                }
                n = Parent;

            }
        }

        // log.severe("path = " + path);
        if (path == null || path.length() == 0) {
            path = "0";

        }

        return path;
    }

    public static int getCurrentPosition(Node refNode) {
        if (refNode == null) {
            return -1;
        }

        int counter = 0;
        Node current = refNode;

        while (current != null) {
            if (current.getNodeType() == Node.ELEMENT_NODE) {
                counter++;
            }

            current = current.getPreviousSibling();
        }

        return counter;
    }
}