Java XML Node Path getNodeCompletePath(Node node)

Here you can find the source of getNodeCompletePath(Node node)

Description

get Node Complete Path

License

Open Source License

Declaration

public static String getNodeCompletePath(Node node) 

Method Source Code

//package com.java2s;
/**/*w  ww. j a v a  2  s. c om*/
 * (The MIT License)
 *
 * Copyright (c) 2008 - 2014:
 *
 * * {Aaron Patterson}[http://tenderlovemaking.com]
 * * {Mike Dalessio}[http://mike.daless.io]
 * * {Charles Nutter}[http://blog.headius.com]
 * * {Sergio Arbeo}[http://www.serabe.com]
 * * {Patrick Mahoney}[http://polycrystal.org]
 * * {Yoko Harada}[http://yokolet.blogspot.com]
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Node;

public class Main {
    public static String getNodeCompletePath(Node node) {

        Node cur, tmp, next;

        // TODO: Rename buffer to path.
        String buffer = "";
        String sep;
        String name;

        int occur = 0;
        boolean generic;

        cur = node;

        do {
            name = "";
            sep = "?";
            occur = 0;
            generic = false;

            if (cur.getNodeType() == Node.DOCUMENT_NODE) {
                if (buffer.startsWith("/"))
                    break;

                sep = "/";
                next = null;
            } else if (cur.getNodeType() == Node.ELEMENT_NODE) {
                generic = false;
                sep = "/";

                name = cur.getLocalName();
                if (name == null)
                    name = cur.getNodeName();
                if (cur.getNamespaceURI() != null) {
                    if (cur.getPrefix() != null) {
                        name = cur.getPrefix() + ":" + name;
                    } else {
                        generic = true;
                        name = "*";
                    }
                }

                next = cur.getParentNode();

                /*
                 * Thumbler index computation
                 */

                tmp = cur.getPreviousSibling();

                while (tmp != null) {
                    if ((tmp.getNodeType() == Node.ELEMENT_NODE)
                            && (generic || fullNamesMatch(tmp, cur))) {
                        occur++;
                    }
                    tmp = tmp.getPreviousSibling();
                }

                if (occur == 0) {
                    tmp = cur.getNextSibling();

                    while (tmp != null && occur == 0) {
                        if ((tmp.getNodeType() == Node.ELEMENT_NODE)
                                && (generic || fullNamesMatch(tmp, cur))) {
                            occur++;
                        }
                        tmp = tmp.getNextSibling();
                    }

                    if (occur != 0)
                        occur = 1;

                } else {
                    occur++;
                }
            } else if (cur.getNodeType() == Node.COMMENT_NODE) {
                sep = "/";
                name = "comment()";
                next = cur.getParentNode();

                /*
                 * Thumbler index computation.
                 */

                tmp = cur.getPreviousSibling();

                while (tmp != null) {
                    if (tmp.getNodeType() == Node.COMMENT_NODE) {
                        occur++;
                    }
                    tmp = tmp.getPreviousSibling();
                }

                if (occur == 0) {
                    tmp = cur.getNextSibling();
                    while (tmp != null && occur == 0) {
                        if (tmp.getNodeType() == Node.COMMENT_NODE) {
                            occur++;
                        }
                        tmp = tmp.getNextSibling();
                    }
                    if (occur != 0)
                        occur = 1;
                } else {
                    occur = 1;
                }

            } else if (cur.getNodeType() == Node.TEXT_NODE
                    || cur.getNodeType() == Node.CDATA_SECTION_NODE) {
                // I'm here. gist:129
                // http://gist.github.com/144923

                sep = "/";
                name = "text()";
                next = cur.getParentNode();

                /*
                 * Thumbler index computation.
                 */

                tmp = cur.getPreviousSibling();
                while (tmp != null) {
                    if (tmp.getNodeType() == Node.TEXT_NODE
                            || tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
                        occur++;
                    }
                    tmp = tmp.getPreviousSibling();
                }

                if (occur == 0) {
                    tmp = cur.getNextSibling();

                    while (tmp != null && occur == 0) {
                        if (tmp.getNodeType() == Node.TEXT_NODE
                                || tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
                            occur++;
                        }
                        tmp = tmp.getNextSibling();
                    }
                } else {
                    occur++;
                }

            } else if (cur.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
                sep = "/";
                name = "processing-instruction('" + cur.getLocalName()
                        + "')";
                next = cur.getParentNode();

                /*
                 * Thumbler index computation.
                 */

                tmp = cur.getParentNode();

                while (tmp != null) {
                    if (tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
                            && tmp.getLocalName()
                                    .equals(cur.getLocalName())) {
                        occur++;
                    }
                    tmp = tmp.getPreviousSibling();
                }

                if (occur == 0) {
                    tmp = cur.getNextSibling();

                    while (tmp != null && occur == 0) {
                        if (tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
                                && tmp.getLocalName().equals(
                                        cur.getLocalName())) {
                            occur++;
                        }
                        tmp = tmp.getNextSibling();
                    }

                    if (occur != 0) {
                        occur = 1;
                    }

                } else {
                    occur++;
                }

            } else if (cur.getNodeType() == Node.ATTRIBUTE_NODE) {
                sep = "/@";
                name = cur.getLocalName();

                if (cur.getNamespaceURI() != null) {
                    if (cur.getPrefix() != null) {
                        name = cur.getPrefix() + ":" + name;
                    }
                }

                next = ((Attr) cur).getOwnerElement();

            } else {
                next = cur.getParentNode();
            }

            if (occur == 0) {
                buffer = sep + name + buffer;
            } else {
                buffer = sep + name + "[" + occur + "]" + buffer;
            }

            cur = next;

        } while (cur != null);

        return buffer;
    }

    public static String getNodeName(Node node) {
        if (node == null) {
            System.out.println("node is null");
            return "";
        }
        String name = node.getNodeName();
        if (name == null) {
            System.out.println("name is null");
            return "";
        }
        if (name.equals("#document")) {
            return "document";
        } else if (name.equals("#text")) {
            return "text";
        } else {
            name = getLocalPart(name);
            return (name == null) ? "" : name;
        }
    }

    /**
     * Return the prefix of a qualified name like "prefix:local".
     * Returns null if there is no prefix.
     */
    public static String getPrefix(String qName) {
        if (qName == null)
            return null;

        int pos = qName.indexOf(':');
        if (pos > 0)
            return qName.substring(0, pos);
        else
            return null;
    }

    protected static boolean fullNamesMatch(Node a, Node b) {
        return a.getNodeName().equals(b.getNodeName());
    }

    /**
     * Return the local part of a qualified name like "prefix:local".
     * Returns <code>qName</code> if there is no prefix.
     */
    public static String getLocalPart(String qName) {
        if (qName == null)
            return null;

        int pos = qName.indexOf(':');
        if (pos > 0)
            return qName.substring(pos + 1);
        else
            return qName;
    }
}

Related

  1. getFullPath(Node node)
  2. getIndividualPath(Node individual)
  3. getMatchingNodes(Node node, String[] nodePath, int cur, List res)
  4. getNode(Node node, String... nodePath)
  5. getNode(Node root, String nodePath)
  6. getNodePath(Node node)
  7. getNodePath(Node node)
  8. getNodePath(Node node)
  9. getNodePath(Node node, String postfix)