KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > XMLPrintHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.update.internal.configurator;
13
14 import java.io.*;
15
16 import org.w3c.dom.*;
17
18
19 public class XMLPrintHandler {
20     // used to print XML file
21
public static final String JavaDoc XML_COMMENT_END_TAG = "-->"; //$NON-NLS-1$
22
public static final String JavaDoc XML_COMMENT_BEGIN_TAG = "<!--"; //$NON-NLS-1$
23
public static final String JavaDoc XML_HEAD = "<?xml version=\"1.0\" encoding=\""; //$NON-NLS-1$
24
public static final String JavaDoc XML_HEAD_END_TAG = "?>"; //$NON-NLS-1$
25
public static final String JavaDoc XML_DBL_QUOTES = "\""; //$NON-NLS-1$
26
public static final String JavaDoc XML_SPACE = " "; //$NON-NLS-1$
27
public static final String JavaDoc XML_BEGIN_TAG = "<"; //$NON-NLS-1$
28
public static final String JavaDoc XML_END_TAG = ">"; //$NON-NLS-1$
29
public static final String JavaDoc XML_EQUAL = "="; //$NON-NLS-1$
30
public static final String JavaDoc XML_SLASH = "/"; //$NON-NLS-1$
31

32     public static void printBeginElement(Writer xmlWriter, String JavaDoc elementString) throws IOException{
33         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_BEGIN_TAG);
34         temp.append(elementString).append(XML_END_TAG).append("\n"); //$NON-NLS-1$
35
xmlWriter.write(temp.toString());
36
37     }
38
39     public static void printEndElement(Writer xmlWriter, String JavaDoc elementString) throws IOException{
40         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_BEGIN_TAG);
41         temp.append(XML_SLASH).append(elementString).append(XML_END_TAG).append("\n"); //$NON-NLS-1$
42
xmlWriter.write(temp.toString());
43
44     }
45
46     
47     public static void printText(Writer xmlWriter, String JavaDoc text) throws IOException{
48         xmlWriter.write(encode(text).toString());
49     }
50
51     public static void printComment(Writer xmlWriter, String JavaDoc comment)throws IOException {
52         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_COMMENT_BEGIN_TAG);
53         temp.append(encode(comment).toString()).append(XML_COMMENT_END_TAG).append("\n"); //$NON-NLS-1$
54
xmlWriter.write(temp.toString());
55     }
56
57     public static void printHead(Writer xmlWriter, String JavaDoc encoding) throws IOException {
58         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_HEAD);
59         temp.append(encoding).append(XML_DBL_QUOTES).append(XML_HEAD_END_TAG).append("\n"); //$NON-NLS-1$
60
xmlWriter.write(temp.toString());
61     }
62
63     public static String JavaDoc wrapAttributeForPrint(String JavaDoc attribute, String JavaDoc value) throws IOException {
64         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(XML_SPACE);
65         temp.append(attribute).append(XML_EQUAL).append(XML_DBL_QUOTES)
66                 .append(encode(value).toString()).append(XML_DBL_QUOTES);
67         return temp.toString();
68
69     }
70
71     public static void printNode(Writer xmlWriter, Node node,String JavaDoc encoding) throws Exception JavaDoc{
72         if (node == null) {
73             return;
74         }
75
76         switch (node.getNodeType()) {
77         case Node.DOCUMENT_NODE: {
78             printHead(xmlWriter,encoding);
79             printNode(xmlWriter, ((Document) node).getDocumentElement(),encoding);
80             break;
81         }
82         case Node.ELEMENT_NODE: {
83             //get the attribute list for this node.
84
StringBuffer JavaDoc tempElementString = new StringBuffer JavaDoc(node.getNodeName());
85             NamedNodeMap attributeList = node.getAttributes();
86             if ( attributeList != null ) {
87                 for(int i= 0; i <attributeList.getLength();i++){
88                     Node attribute = attributeList.item(i);
89                     tempElementString.append(wrapAttributeForPrint(attribute.getNodeName(),attribute.getNodeValue()));
90                 }
91             }
92             printBeginElement(xmlWriter,tempElementString.toString());
93             
94             // do this recursively for the child nodes.
95
NodeList childNodes = node.getChildNodes();
96             if (childNodes != null) {
97                 int length = childNodes.getLength();
98                 for (int i = 0; i < length; i++) {
99                     printNode(xmlWriter, childNodes.item(i),encoding);
100                 }
101             }
102             
103             printEndElement(xmlWriter,node.getNodeName());
104             break;
105         }
106         
107         case Node.TEXT_NODE: {
108             xmlWriter.write(encode(node.getNodeValue()).toString());
109             break;
110         }
111         default: {
112             throw new UnsupportedOperationException JavaDoc(Messages.XMLPrintHandler_unsupportedNodeType);
113             
114         }
115         }
116
117     }
118
119     public static StringBuffer JavaDoc encode(String JavaDoc value) {
120         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
121         for (int i = 0; i < value.length(); i++) {
122             char c = value.charAt(i);
123             switch (c) {
124                 case '&' :
125                     buf.append("&amp;"); //$NON-NLS-1$
126
break;
127                 case '<' :
128                     buf.append("&lt;"); //$NON-NLS-1$
129
break;
130                 case '>' :
131                     buf.append("&gt;"); //$NON-NLS-1$
132
break;
133                 case '\'' :
134                     buf.append("&apos;"); //$NON-NLS-1$
135
break;
136                 case '\"' :
137                     buf.append("&quot;"); //$NON-NLS-1$
138
break;
139                 default :
140                     buf.append(c);
141                     break;
142             }
143         }
144         return buf;
145     }
146 }
147
Popular Tags