1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*--
Copyright (C) 2000 Brett McLaughlin & Jason Hunter.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact license@jdom.org.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management (pm@jdom.org).
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Brett McLaughlin <brett@jdom.org> and
Jason Hunter <jhunter@jdom.org>. For more information on the
JDOM Project, please see <http://www.jdom.org/>.
*/
package sax;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Filter for data- or field-oriented XML.
*
* <i>Code and comments adapted from DataWriter-0.2, written
* by David Megginson and released into the public domain,
* without warranty.</i>
*
* <p>This filter adds indentation and newlines to field-oriented
* XML without mixed content. All added indentation and newlines
* will be passed on down the filter chain.</p>
*
* <p>In general, all whitespace in an XML document is potentially
* significant. There is, however, a large class of XML documents
* where information is strictly fielded: each element contains either
* character data or other elements, but not both. For this special
* case, it is possible for a filter to provide automatic indentation
* and newlines. Note that this class will likely not yield appropriate
* results for document-oriented XML like XHTML pages, which mix character
* data and elements together.</p>
*
* <p>This filter will automatically place each start tag on a new line,
* optionally indented if an indent step is provided (by default, there
* is no indentation). If an element contains other elements, the end
* tag will also appear on a new line with leading indentation. Consider,
* for example, the following code:</p>
*
* <pre>
* DataFormatFilter df = new DataFormatFilter();
* df.setContentHandler(new XMLWriter());
*
* df.setIndentStep(2);
* df.startDocument();
* df.startElement("Person");
* df.dataElement("name", "Jane Smith");
* df.dataElement("date-of-birth", "1965-05-23");
* df.dataElement("citizenship", "US");
* df.endElement("Person");
* df.endDocument();
* </pre>
*
* <p>This code will produce the following document:</p>
*
* <pre>
* <?xml version="1.0"?>
*
* <Person>
* <name>Jane Smith</name>
* <date-of-birth>1965-05-23</date-of-birth>
* <citizenship>US</citizenship>
* </Person>
* </pre>
*
* @see DataUnformatFilter
*/
public class DataFormatFilter extends XMLFilterBase
{
////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////
/**
* Create a new filter.
*/
public DataFormatFilter()
{
}
/**
* Create a new filter.
*
* <p>Use the XMLReader provided as the source of events.</p>
*
* @param xmlreader The parent in the filter chain.
*/
public DataFormatFilter(XMLReader xmlreader)
{
super(xmlreader);
}
////////////////////////////////////////////////////////////////////
// Accessors and setters.
////////////////////////////////////////////////////////////////////
/**
* Return the current indent step.
*
* <p>Return the current indent step: each start tag will be
* indented by this number of spaces times the number of
* ancestors that the element has.</p>
*
* @return The number of spaces in each indentation step,
* or 0 or less for no indentation.
* @see #setIndentStep
*/
public int getIndentStep ()
{
return indentStep;
}
/**
* Set the current indent step.
*
* @param indentStep The new indent step (0 or less for no
* indentation).
* @see #getIndentStep
*/
public void setIndentStep (int indentStep)
{
this.indentStep = indentStep;
}
////////////////////////////////////////////////////////////////////
// Public methods.
////////////////////////////////////////////////////////////////////
/**
* Reset the filter so that it can be reused.
*
* <p>This method is especially useful if the filter failed
* with an exception the last time through.</p>
*/
public void reset ()
{
state = SEEN_NOTHING;
stateStack = new Stack();
}
////////////////////////////////////////////////////////////////////
// Methods from org.xml.sax.ContentHandler.
////////////////////////////////////////////////////////////////////
/**
* Filter a start document event.
*
* <p>Reset state and pass the event on for further processing.</p>
*
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
* @see org.xml.sax.ContentHandler#startDocument
*/
public void startDocument ()
throws SAXException
{
reset();
super.startDocument();
}
/**
* Add newline and indentation prior to start tag.
*
* <p>Each tag will begin on a new line, and will be
* indented by the current indent step times the number
* of ancestors that the element has.</p>
*
* <p>The newline and indentation will be passed on down
* the filter chain through regular characters events.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param qName The element's qualified (prefixed) name.
* @param atts The element's attribute list.
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
* @see org.xml.sax.ContentHandler#startElement
*/
public void startElement (String uri, String localName,
String qName, Attributes atts)
throws SAXException
{
if (!stateStack.empty()) {
doNewline();
doIndent();
}
stateStack.push(SEEN_ELEMENT);
state = SEEN_NOTHING;
super.startElement(uri, localName, qName, atts);
}
/**
* Add newline and indentation prior to end tag.
*
* <p>If the element has contained other elements, the tag
* will appear indented on a new line; otherwise, it will
* appear immediately following whatever came before.</p>
*
* <p>The newline and indentation will be passed on down
* the filter chain through regular characters events.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param qName The element's qualified (prefixed) name.
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
* @see org.xml.sax.ContentHandler#endElement
*/
public void endElement (String uri, String localName, String qName)
throws SAXException
{
boolean seenElement = (state == SEEN_ELEMENT);
state = stateStack.pop();
if (seenElement) {
doNewline();
doIndent();
}
super.endElement(uri, localName, qName);
}
/**
* Filter a character data event.
*
* @param ch The characters to write.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
* @see org.xml.sax.ContentHandler#characters
*/
public void characters (char ch[], int start, int length)
throws SAXException
{
state = SEEN_DATA;
super.characters(ch, start, length);
}
////////////////////////////////////////////////////////////////////
// Internal methods.
////////////////////////////////////////////////////////////////////
/**
* Add newline.
*
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
*/
private void doNewline ()
throws SAXException
{
super.characters(NEWLINE, 0, NEWLINE.length);
}
/**
* Add indentation for the current level.
*
* @exception org.xml.sax.SAXException If a filter
* further down the chain raises an exception.
*/
private void doIndent ()
throws SAXException
{
int n = indentStep * stateStack.size();
if (n > 0) {
char ch[] = new char[n];
for (int i = 0; i < n; i++) {
ch[i] = INDENT_CHAR;
}
super.characters(ch, 0, n);
}
}
////////////////////////////////////////////////////////////////////
// Constants.
////////////////////////////////////////////////////////////////////
private static final Object SEEN_NOTHING = new Object();
private static final Object SEEN_ELEMENT = new Object();
private static final Object SEEN_DATA = new Object();
private static final char[] NEWLINE = new char[] {'\n'};
private static final char INDENT_CHAR = ' ';
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
private Object state = SEEN_NOTHING;
private Stack stateStack = new Stack();
private int indentStep = 0;
}
// end of DataFormatFilter.java
|