KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > XMLWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core.internal.resources;
12
13 import java.io.*;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * A simple XML writer.
19  */

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

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