001/*
002 * $Id: XmlUtil.java,v 1.3 2011/03/24 16:06:35 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 * 
012 * All Rights Reserved
013 * 
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.  
016 * 
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 * 
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025 * GNU Lesser Public License for more details.
026 * 
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program.  If not, see http://www.gnu.org/licenses.
029 */
030
031
032
033
034
035
036package edu.wisc.ssec.mcidasv.util;
037
038
039import java.util.ArrayList;
040import java.util.List;
041
042import org.w3c.dom.Attr;
043import org.w3c.dom.Element;
044import org.w3c.dom.NamedNodeMap;
045import org.w3c.dom.Node;
046import org.w3c.dom.NodeList;
047
048
049
050/**
051 * A collection of utilities for xml.
052 *
053 * @author IDV development team
054 */
055
056public abstract class XmlUtil extends ucar.unidata.xml.XmlUtil {
057
058        /**
059         * Print all the attributes of the given node
060         * 
061         * @param parent
062         */
063        public static void printNode(Node parent) {
064                if (parent==null) {
065                        System.out.println("null node!");
066                        return;
067                }
068                System.out.println(parent.getNodeName() + " node:");
069                NamedNodeMap attrs = parent.getAttributes();
070                for(int i = 0 ; i<attrs.getLength() ; i++) {
071                        Attr attribute = (Attr)attrs.item(i);     
072                        System.out.println("  " + attribute.getName()+" = "+attribute.getValue());
073                }
074        }
075
076    /**
077     *  Find all of the  descendant elements of the given parent Node
078     *  whose tag name.equals the given tag.
079     *
080     *  @param parent The root of the xml dom tree to search.
081     *  @param tag The tag name to match.
082     *  @return The list of descendants that match the given tag.
083     */
084    public static List<String> findDescendantNamesWithSeparator(Node parent, String tag, String separator) {
085        ArrayList<String> found = new ArrayList<String>();
086        findDescendantNamesWithSeparator(parent, tag, "", separator, found);
087        return found;
088    }
089    
090    /**
091     *  Find all of the  descendant elements of the given parent Node
092     *  whose tag name equals the given tag.
093     *
094     *  @param parent The root of the xml dom tree to search.
095     *  @param tag The tag name to match.
096     *  @param found The list of descendants that match the given tag.
097     */
098    private static void findDescendantNamesWithSeparator(Node parent, String tag, String descendants, String separator, List<String> found) {
099            if (parent instanceof Element) {
100                String elementName = ((Element)parent).getAttribute("name");
101                if (!elementName.equals(""))
102                        descendants += ((Element)parent).getAttribute("name");
103                if (parent.getNodeName().equals(tag)) {
104                found.add(descendants);
105                }
106                if (!elementName.equals(""))
107                        descendants += separator;
108        }
109        NodeList children = parent.getChildNodes();
110        for (int i = 0; i < children.getLength(); i++) {
111            Node child = children.item(i);
112                findDescendantNamesWithSeparator(child, tag, descendants, separator, found);
113        }
114    }
115    
116    /**
117     * Find the element described by nameList (path)
118     * 
119     * @param parent
120     * @param nameList
121     * @return
122     */
123    public static Element getElementAtNamedPath(Node parent, List<String>nameList) {
124        return getMakeElementAtNamedPath(parent, nameList, "", false);
125    }
126    
127    /**
128     * Make the element described by nameList (path)
129     * 
130     * @param parent
131     * @param nameList
132     * @return
133     */
134    public static Element makeElementAtNamedPath(Node parent, List<String>nameList, String tagname) {
135        return getMakeElementAtNamedPath(parent, nameList, tagname, true);
136    }
137    
138    /**
139     * Find the element described by nameList (path)
140     * 
141     * @param parent
142     * @param nameList
143     * @return
144     */
145    public static Element getMakeElementAtNamedPath(Node parent, List<String>nameList, String tagName, boolean makeNew) {
146        Element thisElement = null;
147        if (parent instanceof Element && nameList.size() > 0) {
148                for (int i=0; i<nameList.size(); i++) {
149                        String thisName = nameList.get(i);
150                        NodeList children = parent.getChildNodes();
151                        boolean foundChild = false;
152                        for (int j=0; j<children.getLength(); j++) {
153                                Node child = children.item(j);
154                                if (!(child instanceof Element)) continue;
155                                if (XmlUtil.getAttribute(child, "name").equals(thisName)) {
156                                        if (i == nameList.size()-1) thisElement = (Element)child;
157                                        parent = child;
158                                        foundChild = true;
159                                        break;
160                                }
161                        }
162                        
163                        // Didn't find it where we expected to.  Create a new one.
164                        if (makeNew && !foundChild && parent instanceof Element) {
165                                try {
166                                        Element newElement = XmlUtil.create(tagName, (Element)parent);
167                                        newElement.setAttribute("name", thisName);
168                                        parent.appendChild(newElement);
169                                        parent = newElement;
170                                        thisElement = newElement;
171                                }
172                                catch (Exception ex) {
173                                        System.err.println("Error making new " + tagName + " node named " + thisName);
174                                        break;
175                                }
176                        }
177                        
178                }
179        }
180        
181        return thisElement;
182    }
183    
184}