| 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 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the 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 * © CrossWire Bible Society, 2005 - 2016
18 *
19 */
20 package org.crosswire.common.xml;
21
22 import org.xml.sax.Attributes;
23 import org.xml.sax.ContentHandler;
24 import org.xml.sax.Locator;
25
26 /**
27 * Class to convert a SAX stream into a simple String.
28 *
29 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30 * @author Joe Walker
31 */
32 public class SerializingContentHandler implements ContentHandler {
33 /**
34 * Default ctor that does not insert newlines.
35 */
36 public SerializingContentHandler() {
37 this(false);
38 }
39
40 /**
41 * Default ctor that conditionally inserts newlines.
42 *
43 * @param newlines whether newlines are desired
44 */
45 public SerializingContentHandler(boolean newlines) {
46 this.newlines = newlines;
47 this.buffer = new StringBuilder();
48 }
49
50 /* (non-Javadoc)
51 * @see java.lang.Object#toString()
52 */
53 @Override
54 public String toString() {
55 return buffer.toString();
56 }
57
58 /* (non-Javadoc)
59 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
60 */
61 public void setDocumentLocator(Locator locator) {
62 }
63
64 /* (non-Javadoc)
65 * @see org.xml.sax.ContentHandler#startDocument()
66 */
67 public void startDocument() {
68 // buffer.append("<?xml version=\"1.0\"?>");
69
70 if (newlines) {
71 buffer.append('\n');
72 }
73 }
74
75 /* (non-Javadoc)
76 * @see org.xml.sax.ContentHandler#endDocument()
77 */
78 public void endDocument() {
79 }
80
81 /* (non-Javadoc)
82 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
83 */
84 public void startPrefixMapping(String prefix, String uri) {
85 }
86
87 /* (non-Javadoc)
88 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
89 */
90 public void endPrefixMapping(String prefix) {
91 }
92
93 /* (non-Javadoc)
94 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
95 */
96 public void startElement(String uri, String localname, String qname, Attributes attrs) {
97 buffer.append('<');
98 if (qname != null) {
99 buffer.append(qname);
100 } else {
101 buffer.append(localname);
102 }
103
104 for (int i = 0; i < attrs.getLength(); i++) {
105 buffer.append(' ');
106 buffer.append(XMLUtil.getAttributeName(attrs, i));
107 buffer.append("=\"");
108 buffer.append(attrs.getValue(i));
109 buffer.append('\"');
110 }
111
112 buffer.append('>');
113
114 if (newlines) {
115 buffer.append('\n');
116 }
117 }
118
119 /* (non-Javadoc)
120 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
121 */
122 public void endElement(String uri, String localname, String qname) {
123 buffer.append("</");
124 if (qname != null) {
125 buffer.append(qname);
126 } else {
127 buffer.append(localname);
128 }
129
130 buffer.append('>');
131
132 if (newlines) {
133 buffer.append('\n');
134 }
135 }
136
137 /* (non-Javadoc)
138 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
139 */
140 public void characters(char[] chars, int start, int length) {
141 String s = new String(chars, start, length);
142 buffer.append(s);
143 }
144
145 /* (non-Javadoc)
146 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
147 */
148 public void ignorableWhitespace(char[] chars, int start, int length) {
149 String s = new String(chars, start, length);
150 buffer.append(s);
151 }
152
153 /* (non-Javadoc)
154 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
155 */
156 public void processingInstruction(String target, String data) {
157 buffer.append("<!");
158 buffer.append(target);
159 buffer.append(' ');
160 buffer.append(data);
161 buffer.append("!>");
162
163 if (newlines) {
164 buffer.append('\n');
165 }
166 }
167
168 /* (non-Javadoc)
169 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
170 */
171 public void skippedEntity(String name) {
172 }
173
174 private boolean newlines;
175 private StringBuilder buffer;
176 }
177