nx01/nx01-x4o-sax3/src/main/java/org/x4o/sax3/SAX3WriterEnum.java

163 lines
5.1 KiB
Java
Raw Normal View History

2025-05-07 21:46:32 +02:00
/*
* Copyright (c) 2004-2014, Willem Cazander
* 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.
*
* 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 HOLDER 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 org.x4o.sax3;
import java.io.Closeable;
import java.io.IOException;
import java.util.Objects;
import org.x4o.sax3.io.ContentCloseable;
import org.x4o.sax3.io.ContentWriter;
import org.x4o.sax3.io.SAX3XMLConstants;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* ContentWriterXmlTag can write enum based xml events.
*
* @author Willem Cazander
* @version 1.0 May 3, 2013
* @param <TAG> The enum for the XML tag values.
* @param <TAG_WRITER> The tag writer to output to.
2025-05-07 21:46:32 +02:00
*/
public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter> implements SAX3WriterEnumHammer<TAG>, Closeable {
private final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
private final TAG_WRITER contentWriter;
private final String tagNamespaceUri;
private final String tagNamespacePrefix;
public SAX3WriterEnum(TAG_WRITER contentWriter) {
this(contentWriter, SAX3XMLConstants.NULL_NS_URI, SAX3XMLConstants.NULL_NS_URI);
}
public SAX3WriterEnum(TAG_WRITER contentWriter, String tagNamespaceUri, String tagNamespacePrefix) {
this.contentWriter = Objects.requireNonNull(contentWriter, "Can't create wrapper on null ContentWriter");
this.tagNamespaceUri = Objects.requireNonNull(tagNamespaceUri, "Can't create wrapper with null tagNamespaceUri");
this.tagNamespacePrefix = Objects.requireNonNull(tagNamespacePrefix, "Can't create wrapper with null tagNamespacePrefix");
}
@Override
public void close() throws IOException {
contentWriter.close();
}
public TAG_WRITER getContentWriterWrapped() {
return contentWriter;
}
public String getTagNamespaceUri() {
return tagNamespaceUri;
}
public void startDocument() throws IOException {
try {
contentWriter.startDocument();
contentWriter.startPrefixMapping(tagNamespacePrefix, getTagNamespaceUri());
} catch (SAXException e) {
throw new IOException(e);
}
}
public void endDocument() throws IOException {
try {
contentWriter.endDocument();
} catch (SAXException e) {
throw new IOException(e);
}
}
public ContentCloseable printTag(TAG tag) throws IOException {
return printTag(tag, EMPTY_ATTRIBUTES);
}
public ContentCloseable printTag(TAG tag, Attributes atts) throws IOException {
printTagStart(tag, atts);
return () -> printTagEnd(tag);
}
public void printTagStartEnd(TAG tag, Attributes atts) throws IOException {
printTagStart(tag, atts);
printTagEnd(tag);
}
public void printTagStartEnd(TAG tag) throws IOException {
printTagStart(tag);
printTagEnd(tag);
}
public void printTagStart(TAG tag) throws IOException {
printTagStart(tag, EMPTY_ATTRIBUTES);
}
public void printTagStart(TAG tag, Attributes atts) throws IOException {
try {
contentWriter.startElement(getTagNamespaceUri(), toTagString(tag), "", atts);
} catch (SAXException e) {
throw new IOException(e);
}
}
public void printTagEnd(TAG tag) throws IOException {
try {
contentWriter.endElement(getTagNamespaceUri(),toTagString(tag) , "");
} catch (SAXException e) {
throw new IOException(e);
}
}
private String toTagString(TAG tag) {
String result = tag.name();
if (result.startsWith("_")) {
result = result.substring(1); // remove _
}
return result;
}
public void printTagCharacters(TAG tag, String text) throws IOException {
printTagStart(tag);
if (text == null) {
text = " ";
}
printCharacters(text);
printTagEnd(tag);
}
public void printCharacters(String text) throws IOException {
try {
contentWriter.characters(text);
} catch (SAXException e) {
throw new IOException(e);
}
}
public void printComment(String text) throws IOException {
try {
contentWriter.comment(text);
} catch (SAXException e) {
throw new IOException(e);
}
}
}