1 4 package gnu.kawa.sax; 5 import gnu.lists.*; 6 import gnu.xml.*; 7 import org.xml.sax.*; 8 import gnu.mapping.Symbol; 9 import gnu.text.Char; 10 import org.xml.sax.helpers.AttributesImpl ; 11 12 14 15 public class ContentConsumer implements Consumer 16 { 17 ContentHandler out; 18 19 int nesting = 0; 20 String [] names = new String [15]; 21 String attrQName, attrURI, attrLocalName; 22 AttributesImpl attributes = new AttributesImpl (); 23 char[] chBuffer; 24 25 27 StringBuffer strBuffer = new StringBuffer (200); 28 29 30 int inStartTag; 31 32 public ContentConsumer () 33 { 34 } 35 36 public ContentConsumer (ContentHandler handler) 37 { 38 out = handler; 39 } 40 41 public void error(String method, SAXException ex) 42 { 43 throw new RuntimeException ("caught "+ex+" in "+method); 44 } 45 46 void endStartTag() 47 { 48 if (inStartTag != 1) 49 return; 50 int i = 3 * (nesting - 1); 51 try 52 { 53 out.startElement(names[i], names[i+1], names[i+2], attributes); 54 } 55 catch (SAXException ex) 56 { 57 error("startElement", ex); 58 } 59 attributes.clear(); 60 inStartTag = 0; 61 } 62 63 public void beginGroup(Object type) 64 { 65 if (inStartTag == 1) 66 endStartTag(); 67 flushStrBuffer(); 68 int i = 3 * nesting; 69 if (i >= names.length) 70 { 71 String [] tmp = new String [2 * i]; 72 System.arraycopy(names, 0, tmp, 0, i); 73 names = tmp; 74 } 75 String namespaceURI, localName; 76 if (type instanceof Symbol) 77 { 78 Symbol sym = (Symbol) type; 79 namespaceURI = sym.getNamespaceURI(); 80 localName = sym.getLocalName(); 81 } 82 else if (type instanceof XName) 83 { 84 XName sym = (XName) type; 85 namespaceURI = sym.getNamespaceURI(); 86 localName = sym.getLocalName(); 87 } 88 else 89 { 90 namespaceURI = ""; 91 localName = type.toString(); 92 } 93 names[i] = namespaceURI; 94 names[i+1] = localName; 95 names[i+2] = type.toString(); 96 inStartTag = 1; 97 nesting++; 98 } 99 100 public void beginAttribute(Object attrType) 101 { 102 attrURI = ((Symbol) attrType).getNamespaceURI(); 103 attrLocalName = ((Symbol) attrType).getLocalName(); 104 attrQName = attrType.toString(); 105 inStartTag = 2; 106 } 107 108 public void endAttribute() 109 { 110 attributes.addAttribute(attrURI, attrLocalName, attrQName, "CDATA", 111 strBuffer.toString()); 112 strBuffer.setLength(0); 113 inStartTag = 1; 114 } 115 116 public void beginDocument() 117 { 118 try 119 { 120 out.startDocument(); 121 } 122 catch (SAXException ex) 123 { 124 error("beginDocument", ex); 125 } 126 } 127 128 public void endDocument() 129 { 130 try 131 { 132 out.endDocument(); 133 } 134 catch (SAXException ex) 135 { 136 error("endDocument", ex); 137 } 138 } 139 140 public void endGroup() 141 { 142 endStartTag(); 143 flushStrBuffer(); 144 nesting--; 145 int i = 3 * nesting; 146 try 147 { 148 out.endElement(names[i], names[i+1], names[i+2]); 149 } 150 catch (SAXException ex) 151 { 152 error("endElement", ex); 153 } 154 names[i] = null; 155 names[i+1] = null; 156 names[i+2] = null; 157 } 158 159 void flushStrBuffer() 160 { 161 if (strBuffer.length() > 0) 162 { 163 if (chBuffer == null) 164 chBuffer = new char[200]; 165 try 166 { 167 int slen = strBuffer.length(); 168 int start = 0; 169 for (;;) 170 { 171 int len = slen - start; 172 if (len <= 0) 173 break; 174 if (len > chBuffer.length) 175 len = chBuffer.length; 176 strBuffer.getChars(start, start + len, chBuffer, start); 177 out.characters(chBuffer, 0, len); 178 start += len; 179 } 180 strBuffer.setLength(0); 181 } 182 catch (SAXException ex) 183 { 184 error("characters", ex); 185 } 186 } 187 } 188 189 public void write(char[] buf, int off, int len) 190 { 191 if (inStartTag == 1) 192 endStartTag(); 193 if (inStartTag == 2) 194 strBuffer.append(buf, off, len); 195 else 196 { 197 flushStrBuffer(); 198 try 199 { 200 out.characters(buf, off, len); 201 } 202 catch (SAXException ex) 203 { 204 error("characters", ex); 205 } 206 } 207 } 208 209 public void write (int v) 210 { 211 if (inStartTag == 1) 212 endStartTag(); 213 if (v >= 0x10000) 214 { 215 strBuffer.append((char) (((v - 0x10000) >> 10) + 0xD800)); 216 v = (v & 0x3FF) + 0xDC00; 217 } 218 strBuffer.append((char) v); 219 } 220 221 public void write (String v) 222 { 223 if (inStartTag == 1) 224 endStartTag(); 225 strBuffer.append(v); 226 } 227 228 229 public void write (CharSequence str, int start, int end) 230 231 233 { 234 if (inStartTag == 1) 235 endStartTag(); 236 237 238 240 strBuffer.append(str.subSequence(start, end).toString()); 241 242 243 245 } 246 247 248 268 269 public void writeObject(Object v) 270 { 271 if (v instanceof Consumable) 273 ((Consumable) v).consume(this); 274 else if (v instanceof SeqPosition) 275 { 276 SeqPosition pos = (SeqPosition) v; 277 pos.sequence.consumeNext(pos.ipos, this); 278 } 279 else if (v instanceof Char) 280 ((Char) v).print(this); 281 else 282 write(v == null ? "(null)" : v.toString()); 283 } 284 285 public void writeBoolean(boolean v) 286 { 287 if (inStartTag == 1) 288 endStartTag(); 289 strBuffer.append(v); 291 } 292 293 public void writeLong(long v) 294 { 295 if (inStartTag == 1) 296 endStartTag(); 297 strBuffer.append(v); 299 } 300 301 public void writeInt(int v) 302 { 303 if (inStartTag == 1) 304 endStartTag(); 305 strBuffer.append(v); 307 } 308 309 public void writeFloat(float v) 310 { 311 if (inStartTag == 1) 312 endStartTag(); 313 strBuffer.append(v); 315 } 316 317 public void writeDouble(double v) 318 { 319 if (inStartTag == 1) 320 endStartTag(); 321 strBuffer.append(v); 323 } 324 325 public void finalize() 326 { 327 flushStrBuffer(); 328 } 329 330 public boolean ignoring () 331 { 332 return false; 333 } 334 335 public void setContentHandler (ContentHandler handler) 336 { 337 out = handler; 338 } 339 340 public ContentHandler getContentHandler () 341 { 342 return out; 343 } 344 } 345 | Popular Tags |