KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.help.internal.webapp.servlet;
13
14 import java.io.*;
15
16 /**
17  * Filters output stream and inserts specified bytes before the end of HEAD
18  * element (before </html> tag) of HTML in the stream.
19  */

20 public class FilterHTMLHeadOutputStream extends FilterOutputStream {
21     private static final int STATE_START = 0;
22
23     private static final int STATE_LT = 1;
24
25     private static final int STATE_LT_SLASH = 2;
26
27     private static final int STATE_LT_SLASH_H = 3;
28
29     private static final int STATE_LT_SLASH_HE = 4;
30
31     private static final int STATE_LT_SLASH_HEA = 5;
32
33     private static final int STATE_LT_SLASH_HEAD = 6;
34
35     private static final int STATE_DONE = 7;
36
37     private int state = STATE_START;
38
39     private byte[] toInsert;
40
41     ByteArrayOutputStream buffer = new ByteArrayOutputStream(7);
42
43     /**
44      * Constructor.
45      *
46      * @param out
47      * sink output stream
48      * @param bytesToInsert
49      * bytes to insert in head of HTML
50      */

51     public FilterHTMLHeadOutputStream(OutputStream out, byte[] bytesToInsert) {
52         super(out);
53         toInsert = bytesToInsert;
54     }
55
56     /**
57      * Writes the specified <code>byte</code> to this output stream.
58      * <p>
59      * The underlying stream might have a more bytes written to it, following
60      * the &lt;head&gt; HTML element.
61      * <p>
62      * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
63      *
64      * @param b
65      * the <code>byte</code>.
66      * @exception IOException
67      * if an I/O error occurs.
68      */

69     public final void write(int b) throws IOException {
70         switch (state) {
71         case STATE_START:
72             if (b == '<') {
73                 buffer.write(b);
74                 state = STATE_LT;
75             } else {
76                 out.write(b);
77             }
78             break;
79         case STATE_LT:
80             buffer.write(b);
81             if (b == '/') {
82                 state = STATE_LT_SLASH;
83             } else {
84                 reset();
85             }
86             break;
87         case STATE_LT_SLASH:
88             buffer.write(b);
89             if (b == 'h' || b == 'H') {
90                 state = STATE_LT_SLASH_H;
91             } else {
92                 reset();
93             }
94             break;
95         case STATE_LT_SLASH_H:
96             buffer.write(b);
97             if (b == 'e' || b == 'E') {
98                 state = STATE_LT_SLASH_HE;
99             } else {
100                 reset();
101             }
102             break;
103         case STATE_LT_SLASH_HE:
104             buffer.write(b);
105             if (b == 'a' || b == 'A') {
106                 state = STATE_LT_SLASH_HEA;
107             } else {
108                 reset();
109             }
110             break;
111         case STATE_LT_SLASH_HEA:
112             buffer.write(b);
113             if (b == 'd' || b == 'D') {
114                 state = STATE_LT_SLASH_HEAD;
115             } else {
116                 reset();
117             }
118             break;
119         case STATE_LT_SLASH_HEAD:
120             buffer.write(b);
121             if (b == '>') {
122                 out.write(toInsert);
123                 out.write('\n');
124                 reset();
125                 state = STATE_DONE;
126             } else {
127                 reset();
128             }
129             break;
130         default: // (case STATE_DONE):
131
out.write(b);
132             break;
133         }
134     }
135
136     private void reset() throws IOException {
137         out.write(buffer.toByteArray());
138         buffer.reset();
139         state = STATE_START;
140     }
141
142     public void write(byte b[], int off, int len) throws IOException {
143         if (state == STATE_DONE) {
144             out.write(b, off, len);
145         } else {
146             for (int i = 0; i < len; i++) {
147                 write(b[off + i]);
148             }
149         }
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see java.io.FilterOutputStream#close()
156      */

157     public void close() throws IOException {
158         reset();
159         super.close();
160     }
161 }
162
Popular Tags