FileDocCategorySizeDatePackage
DOMUtils.javaAPI DocApache Lucene 2.0.06258Fri May 26 09:53:50 BST 2006org.apache.lucene.xmlparser

DOMUtils

public class DOMUtils extends Object

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringgetAttribute(org.w3c.dom.Element element, java.lang.String attributeName, java.lang.String deflt)

		String result=element.getAttribute(attributeName);
		if( (result==null)|| ("".equals(result) ) )
		{
			return deflt;
		}
		return result;
	
public static floatgetAttribute(org.w3c.dom.Element element, java.lang.String attributeName, float deflt)

		String result=element.getAttribute(attributeName);
		if( (result==null)|| ("".equals(result) ) )
		{
			return deflt;
		}
		return Float.parseFloat(result);
	
public static intgetAttribute(org.w3c.dom.Element element, java.lang.String attributeName, int deflt)

		String result=element.getAttribute(attributeName);
		if( (result==null)|| ("".equals(result) ) )
		{
			return deflt;
		}
		return Integer.parseInt(result);
	
public static booleangetAttribute(org.w3c.dom.Element element, java.lang.String attributeName, boolean deflt)

		String result = element.getAttribute(attributeName);
		if ((result == null) || ("".equals(result)))
		{
			return deflt;
		}
		return Boolean.getBoolean(result);
	
public static java.lang.StringgetAttributeOrFail(org.w3c.dom.Element e, java.lang.String name)

		String v = e.getAttribute(name);
		if (null == v)
		{
			throw new ParserException(e.getTagName() + " missing \"" + name
					+ "\" attribute");
		}
		return v;
	
public static java.lang.StringgetAttributeWithInheritance(org.w3c.dom.Element element, java.lang.String attributeName)
Returns an attribute value from this node, or first parent node with this attribute defined

param
element
param
attributeName
return
A non-zero-length value if defined, otherwise null

		String result=element.getAttribute(attributeName);
		if( (result==null)|| ("".equals(result) ) )
		{
			Node n=element.getParentNode();
			if((n==element)||(n==null))
			{
				return null;
			}
			Element parent=(Element) n;
			return getAttributeWithInheritance(parent,attributeName);
		}
		return result;		
	
public static java.lang.StringgetAttributeWithInheritanceOrFail(org.w3c.dom.Element e, java.lang.String name)

		String v = getAttributeWithInheritance(e, name);
		if (null == v)
		{
			throw new ParserException(e.getTagName() + " missing \"" + name
					+ "\" attribute");
		}
		return v;
	
public static org.w3c.dom.ElementgetChildByTagName(org.w3c.dom.Element e, java.lang.String name)

	       for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling())
		{
			if( (kid.getNodeType()==Node.ELEMENT_NODE) && (name.equals(kid.getNodeName())) )
			{
				return (Element)kid;
			}
		}
		return null;
	
public static org.w3c.dom.ElementgetChildByTagOrFail(org.w3c.dom.Element e, java.lang.String name)

		Element kid = getChildByTagName(e, name);
		if (null == kid)
		{
			throw new ParserException(e.getTagName() + " missing \"" + name
					+ "\" child element");
		}
		return kid;
	
public static java.lang.StringgetChildTextByTagName(org.w3c.dom.Element e, java.lang.String tagName)

		Element child=getChildByTagName(e,tagName);
		if(child!=null)
		{
			return getText(child);
		}
		return null;
	
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Element element)

		for (Node kid = element.getFirstChild(); kid != null; kid = kid
				.getNextSibling())
		{
			if (kid.getNodeType() == Node.ELEMENT_NODE) 
			{
				return (Element) kid;
			}
		}
		return null;
	
public static org.w3c.dom.ElementgetFirstChildOrFail(org.w3c.dom.Element e)

		Element kid = getFirstChildElement(e);
		if (null == kid)
		{
			throw new ParserException(e.getTagName()
					+ " does not contain a child element");
		}
		return kid;
	
public static java.lang.StringgetNonBlankTextOrFail(org.w3c.dom.Element e)

		String v = getText(e);
		if (null != v)
			v = v.trim();
		if (null == v || 0 == v.length())
		{
			throw new ParserException(e.getTagName() + " has no text");
		}
		return v;
	
public static java.lang.StringgetText(org.w3c.dom.Node e)

		StringBuffer sb=new StringBuffer();
		getTextBuffer(e, sb);
		return sb.toString();
	
private static voidgetTextBuffer(org.w3c.dom.Node e, java.lang.StringBuffer sb)

	    for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling())
		{
			switch(kid.getNodeType())
			{
				case Node.TEXT_NODE:
				{
					sb.append(kid.getNodeValue());
					break;
				}
				case Node.ELEMENT_NODE:
				{
					getTextBuffer(kid, sb);
					break;
				}
				case Node.ENTITY_REFERENCE_NODE:
				{
					getTextBuffer(kid, sb);
					break;
				}
			}
		}
	
public static org.w3c.dom.ElementinsertChild(org.w3c.dom.Element parent, java.lang.String tagName, java.lang.String text)

	  	Element child = parent.getOwnerDocument().createElement(tagName);
		parent.appendChild(child);
		if(text!=null)
		{
		  	child.appendChild(child.getOwnerDocument().createTextNode(text));
		}
		return child;
	
public static org.w3c.dom.DocumentloadXML(java.io.Reader is)
Helper method to parse an XML file into a DOM tree, given a filename.

param
pXmlFile name of the XML file to be parsed
return
an org.w3c.dom.Document object


		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		try
		{
			db = dbf.newDocumentBuilder();
		}
		catch (Exception se)
		{
			throw new RuntimeException("Parser configuration error", se);
		}

		// Step 3: parse the input file
		org.w3c.dom.Document doc = null;
		try
		{
			doc = db.parse(new InputSource(is));
			//doc = db.parse(is);
		}
		catch (Exception se)
		{
			throw new RuntimeException("Error parsing file:" + se, se);
		}

		return doc;