KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > app > bugdb > HtmlWriter


1 package com.quadcap.app.bugdb;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43
44 /**
45  * Perform HTML encoding, replacing HTML markup symbols with their entity
46  * representation.
47  *
48  * @author Stan Bailes
49  */

50 public class HtmlWriter extends Writer JavaDoc {
51     Writer JavaDoc os;
52     static final char[] ampLT = {
53     '&', 'l', 't', ';'
54     };
55     static final char[] ampGT = {
56     '&', 'g', 't', ';'
57     };
58     static final char[] ampAMP = {
59     '&', 'a', 'm', 'p', ';'
60     };
61
62     /**
63      * Construct a new HtmlWriter, as a filter on the specified writer
64      *
65      * @param os the underlying writer
66      */

67     public HtmlWriter(Writer JavaDoc os) {
68     this.os = os;
69     }
70     
71     /**
72      * Write a single character.
73      *
74      * @param c the character
75      * @exception IOException may be thrown by the underlying writer
76      */

77     public void write(int c) throws IOException JavaDoc {
78     switch (c) {
79     case '<':
80         os.write(ampLT);
81         break;
82     case '>':
83         os.write(ampGT);
84         break;
85     case '&':
86         os.write(ampAMP);
87         break;
88     default:
89         os.write(c);
90     }
91     }
92
93     /**
94      * Write a portion of a character array.
95      *
96      * @param cbuf the character array
97      * @param offset the first position to write
98      * @param len the number of characters to write
99      *
100      * @exception IOException may be thrown by the underlying writer
101      */

102     public void write(char cbuf[], int offset, int len) throws IOException JavaDoc {
103     int lim = offset + len;
104     while (offset < lim) write(cbuf[offset++]);
105     }
106
107     /**
108      * Flush the underlying writer.
109      *
110      * @exception IOException may be thrown by the underlying writer
111      */

112     public void flush() throws IOException JavaDoc {
113     os.flush();
114     }
115
116     /**
117      * Close the underlying writer.
118      *
119      * @exception IOException may be thrown by the underlying writer
120      */

121     public void close() throws IOException JavaDoc {
122     os.close();
123     }
124 }
125
Popular Tags