KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > XMLGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.help.internal.webapp.servlet;
12
13 import java.io.*;
14
15 import org.eclipse.help.internal.base.util.*;
16 import org.eclipse.help.internal.webapp.*;
17
18 /**
19  * Helper class to generate xml files.
20  */

21 public class XMLGenerator {
22     private File outFile = null;
23
24     private PrintWriter out = null;
25
26     public int pad = 0;
27
28     // XML escaped characters mapping
29
private static final String JavaDoc invalidXML[] = { "&", ">", "<", "\"", "\'" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
30

31     private static final String JavaDoc escapedXML[] = {
32             "&amp;", "&gt;", "&lt;", "&quot;", "&apos;" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
33

34     /**
35      * Constructor.
36      */

37     public XMLGenerator(Writer writer) {
38         if (writer instanceof PrintWriter)
39             this.out = (PrintWriter) writer;
40         else
41             this.out = new PrintWriter(writer);
42     }
43
44     /**
45      * Constructor.
46      */

47     public XMLGenerator(File outFile) {
48         super();
49         this.outFile = outFile;
50         try {
51             out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
52                     new FileOutputStream(outFile), "UTF8")), //$NON-NLS-1$
53
false /* no aotoFlush */
54             );
55             println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
56
} catch (IOException ioe) {
57             HelpWebappPlugin.logError("Error accessing file: " //$NON-NLS-1$
58
+ outFile.getAbsolutePath() + "", ioe); //$NON-NLS-1$
59
}
60     }
61
62     // returns a String that is a valid XML string
63
// by XML escaping special characters
64
public static String JavaDoc xmlEscape(String JavaDoc cdata) {
65         for (int i = 0; i < invalidXML.length; i++)
66             cdata = TString.change(cdata, invalidXML[i], escapedXML[i]);
67         return cdata;
68     }
69
70     public void close() {
71         out.flush();
72         out.close();
73         if (out.checkError())
74             if (outFile != null)
75                 HelpWebappPlugin.logError("Errors occurred generating file: " //$NON-NLS-1$
76
+ outFile.getAbsolutePath() + "", null); //$NON-NLS-1$
77
out = null;
78     }
79
80     public void print(Object JavaDoc o) {
81         if (out != null)
82             out.print(o);
83     }
84
85     public void println(Object JavaDoc o) {
86         print(o);
87         print("\n"); //$NON-NLS-1$
88
}
89
90     public void printPad() {
91         for (int i = 0; i < pad; i++)
92             print(" "); //$NON-NLS-1$
93
}
94 }
95
Popular Tags