Subversion Repositories DevTools

Rev

Rev 6914 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.utilities;
2
 
3
import java.util.ArrayList;
4
 
5
public class XmlBuilder {
6
	/**
7
	 * A class to simply the process of creating nice XML
8
	 * Designed to only write xml fragments
9
	 */
7082 dpurdie 10
	private static final  String mlf = System.getProperty("line.separator");
6914 dpurdie 11
 
12
	String mTagName;
13
	boolean mComment = false;
14
	boolean mNoShrink = false;
7082 dpurdie 15
	ArrayList<Attribute> mAttributes = new ArrayList<Attribute>();
6914 dpurdie 16
	ArrayList<XmlBuilder> mElements = new ArrayList<XmlBuilder>();
17
 
18
	/**
19
	 * Class to describe an an XML attribute
20
	 */
7082 dpurdie 21
	private class Attribute
6914 dpurdie 22
	{
23
		private String aName;
24
		private String aValue;
25
 
7082 dpurdie 26
		Attribute( String name, String value)
6914 dpurdie 27
		{
28
			aName = name;
29
			aValue = value;
30
		}
31
 
32
		public String toString()
33
		{
34
			String result;
35
			result = " " + aName + "=\"" + aValue + "\"";
36
			return result;
37
		}
38
	}
39
 
40
	/**	Create an XML element
41
	 * 
42
	 * @param name	- Tag name of the element
43
	 */
44
	public XmlBuilder( String name)
45
	{
46
		mTagName = name;
47
	}
48
 
49
	/**	Add an attribute the the XML element
50
	 * 
51
	 * @param name - The name of the attribute
52
	 * @param value - The value of the attribute
53
	 * @return The XML element. This allows chaining of operations
54
	 */
55
	public XmlBuilder addAttribute ( String name, String value)
56
	{
7082 dpurdie 57
		mAttributes.add(new Attribute(name,value));
6914 dpurdie 58
		return this;
59
	}
60
 
61
	/**	Add an attribute the the XML element
62
	 * 
63
	 * @param name - The name of the attribute
64
     * @param value - The value of the attribute
65
	 * @return The XML element. This allows chaining of operations
66
	 */
67
	public XmlBuilder addAttribute(String name, int value) {
7082 dpurdie 68
		mAttributes.add(new Attribute(name,Integer.toString(value)));
6914 dpurdie 69
		return this;
70
	}
71
 
72
	/**	Add an attribute the the XML element
73
	 * 
74
	 * @param name - The name of the attribute
75
     * @param value - The value of the attribute
76
	 * @return The XML element. This allows chaining of operations
77
	 */
78
	public XmlBuilder addAttribute(String name, long value) {
7082 dpurdie 79
		mAttributes.add(new Attribute(name,Long.toString(value)));
6914 dpurdie 80
		return this;
81
	}
82
 
83
	/**Add a child element to the XML element
84
	 * 
85
	 * @param element - The element to extend
86
	 * @return The XML element. This allows chaining of operations
87
	 */
88
	public XmlBuilder addElement(XmlBuilder element)
89
	{
90
		mElements.add(element);
91
		return this;
92
	}
93
 
94
	/**Create and add a new XmlBuild entry
95
	 * 
96
	 * @param name	- Name of the element to add
97
	 * @return The element just added
98
	 */
99
	public XmlBuilder addNewElement(String name) {
100
 
101
		XmlBuilder element = new XmlBuilder(name);
102
		addElement(element);
103
		return element;
104
	}
105
 
106
	/**Add a comment element
107
	 * 
108
	 */
109
	public XmlBuilder addComment(String comment)
110
	{
111
		addNewElement(comment).isComment();
112
		return this;
113
	}
114
 
115
	/** Create a property string
116
	 * 
117
	 * @param name		- Name of the property
118
	 * @param value		- Value of the property
119
	 */
120
	public XmlBuilder addProperty(String name, String value)
121
	{
122
		addNewElement("property")
123
			.addAttribute("name", name)
124
			.addAttribute("value", value);
125
 
126
		return this;
127
	}
128
 
129
	/** Create a property string
130
	 * 
131
	 * @param name		- Name of the property
132
	 * @param value		- Integer Value of the property
133
	 */
134
	public XmlBuilder addProperty(String name, int value)
135
	{
136
		addNewElement("property")
137
			.addAttribute("name", name)
138
			.addAttribute("value", value);
139
 
140
		return this;
141
	}
142
 
143
	/** Create a property string
144
	 * 
145
	 * @param name		- Name of the property
146
	 * @param value		- Long Integer Value of the property
147
	 */
148
	public XmlBuilder addProperty(String name, long value)
149
	{
150
		addNewElement("property")
151
			.addAttribute("name", name)
152
			.addAttribute("value", value);
153
 
154
		return this;
155
	}
156
 
157
	/** Create a property tag - will only appear if boolean value is true
158
	 * @param name		- Name of the property
159
	 * @param flag		- Create property if true
160
	 */
161
	public XmlBuilder makePropertyTag(String name, boolean flag)
162
	{
163
		if (flag)
164
		{
165
			addProperty(name, "");
166
		}
167
		return this;
168
	}
169
 
170
	/**Convert the XML to a string
171
	 * 
172
	 * @param indent	- Indent prefix
173
	 * @return String version of the XML
174
	 */
175
	public String toString(String indent)
176
	{
177
		String leadin = "<";
178
		String leadout = "/>";
179
 
180
		if (mComment)
181
		{
182
			leadin = "<!-- ";
183
			leadout = " -->";
184
		}
185
 
186
		String result = indent + leadin + mTagName;
187
 
188
		// Process each attribute
7082 dpurdie 189
		for (Attribute e : mAttributes) {
6914 dpurdie 190
		    result += e.toString();
191
		}
192
 
193
		// Process child elements
194
		if (mElements.isEmpty() && ! mNoShrink)
195
		{
196
			result += leadout + mlf;	
197
		}
198
		else
199
		{
200
			result += ">" + mlf;
201
			// Process each child
202
			for (XmlBuilder e : mElements) {
203
			    result += e.toString( "  " + indent);
204
			}
205
 
206
			result += indent + "</"+ mTagName +">" + mlf;
207
		}
208
 
209
		return result;
210
	}
211
 
212
	/**Convert the XML to a string with no leading indent
213
	 * 
214
	 * @return String version of the XML
215
	 */
216
	public String toString()
217
	{
218
		return toString("");
219
	}
220
 
221
	/** Convert the XML to a single line of text
222
	 *  Remove end-of-line characters.
223
	 *  Replace double quotes with single quotes
224
	 *  Simplifies UTF processing
225
	 */
226
	public String toOneLine()
227
	{
228
		return toString("").replace(mlf, "").replace("\"", "'");
229
	}
230
 
231
	/**Mark this XML element as a comment
232
	 * Comments do not have attributes or child elements
233
	 * 
234
	 * Chaining with this method is not possible. Use addComment.
235
	 * 
236
	 * @return
237
	 */
238
	public XmlBuilder isComment() {
239
		mComment = true;
240
		return this;
241
	}
242
 
243
	/**Mark this XML element as always expanded.
244
	 * This will prevent the XML tag being shrink if it has no child elements
245
	 * 
246
	 * @return The current element
247
	 */
248
	public XmlBuilder isExpanded() {
249
		mNoShrink = true;
250
		return this;
251
	}
252
 
253
}