Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca3e01f4ca70087f1617d731dc5738d459689be1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
 * Copyright (c) 1997, 2008 by ProSyst Software GmbH
 * http://www.prosyst.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.util.xml.impl;

import java.io.*;
import java.util.*;
import org.eclipse.equinox.internal.util.string.CharBuffer;
import org.eclipse.equinox.internal.util.xml.Tag;

/**
 * @author Ivan Dimitrov
 * @author Pavlin Dobrev
 * @version 1.0
 */

public class TagImpl implements Tag {

	private CharBuffer fContent = null;
	private String fContentString = null;
	private String fName = null;
	private Hashtable fAttributes = null;
	private Vector fTags = null;
	private int fLine = -1;
	protected boolean fInline = false;

	/**
	 * A hashtable enumerator class for empty hash tables, specializes the
	 * general Enumerator
	 */
	private static Enumeration fEmptyEnumeration = new Enumeration() {
		public boolean hasMoreElements() {
			return false;
		}

		public Object nextElement() {
			throw new NoSuchElementException("Hashtable Enumerator");
		}
	};

	/**
	 * 
	 */
	public TagImpl() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getAttribute(java.lang.String)
	 */
	public String getAttribute(String aAttrName) {
		return (String) ((fAttributes != null) ? fAttributes.get(aAttrName) : null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getAttributes()
	 */
	public Enumeration getAttributeNames() {
		return (fAttributes != null) ? fAttributes.keys() : fEmptyEnumeration;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getAttributeValues()
	 */
	public Enumeration getAttributeValues() {
		return (fAttributes != null) ? fAttributes.elements() : fEmptyEnumeration;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getName()
	 */
	public String getName() {
		return fName;
	}

	/**
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getContent() note: The content that
	 *      is to be returned will be trimmed first
	 */
	public String getContent() {
		if (fContentString != null) {
			return fContentString;
		} else if (fContent != null) {
			fContentString = fContent.trim();
			fContent = null;
			return fContentString;
		}
		return "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getLine()
	 */
	public int getLine() {
		return fLine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#size()
	 */
	public int size() {
		return (fTags != null) ? fTags.size() : 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getTagAt(int)
	 */
	public Tag getTagAt(int aPosition) {
		return (Tag) fTags.elementAt(aPosition);
	}

	protected void setName(String aName) {
		fName = aName;
	}

	protected void addAttribute(String aAttrName, String aAttrValue) {
		if (fAttributes == null) {
			fAttributes = new Hashtable(1);
		}
		fAttributes.put(aAttrName, aAttrValue);
	}

	protected void addTag(Tag aTag) {
		if (fTags == null) {
			fTags = new Vector(2, 3);
		}
		fTags.addElement(aTag);
	}

	protected void appendContent(CharBuffer toAppend) {
		if (fContent == null) {
			fContent = new CharBuffer(toAppend.length());
		}
		fContent.append(toAppend.getValue());
	}

	protected void appendContent(String toAppend) {
		if (fContent == null) {
			fContent = new CharBuffer(toAppend.length());
		}
		fContent.append(toAppend);
	}

	protected CharBuffer getContentBuffer() {
		if (fContent == null) {
			fContent = new CharBuffer(5);
		}
		return fContent;
	}

	protected void setLine(int aLine) {
		fLine = aLine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.internal.util.xml.Tag#getContent(int, java.lang.String)
	 */
	public String getContent(int aPos, String aName) {
		Tag child = getTagAt(aPos);
		if (child == null)
			throw new NullPointerException("There is no such a tag. [Parent tag name] = [" + aName + "], [child tag index] = " + aPos + ", [child tag name] = [" + aName + ']');

		if (child.getName().equals(aName))
			return child.getContent();

		throw new IllegalArgumentException("There is no such a tag. [Parent tag name] = [" + aName + "], [child tag index] = " + aPos + ", [child tag name] = [" + aName + ']');
	}

	/* begin serialization see FR [ 13325 ] XML: Make Tag externalizable */

	/**
	 * @see org.eclipse.equinox.internal.util.io.Externalizable#readObject(java.io.InputStream)
	 */
	public void readObject(InputStream is) throws Exception {
		if (!(is instanceof ObjectInput)) {
			is = new ObjectInputStream(is);
		}
		readExternal((ObjectInput) is);
	}

	/**
	 * @see org.eclipse.equinox.internal.util.io.Externalizable#writeObject(java.io.OutputStream)
	 */
	public void writeObject(OutputStream os) throws Exception {
		if (!(os instanceof ObjectOutput)) {
			os = new ObjectOutputStream(os);
		}
		writeExternal((ObjectOutput) os);
		os.flush();
	}

	/**
	 * @param input
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
		fName = input.readUTF();
		/* read content */
		boolean hasContent = input.readBoolean();
		if (hasContent) {
			fContentString = input.readUTF();
		}
		int size;
		/* read attributes */
		size = input.readInt();
		if (size > 0) {
			fAttributes = new Hashtable(size, 1);
			for (int i = 0; i < size; i++) {
				String key = input.readUTF();
				String val = input.readUTF();
				fAttributes.put(key, val);
			}
		}
		/* read elements */
		size = input.readInt();
		if (size > 0) {
			fTags = new Vector(size);
			for (int i = 0; i < size; i++) {
				TagImpl tag = new TagImpl();
				tag.readExternal(input);
				fTags.addElement(tag);
			}
		}
	}

	/**
	 * @param output
	 * @throws IOException
	 */
	public void writeExternal(ObjectOutput output) throws IOException {
		output.writeUTF(fName);
		/* write content */
		String content = null;
		if (fContentString != null) {
			content = fContentString;
		} else if (fContent != null) {
			content = fContent.trim();
		}
		if (content != null) {
			output.writeBoolean(true);
			output.writeUTF(content);
		} else {
			output.writeBoolean(false);
		}
		/* write attributes */
		if (fAttributes != null) {
			output.writeInt(fAttributes.size());
			for (Enumeration e = fAttributes.keys(); e.hasMoreElements();) {
				String key = (String) e.nextElement();
				String val = (String) fAttributes.get(key);
				output.writeUTF(key);
				output.writeUTF(val);
			}
		} else {
			output.writeInt(0);
		}
		/* write elements */
		if (fTags != null) {
			output.writeInt(fTags.size());
			for (int i = 0, size = fTags.size(); i < size; i++) {
				TagImpl current = (TagImpl) fTags.elementAt(i);
				current.writeExternal(output);
			}
		} else {
			output.writeInt(0);
		}
		output.flush();
	}

	/* begin simple, utility method for debugging */

	/**
	 * Writes the tag as formatted XML into the given writer.
	 * 
	 * @param out
	 *            the output writer, where tag is serialized to XML
	 * @param level
	 *            the initial level. If set to negative value, the indent
	 *            /formatting/ of the XML is disable, and it is written on a
	 *            single line. Otherwise, the level is used to calculate the
	 *            offset from the left margin.
	 * @throws IOException
	 *             on I/O error
	 */
	public void writeXml(Writer out, int level) throws IOException {
		boolean indent = level >= 0;
		for (int i = 0; indent && i < level; i++)
			out.write(' '); /* indent */
		out.write('<');
		out.write(fName);
		if (fAttributes != null) {
			for (Enumeration e = fAttributes.keys(); e.hasMoreElements();) {
				out.write(' ');
				String key = (String) e.nextElement();
				String val = (String) fAttributes.get(key);
				out.write(key);
				out.write("=\"");
				out.write(val);
				out.write('"');
			}
		}
		/* don't use getContent() - this will demolish the buffer */
		String content = null;
		if (fContentString != null) {
			content = fContentString;
		} else if (fContent != null) {
			content = fContent.trim();
		}
		boolean isShort = content == null && fTags == null;
		if (isShort) {
			/* close immediately */
			out.write("/>");
			if (indent)
				out.write('\n');
		} else {
			out.write('>');
			if (indent)
				out.write('\n');
			/* write elements */
			for (int i = 0; fTags != null && i < fTags.size(); i++) {
				TagImpl child = (TagImpl) fTags.elementAt(i);
				child.writeXml(out, level < 0 ? -1 : (level + 1));
			}
			/* write contents */
			if (content != null) {
				for (int i = 0; indent && i < level + 1; i++)
					out.write(' '); /* indent */
				out.write(content);
				if (indent)
					out.write('\n');
			}
			/* write closing tag */
			for (int i = 0; indent && i < level; i++)
				out.write(' '); /* indent */
			out.write("</");
			out.write(fName);
			out.write('>');
			if (indent)
				out.write('\n');
		}
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringWriter out = new StringWriter();
		try {
			writeXml(out, 0);
		} catch (IOException e) {
			/* this will never happen */
		}
		return out.toString();
	}

}

Back to the top