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

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

Description

Same as getNXInfo except that It checks general first( NXclassNameList is "") then repeatedly add elements of the NXclassNameList and/or field name , then does the same with the runs node

License

Open Source License

Parameter

Parameter Description
xmlDoc The node in the xml document
NXclassPath The dot separated string of class names(<....<--class naem) to find
NXclassNameList The dot separated string of name attributes for the above
fieldName The dot separated string name of attribute names or field names
filename The filename attribute for the Runs section of the xmldoc

Return

the first Node that matches the criteria or null if none can be found

Declaration

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

Method Source Code


//package com.java2s;
/* // www.j av a2  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 java.util.Arrays;

import org.w3c.dom.*;

public class Main {
    /**
     *  Same as getNXInfo except that It checks general first( NXclassNameList 
     *  is "") then repeatedly add elements of the NXclassNameList and/or field 
     *  name , then does the same with the runs node
     * @param xmlDoc      The node in the xml document
     * @param NXclassPath  The dot separated string of class names(<....<--class naem) to find
     * @param NXclassNameList The dot separated string of name attributes for the above 
     * @param fieldName     The dot separated string name of attribute names or field names
     * @param filename     The filename attribute for the Runs section of the xmldoc
     * @return the first Node that matches the criteria or null if none can be found
     */
    public static Node getNXInfoDefault(Node xmlDoc, String NXclassPath, String NXclassNameList, String fieldName,
            String filename) {

        if (NXclassPath == null)
            return null;
        if (xmlDoc == null)
            return null;

        NXclassNameList = standardize_dot_sep_list(NXclassNameList);

        fieldName = standardize_dot_sep_list(fieldName);

        Node N = getNXInfo(xmlDoc, NXclassPath, "", fieldName, "");

        int i = 1;
        //      String S= "";

        int NClassNames = 0;
        if (NXclassNameList != null)
            for (int j = NXclassNameList.indexOf(".", i); j >= i;) {

                NClassNames++;

                i = j + 1;
                j = NXclassNameList.indexOf(".", i);
            }
        int NFieldNames = 0;
        i = 1;
        if (fieldName != null)
            for (int j = fieldName.indexOf(".", i); j >= i;) {
                NFieldNames++;
                i = j + 1;
                j = fieldName.indexOf(".", i);
            }
        Node N1 = null;
        boolean[] classes = new boolean[NClassNames];
        boolean[] fields = new boolean[NFieldNames];
        for (i = 0; i <= NClassNames; i++) {
            Arrays.fill(classes, false);
            if (i < NClassNames)
                for (int k = 0; k < i; k++)
                    classes[k] = true;
            if ((i != NClassNames) || NClassNames == 0)
                for (boolean done = false; !done; done = incr(classes))
                    for (int j = 0; j <= NFieldNames; j++) {
                        Arrays.fill(fields, false);
                        if (j < NFieldNames)
                            for (int k = 0; k < NFieldNames; k++)
                                fields[k] = true;
                        if (i != NFieldNames || NFieldNames == 0)
                            for (boolean done1 = false; !done1; done1 = incr(fields)) {
                                String ClassNames = getDotString(classes, NXclassNameList);
                                String FieldNames = getDotString(fields, fieldName);
                                N1 = getNXInfo(xmlDoc, NXclassPath, ClassNames, FieldNames, "");
                                if (N1 != null)
                                    N = N1;
                            }

                    }

        } //

        Node Runs = getNXInfo(xmlDoc, "Runs", null, null, filename);

        N1 = getNXInfoDefault(Runs, NXclassPath, NXclassNameList, fieldName, null);

        if (N1 != null)
            N = N1;
        return N;

    }

    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;

    }

    /**
     * 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 boolean incr(boolean[] list) {
        if (list == null)
            return true;
        if (list.length <= 1)
            return true;
        boolean done = false;
        int C = 0;
        while (!done) {
            int x = -1;
            for (int i = list.length - 1; i >= 0 && x < 0; i--)
                if (list[i]) {
                    x = i;
                    list[i] = false;
                    C++;
                }
            if (x < 0)
                return true;
            if (x < list.length - 1 - C) {
                done = true;
                for (int k = 0; k < C; k++)
                    list[x + k] = true;
            }

        }
        return false;

    }

    private static String getDotString(boolean[] dots, String NamesList) {
        String Res = "";
        int i = 1;
        if (dots == null || dots.length < 1)
            return Res;
        if (NamesList == null)
            return Res;
        if (NamesList.length() <= 2)
            return Res;
        Res = ".";
        int dotIndex = 0;
        for (int j = NamesList.indexOf(".", i); j > 0; j = NamesList.indexOf(".", i)) {
            if (dots[dotIndex])
                Res += NamesList.indexOf(i, j - 1) + ".";
            else
                Res += ".";
            i = j + 1;
        }
        while (Res != null && Res.startsWith("."))
            Res = Res.substring(1);

        while (Res != null && Res.endsWith("."))
            Res = Res.substring(0, Res.length() - 1);
        if (Res != null && Res.trim().length() < 1)
            Res = null;
        return Res;
    }
}

Related

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