001: package com.quadcap.app.bugdb;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.Writer;
043:
044: /**
045: * Perform HTML encoding, replacing HTML markup symbols with their entity
046: * representation.
047: *
048: * @author Stan Bailes
049: */
050: public class HtmlWriter extends Writer {
051: Writer os;
052: static final char[] ampLT = { '&', 'l', 't', ';' };
053: static final char[] ampGT = { '&', 'g', 't', ';' };
054: static final char[] ampAMP = { '&', 'a', 'm', 'p', ';' };
055:
056: /**
057: * Construct a new HtmlWriter, as a filter on the specified writer
058: *
059: * @param os the underlying writer
060: */
061: public HtmlWriter(Writer os) {
062: this .os = os;
063: }
064:
065: /**
066: * Write a single character.
067: *
068: * @param c the character
069: * @exception IOException may be thrown by the underlying writer
070: */
071: public void write(int c) throws IOException {
072: switch (c) {
073: case '<':
074: os.write(ampLT);
075: break;
076: case '>':
077: os.write(ampGT);
078: break;
079: case '&':
080: os.write(ampAMP);
081: break;
082: default:
083: os.write(c);
084: }
085: }
086:
087: /**
088: * Write a portion of a character array.
089: *
090: * @param cbuf the character array
091: * @param offset the first position to write
092: * @param len the number of characters to write
093: *
094: * @exception IOException may be thrown by the underlying writer
095: */
096: public void write(char cbuf[], int offset, int len)
097: throws IOException {
098: int lim = offset + len;
099: while (offset < lim)
100: write(cbuf[offset++]);
101: }
102:
103: /**
104: * Flush the underlying writer.
105: *
106: * @exception IOException may be thrown by the underlying writer
107: */
108: public void flush() throws IOException {
109: os.flush();
110: }
111:
112: /**
113: * Close the underlying writer.
114: *
115: * @exception IOException may be thrown by the underlying writer
116: */
117: public void close() throws IOException {
118: os.close();
119: }
120: }
|