001: /*******************************************************************************
002: * Copyright (c) 2000, 2004 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 - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build;
011:
012: import java.io.*;
013: import java.util.*;
014:
015: /**
016: * A simple XML writer.
017: */
018: public class XMLWriter extends PrintWriter {
019: protected int tab;
020:
021: /* constants */
022: protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
023:
024: public XMLWriter(OutputStream output)
025: throws UnsupportedEncodingException {
026: super (new OutputStreamWriter(output, "UTF8")); //$NON-NLS-1$
027: tab = 0;
028: println(XML_VERSION);
029: }
030:
031: public void endTag(String name) {
032: tab--;
033: printTag('/' + name, null);
034: }
035:
036: public void printSimpleTag(String name, Object value) {
037: if (value == null)
038: return;
039: printTag(name, null, true, false, false);
040: print(getEscaped(String.valueOf(value)));
041: printTag('/' + name, null, false, true, false);
042: }
043:
044: public void printTabulation() {
045: for (int i = 0; i < tab; i++)
046: super .print('\t');
047: }
048:
049: public void printTag(String name, Map parameters) {
050: printTag(name, parameters, true, true, false);
051: }
052:
053: public void printTag(String name, Map parameters,
054: boolean shouldTab, boolean newLine, boolean close) {
055: StringBuffer sb = new StringBuffer();
056: sb.append("<"); //$NON-NLS-1$
057: sb.append(name);
058: if (parameters != null)
059: for (Enumeration enumeration = Collections
060: .enumeration(parameters.keySet()); enumeration
061: .hasMoreElements();) {
062: sb.append(" "); //$NON-NLS-1$
063: String key = (String) enumeration.nextElement();
064: if (parameters.get(key) != null) {
065: sb.append(key);
066: sb.append("=\""); //$NON-NLS-1$
067: sb.append(getEscaped(String.valueOf(parameters
068: .get(key))));
069: sb.append("\""); //$NON-NLS-1$
070: }
071: }
072: if (close)
073: sb.append("/>"); //$NON-NLS-1$
074: else
075: sb.append(">"); //$NON-NLS-1$
076: if (shouldTab)
077: printTabulation();
078: if (newLine)
079: println(sb.toString());
080: else
081: print(sb.toString());
082: }
083:
084: public void startTag(String name, Map parameters) {
085: startTag(name, parameters, true);
086: }
087:
088: public void startTag(String name, Map parameters, boolean newLine) {
089: printTag(name, parameters, true, newLine, false);
090: tab++;
091: }
092:
093: private static void appendEscapedChar(StringBuffer buffer, char c) {
094: buffer.append(getReplacement(c));
095: }
096:
097: public static String getEscaped(String s) {
098: StringBuffer result = new StringBuffer(s.length() + 10);
099: for (int i = 0; i < s.length(); ++i)
100: appendEscapedChar(result, s.charAt(i));
101: return result.toString();
102: }
103:
104: private static String getReplacement(char c) {
105: // Encode special XML characters into the equivalent character references.
106: // These five are defined by default for all XML documents.
107: switch (c) {
108: case '<':
109: return "<"; //$NON-NLS-1$
110: case '>':
111: return ">"; //$NON-NLS-1$
112: case '"':
113: return """; //$NON-NLS-1$
114: case '\'':
115: return "'"; //$NON-NLS-1$
116: case '&':
117: return "&"; //$NON-NLS-1$
118: default:
119: return String.valueOf(c);
120: }
121: }
122:
123: public void printlnEscaped(String s) {
124: if (s != null)
125: println(getEscaped(s));
126: else
127: println();
128: }
129: }
|