| 1 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 ; 10 11 14 15 public class HttpPrinter extends FilterConsumer 16 { 17 Vector headers = new Vector (); 18 19 21 StringBuffer sbuf = new StringBuffer (100); 22 23 Object currentHeader; 24 25 26 private int seenBeginDocument; 27 28 protected String sawContentType; 29 30 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 } 42 43 public HttpPrinter(OutPort out) 44 { 45 super(null); 46 writer = out; 47 } 49 50 public static HttpPrinter make (OutPort out) 51 { 52 return new HttpPrinter(out); 53 } 54 55 private void writeRaw(String str) 56 throws java.io.IOException  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 label, String value) 69 throws java.io.IOException  70 { 71 writeRaw(label); 72 writeRaw(": "); 73 writeRaw(value); writeRaw("\n"); 75 } 76 77 public void printHeaders() 78 throws java.io.IOException  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 writeRaw("\n"); 86 } 87 88 public void addHeader(String label, String 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 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); String 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 ex) 144 { 145 throw new RuntimeException (ex.toString()); 146 } 147 } 148 149 append(sbuf); 150 151 153 sbuf.setLength(0); 154 } 155 156 public void beginGroup(Object type) 157 { 158 if (sawContentType == null) 159 { 160 String 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 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 195 public Consumer append (CharSequence csq, int start, int end) 196 { 197 if (base == null) 198 { 199 200 202 if (csq == null) 203 csq = "null"; 204 sbuf.append(csq.subSequence(start, end).toString()); 205 206 } 207 else 208 base.write(csq, start, end); 209 return this; 210 } 211 212 public Consumer append (CharSequence csq) 213 { 214 if (base == null) 215 { 216 217 219 sbuf.append(csq.toString()); 220 221 } 222 else if (csq == null) 223 base.write("null"); 224 else 225 base.write(csq, 0, csq.length()); 226 return this; 227 } 228 229 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 if (writer != null) 262 writer.close(); 263 if (ostream != null) 264 ostream.flush(); 265 } 266 catch (Throwable ex) 267 { 268 } 269 } 270 } 271 | Popular Tags |