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

Here you can find the source of getNXInfo(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 listed
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 set
fieldName The specific field name( tag or name attribute) to search for. No Dots
filename The tag of the node must have a filename attribute corresponding to this filename

Return

The top node or the value if only one simple child

Declaration

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

Method Source Code

//package com.java2s;
/* //from  w w w .j av  a  2  s  .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 listed
     * @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 set
     * @param fieldName      The specific field name( tag or name attribute) to search for. No Dots
     * @param filename       The tag of the node must have a filename attribute corresponding to this filename 
     * @return     The top node or the value if only one simple child
     */
    public static Node getNXInfo(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);
        //fieldName = standardize_dot_sep_list( fieldName);
        if (filename != null && filename.length() < 1)
            filename = null;
        if (fieldName != null && fieldName.length() < 1)
            fieldName = 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);
            int k = -1;
            if (NXclassPath != null)
                k = NXclassPath.indexOf("." + NN.getNodeName() + ".");

            String ThisNodeName = null;//Non-null only if matches the match nodeName
            String ThisNodeFileName = null;//Non-null only if matches the match match filename
            boolean ClassHasName = false;
            boolean ClassHasFile = false;
            NamedNodeMap atts = NN.getAttributes();
            if (NXclassNameList != null) {

                String NodeName2Match = NXclassNameList.substring(1, NXclassNameList.indexOf(".", 1));

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

                if (NodeName2Match != null)
                    if (NodeName2Match.trim().length() > 0)
                        if (atts != null) {

                            Node attNode = atts.getNamedItem("name");
                            if (attNode != null)
                                ClassHasName = true;
                            if (attNode != null)
                                if (NodeName2Match.equals(attNode.getNodeValue()))
                                    ThisNodeName = NodeName2Match;
                        }
            }

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

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

                if (attNode != null)
                    ClassHasFile = true;

            }

            boolean OkToEnter = false;// are there child nodes to search??
            if ((NXclassPath != null) || (NXclassNameList != null))
                if (filename == null) {

                    if (k == 0)
                        if (ThisNodeName != null)
                            OkToEnter = true;
                    if (k == 0)
                        if ((ThisNodeName == null))
                            OkToEnter = true;

                    if (k == 0)
                        if (!ClassHasName)
                            OkToEnter = true;

                    if (NXclassPath == null)
                        if (ThisNodeName != null)
                            OkToEnter = true;

                    if (NXclassPath == null)
                        if (!ClassHasName)
                            OkToEnter = true;

                } else {

                    if (k == 0)
                        if (ThisNodeName != null)
                            if ((ThisNodeFileName != null) || !ClassHasFile)
                                OkToEnter = true;

                    if (k == 0)
                        if ((NXclassNameList == null))
                            if ((ThisNodeFileName != null) || !ClassHasFile)
                                OkToEnter = true;

                    if (k == 0)
                        if (!ClassHasName)
                            if ((ThisNodeFileName != null) || !ClassHasFile)
                                OkToEnter = true;

                    if (NXclassPath == null)
                        if (ThisNodeName != null)
                            if ((ThisNodeFileName != null) || !ClassHasFile)
                                OkToEnter = true;

                    if (NXclassPath == null)
                        if (!ClassHasName)
                            if ((ThisNodeFileName != null) || !ClassHasFile)
                                OkToEnter = true;

                }

            if (OkToEnter) {//Has child nodes to search
                            //REDO How about adding a variable changed. if any of the other stuff changes 
                            //     Woops go deeper cause child node and not done

                String Clist = null, CNameList = null;

                if (k >= 0 && NXclassPath != null) {
                    k = NXclassPath.indexOf('.', k + 1);
                    if (k >= 0)
                        Clist = NXclassPath.substring(k);
                }

                if (ThisNodeFileName != null)
                    filename = null;

                if (ThisNodeName != null && NXclassNameList != null) {
                    k = NXclassNameList.indexOf("." + ThisNodeName + ".");
                    if (k >= 0) {
                        k = k + 2 + ThisNodeName.length();
                        CNameList = NXclassNameList.substring(k);
                    }
                } else
                    CNameList = NXclassNameList;

                Node X = getNXInfo(NN, Clist, CNameList, fieldName, filename);

                if (X != null)
                    return X;

            } else if ((NXclassPath != null) || (NXclassNameList != null)) {//Check for fieldName

                Node X = getNXInfo(NN, NXclassPath, NXclassNameList, fieldName, filename);
                if (X != null)
                    return X;

            } else { //Do not check child nodes. Check if this fits

                if (fieldName != null)
                    if (fieldName.equals(NN.getNodeName()))
                        return NN;

                atts = NN.getAttributes();
                if (atts != null) {

                    Node attNode = atts.getNamedItem("name");
                    if (fieldName != null)
                        if (attNode != null)
                            if (fieldName.equals(attNode.getNodeValue()))
                                return NN;

                    attNode = atts.getNamedItem("filename");
                    if ((filename != null) && (attNode != null))
                        if (filename.equals(attNode.getNodeValue()))
                            return NN;

                }
                if ((fieldName == null) && (filename == null))
                    return NN;

                Node X = getNXInfo(NN, NXclassPath, NXclassNameList, fieldName, filename);
                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)
  3. getNodePath(Node node, String postfix)
  4. getNodes(Node node, String path)
  5. getNodesPathName(Node node)
  6. getNXInfo1(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  7. getNXInfoDefault(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName, String filename)
  8. getPath(Node n)
  9. getPath(Node node)