KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > util > GenericXMLWriter


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.jdt.internal.compiler.util;
12
13 import java.io.OutputStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.Writer JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 public class GenericXMLWriter extends PrintWriter JavaDoc {
22     /* constants */
23     private static final String JavaDoc XML_VERSION= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
24
private static void appendEscapedChar(StringBuffer JavaDoc buffer, char c) {
25         String JavaDoc replacement= getReplacement(c);
26         if (replacement != null) {
27             buffer.append('&');
28             buffer.append(replacement);
29             buffer.append(';');
30         } else {
31             buffer.append(c);
32         }
33     }
34     private static String JavaDoc getEscaped(String JavaDoc s) {
35         StringBuffer JavaDoc result= new StringBuffer JavaDoc(s.length() + 10);
36         for (int i= 0; i < s.length(); ++i)
37             appendEscapedChar(result, s.charAt(i));
38         return result.toString();
39     }
40     private static String JavaDoc getReplacement(char c) {
41         // Encode special XML characters into the equivalent character references.
42
// These five are defined by default for all XML documents.
43
switch (c) {
44             case '<' :
45                 return "lt"; //$NON-NLS-1$
46
case '>' :
47                 return "gt"; //$NON-NLS-1$
48
case '"' :
49                 return "quot"; //$NON-NLS-1$
50
case '\'' :
51                 return "apos"; //$NON-NLS-1$
52
case '&' :
53                 return "amp"; //$NON-NLS-1$
54
}
55         return null;
56     }
57     private String JavaDoc lineSeparator;
58     private int tab;
59     public GenericXMLWriter(OutputStream JavaDoc stream, String JavaDoc lineSeparator, boolean printXmlVersion) {
60         this(new PrintWriter JavaDoc(stream), lineSeparator, printXmlVersion);
61     }
62     public GenericXMLWriter(Writer JavaDoc writer, String JavaDoc lineSeparator, boolean printXmlVersion) {
63         super(writer);
64         this.tab= 0;
65         this.lineSeparator = lineSeparator;
66         if (printXmlVersion) {
67             print(XML_VERSION);
68             print(this.lineSeparator);
69         }
70     }
71     public void endTag(String JavaDoc name, boolean insertTab, boolean insertNewLine) {
72         this.tab --;
73         printTag('/' + name, null/*no parameters*/, insertTab, insertNewLine, false/*don't close tag*/);
74     }
75     /*
76      * External API
77      */

78     public void printString(String JavaDoc string, boolean insertTab, boolean insertNewLine) {
79         if (insertTab) {
80             printTabulation();
81         }
82         print(string);
83         if (insertNewLine) {
84             print(this.lineSeparator);
85         }
86     }
87     private void printTabulation() {
88         for (int i= 0; i < this.tab; i++) this.print('\t');
89     }
90     public void printTag(String JavaDoc name, HashMap JavaDoc parameters, boolean insertTab, boolean insertNewLine, boolean closeTag) {
91         if (insertTab) {
92             this.printTabulation();
93         }
94         this.print('<');
95         this.print(name);
96         if (parameters != null) {
97             int length = parameters.size();
98             Map.Entry JavaDoc[] entries = new Map.Entry JavaDoc[length];
99             parameters.entrySet().toArray(entries);
100             Arrays.sort(entries, new Comparator JavaDoc() {
101                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
102                     Map.Entry JavaDoc entry1 = (Map.Entry JavaDoc) o1;
103                     Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc) o2;
104                     return ((String JavaDoc) entry1.getKey()).compareTo((String JavaDoc) entry2.getKey());
105                 }
106             });
107             for (int i = 0; i < length; i++) {
108                 this.print(' ');
109                 this.print(entries[i].getKey());
110                 this.print("=\""); //$NON-NLS-1$
111
this.print(getEscaped(String.valueOf(entries[i].getValue())));
112                 this.print('\"');
113             }
114         }
115         if (closeTag) {
116             this.print("/>"); //$NON-NLS-1$
117
} else {
118             this.print(">"); //$NON-NLS-1$
119
}
120         if (insertNewLine) {
121             print(this.lineSeparator);
122         }
123         if (parameters != null && !closeTag)
124             this.tab++;
125
126     }
127     public void startTag(String JavaDoc name, boolean insertTab) {
128         printTag(name, null/*no parameters*/, insertTab, true/*insert new line*/, false/*don't close tag*/);
129         this.tab++;
130     }
131 }
132
Popular Tags