KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > HttpPrinter


1 // Copyright (c) 2002. 2006 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.xml;
5 import gnu.lists.*;
6 import gnu.xml.*;
7 import java.io.*;
8 import gnu.mapping.*;
9 import java.util.Vector JavaDoc;
10
11 /** Output as an Http response.
12  * Used for both CGI scripts (default) and HttpServletResponse (future).
13  */

14
15 public class HttpPrinter extends FilterConsumer
16 {
17   Vector JavaDoc headers = new Vector JavaDoc();
18   /* #ifdef JAVA5 */
19   // StringBuilder sbuf = new StringBuilder(100);
20
/* #else */
21   StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
22   /* #endif */
23   Object JavaDoc currentHeader;
24
25   /** 1 - implicit; 2: explicit. */
26   private int seenBeginDocument;
27
28   protected String JavaDoc sawContentType;
29
30   /** Difference between number of beginGroup and endGroup calls so far. */
31   private int groupNesting;
32
33   protected OutputStream ostream;
34   OutPort writer;
35
36   public HttpPrinter(OutputStream out)
37   {
38     super(null);
39     ostream = out;
40     //ostream = System.out;
41
}
42
43   public HttpPrinter(OutPort out)
44   {
45     super(null);
46     writer = out;
47     //ostream = System.out;
48
}
49
50   public static HttpPrinter make (OutPort out)
51   {
52     return new HttpPrinter(out);
53   }
54
55   private void writeRaw(String JavaDoc str)
56     throws java.io.IOException JavaDoc
57   {
58     if (writer != null)
59       writer.write(str);
60     else
61       {
62     int len = str.length();
63     for (int i = 0; i < len; i++)
64       ostream.write((byte) str.charAt(i));
65       }
66   }
67
68   public void printHeader(String JavaDoc label, String JavaDoc value)
69     throws java.io.IOException JavaDoc
70   {
71     writeRaw(label);
72     writeRaw(": ");
73     writeRaw(value); // FIXME - need to quote?
74
writeRaw("\n");
75   }
76
77   public void printHeaders()
78     throws java.io.IOException JavaDoc
79   {
80     int num = headers.size();
81     for (int i = 0; i < num; i += 2)
82       printHeader(headers.elementAt(i).toString(),
83           headers.elementAt(i + 1).toString());
84     // if (sawContentType == null) writeRaw("Content-Type: text/html"); FIXME
85
writeRaw("\n");
86   }
87
88   public void addHeader(String JavaDoc label, String JavaDoc value)
89   {
90     if (label.equalsIgnoreCase("Content-type"))
91       sawContentType = value;
92     headers.addElement(label);
93     headers.addElement(value);
94   }
95
96   public void beginAttribute(Object JavaDoc attrType)
97   {
98     if (base == null)
99       currentHeader = attrType;
100     else
101       base.beginAttribute(attrType);
102   }
103
104   public void endAttribute()
105   {
106     if (currentHeader != null)
107       {
108     addHeader(currentHeader.toString(), sbuf.toString());
109     sbuf.setLength(0);
110     currentHeader = null;
111       }
112     else
113       base.endAttribute();
114   }
115
116   boolean seenXmlHeader;
117
118   public void beginData()
119   {
120     if (base == null)
121       {
122     if (sawContentType == null)
123       addHeader("Content-type", "text/html");
124     if (writer == null)
125       writer = new OutPort(ostream); // FIXME use encoding.
126
String JavaDoc style = null;
127     if ("text/html".equalsIgnoreCase(sawContentType))
128       style = "html";
129     else if ("text/xhtml".equalsIgnoreCase(sawContentType))
130       style = "xhtml";
131     else if ("text/plain".equalsIgnoreCase(sawContentType))
132       style = "plain";
133     base = XMLPrinter.make(writer, style);
134         if (seenBeginDocument == 0)
135           {
136             base.beginDocument();
137             seenBeginDocument = 1;
138           }
139     try
140       {
141         printHeaders();
142       }
143     catch (Throwable JavaDoc ex)
144       {
145         throw new RuntimeException JavaDoc(ex.toString());
146       }
147       }
148     /* #ifdef use:java.lang.CharSequence */
149     append(sbuf);
150     /* #else */
151     // append(sbuf.toString());
152
/* #endif */
153     sbuf.setLength(0);
154   }
155
156   public void beginGroup(Object JavaDoc type)
157   {
158     if (sawContentType == null)
159       {
160     String JavaDoc mimeType;
161     if (! seenXmlHeader)
162       mimeType = "text/html";
163     else if (type instanceof Symbol
164                  && "html".equals(((Symbol) type).getLocalPart()))
165       mimeType = "text/xhtml";
166     else
167       mimeType = "text/xml";
168     addHeader("Content-type", mimeType);
169       }
170     beginData();
171     base.beginGroup(type);
172     groupNesting++;
173   }
174
175   public void endGroup()
176   {
177     super.endGroup();
178     groupNesting--;
179     if (groupNesting == 0 && seenBeginDocument == 1)
180       endDocument();
181   }
182
183   public void writeObject(Object JavaDoc v)
184   {
185     if (v instanceof Consumable && ! (v instanceof UnescapedData))
186       ((Consumable) v).consume(this);
187     else
188       {
189     beginData();
190     super.writeObject(v);
191       }
192   }
193
194   /* #ifdef use:java.lang.CharSequence */
195   public Consumer append (CharSequence JavaDoc csq, int start, int end)
196   {
197     if (base == null)
198       {
199         /* #ifdef JAVA5 */
200         // sbuf.append(csq, start, end);
201
/* #else */
202         if (csq == null)
203           csq = "null";
204         sbuf.append(csq.subSequence(start, end).toString());
205         /* #endif */
206       }
207     else
208       base.write(csq, start, end);
209     return this;
210   }
211
212   public Consumer append (CharSequence JavaDoc csq)
213   {
214     if (base == null)
215       {
216         /* #ifdef JAVA5 */
217         // sbuf.append(csq);
218
/* #else */
219         sbuf.append(csq.toString());
220         /* #endif */
221       }
222     else if (csq == null)
223       base.write("null");
224     else
225       base.write(csq, 0, csq.length());
226     return this;
227   }
228   /* #else */
229   // public Consumer append (String str)
230
// {
231
// if (base == null)
232
// sbuf.append(str);
233
// else
234
// base.append(str);
235
// return this;
236
// }
237
/* #endif */
238
239   public void write(char[] buf, int off, int len)
240   {
241     if (base == null)
242       sbuf.append(buf, off, len);
243     else
244       base.write(buf, off, len);
245   }
246
247   public void beginDocument()
248   {
249     if (base != null)
250       base.beginDocument();
251     seenBeginDocument = 2;
252   }
253
254   public void endDocument()
255   {
256     if (base != null)
257       base.endDocument();
258     try
259       {
260     // else ???;
261
if (writer != null)
262       writer.close();
263     if (ostream != null)
264       ostream.flush();
265       }
266     catch (Throwable JavaDoc ex)
267       {
268       }
269   }
270 }
271
Popular Tags