[Major] moved javanet.staxutils to ch.eitchnet.utils

This commit is contained in:
Robert von Burg 2015-04-19 16:12:55 +02:00
parent 2371cd7853
commit f72ff76b40
3 changed files with 0 additions and 619 deletions

View File

@ -1,36 +0,0 @@
package javanet.staxutils;
/**
* Characters that represent line breaks and indentation. These are represented as String-valued JavaBean properties.
*/
@SuppressWarnings("nls")
public interface Indentation {
/** Two spaces; the default indentation. */
public static final String DEFAULT_INDENT = " ";
/**
* Set the characters used for one level of indentation. The default is {@link #DEFAULT_INDENT}. "\t" is a popular
* alternative.
*/
void setIndent(String indent);
/** The characters used for one level of indentation. */
String getIndent();
/**
* "\n"; the normalized representation of end-of-line in <a
* href="http://www.w3.org/TR/xml11/#sec-line-ends">XML</a>.
*/
public static final String NORMAL_END_OF_LINE = "\n";
/**
* Set the characters that introduce a new line. The default is {@link #NORMAL_END_OF_LINE}.
* {@link IndentingXMLStreamWriter#getLineSeparator}() is a popular alternative.
*/
public void setNewLine(String newLine);
/** The characters that introduce a new line. */
String getNewLine();
}

View File

@ -1,370 +0,0 @@
/*
* Copyright (c) 2006, John Kristian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of StAX-Utils nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
*
*/
package javanet.staxutils;
import javanet.staxutils.helpers.StreamWriterDelegate;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
/**
* A filter that indents an XML stream. To apply it, construct a filter that contains another {@link XMLStreamWriter},
* which you pass to the constructor. Then call methods of the filter instead of the contained stream. For example:
*
* <pre>
* {@link XMLStreamWriter} stream = ...
* stream = new {@link IndentingXMLStreamWriter}(stream);
* stream.writeStartDocument();
* ...
* </pre>
*
* <p>
* The filter inserts characters to format the document as an outline, with nested elements indented. Basically, it
* inserts a line break and whitespace before:
* <ul>
* <li>each DTD, processing instruction or comment that's not preceded by data</li>
* <li>each starting tag that's not preceded by data</li>
* <li>each ending tag that's preceded by nested elements but not data</li>
* </ul>
* This works well with 'data-oriented' XML, wherein each element contains either data or nested elements but not both.
* It can work badly with other styles of XML. For example, the data in a 'mixed content' document are apt to be
* polluted with indentation characters.
* <p>
* Indentation can be adjusted by setting the newLine and indent properties. But set them to whitespace only, for best
* results. Non-whitespace is apt to cause problems, for example when this class attempts to insert newLine before the
* root element.
*
* @author <a href="mailto:jk2006@engineer.com">John Kristian</a>
*/
@SuppressWarnings("nls")
public class IndentingXMLStreamWriter extends StreamWriterDelegate implements Indentation {
public IndentingXMLStreamWriter(XMLStreamWriter out) {
this(out, DEFAULT_INDENT, NORMAL_END_OF_LINE);
}
public IndentingXMLStreamWriter(XMLStreamWriter out, String indent) {
this(out, indent, NORMAL_END_OF_LINE);
}
public IndentingXMLStreamWriter(XMLStreamWriter out, String indent, String newLine) {
super(out);
setIndent(indent);
setNewLine(newLine);
}
/** How deeply nested the current scope is. The root element is depth 1. */
private int depth = 0; // document scope
/** stack[depth] indicates what's been written into the current scope. */
private int[] stack = new int[] { 0, 0, 0, 0 }; // nothing written yet
private static final int WROTE_MARKUP = 1;
private static final int WROTE_DATA = 2;
private String indent = DEFAULT_INDENT;
private String newLine = NORMAL_END_OF_LINE;
/** newLine followed by copies of indent. */
private char[] linePrefix = null;
@Override
public void setIndent(String indent) {
if (!indent.equals(this.indent)) {
this.indent = indent;
this.linePrefix = null;
}
}
@Override
public String getIndent() {
return this.indent;
}
@Override
public void setNewLine(String newLine) {
if (!newLine.equals(this.newLine)) {
this.newLine = newLine;
this.linePrefix = null;
}
}
/**
* @return System.getProperty("line.separator"); or {@link #NORMAL_END_OF_LINE} if that fails.
*/
public static String getLineSeparator() {
try {
return System.getProperty("line.separator");
} catch (SecurityException ignored) {
//
}
return NORMAL_END_OF_LINE;
}
@Override
public String getNewLine() {
return this.newLine;
}
@Override
public void writeStartDocument() throws XMLStreamException {
beforeMarkup();
this.out.writeStartDocument();
afterMarkup();
}
@Override
public void writeStartDocument(String version) throws XMLStreamException {
beforeMarkup();
this.out.writeStartDocument(version);
afterMarkup();
}
@Override
public void writeStartDocument(String encoding, String version) throws XMLStreamException {
beforeMarkup();
this.out.writeStartDocument(encoding, version);
afterMarkup();
}
@Override
public void writeDTD(String dtd) throws XMLStreamException {
beforeMarkup();
this.out.writeDTD(dtd);
afterMarkup();
}
@Override
public void writeProcessingInstruction(String target) throws XMLStreamException {
beforeMarkup();
this.out.writeProcessingInstruction(target);
afterMarkup();
}
@Override
public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
beforeMarkup();
this.out.writeProcessingInstruction(target, data);
afterMarkup();
}
@Override
public void writeComment(String data) throws XMLStreamException {
beforeMarkup();
this.out.writeComment(data);
afterMarkup();
}
@Override
public void writeEmptyElement(String localName) throws XMLStreamException {
beforeMarkup();
this.out.writeEmptyElement(localName);
afterMarkup();
}
@Override
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
beforeMarkup();
this.out.writeEmptyElement(namespaceURI, localName);
afterMarkup();
}
@Override
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
beforeMarkup();
this.out.writeEmptyElement(prefix, localName, namespaceURI);
afterMarkup();
}
@Override
public void writeStartElement(String localName) throws XMLStreamException {
beforeStartElement();
this.out.writeStartElement(localName);
afterStartElement();
}
@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
beforeStartElement();
this.out.writeStartElement(namespaceURI, localName);
afterStartElement();
}
@Override
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
beforeStartElement();
this.out.writeStartElement(prefix, localName, namespaceURI);
afterStartElement();
}
@Override
public void writeCharacters(String text) throws XMLStreamException {
this.out.writeCharacters(text);
afterData();
}
@Override
public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
this.out.writeCharacters(text, start, len);
afterData();
}
@Override
public void writeCData(String data) throws XMLStreamException {
this.out.writeCData(data);
afterData();
}
@Override
public void writeEntityRef(String name) throws XMLStreamException {
this.out.writeEntityRef(name);
afterData();
}
@Override
public void writeEndElement() throws XMLStreamException {
beforeEndElement();
this.out.writeEndElement();
afterEndElement();
}
@Override
public void writeEndDocument() throws XMLStreamException {
try {
while (this.depth > 0) {
writeEndElement(); // indented
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
this.out.writeEndDocument();
afterEndDocument();
}
/** Prepare to write markup, by writing a new line and indentation. */
protected void beforeMarkup() {
int soFar = this.stack[this.depth];
if ((soFar & WROTE_DATA) == 0 // no data in this scope
&& (this.depth > 0 || soFar != 0)) // not the first line
{
try {
writeNewLine(this.depth);
if (this.depth > 0 && getIndent().length() > 0) {
afterMarkup(); // indentation was written
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/** Note that markup or indentation was written. */
protected void afterMarkup() {
this.stack[this.depth] |= WROTE_MARKUP;
}
/** Note that data were written. */
protected void afterData() {
this.stack[this.depth] |= WROTE_DATA;
}
/** Prepare to start an element, by allocating stack space. */
protected void beforeStartElement() {
beforeMarkup();
if (this.stack.length <= this.depth + 1) {
// Allocate more space for the stack:
int[] newStack = new int[this.stack.length * 2];
System.arraycopy(this.stack, 0, newStack, 0, this.stack.length);
this.stack = newStack;
}
this.stack[this.depth + 1] = 0; // nothing written yet
}
/** Note that an element was started. */
protected void afterStartElement() {
afterMarkup();
++this.depth;
}
/** Prepare to end an element, by writing a new line and indentation. */
protected void beforeEndElement() {
if (this.depth > 0 && this.stack[this.depth] == WROTE_MARKUP) { // but not data
try {
writeNewLine(this.depth - 1);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
}
/** Note that an element was ended. */
protected void afterEndElement() {
if (this.depth > 0) {
--this.depth;
}
}
/** Note that a document was ended. */
protected void afterEndDocument() {
if (this.stack[this.depth = 0] == WROTE_MARKUP) { // but not data
try {
writeNewLine(0);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
this.stack[this.depth] = 0; // start fresh
}
/** Write a line separator followed by indentation. */
protected void writeNewLine(int indentation) throws XMLStreamException {
final int newLineLength = getNewLine().length();
final int prefixLength = newLineLength + (getIndent().length() * indentation);
if (prefixLength > 0) {
if (this.linePrefix == null) {
this.linePrefix = (getNewLine() + getIndent()).toCharArray();
}
while (prefixLength > this.linePrefix.length) {
// make linePrefix longer:
char[] newPrefix = new char[newLineLength + ((this.linePrefix.length - newLineLength) * 2)];
System.arraycopy(this.linePrefix, 0, newPrefix, 0, this.linePrefix.length);
System.arraycopy(this.linePrefix, newLineLength, newPrefix, this.linePrefix.length,
this.linePrefix.length - newLineLength);
this.linePrefix = newPrefix;
}
this.out.writeCharacters(this.linePrefix, 0, prefixLength);
}
}
}

View File

@ -1,213 +0,0 @@
/*
* Copyright (c) 2006, John Kristian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of StAX-Utils nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
*
*/
package javanet.staxutils.helpers;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
/**
* Abstract class for writing filtered XML streams. This class provides methods that merely delegate to the contained
* stream. Subclasses should override some of these methods, and may also provide additional methods and fields.
*
* @author <a href="mailto:jk2006@engineer.com">John Kristian</a>
*/
public abstract class StreamWriterDelegate implements XMLStreamWriter {
protected StreamWriterDelegate(XMLStreamWriter out) {
this.out = out;
}
protected XMLStreamWriter out;
@Override
public Object getProperty(String name) throws IllegalArgumentException {
return this.out.getProperty(name);
}
@Override
public NamespaceContext getNamespaceContext() {
return this.out.getNamespaceContext();
}
@Override
public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
this.out.setNamespaceContext(context);
}
@Override
public void setDefaultNamespace(String uri) throws XMLStreamException {
this.out.setDefaultNamespace(uri);
}
@Override
public void writeStartDocument() throws XMLStreamException {
this.out.writeStartDocument();
}
@Override
public void writeStartDocument(String version) throws XMLStreamException {
this.out.writeStartDocument(version);
}
@Override
public void writeStartDocument(String encoding, String version) throws XMLStreamException {
this.out.writeStartDocument(encoding, version);
}
@Override
public void writeDTD(String dtd) throws XMLStreamException {
this.out.writeDTD(dtd);
}
@Override
public void writeProcessingInstruction(String target) throws XMLStreamException {
this.out.writeProcessingInstruction(target);
}
@Override
public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
this.out.writeProcessingInstruction(target, data);
}
@Override
public void writeComment(String data) throws XMLStreamException {
this.out.writeComment(data);
}
@Override
public void writeEmptyElement(String localName) throws XMLStreamException {
this.out.writeEmptyElement(localName);
}
@Override
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
this.out.writeEmptyElement(namespaceURI, localName);
}
@Override
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
this.out.writeEmptyElement(prefix, localName, namespaceURI);
}
@Override
public void writeStartElement(String localName) throws XMLStreamException {
this.out.writeStartElement(localName);
}
@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
this.out.writeStartElement(namespaceURI, localName);
}
@Override
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
this.out.writeStartElement(prefix, localName, namespaceURI);
}
@Override
public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
this.out.writeDefaultNamespace(namespaceURI);
}
@Override
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
this.out.writeNamespace(prefix, namespaceURI);
}
@Override
public String getPrefix(String uri) throws XMLStreamException {
return this.out.getPrefix(uri);
}
@Override
public void setPrefix(String prefix, String uri) throws XMLStreamException {
this.out.setPrefix(prefix, uri);
}
@Override
public void writeAttribute(String localName, String value) throws XMLStreamException {
this.out.writeAttribute(localName, value);
}
@Override
public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
this.out.writeAttribute(namespaceURI, localName, value);
}
@Override
public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
throws XMLStreamException {
this.out.writeAttribute(prefix, namespaceURI, localName, value);
}
@Override
public void writeCharacters(String text) throws XMLStreamException {
this.out.writeCharacters(text);
}
@Override
public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
this.out.writeCharacters(text, start, len);
}
@Override
public void writeCData(String data) throws XMLStreamException {
this.out.writeCData(data);
}
@Override
public void writeEntityRef(String name) throws XMLStreamException {
this.out.writeEntityRef(name);
}
@Override
public void writeEndElement() throws XMLStreamException {
this.out.writeEndElement();
}
@Override
public void writeEndDocument() throws XMLStreamException {
this.out.writeEndDocument();
}
@Override
public void flush() throws XMLStreamException {
this.out.flush();
}
@Override
public void close() throws XMLStreamException {
this.out.close();
}
}