package com.fasterxml.aalto.out;
import javax.xml.namespace.QName;
import javax.xml.stream.*;
import org.codehaus.stax2.ri.typed.AsciiValueEncoder;
import com.fasterxml.aalto.impl.ErrorConsts;
public final class NonRepairingStreamWriter
extends StreamWriterBase
{
public NonRepairingStreamWriter(WriterConfig cfg, XmlWriter writer,
WNameTable symbols)
{
super(cfg, writer, symbols);
}
@Override
public void setDefaultNamespace(String uri)
throws XMLStreamException
{
_currElem.setDefaultNsURI(uri);
}
@Override
public void _setPrefix(String prefix, String uri)
{
_currElem.addPrefix(prefix, uri);
}
@Override
public void writeAttribute(String nsURI, String localName, String value)
throws XMLStreamException
{
if (!_stateStartElementOpen) {
throwOutputError(ErrorConsts.WERR_ATTR_NO_ELEM);
}
WName name;
if (nsURI == null || nsURI.length() == 0) {
name = _symbols.findSymbol(localName);
} else {
String prefix = _currElem.getExplicitPrefix(nsURI, _rootNsContext);
if (prefix == null) {
throwOutputError("Unbound namespace URI '"+nsURI+"'");
}
name = _symbols.findSymbol(prefix, localName);
}
_writeAttribute(name, value);
}
@Override
public void writeAttribute(String prefix, String nsURI,
String localName, String value)
throws XMLStreamException
{
if (!_stateStartElementOpen) {
throwOutputError(ErrorConsts.WERR_ATTR_NO_ELEM);
}
WName name = (prefix == null || prefix.length() == 0)
? _symbols.findSymbol(localName)
: _symbols.findSymbol(prefix, localName);
_writeAttribute(name, value);
}
@Override
public void writeDefaultNamespace(String nsURI)
throws XMLStreamException
{
if (!_stateStartElementOpen) {
throwOutputError(ErrorConsts.WERR_NS_NO_ELEM);
}
_writeDefaultNamespace(nsURI);
setDefaultNamespace(nsURI);
}
@Override
public void writeEmptyElement(String nsURI, String localName)
throws XMLStreamException
{
String prefix = _currElem.getPrefix(nsURI);
if (prefix == null) {
throwOutputError("Unbound namespace URI '"+nsURI+"'");
}
WName name;
if (prefix.length() == 0) {
name = _symbols.findSymbol(localName);
prefix = null;
} else {
name = _symbols.findSymbol(prefix, localName);
}
_verifyStartElement(prefix, localName);
_writeStartTag(name, true, nsURI);
}
@Override
public void writeEmptyElement(String prefix, String localName, String nsURI)
throws XMLStreamException
{
_verifyStartElement(prefix, localName);
WName name;
if (prefix == null || prefix.length() == 0) {
name = _symbols.findSymbol(localName);
} else {
name = _symbols.findSymbol(prefix, localName);
}
_writeStartTag(name, true, nsURI);
}
@Override
public void writeNamespace(String prefix, String nsURI)
throws XMLStreamException
{
if (prefix == null || prefix.length() == 0) {
writeDefaultNamespace(nsURI);
return;
}
if (!_stateStartElementOpen) {
throwOutputError(ErrorConsts.WERR_NS_NO_ELEM);
}
_writeNamespace(prefix, nsURI);
setPrefix(prefix, nsURI);
}
@Override
public void writeStartElement(String nsURI, String localName)
throws XMLStreamException
{
String prefix = _currElem.getPrefix(nsURI);
if (prefix == null) {
throwOutputError("Unbound namespace URI '"+nsURI+"'");
}
WName name;
if (prefix.length() == 0) {
name = _symbols.findSymbol(localName);
prefix = null;
} else {
name = _symbols.findSymbol(prefix, localName);
}
_verifyStartElement(prefix, localName);
_writeStartTag(name, false);
}
@Override
public void writeStartElement(String prefix, String localName,
String nsURI)
throws XMLStreamException
{
_verifyStartElement(prefix, localName);
WName name;
if (prefix == null || prefix.length() == 0) {
name = _symbols.findSymbol(localName);
} else {
name = _symbols.findSymbol(prefix, localName);
}
_writeStartTag(name, false, nsURI);
}
@Override
public void writeTypedAttribute(String prefix, String nsURI, String localName,
AsciiValueEncoder enc)
throws XMLStreamException
{
if (!_stateStartElementOpen) {
throwOutputError(ErrorConsts.WERR_ATTR_NO_ELEM);
}
WName name = (prefix == null || prefix.length() == 0)
? _symbols.findSymbol(localName)
: _symbols.findSymbol(prefix, localName);
_writeAttribute(name, enc);
}
@Override
protected String _serializeQName(QName name)
{
String prefix = name.getPrefix();
String local = name.getLocalPart();
if (prefix == null || prefix.length() == 0) {
return local;
}
return prefix + ":" + local;
}
}