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  // Utilities.java
20  // Since: 2007/06/12
21  //
22  // $URL$ 
23  // $Author$ ssksn
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib.old;
26  
27  import java.util.Iterator;
28  import java.util.Set;
29  
30  import org.utgenome.gwt.utgb.client.track.TrackGroupProperty;
31  import org.utgenome.gwt.utgb.client.util.xml.NodeListImpl;
32  
33  import com.google.gwt.xml.client.NamedNodeMap;
34  import com.google.gwt.xml.client.Node;
35  import com.google.gwt.xml.client.NodeList;
36  
37  /**
38   * Utilitiy methods. No constructor is provided for the class.
39   * 
40   * @author ssksn
41   * @since GWT1.4
42   * @version 0.1
43   */
44  public final class Utilities {
45  
46  	/**
47  	 * Non constractable 
48  	 */
49  	private Utilities() {
50  		throw new AssertionError();
51  	}
52  
53  	/**
54  	 * Gets the key and value string
55  	 * 
56  	 * @param key
57  	 *            key
58  	 * @param connector
59  	 * @param value
60  	 *            value
61  	 * @return key + connector + value
62  	 */
63  	public static final String getKeyAndValueString(final String key, final String connector, final String value) {
64  		return key + connector + value;
65  	}
66  
67  	/**
68  	 * Gets the key and value string
69  	 * 
70  	 * @param key
71  	 *            key
72  	 * @param value
73  	 *            value
74  	 * @return "key=value"
75  	 * @see #getKeyAndValueString(String, String, String)
76  	 */
77  	public static final String getKeyAndValueString(final String key, final String value) {
78  		return getKeyAndValueString(key, "=", value);
79  	}
80  
81  	public static final String getAttributeValue(final Node node, final String attributeName) {
82  		final NamedNodeMap attributeMap = node.getAttributes();
83  		final Node attributeNode = attributeMap.getNamedItem(attributeName);
84  		if (attributeNode == null)
85  			return null;
86  		else
87  			return attributeNode.getNodeValue();
88  	}
89  
90  	public static final String getAttributeValue(final Node node, final String attributeName, final String defaultValue) {
91  		String value = getAttributeValue(node, attributeName);
92  		return value != null ? value : defaultValue;
93  	}
94  
95  	public static final NodeList getTagChildNodes(final Node parent) {
96  		final NodeList originalNodeList = parent.getChildNodes();
97  
98  		final NodeListImpl tagNodeList = new NodeListImpl();
99  
100 		final int SIZE = originalNodeList.getLength();
101 		for (int i = 0; i < SIZE; i++) {
102 			final Node originalNode = originalNodeList.item(i);
103 
104 			final short nodeType = originalNode.getNodeType();
105 			if (nodeType != Node.TEXT_NODE) {
106 				tagNodeList.add(originalNode);
107 			}
108 		}
109 
110 		return tagNodeList;
111 	}
112 
113 	public static final String getPropertyXMLTag(final String key, final String value) {
114 		return "<property key=\"" + key + "\">" + value + "</property>";
115 	}
116 
117 	public static final String convertTrackGroupPropertyToXML(final TrackGroupProperty trackGroupProperty) {
118 		final Set<String> keySet = trackGroupProperty.keySet();
119 		if (keySet.size() == 0)
120 			return null;
121 
122 		final StringBuffer buf = new StringBuffer();
123 
124 		final Iterator<String> keyIterator = keySet.iterator();
125 		while (keyIterator.hasNext()) {
126 			final String key = (keyIterator.next());
127 
128 			final String value = (trackGroupProperty.getProperty(key));
129 
130 			if (buf.length() > 0)
131 				buf.append('\n');
132 			buf.append(getPropertyXMLTag(key, value));
133 		}
134 
135 		return buf.toString();
136 	}
137 
138 	public static final String convertTrackGroupPropertyToXML(final TrackGroupProperty trackGroupProperty, final Set<String> outputKeySet) {
139 		final Set<String> keySet = trackGroupProperty.keySet();
140 		if (keySet.size() == 0)
141 			return null;
142 
143 		final StringBuffer buf = new StringBuffer();
144 
145 		final Iterator<String> keyIterator = keySet.iterator();
146 		while (keyIterator.hasNext()) {
147 			final String key = (keyIterator.next());
148 
149 			if (outputKeySet.contains(key)) {
150 				final String value = (trackGroupProperty.getProperty(key));
151 
152 				if (buf.length() > 0)
153 					buf.append('\n');
154 				buf.append(getPropertyXMLTag(key, value));
155 			}
156 		}
157 
158 		if (buf.length() == 0)
159 			return null;
160 		else
161 			return buf.toString();
162 	}
163 }