Java XML Node Path getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)

Here you can find the source of getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)

Description

Finds information in an XML document

License

Open Source License

Parameter

Parameter Description
xmlDoc The top node( or what is left) of a DOM document
NXclassPath A dot separated list of Nexus Classes. These appear as <NXentry name="...in the XML file. Only those parts of the xml document will be searched. This can be null for all NeXus classnames to be searched
NXclassNameList The name of the NeXus Class. The name is in the name= attribute. This also can be null( all will be considered) , or a dot separated set of names (two consecutive dots mean anything, - means no name ). NOTE: the names MUST correspond to the Classes in NXclassPath
fieldName NOT USED The specific field name( tag or name attribute) to search for. No Dots. Include this at the end of NXclassPath if a class name or at the end of NXclassNameList if a name of a class.
filename The tag of the node must have a filename attribute corresponding to this filename

Return

The first node or [sub]node satisfying the parameters NOTE: Use getLeafNodeValue to get the value of the node if it is a leaf.

Declaration

public static Node getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName,
        String filename) 

Method Source Code

//package com.java2s;
/* //ww  w .  ja va  2s .  c o  m
 * File: Util.java 
 *  
 * Copyright (C) 2007     Ruth Mikkelson
 *
 * 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact :  Dennis Mikkelson<mikkelsond@uwstout.edu>
 *            MSCS Department
 *            HH237H
 *            Menomonie, WI. 54751
 *            (715)-232-2291
 *
 * This work was supported by the National Science Foundation under grant
 * number DMR-0426797, and by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 *
 * Modified:
 * $Log$
 * Revision 1.7  2007/08/16 17:46:14  rmikk
 * Added GPL
 * Fixed calculations in working with non trailing *'s in position fields of a NeXus
 *   file
 *
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Finds information in an XML document
     * @param xmlDoc  The top node( or what is left) of a DOM document
     * @param NXclassPath   A dot separated list of Nexus Classes. These appear
     *                      as <NXentry  name="...in the XML file. Only those 
     *                      parts of the xml document will be searched.
     *                      This can be null for all NeXus classnames to be 
     *                      searched
     *                      
     * @param NXclassNameList The name of the NeXus Class. The name is in the  
     *                        name= attribute. This also can  be null( all will
     *                         be considered) , or a dot separated set of names
     *                         (two consecutive dots mean anything, - means no name
     *                         ).  NOTE: the names MUST correspond to the Classes in 
     *                          NXclassPath
     *                          
     * @param fieldName      NOT USED The specific field name( tag or name 
     *                       attribute) to search for. No Dots. Include this 
     *                       at the end of NXclassPath if a class name or at 
     *                       the end of NXclassNameList if a name of a class.
     *                       
     * @param filename       The tag of the node must have a filename attribute
     *                        corresponding to this filename 
     *                        
     * @return     The first node or [sub]node satisfying the parameters
     * 
     * NOTE: Use getLeafNodeValue to get the value of the node if it is a leaf.
     *    
     */
    public static Node getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName,
            String filename) {

        if (xmlDoc == null)
            return null;

        NXclassPath = standardize_dot_sep_list(NXclassPath);
        NXclassNameList = standardize_dot_sep_list(NXclassNameList);

        if (filename != null && filename.length() < 1)
            filename = null;

        NodeList children = xmlDoc.getChildNodes();
        if (children.getLength() == 1)
            if ("data".equals(children.item(0).getNodeName())) {
                xmlDoc = children.item(0);
                children = xmlDoc.getChildNodes();
            }

        if ((NXclassPath == null) && (NXclassNameList == null) && (fieldName == null) && (filename == null))
            return xmlDoc;

        for (int ik = 0; ik < children.getLength(); ik++) {

            Node NN = children.item(ik);
            String NextNXclassPath = NXclassPath;
            String NextNXclassNameList = NXclassNameList;
            String Nextfilename = filename;

            if (NXclassPath != null)
                if (NXclassPath.startsWith(".."))

                    NextNXclassPath = NXclassPath.substring(1);

                else if (NXclassPath.indexOf("." + NN.getNodeName() + ".") == 0)
                    NextNXclassPath = NXclassPath.substring(NXclassPath.indexOf(".", 1));

            NamedNodeMap atts = NN.getAttributes();
            if (NXclassNameList != null && atts != null)
                if (NXclassNameList.startsWith(".."))
                    NextNXclassNameList = NXclassNameList.substring(1);
                else {

                    Node attNode = atts.getNamedItem("name");
                    if (attNode == null && NXclassNameList.startsWith(".-."))
                        NextNXclassNameList = NXclassNameList.substring(3);
                    else if (attNode != null && NXclassNameList.indexOf("." + attNode.getNodeValue() + ".") == 0)
                        NextNXclassNameList = NXclassNameList.substring(NXclassNameList.indexOf(".", 1));

                }

            if ((filename != null) && (atts != null)) {

                Node attNode = atts.getNamedItem("filename");
                if (attNode != null)
                    if (filename.equals(attNode.getNodeValue()))
                        Nextfilename = null;
            }

            Node X = null;

            if (NXclassPath == null || NXclassNameList == null
                    || (NXclassPath != NextNXclassPath && NXclassNameList != NextNXclassNameList))// only pop name list
                                                                                                                                                     //if class popped       
                X = getNXInfo1(NN, NextNXclassPath, NextNXclassNameList, fieldName, Nextfilename);
            else
                X = getNXInfo1(NN, NXclassPath, NXclassNameList, fieldName, Nextfilename);

            if (X != null)
                return X;

        } //for each child

        return null;

    }

    private static String standardize_dot_sep_list(String S) {

        if (S == null)
            return null;

        S = S.trim();
        if (S.length() < 1)
            return null;

        if (!S.endsWith("."))
            S += ".";

        if (!S.startsWith("."))
            S = "." + S;

        if (S.length() <= 2)
            return null;

        //eliminate all dots
        boolean nonDotFound = false;
        for (int i = 0; i < S.length() && !nonDotFound; i++)
            if (S.charAt(i) != '.')
                if (S.charAt(i) != ' ')
                    nonDotFound = true;
        if (!nonDotFound)
            return null;
        return S;

    }
}

Related

  1. getNodePath(Node node)
  2. getNodePath(Node node, String postfix)
  3. getNodes(Node node, String path)
  4. getNodesPathName(Node node)
  5. getNXInfo(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  6. getNXInfoDefault(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  7. getPath(Node n)
  8. getPath(Node node)
  9. getValueViaPath(Node node, String path)