1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / XMLWriter.java
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 Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.io.PrintWriter;
14 import java.io.Writer;
15 import java.util.Collections;
16 import java.util.Enumeration;
17 import java.util.HashMap;
18
19 /**
20  * @since 3.0
21  */
22 class XMLWriter extends PrintWriter {
23         /* constants */
24         private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
25
26         private static void appendEscapedChar(StringBuffer buffer, char c) {
27                 String replacement = getReplacement(c);
28                 if (replacement != null) {
29                         buffer.append('&');
30                         buffer.append(replacement);
31                         buffer.append(';');
32                 } else {
33                         buffer.append(c);
34                 }
35         }
36
37         private static String getEscaped(String s) {
38                 StringBuffer result = new StringBuffer(s.length() + 10);
39                 for (int i = 0; i < s.length(); ++i)
40                         appendEscapedChar(result, s.charAt(i));
41                 return result.toString();
42         }
43
44         private static String getReplacement(char c) {
45                 // Encode special XML characters into the equivalent character
46                 // references.
47                 // These five are defined by default for all XML documents.
48                 switch (c) {
49                 case '<':
50                         return "lt"; //$NON-NLS-1$
51                 case '>':
52                         return "gt"; //$NON-NLS-1$
53                 case '"':
54                         return "quot"; //$NON-NLS-1$
55                 case '\'':
56                         return "apos"; //$NON-NLS-1$
57                 case '&':
58                         return "amp"; //$NON-NLS-1$
59                 }
60                 return null;
61         }
62
63         private int tab;
64
65         public XMLWriter(Writer writer) {
66                 super(writer);
67                 tab = 0;
68                 println(XML_VERSION);
69         }
70
71         public void endTag(String name, boolean insertTab) {
72                 tab--;
73                 printTag('/' + name, null, insertTab, true, false);
74         }
75
76         private void printTabulation() {
77                 for (int i = 0; i < tab; i++)
78                         super.print('\t');
79         }
80
81         public void printTag(String name, HashMap parameters, boolean insertTab,
82                         boolean insertNewLine, boolean closeTag) {
83                 StringBuffer sb = new StringBuffer();
84                 sb.append("<"); //$NON-NLS-1$
85                 sb.append(name);
86                 if (parameters != null) {
87                         for (Enumeration en = Collections.enumeration(parameters.keySet()); en
88                                         .hasMoreElements();) {
89                                 sb.append(" "); //$NON-NLS-1$
90                                 String key = (String) en.nextElement();
91                                 sb.append(key);
92                                 sb.append("=\""); //$NON-NLS-1$
93                                 sb.append(getEscaped(String.valueOf(parameters.get(key))));
94                                 sb.append("\""); //$NON-NLS-1$
95                         }
96                 }
97                 if (closeTag) {
98                         sb.append("/>"); //$NON-NLS-1$
99                 } else {
100                         sb.append(">"); //$NON-NLS-1$
101                 }
102                 if (insertTab) {
103                         printTabulation();
104                 }
105                 if (insertNewLine) {
106                         println(sb.toString());
107                 } else {
108                         print(sb.toString());
109                 }
110         }
111
112         public void startTag(String name, boolean insertTab) {
113                 printTag(name, null, insertTab, true, false);
114                 tab++;
115         }
116 }