001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.OutputStream;
016: import java.io.OutputStreamWriter;
017: import java.io.Writer;
018:
019: import org.w3c.dom.Document;
020: import org.w3c.dom.NamedNodeMap;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.NodeList;
023:
024: public class XMLPrintHandler {
025: // used to print XML file
026: public static final String XML_COMMENT_END_TAG = "-->"; //$NON-NLS-1$
027: public static final String XML_COMMENT_BEGIN_TAG = "<!--"; //$NON-NLS-1$
028: public static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\""; //$NON-NLS-1$
029: public static final String XML_HEAD_END_TAG = "?>"; //$NON-NLS-1$
030: public static final String XML_DBL_QUOTES = "\""; //$NON-NLS-1$
031: public static final String XML_SPACE = " "; //$NON-NLS-1$
032: public static final String XML_BEGIN_TAG = "<"; //$NON-NLS-1$
033: public static final String XML_END_TAG = ">"; //$NON-NLS-1$
034: public static final String XML_EQUAL = "="; //$NON-NLS-1$
035: public static final String XML_SLASH = "/"; //$NON-NLS-1$
036: public static final String XML_INDENT = " "; //$NON-NLS-1$
037:
038: /**
039: * @param level
040: * @return
041: */
042: public static String generateIndent(int level) {
043: StringBuffer buffer = new StringBuffer();
044: for (int i = 0; i < level; i++) {
045: buffer.append(XML_INDENT);
046: }
047: return buffer.toString();
048: }
049:
050: public static void printBeginElement(Writer xmlWriter,
051: String elementString, String indent, boolean terminate)
052: throws IOException {
053: StringBuffer temp = new StringBuffer(indent);
054: temp.append(XML_BEGIN_TAG);
055: temp.append(elementString);
056: if (terminate)
057: temp.append(XML_SLASH);
058: temp.append(XML_END_TAG);
059: temp.append("\n"); //$NON-NLS-1$
060: xmlWriter.write(temp.toString());
061:
062: }
063:
064: public static void printEndElement(Writer xmlWriter,
065: String elementString, String indent) throws IOException {
066: StringBuffer temp = new StringBuffer(indent);
067: temp.append(XML_BEGIN_TAG);
068: temp.append(XML_SLASH).append(elementString)
069: .append(XML_END_TAG).append("\n"); //$NON-NLS-1$
070: xmlWriter.write(temp.toString());
071:
072: }
073:
074: public static void printText(Writer xmlWriter, String text,
075: String indent) throws IOException {
076: StringBuffer temp = new StringBuffer(indent);
077: temp.append(encode(text).toString());
078: temp.append("\n"); //$NON-NLS-1$
079: xmlWriter.write(temp.toString());
080: }
081:
082: public static void printComment(Writer xmlWriter, String comment,
083: String indent) throws IOException {
084: StringBuffer temp = new StringBuffer("\n"); //$NON-NLS-1$
085: temp.append(indent);
086: temp.append(XML_COMMENT_BEGIN_TAG);
087: temp.append(encode(comment).toString()).append(
088: XML_COMMENT_END_TAG).append("\n\n"); //$NON-NLS-1$
089: xmlWriter.write(temp.toString());
090: }
091:
092: public static void printHead(Writer xmlWriter, String encoding)
093: throws IOException {
094: StringBuffer temp = new StringBuffer(XML_HEAD);
095: temp.append(encoding).append(XML_DBL_QUOTES).append(
096: XML_HEAD_END_TAG).append("\n"); //$NON-NLS-1$
097: xmlWriter.write(temp.toString());
098: }
099:
100: public static String wrapAttributeForPrint(String attribute,
101: String value) throws IOException {
102: StringBuffer temp = new StringBuffer(XML_SPACE);
103: temp.append(attribute).append(XML_EQUAL).append(XML_DBL_QUOTES)
104: .append(encode(value).toString())
105: .append(XML_DBL_QUOTES);
106: return temp.toString();
107: }
108:
109: public static String wrapAttribute(String attribute, String value) {
110: StringBuffer buffer = new StringBuffer(XML_SPACE);
111: buffer.append(attribute);
112: buffer.append(XML_EQUAL);
113: buffer.append(XML_DBL_QUOTES);
114: buffer.append(value);
115: buffer.append(XML_DBL_QUOTES);
116: return buffer.toString();
117: }
118:
119: public static void printNode(Writer xmlWriter, Node node,
120: String encoding, String indent) throws IOException {
121: if (node == null) {
122: return;
123: }
124:
125: switch (node.getNodeType()) {
126: case Node.DOCUMENT_NODE: {
127: printHead(xmlWriter, encoding);
128: printNode(xmlWriter,
129: ((Document) node).getDocumentElement(), encoding,
130: indent);
131: break;
132: }
133: case Node.ELEMENT_NODE: {
134: //get the attribute list for this node.
135: StringBuffer tempElementString = new StringBuffer(node
136: .getNodeName());
137: NamedNodeMap attributeList = node.getAttributes();
138: if (attributeList != null) {
139: for (int i = 0; i < attributeList.getLength(); i++) {
140: Node attribute = attributeList.item(i);
141: tempElementString.append(wrapAttributeForPrint(
142: attribute.getNodeName(), attribute
143: .getNodeValue()));
144: }
145: }
146:
147: // do this recursively for the child nodes.
148: NodeList childNodes = node.getChildNodes();
149: int length = childNodes.getLength();
150: printBeginElement(xmlWriter, tempElementString.toString(),
151: indent, length == 0);
152:
153: for (int i = 0; i < length; i++)
154: printNode(xmlWriter, childNodes.item(i), encoding,
155: indent + "\t"); //$NON-NLS-1$
156:
157: if (length > 0)
158: printEndElement(xmlWriter, node.getNodeName(), indent);
159: break;
160: }
161:
162: case Node.TEXT_NODE: {
163: xmlWriter.write(encode(node.getNodeValue()).toString());
164: break;
165: }
166: default: {
167: throw new UnsupportedOperationException(
168: "Unsupported XML Node Type."); //$NON-NLS-1$
169: }
170: }
171:
172: }
173:
174: public static StringBuffer encode(String value) {
175: StringBuffer buf = new StringBuffer();
176: for (int i = 0; i < value.length(); i++) {
177: char c = value.charAt(i);
178: switch (c) {
179: case '&':
180: buf.append("&"); //$NON-NLS-1$
181: break;
182: case '<':
183: buf.append("<"); //$NON-NLS-1$
184: break;
185: case '>':
186: buf.append(">"); //$NON-NLS-1$
187: break;
188: case '\'':
189: buf.append("'"); //$NON-NLS-1$
190: break;
191: case '\"':
192: buf.append("""); //$NON-NLS-1$
193: break;
194: default:
195: buf.append(c);
196: break;
197: }
198: }
199: return buf;
200: }
201:
202: public static void writeFile(Document doc, File file)
203: throws IOException {
204: Writer writer = null;
205: OutputStream out = null;
206: try {
207: out = new FileOutputStream(file);
208: writer = new OutputStreamWriter(out, "UTF-8"); //$NON-NLS-1$
209: XMLPrintHandler.printNode(writer, doc, "UTF-8", ""); //$NON-NLS-1$ //$NON-NLS-2$
210: } finally {
211: try {
212: if (writer != null)
213: writer.close();
214: } catch (IOException e1) {
215: }
216: try {
217: if (out != null)
218: out.close();
219: } catch (IOException e1) {
220: }
221: }
222: }
223:
224: }
|