/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.util; import org.w3c.dom.*; /** * A class which wraps around the DOM Element class, providing some * additional functionality. * * @author Eric J. Bowersox <erbo@silcom.com> * @version X * @see org.w3c.dom.Element */ public class DOMElementHelper { /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private Element elt; // element housed by this helper class /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ /** * Constructs a new DOMElementHelper to wrap a specific Element. * * @param elt The Element to be wrapped. */ public DOMElementHelper(Element elt) { this.elt = elt; } // end constructor /*-------------------------------------------------------------------------------- * Internal static operations *-------------------------------------------------------------------------------- */ /** * Returns the content of all text nodes underneath a specified Element, concatenated * together into a single string. * * @param e The Element to extract text from. * @return The text content under this Element node. If the specified Element * has no text nodes underneath it, returns null. */ private static String getTextOfElement(Element e) { NodeList kids = e.getChildNodes(); if (kids==null) return null; // no text? StringBuffer b = null; for (int i=0; iElement wrapped by this object. * * @return See above. */ public Element getElement() { return elt; } // end getElement /** * Searches for the first sub-element of the wrapped Element with the given name. * * @param name Name of the sub-element to search for. * @return The first sub-element of the wrapped Element with the specified name. * If the Element has no child Elements with the given name, * the method returns null. */ public Element getSubElement(String name) { NodeList kids = elt.getChildNodes(); if (kids==null) return null; // no children? for (int i=0; iElement, concatenated * together into a single string. * * @return The text content under the wrapped Element node. If the wrapped Element * has not text nodes underneath it, returns null. */ public String getElementText() { return getTextOfElement(elt); } // end getElementText /** * Returns the content of all text nodes underneath the first sub-element of the wrapped * Element, with the given name, concatenated together into a single string. * * @param name The name of the sub-element to search for. * @return The text content under the specified sub-element of the wrapped Element node. * If the wrapped Element does not have a sub-element with the given name, or * that sub-element has no text nodes underneath it, returns null. */ public String getSubElementText(String name) { Element se = getSubElement(name); if (se==null) return null; else return getTextOfElement(se); } // end getSubElementText /** * Determines whether the wrapped Element has a sub-element with the given name. * * @param name Name of the sub-element to search for. * @return true if the wrapped Element has a sub-element with the * specified name, false if not. */ public boolean hasChildElement(String name) { Element tmp = getSubElement(name); return (tmp==null) ? false : true; } // end hasChildElement /** * Determines whether the wrapped Element has an attribute with the given name. * * @param name Name of the attribute to search for. * @return true if the wrapped Element has an attribute with the * specified name, false if not. */ public boolean hasAttribute(String name) { return !(StringUtil.isStringEmpty(elt.getAttribute(name))); } // end hasAttribute /** * Returns the value of a specified attribute of the wrapped Element, expressed as * an integer. * * @param name Name of the attribute to search for. * @return An Integer object containing the value of the specified attribute. If * the wrapped Element has no such attribute, or if the attribute's value * cannot be expressed as an integer, returns null. */ public Integer getAttributeInt(String name) { String tmp = elt.getAttribute(name); if (StringUtil.isStringEmpty(tmp)) return null; try { // convert to an Integer return new Integer(tmp); } // end try catch (NumberFormatException nfe) { // return a null value on error return null; } // end catch } // end getAttributeInt } // end DOMElementHelper