KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > XMLPrintHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.pde.internal.core;
13
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.OutputStreamWriter JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25
26
27 public class XMLPrintHandler {
28     // used to print XML file
29
public static final String JavaDoc XML_COMMENT_END_TAG = "-->"; //$NON-NLS-1$
30
public static final String JavaDoc XML_COMMENT_BEGIN_TAG = "<!--"; //$NON-NLS-1$
31
public static final String JavaDoc XML_HEAD = "<?xml version=\"1.0\" encoding=\""; //$NON-NLS-1$
32
public static final String JavaDoc XML_HEAD_END_TAG = "?>"; //$NON-NLS-1$
33
public static final String JavaDoc XML_DBL_QUOTES = "\""; //$NON-NLS-1$
34
public static final String JavaDoc XML_SPACE = " "; //$NON-NLS-1$
35
public static final String JavaDoc XML_BEGIN_TAG = "<"; //$NON-NLS-1$
36
public static final String JavaDoc XML_END_TAG = ">"; //$NON-NLS-1$
37
public static final String JavaDoc XML_EQUAL = "="; //$NON-NLS-1$
38
public static final String JavaDoc XML_SLASH = "/"; //$NON-NLS-1$
39
public static final String JavaDoc XML_INDENT = " "; //$NON-NLS-1$
40

41     
42     /**
43      * @param level
44      * @return
45      */

46     public static String JavaDoc generateIndent(int level) {
47         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
48         for (int i = 0; i < level; i++) {
49             buffer.append(XML_INDENT);
50         }
51         return buffer.toString();
52     }
53     
54     public static void printBeginElement(Writer JavaDoc xmlWriter, String JavaDoc elementString, String JavaDoc indent, boolean terminate) throws IOException JavaDoc{
55         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(indent);
56         temp.append(XML_BEGIN_TAG);
57         temp.append(elementString);
58         if (terminate)
59             temp.append(XML_SLASH);
60         temp.append(XML_END_TAG);
61         temp.append("\n"); //$NON-NLS-1$
62
xmlWriter.write(temp.toString());
63
64     }
65
66     public static void printEndElement(Writer JavaDoc xmlWriter, String JavaDoc elementString, String JavaDoc indent) throws IOException JavaDoc{
67         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(indent);
68         temp.append(XML_BEGIN_TAG);
69         temp.append(XML_SLASH).append(elementString).append(XML_END_TAG).append("\n"); //$NON-NLS-1$
70
xmlWriter.write(temp.toString());
71
72     }
73
74     public static void printText(Writer JavaDoc xmlWriter, String JavaDoc text, String JavaDoc indent) throws IOException JavaDoc{
75         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(indent);
76         temp.append(encode(text).toString());
77         temp.append("\n"); //$NON-NLS-1$
78
xmlWriter.write(temp.toString());
79     }
80
81     public static void printComment(Writer JavaDoc xmlWriter, String JavaDoc comment, String JavaDoc indent)throws IOException JavaDoc {
82         StringBuffer JavaDoc temp = new StringBuffer JavaDoc("\n"); //$NON-NLS-1$
83
temp.append(indent);
84         temp.append(XML_COMMENT_BEGIN_TAG);
85         temp.append(encode(comment).toString()).append(XML_COMMENT_END_TAG).append("\n\n"); //$NON-NLS-1$
86
xmlWriter.write(temp.toString());
87     }
88
89     public static void printHead(Writer JavaDoc xmlWriter, String JavaDoc encoding) throws IOException JavaDoc {
90         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_HEAD);
91         temp.append(encoding).append(XML_DBL_QUOTES).append(XML_HEAD_END_TAG).append("\n"); //$NON-NLS-1$
92
xmlWriter.write(temp.toString());
93     }
94
95     public static String JavaDoc wrapAttributeForPrint(String JavaDoc attribute, String JavaDoc value) throws IOException JavaDoc {
96         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_SPACE);
97         temp.append(attribute).append(XML_EQUAL).append(XML_DBL_QUOTES)
98                 .append(encode(value).toString()).append(XML_DBL_QUOTES);
99         return temp.toString();
100     }
101
102     public static String JavaDoc wrapAttribute(String JavaDoc attribute, String JavaDoc value) {
103         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(XML_SPACE);
104         buffer.append(attribute);
105         buffer.append(XML_EQUAL);
106         buffer.append(XML_DBL_QUOTES);
107         buffer.append(value);
108         buffer.append(XML_DBL_QUOTES);
109         return buffer.toString();
110     }
111     
112     public static void printNode(Writer JavaDoc xmlWriter, Node JavaDoc node,String JavaDoc encoding, String JavaDoc indent) throws IOException JavaDoc {
113         if (node == null) {
114             return;
115         }
116
117         switch (node.getNodeType()) {
118         case Node.DOCUMENT_NODE: {
119             printHead(xmlWriter,encoding);
120             printNode(xmlWriter, ((Document JavaDoc) node).getDocumentElement(),encoding, indent);
121             break;
122         }
123         case Node.ELEMENT_NODE: {
124             //get the attribute list for this node.
125
StringBuffer JavaDoc tempElementString = new StringBuffer JavaDoc(node.getNodeName());
126             NamedNodeMap JavaDoc attributeList = node.getAttributes();
127             if ( attributeList != null ) {
128                 for(int i= 0; i <attributeList.getLength();i++){
129                     Node JavaDoc attribute = attributeList.item(i);
130                     tempElementString.append(wrapAttributeForPrint(attribute.getNodeName(),attribute.getNodeValue()));
131                 }
132             }
133
134             // do this recursively for the child nodes.
135
NodeList JavaDoc childNodes = node.getChildNodes();
136             int length = childNodes.getLength();
137             printBeginElement(xmlWriter,tempElementString.toString(), indent, length == 0);
138
139             for (int i = 0; i < length; i++)
140                 printNode(xmlWriter, childNodes.item(i),encoding, indent + "\t"); //$NON-NLS-1$
141

142             if (length > 0)
143                 printEndElement(xmlWriter,node.getNodeName(), indent);
144             break;
145         }
146         
147         case Node.TEXT_NODE: {
148             xmlWriter.write(encode(node.getNodeValue()).toString());
149             break;
150         }
151         default: {
152             throw new UnsupportedOperationException JavaDoc("Unsupported XML Node Type."); //$NON-NLS-1$
153
}
154     }
155
156     }
157
158     public static StringBuffer JavaDoc encode(String JavaDoc value) {
159         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
160         for (int i = 0; i < value.length(); i++) {
161             char c = value.charAt(i);
162             switch (c) {
163                 case '&' :
164                     buf.append("&amp;"); //$NON-NLS-1$
165
break;
166                 case '<' :
167                     buf.append("&lt;"); //$NON-NLS-1$
168
break;
169                 case '>' :
170                     buf.append("&gt;"); //$NON-NLS-1$
171
break;
172                 case '\'' :
173                     buf.append("&apos;"); //$NON-NLS-1$
174
break;
175                 case '\"' :
176                     buf.append("&quot;"); //$NON-NLS-1$
177
break;
178                 default :
179                     buf.append(c);
180                     break;
181             }
182         }
183         return buf;
184     }
185     
186     public static void writeFile(Document JavaDoc doc, File JavaDoc file) throws IOException JavaDoc {
187         Writer JavaDoc writer = null;
188         OutputStream JavaDoc out = null;
189         try {
190             out = new FileOutputStream JavaDoc(file);
191             writer = new OutputStreamWriter JavaDoc(out, "UTF-8"); //$NON-NLS-1$
192
XMLPrintHandler.printNode(writer, doc, "UTF-8", ""); //$NON-NLS-1$ //$NON-NLS-2$
193
} finally {
194             try {
195                 if (writer != null)
196                     writer.close();
197             } catch (IOException JavaDoc e1) {
198             }
199             try {
200                 if (out != null)
201                     out.close();
202             } catch (IOException JavaDoc e1) {
203             }
204         }
205     }
206
207 }
208
Popular Tags