Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0ecdde471c207100dc76f241c08c30fb153923d0 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

import java.io.*;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * A simple XML writer.
 */
public class XMLWriter extends PrintWriter {
	protected int tab;

	/* constants */
	protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$

public XMLWriter(OutputStream output) throws UnsupportedEncodingException {
	super(new OutputStreamWriter(output, "UTF8")); //$NON-NLS-1$
	tab = 0;
	println(XML_VERSION);
}
public void endTag(String name) {
	tab--;
	printTag('/' + name, null);
}
public void printSimpleTag(String name, Object value) {
	if (value != null) {
		printTag(name, null, true, false);
		print(getEscaped(String.valueOf(value)));
		printTag('/' + name, null, false, true);
	}
}
public void printTabulation() {
	for (int i = 0; i < tab; i++)
		super.print('\t');
}
private void printTag(String name, HashMap parameters) {
	printTag(name, parameters, true, true);
}
private void printTag(String name, HashMap parameters, boolean tab, boolean newLine) {
	printTag(name, parameters, tab, newLine, false);
}
private void printTag(String name, HashMap parameters, boolean tab, boolean newLine, boolean end) {
	StringBuffer sb = new StringBuffer();
	sb.append("<"); //$NON-NLS-1$
	sb.append(name);
	if (parameters != null)
		for (Enumeration e = Collections.enumeration(parameters.keySet()); e.hasMoreElements();) {
			sb.append(" "); //$NON-NLS-1$
			String key = (String) e.nextElement();
			sb.append(key);
			sb.append("=\""); //$NON-NLS-1$
			sb.append(getEscaped(String.valueOf(parameters.get(key))));
			sb.append("\""); //$NON-NLS-1$
		}
	if (end)
		sb.append('/');
	sb.append(">"); //$NON-NLS-1$
	if (tab)
		printTabulation();
	if (newLine)
		println(sb.toString());
	else
		print(sb.toString());
}
public void startTag(String name, HashMap parameters) {
	startTag(name, parameters, true);
}
public void startTag(String name, HashMap parameters, boolean newLine) {
	printTag(name, parameters, true, newLine);
	tab++;
}
public void startAndEndTag(String name, HashMap parameters, boolean newLine) {
	printTag(name, parameters, true, true, true);
}
private static void appendEscapedChar(StringBuffer buffer, char c) {
	String replacement = getReplacement(c);
	if (replacement != null) {
		buffer.append('&');
		buffer.append(replacement);
		buffer.append(';');
	} else {
		buffer.append(c);
	}
}
public static String getEscaped(String s) {
	StringBuffer result = new StringBuffer(s.length() + 10);
	for (int i = 0; i < s.length(); ++i)
		appendEscapedChar(result, s.charAt(i));
	return result.toString();
}
private static String getReplacement(char c) {
	// Encode special XML characters into the equivalent character references.
	// These five are defined by default for all XML documents.
	switch (c) {
		case '<' :
			return "lt"; //$NON-NLS-1$
		case '>' :
			return "gt"; //$NON-NLS-1$
		case '"' :
			return "quot"; //$NON-NLS-1$
		case '\'' :
			return "apos"; //$NON-NLS-1$
		case '&' :
			return "amp"; //$NON-NLS-1$
	}
	return null;
}
}

Back to the top