| SerializingContentHandler.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: SerializingContentHandler.java 2050 2010-12-09 15:31:45Z dmsmith $
21 */
22 package org.crosswire.common.xml;
23
24 import org.xml.sax.Attributes;
25 import org.xml.sax.ContentHandler;
26 import org.xml.sax.Locator;
27
28 /**
29 * Class to convert a SAX stream into a simple String.
30 *
31 * @see gnu.lgpl.License for license details.<br>
32 * The copyright to this program is held by it's authors.
33 * @author Joe Walker [joe at eireneh dot com]
34 */
35 public class SerializingContentHandler implements ContentHandler {
36 /**
37 * Default ctor that does not insert newlines.
38 */
39 public SerializingContentHandler() {
40 newlines = false;
41 }
42
43 /**
44 * Default ctor that does not insert newlines.
45 */
46 public SerializingContentHandler(boolean newlines) {
47 this.newlines = newlines;
48 }
49
50 /*
51 * (non-Javadoc)
52 *
53 * @see java.lang.Object#toString()
54 */
55 @Override
56 public String toString() {
57 return buffer.toString();
58 }
59
60 /*
61 * (non-Javadoc)
62 *
63 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
64 */
65 public void setDocumentLocator(Locator locator) {
66 }
67
68 /*
69 * (non-Javadoc)
70 *
71 * @see org.xml.sax.ContentHandler#startDocument()
72 */
73 public void startDocument() {
74 // buffer.append("<?xml version=\"1.0\"?>");
75
76 if (newlines) {
77 buffer.append('\n');
78 }
79 }
80
81 /*
82 * (non-Javadoc)
83 *
84 * @see org.xml.sax.ContentHandler#endDocument()
85 */
86 public void endDocument() {
87 }
88
89 /*
90 * (non-Javadoc)
91 *
92 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
93 * java.lang.String)
94 */
95 public void startPrefixMapping(String prefix, String uri) {
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
102 */
103 public void endPrefixMapping(String prefix) {
104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
110 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
111 */
112 public void startElement(String uri, String localname, String qname, Attributes attrs) {
113 buffer.append('<');
114 if (qname != null) {
115 buffer.append(qname);
116 } else {
117 buffer.append(localname);
118 }
119
120 for (int i = 0; i < attrs.getLength(); i++) {
121 buffer.append(' ');
122 buffer.append(XMLUtil.getAttributeName(attrs, i));
123 buffer.append("=\"");
124 buffer.append(attrs.getValue(i));
125 buffer.append('\"');
126 }
127
128 buffer.append('>');
129
130 if (newlines) {
131 buffer.append('\n');
132 }
133 }
134
135 /*
136 * (non-Javadoc)
137 *
138 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
139 * java.lang.String, java.lang.String)
140 */
141 public void endElement(String uri, String localname, String qname) {
142 buffer.append("</");
143 if (qname != null) {
144 buffer.append(qname);
145 } else {
146 buffer.append(localname);
147 }
148
149 buffer.append('>');
150
151 if (newlines) {
152 buffer.append('\n');
153 }
154 }
155
156 /*
157 * (non-Javadoc)
158 *
159 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
160 */
161 public void characters(char[] chars, int start, int length) {
162 String s = new String(chars, start, length);
163 buffer.append(s);
164 }
165
166 /*
167 * (non-Javadoc)
168 *
169 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
170 */
171 public void ignorableWhitespace(char[] chars, int start, int length) {
172 String s = new String(chars, start, length);
173 buffer.append(s);
174 }
175
176 /*
177 * (non-Javadoc)
178 *
179 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
180 * java.lang.String)
181 */
182 public void processingInstruction(String target, String data) {
183 buffer.append("<!");
184 buffer.append(target);
185 buffer.append(' ');
186 buffer.append(data);
187 buffer.append("!>");
188
189 if (newlines) {
190 buffer.append('\n');
191 }
192 }
193
194 /*
195 * (non-Javadoc)
196 *
197 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
198 */
199 public void skippedEntity(String name) {
200 }
201
202 private boolean newlines;
203 private StringBuilder buffer = new StringBuilder();
204 }
205