Java XML Node Namespace matches(Node n, String namespace, String tagName)

Here you can find the source of matches(Node n, String namespace, String tagName)

Description

Compare a DOM nod3 by name

License

Open Source License

Parameter

Parameter Description
n DOM node
namespace Node namespace
tagName Node name

Return

true if the node has this name

Declaration

public static boolean matches(Node n, String namespace, String tagName) 

Method Source Code


//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
 *
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 *
 * For more information, contact://from w  w  w .  j a  va2s  . co m
 *
 *  Generalitat Valenciana
 *   Conselleria d'Infraestructures i Transport
 *   Av. Blasco Ib??ez, 50
 *   46010 VALENCIA
 *   SPAIN
 *
 *      +34 963862235
 *   gvsig@gva.es
 *      www.gvsig.gva.es
 *
 *    or
 *
 *   IVER T.I. S.A
 *   Salamanca 50
 *   46005 Valencia
 *   Spain
 *
 *   +34 963163400
 *   dac@iver.es
 */

import javax.xml.namespace.QName;

import org.w3c.dom.Node;

public class Main {
    /**
     * Compare a DOM nod3 by name
     * @param n
     * DOM node
     * @param namespace
     * Node namespace
     * @param tagName
     * Node name
     * @return
     * <code>true</code> if the node has this name
     */
    public static boolean matches(Node n, String namespace, String tagName) {
        String nodeName = n.getNodeName().substring(n.getNodeName().indexOf(":") + 1, n.getNodeName().length());
        int index = n.getNodeName().indexOf(":");
        String nodeNSURI = null;
        if (index > 0) {
            nodeNSURI = n.getNodeName().substring(0, n.getNodeName().indexOf(":"));
        }
        if (nodeNSURI == null) {
            return objNullEq(tagName.toUpperCase(), nodeName.toUpperCase());
        }
        return objNullEq(nodeNSURI.toUpperCase(), namespace.toUpperCase())
                && objNullEq(tagName.toUpperCase(), nodeName.toUpperCase());
    }

    /**
     * Determines if two QNames are equal
     * @param qname1
     * First qname to compare
     * @param qname2
     * Second qname to compare
     * @return
     * <code>true</code> or <code>false</code> according
     * to the above description.
     */
    public static boolean matches(QName qname1, QName qname2) {
        return objNullEq(qname1.getNamespaceURI().toUpperCase(), qname2.getNamespaceURI().toUpperCase())
                && objNullEq(qname1.getLocalPart().toUpperCase(), qname2.getLocalPart().toUpperCase());
    }

    /**
     * Determines if two objects are equal.  
     * @param object1 
     * First object to compare.
     * @param object2 
     * Second object to compare.
     * @return 
     * <code>true</code> or <code>false</code> according
     * to the above description.
     */
    private static boolean objNullEq(Object object1, Object object2) {
        if ((object1 == null) && (object2 == null)) {
            return true;
        }
        if ((object1 == null) && (object2 != null)) {
            return false;
        }
        if ((object1 != null) && (object2 == null)) {
            return false;
        }
        return object1.equals(object2);
    }
}

Related

  1. isNamespaceElement(Node node, String namespace)
  2. isNonDefaultNamespace(Node node)
  3. isSameNamespace(Node node, String namespace)
  4. match(final Node node, final String namespace, final String localname)
  5. matches(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)
  6. parseStaticInterface(String interfaceName, Node module, String nameSpace, PrintWriter out)
  7. removeNamedItemNS(final NamedNodeMap nodeMap, final String namespaceURI, final String localName)