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  // XMLAttribute.java
20  // Since: Jul 20, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.util.xml;
26  
27  import java.util.ArrayList;
28  
29  public class XMLAttribute {
30  	class Pair {
31  		private String name;
32  		private String value;
33  
34  		Pair(String name, String value) {
35  			this.name = name;
36  			this.value = value;
37  		}
38  
39  		@Override
40  		public String toString() {
41  			return name + "=\"" + XMLWriter.escape(value) + "\"";
42  		}
43  	}
44  
45  	private ArrayList<Pair> attributeTable = new ArrayList<Pair>();
46  
47  	public XMLAttribute() {
48  
49  	}
50  
51  	public XMLAttribute(String name, String value) {
52  		attributeTable.add(new Pair(name, value));
53  	}
54  
55  	public XMLAttribute add(String name, String value) {
56  		attributeTable.add(new Pair(name, value));
57  		return this;
58  	}
59  
60  	public XMLAttribute add(String name, int value) {
61  		attributeTable.add(new Pair(name, Integer.toString(value)));
62  		return this;
63  	}
64  
65  	public XMLAttribute add(String name, long value) {
66  		attributeTable.add(new Pair(name, Long.toString(value)));
67  		return this;
68  	}
69  
70  	public XMLAttribute add(String name, boolean value) {
71  		attributeTable.add(new Pair(name, value ? "true" : "false"));
72  		return this;
73  	}
74  
75  	@Override
76  	public String toString() {
77  		StringBuffer buffer = new StringBuffer();
78  		int i = 0;
79  		for (; i < attributeTable.size() - 1; i++) {
80  			Pair pair = attributeTable.get(i);
81  			buffer.append(pair.toString());
82  			buffer.append(" ");
83  		}
84  		if (i < attributeTable.size())
85  			buffer.append(attributeTable.get(i).toString());
86  		return buffer.toString();
87  	}
88  
89  }