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