Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SerializingContentHandler |
|
| 1.5;1.5 |
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 | 0 | this(false); |
38 | 0 | } |
39 | ||
40 | /** | |
41 | * Default ctor that conditionally inserts newlines. | |
42 | * | |
43 | * @param newlines whether newlines are desired | |
44 | */ | |
45 | 0 | public SerializingContentHandler(boolean newlines) { |
46 | 0 | this.newlines = newlines; |
47 | 0 | this.buffer = new StringBuilder(); |
48 | 0 | } |
49 | ||
50 | /* (non-Javadoc) | |
51 | * @see java.lang.Object#toString() | |
52 | */ | |
53 | @Override | |
54 | public String toString() { | |
55 | 0 | 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 | 0 | } |
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 | 0 | if (newlines) { |
71 | 0 | buffer.append('\n'); |
72 | } | |
73 | 0 | } |
74 | ||
75 | /* (non-Javadoc) | |
76 | * @see org.xml.sax.ContentHandler#endDocument() | |
77 | */ | |
78 | public void endDocument() { | |
79 | 0 | } |
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 | 0 | } |
86 | ||
87 | /* (non-Javadoc) | |
88 | * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) | |
89 | */ | |
90 | public void endPrefixMapping(String prefix) { | |
91 | 0 | } |
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 | 0 | buffer.append('<'); |
98 | 0 | if (qname != null) { |
99 | 0 | buffer.append(qname); |
100 | } else { | |
101 | 0 | buffer.append(localname); |
102 | } | |
103 | ||
104 | 0 | for (int i = 0; i < attrs.getLength(); i++) { |
105 | 0 | buffer.append(' '); |
106 | 0 | buffer.append(XMLUtil.getAttributeName(attrs, i)); |
107 | 0 | buffer.append("=\""); |
108 | 0 | buffer.append(attrs.getValue(i)); |
109 | 0 | buffer.append('\"'); |
110 | } | |
111 | ||
112 | 0 | buffer.append('>'); |
113 | ||
114 | 0 | if (newlines) { |
115 | 0 | buffer.append('\n'); |
116 | } | |
117 | 0 | } |
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 | 0 | buffer.append("</"); |
124 | 0 | if (qname != null) { |
125 | 0 | buffer.append(qname); |
126 | } else { | |
127 | 0 | buffer.append(localname); |
128 | } | |
129 | ||
130 | 0 | buffer.append('>'); |
131 | ||
132 | 0 | if (newlines) { |
133 | 0 | buffer.append('\n'); |
134 | } | |
135 | 0 | } |
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 | 0 | String s = new String(chars, start, length); |
142 | 0 | buffer.append(s); |
143 | 0 | } |
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 | 0 | String s = new String(chars, start, length); |
150 | 0 | buffer.append(s); |
151 | 0 | } |
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 | 0 | buffer.append("<!"); |
158 | 0 | buffer.append(target); |
159 | 0 | buffer.append(' '); |
160 | 0 | buffer.append(data); |
161 | 0 | buffer.append("!>"); |
162 | ||
163 | 0 | if (newlines) { |
164 | 0 | buffer.append('\n'); |
165 | } | |
166 | 0 | } |
167 | ||
168 | /* (non-Javadoc) | |
169 | * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) | |
170 | */ | |
171 | public void skippedEntity(String name) { | |
172 | 0 | } |
173 | ||
174 | private boolean newlines; | |
175 | private StringBuilder buffer; | |
176 | } |