Tuesday, 2 August 2011

General XML Utility Functions For JDOM

 
package net.firstpartners.nounit.utility;

/**
 * Title:        NoUnit - Identify Classes that are not being unit Tested
 *
 * Copyright (C) 2001  Paul Browne , FirstPartners.net
 *
 *
 * 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.
 *
 @author Paul Browne
 @version 0.6
 */

import java.io.*;
import java.util.*;
import java.util.zip.*;

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;


/**
 * General XML Utility Functions
 */
public class XmlUtil {
    
  /**
   * indexes the nodes in the document that is passed in , via a HashMap mapping
   * mapping is in the format <index> as String , handle to <element> of node<BR>
   * Strings are used as they are better lookup in the hashmap.
   @param inXmlDocument to generated the hashmap from
   @param uniqueAttribute to do the index on (i.e. key in HashMap). Examples
   *        of uniqueAttributes are id's or names.
   @return HashMap containing mappings
   */
  public static HashMap getNodeIndex(Document inXmlDocument, 
                                        String uniqueAttribute) {

    //Internal Variables
    int stackPointer=0;
    String locationId =null;
    Attribute tmpAttribute=null;
    Element thisElement=null;
    ListIterator deepestList=null;

    HashMap mappings = new HashMap();
    List stack = new Vector();

    //Get the list information for the entire document
    stack.add(inXmlDocument.getContent().listIterator());

    //Loop though the elements on list
    while (!stack.isEmpty()){

      //Get the last list on the stack
      deepestList = (ListIterator)stack.get(stack.size()-1);

      //Does this list have more elements?
      if (deepestList.hasNext()) {

        //if so Get Next element from this list
        thisElement = (Element)deepestList.next();

        //Add Mapping for this element to hashtable
        tmpAttribute = thisElement.getAttribute(uniqueAttribute);

        //Attibute can be null for non folder elements (e.g. root element) - if so ignore
        if (tmpAttribute!=null) {
          locationId= tmpAttribute.getValue();
          if ((locationId!=null)&&(locationId!="")) {
             mappings.put(locationId.toString(),thisElement);

          }
        //end add mapping

        //does this list have children ?
        if(thisElement.hasChildren()){

          //if so add to the stack
          stackPointer++;
          stack.add(thisElement.getChildren().listIterator());
        }
      }
      else
      {
        //if not , remove this list from the stack
        stack.remove(stackPointer);
        stackPointer--;

      // end if stack has more elements

    }

    return mappings;
  }

  /**
   * gets all elements in the XML Document Being Passed in <BR>
   @param inXmlDocument to generated the hashmap from
   @return nodeList containing nodes
   */
  public static HashSet getAllNodes(Document inXmlDocument) {

    //Internal Variables
    int stackPointer=0;
    int index=1;
    String locationId=null;
    Element currentElement=null;
    Element parentElement=null;
    Element thisElement=null;
    Attribute tmpAttribute=null;
    List elementList=null;
    ListIterator deepestList=null;

    HashMap mappings = new HashMap();
    HashSet nodeList = new HashSet();
    List stack = new Vector();              //Implements list interface

    //Get the list information for the entire document - kick start loop
    stack.add(inXmlDocument.getContent().listIterator());

    //Loop though the elements on list
    while (!stack.isEmpty()){

      //Get the last list on the stack
      deepestList = (ListIterator)stack.get(stack.size()-1);

      //Does this list have more elements?
      if (deepestList.hasNext()) {

        //if so Get Next element from this list
        thisElement = (Element)deepestList.next();

        // add this element to the list
        nodeList.add(thisElement);
 
        //does this list have children ?
        if(thisElement.hasChildren()){

          //if so add to the stack
          stackPointer++;
          stack.add(thisElement.getChildren().listIterator());
        }
      }
      else
      {
        //if not , remove this list from the stack
        stack.remove(stackPointer);
        stackPointer--;

      // end if stack has more elements

    }

    return nodeList;
  }
  
  /**
   * Search for Nodes within Jdom Document<BR>
   @param inDocumentToSearch XML-JDOM Document  
   @param  uniqueIdentifierName we can use to index the document (unique
   *    attribute like id or name present on the node we are searching for)
   @param uniqueIdentifierToFind in the indexed document
   */
   public static Element findNode(Document inDocumentToSearch 
                            , String uniqueIdentifierName
                            , String uniqueIdentifierToFind) {
       
        // index document                        
        HashMap index = getNodeIndex(inDocumentToSearch, 
                                     uniqueIdentifierName);
                                        
        // Now get required element from index
        return (Element)index.get(uniqueIdentifierToFind);
                                
                        
   }
}

   
  

No comments:

Post a Comment