package net.sourceforge.javautil.common.jaxb.xml.writer;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.jaxb.xml.IXMLCharacterData;
import net.sourceforge.javautil.common.jaxb.xml.IXMLElement;
import net.sourceforge.javautil.common.jaxb.xml.XMLContext;
import net.sourceforge.javautil.common.jaxb.xml.XMLContext.Tag;
/**
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class XMLWriterElement extends XMLWriterAbstract implements IXMLElement<XMLWriterContext> {
protected static final XMLWriterElement ELEMENT_WRITER = new XMLWriterElement();
protected static final XMLWriterCharacterData CDATA_WRITER = new XMLWriterCharacterData();
@Override public void visitAttribute(XMLWriterContext context, String prefix, String name, String value) {
this.writeAttribute(context, prefix, name, value);
}
@Override public void visitXmlNSDeclaration(XMLWriterContext context, String prefix, String namespace) {
if (prefix == null) {
this.writeAttribute(context, null, "xmlns", namespace);
} else {
this.writeAttribute(context, "xmlns", prefix, namespace);
}
}
@Override public void visitComment(XMLWriterContext context, char[] buffer, int offset, int length) {
this.closeTagIfNeccesary(context);
this.write(context, "<!--");
this.write(context, buffer, offset, length);
this.write(context, "-->");
}
@Override public void visitWhitespace(XMLWriterContext context, char[] buffer, int offset, int length) {
this.closeTagIfNeccesary(context);
this.write(context, buffer, offset, length);
}
@Override public void visitContent(XMLWriterContext context, char[] buffer, int offset, int length) {
this.closeTagIfNeccesary(context);
this.write(context, buffer, offset, length);
}
@Override public IXMLCharacterData visitCharacterData(XMLWriterContext context) {
this.closeTagIfNeccesary(context);
this.write(context, "<![CDATA[");
return CDATA_WRITER;
}
@Override public IXMLElement visitElement(XMLWriterContext context, String prefix, String elementName) {
this.closeTagIfNeccesary(context);
context.push(prefix, elementName);
this.write(context, "<" + (prefix == null ? elementName : (prefix + ":" + elementName)));
return ELEMENT_WRITER;
}
@Override public void visitEnd(XMLWriterContext context) {
Tag tag = context.getCurrentTag();
if (tag.isStartTagOpen()) {
this.write(context, " />");
} else {
this.write(context, "</" + tag.toString() + ">");
}
context.pop();
}
protected void writeAttribute (XMLWriterContext context, String prefix, String name, String value) {
StringBuilder attribute = new StringBuilder();
if (prefix != null) attribute.append(prefix).append(":");
this.write(context, attribute.append(" ")
.append(name).append("=")
.append("\"").append(value)
.append("\"").toString()
);
}
protected void closeTagIfNeccesary (XMLWriterContext context) {
if (context.getCurrentTag() != null && context.getCurrentTag().isStartTagOpen()) {
this.write(context, ">");
context.getCurrentTag().setStartTagClosed();
}
}
}