1 16 package org.apache.commons.betwixt.io; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.DefaultHandler ; 24 25 31 public class SAXContentHandler extends DefaultHandler { 32 33 private Writer out; 34 37 public SAXContentHandler(Writer out) { 38 this.out = out; 39 } 40 41 44 public void characters(char[] ch, int start, int length) 45 throws SAXException 46 { 47 try { 48 out.write(" "+new String (ch, start, length)+"\n"); 49 }catch(IOException ioe) { 50 } 51 } 52 53 56 public void endElement(String namespaceURI, String localName, String qName) 57 throws SAXException 58 { 59 try { 60 out.write("</"+qName+">\n"); 61 }catch (IOException e) { 62 } 63 } 64 65 68 public void startDocument() throws SAXException 69 { 70 try { 71 out.write("<?xml version=\"1.0\"?>\n"); 72 }catch (IOException e){ 73 } 74 } 75 76 79 public void startElement( 80 String namespaceURI, 81 String localName, 82 String qName, 83 Attributes atts) 84 throws SAXException 85 { 86 try { 87 StringBuffer sb = new StringBuffer (); 88 sb.append("<"+qName); 89 for (int i=0; i < atts.getLength();i++) 90 { 91 sb.append(" "+atts.getQName(i)); 92 sb.append("=\""); 93 sb.append(atts.getValue(i)); 94 sb.append("\""); 95 } 96 sb.append(">\n"); 97 out.write(sb.toString()); 98 } catch (IOException e) { 99 } 100 } 101 102 } 103 | Popular Tags |