View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // DOMUtil.java
20  // Since: Jul 23, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.util.xml;
26  
27  
28  import com.google.gwt.core.client.GWT;
29  import com.google.gwt.xml.client.Node;
30  import com.google.gwt.xml.client.NodeList;
31  
32  /**
33   * Utilities to traverse DOM elements
34   * @author leo
35   *
36   */
37  public class DOMUtil {
38  
39  	/**
40  	 * Non constractable
41  	 */
42  	private DOMUtil()
43  	{}
44  	
45  	public static String getAttributeValue(Node node, String attributeName)
46  	{
47  		Node attributeValue = node.getAttributes().getNamedItem(attributeName);
48  		return attributeValue != null ? attributeValue.getNodeValue() : null;
49  	}
50  	
51  	public static String getAttributeValue(Node node, String attributeName, String defaultValue)
52  	{
53  		String value = getAttributeValue(node, attributeName);
54  		return value != null ? value : defaultValue;
55  	}
56  	
57  	public static boolean getBooleanAttributeValue(Node node, String attributeName, boolean defaultValue)
58  	{
59  		String value = getAttributeValue(node, attributeName);
60  		return value != null ? (value.equals("true") ? true : false) : defaultValue;
61  	}
62  	
63  	public static int getIntAttributeValue(Node node, String attributeName, int defaultValue)
64  	{
65  		String value = getAttributeValue(node, attributeName);
66  		try
67  		{
68  			return value != null ? Integer.parseInt(value) : defaultValue;
69  		}
70  		catch(NumberFormatException e)
71  		{
72  			GWT.log("parseInt: " + value, e);
73  			return defaultValue;
74  		}
75  	}
76  	
77  	public static NodeList getChildNodeList(Node baseNode, String tagName)
78  	{
79  		NodeList childNodeList = baseNode.getChildNodes();
80  		NodeListImpl returnNodeList = new NodeListImpl();
81          for ( int i = 0; i < childNodeList.getLength(); i++ ) {
82              Node childNode = childNodeList.item(i);
83              short nodeType = childNode.getNodeType();
84              if ( nodeType != Node.TEXT_NODE && childNode.getNodeName().equalsIgnoreCase(tagName) ) {
85                  returnNodeList.add(childNode);
86              }
87          }
88  		return returnNodeList;
89  	}
90  	
91  }
92  
93  
94  
95