001: package vicazh.hyperpool.stream.net.http.html;
002:
003: import java.io.*;
004: import javax.swing.text.html.*;
005: import javax.swing.text.*;
006: import vicazh.hyperpool.stream.*;
007: import vicazh.hyperpool.stream.net.http.*;
008: import java.util.*;
009: import vicazh.hyperpool.stream.net.http.Session;
010:
011: /**
012: * This class implements an http server stream
013: *
014: * @author Victor Zhigunov
015: * @version 0.4.0
016: */
017: public class Stream extends ServerStream implements Runnable {
018: public Stream() {
019: }
020:
021: private OutputStream save;
022:
023: /**
024: * @param session
025: * parent session
026: * @param outputstream
027: * linked output stream
028: */
029: public Stream(Session session, OutputStream outputstream) {
030: super (session, new NullStream(outputstream));
031: save = outputstream;
032: }
033:
034: private boolean b;
035:
036: public void field(String s1, String s2) throws IOException {
037: super .field(s1, s2);
038: if (s1.equalsIgnoreCase("Content-Type")
039: && s2.startsWith("text/html"))
040: go();
041: }
042:
043: protected void go() {
044: b = true;
045: new Thread(this ).start();
046: }
047:
048: protected void line(String s) throws IOException {
049: super .line(s);
050: save.write((s + "\r\n").getBytes());
051: }
052:
053: static private final String ST = "<";
054:
055: static private final String ET = ">";
056:
057: private void put(HTML.Tag tag, MutableAttributeSet attributes)
058: throws IOException {
059: String s = ST + tag;
060: Enumeration<?> e = attributes.getAttributeNames();
061: while (e.hasMoreElements()) {
062: Object name = e.nextElement();
063: s += (" " + name + "=\"" + attributes.getAttribute(name) + "\"");
064: }
065: s += ET;
066: save.write(s.getBytes());
067: }
068:
069: public void start(HTML.Tag tag, MutableAttributeSet attributes,
070: int position) throws IOException {
071: put(tag, attributes);
072: }
073:
074: public void end(HTML.Tag tag, int position) throws IOException {
075: save.write(new String(ST + "/" + tag + ET).getBytes());
076: }
077:
078: public void text(char[] text, int position) throws IOException {
079: // save.write(new String(text).replace(ST, "<").replace(ET,
080: // ">").getBytes());
081: save.write(new String(text).getBytes());
082: }
083:
084: public void simple(HTML.Tag tag, MutableAttributeSet attributes,
085: int position) throws IOException {
086: put(tag, attributes);
087: }
088:
089: public void comment(char[] c, int i) throws IOException {
090: save.write(new String(ST + "!--" + new String(c) + "--" + ET)
091: .getBytes());
092: }
093:
094: /*
095: * static private final HTMLEditorKit.Parser parser = new HTMLEditorKit() {
096: * public HTMLEditorKit.Parser getParser() { return super.getParser(); } }
097: * .getParser();
098: */
099: private boolean available;
100:
101: private int contents;
102:
103: synchronized private int r() {
104: while (!available)
105: try {
106: wait();
107: } catch (InterruptedException e) {
108: }
109: if (contents != -1)
110: available = false;
111: int c = contents;
112: notifyAll();
113: return c;
114: }
115:
116: private boolean isCancel;
117:
118: public void run() {
119: try {
120: new HTMLEditorKit() {
121: public HTMLEditorKit.Parser getParser() {
122: return super .getParser();
123: }
124: }.getParser().parse(
125: new InputStreamReader(new InputStream() {
126: public int read() {
127: return r();
128: }
129: }), new HTMLEditorKit.ParserCallback() {
130: public void handleStartTag(HTML.Tag tag,
131: MutableAttributeSet attributes,
132: int position) {
133: try {
134: start(tag, attributes, position);
135: } catch (Exception e) {
136: // e.printStackTrace();
137: }
138: }
139:
140: public void handleEndTag(HTML.Tag tag,
141: int position) {
142: try {
143: end(tag, position);
144: } catch (Exception e) {
145: }
146: }
147:
148: public void handleText(char[] text, int position) {
149: try {
150: text(text, position);
151: } catch (Exception e) {
152: }
153: }
154:
155: public void handleSimpleTag(HTML.Tag tag,
156: MutableAttributeSet attributes,
157: int position) {
158: try {
159: simple(tag, attributes, position);
160: } catch (Exception e) {
161: }
162: }
163:
164: public void handleComment(char[] c, int i) {
165: try {
166: comment(c, i);
167: } catch (Exception e) {
168: }
169: }
170: }, true);
171: } catch (Exception e) {
172: // e.printStackTrace();
173: }
174: isCancel = true;
175: synchronized (this ) {
176: notifyAll();
177: }
178: }
179:
180: synchronized public void content(int i) throws IOException {
181: if (b) {
182: while (available)
183: try {
184: wait();
185: } catch (InterruptedException e) {
186: }
187: contents = i;
188: available = true;
189: notifyAll();
190: }
191: super .content(i);
192: }
193:
194: synchronized public void end() {
195: if (b) {
196: while (available)
197: try {
198: wait();
199: } catch (InterruptedException e) {
200: }
201: contents = -1;
202: available = true;
203: notifyAll();
204: while (!isCancel)
205: try {
206: wait();
207: } catch (InterruptedException e) {
208: }
209: }
210: super.end();
211: outputstream = save;
212: save = null;
213: }
214: }
|