KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > XMLWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build;
12
13 import java.io.*;
14 import java.util.*;
15
16 /**
17  * A simple XML writer.
18  */

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

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