KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > XMLWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import java.io.*;
14 import java.util.Collections JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 /**
19  * A simple XML writer.
20  */

21 public class XMLWriter extends PrintWriter {
22     protected int tab;
23
24     /* constants */
25     protected static final String JavaDoc XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
26

27 public XMLWriter(OutputStream output) throws UnsupportedEncodingException {
28     super(new OutputStreamWriter(output, "UTF8")); //$NON-NLS-1$
29
tab = 0;
30     println(XML_VERSION);
31 }
32 public void endTag(String JavaDoc name) {
33     tab--;
34     printTag('/' + name, null);
35 }
36 public void printSimpleTag(String JavaDoc name, Object JavaDoc value) {
37     if (value != null) {
38         printTag(name, null, true, false);
39         print(getEscaped(String.valueOf(value)));
40         printTag('/' + name, null, false, true);
41     }
42 }
43 public void printTabulation() {
44     for (int i = 0; i < tab; i++)
45         super.print('\t');
46 }
47 private void printTag(String JavaDoc name, HashMap JavaDoc parameters) {
48     printTag(name, parameters, true, true);
49 }
50 private void printTag(String JavaDoc name, HashMap JavaDoc parameters, boolean tab, boolean newLine) {
51     printTag(name, parameters, tab, newLine, false);
52 }
53 private void printTag(String JavaDoc name, HashMap JavaDoc parameters, boolean tab, boolean newLine, boolean end) {
54     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55     sb.append("<"); //$NON-NLS-1$
56
sb.append(name);
57     if (parameters != null)
58         for (Enumeration JavaDoc e = Collections.enumeration(parameters.keySet()); e.hasMoreElements();) {
59             sb.append(" "); //$NON-NLS-1$
60
String JavaDoc key = (String JavaDoc) e.nextElement();
61             sb.append(key);
62             sb.append("=\""); //$NON-NLS-1$
63
sb.append(getEscaped(String.valueOf(parameters.get(key))));
64             sb.append("\""); //$NON-NLS-1$
65
}
66     if (end)
67         sb.append('/');
68     sb.append(">"); //$NON-NLS-1$
69
if (tab)
70         printTabulation();
71     if (newLine)
72         println(sb.toString());
73     else
74         print(sb.toString());
75 }
76 public void startTag(String JavaDoc name, HashMap JavaDoc parameters) {
77     startTag(name, parameters, true);
78 }
79 public void startTag(String JavaDoc name, HashMap JavaDoc parameters, boolean newLine) {
80     printTag(name, parameters, true, newLine);
81     tab++;
82 }
83 public void startAndEndTag(String JavaDoc name, HashMap JavaDoc parameters, boolean newLine) {
84     printTag(name, parameters, true, true, true);
85 }
86 private static void appendEscapedChar(StringBuffer JavaDoc buffer, char c) {
87     String JavaDoc replacement = getReplacement(c);
88     if (replacement != null) {
89         buffer.append('&');
90         buffer.append(replacement);
91         buffer.append(';');
92     } else {
93         buffer.append(c);
94     }
95 }
96 public static String JavaDoc getEscaped(String JavaDoc s) {
97     StringBuffer JavaDoc result = new StringBuffer JavaDoc(s.length() + 10);
98     for (int i = 0; i < s.length(); ++i)
99         appendEscapedChar(result, s.charAt(i));
100     return result.toString();
101 }
102 private static String JavaDoc getReplacement(char c) {
103     // Encode special XML characters into the equivalent character references.
104
// These five are defined by default for all XML documents.
105
switch (c) {
106         case '<' :
107             return "lt"; //$NON-NLS-1$
108
case '>' :
109             return "gt"; //$NON-NLS-1$
110
case '"' :
111             return "quot"; //$NON-NLS-1$
112
case '\'' :
113             return "apos"; //$NON-NLS-1$
114
case '&' :
115             return "amp"; //$NON-NLS-1$
116
}
117     return null;
118 }
119 }
120
Popular Tags