package org.xins.common.xml;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.Utils;
import org.znerd.xmlenc.XMLOutputter;
@Deprecated
public final class ElementSerializer {
private final Object _lock;
private boolean _inUse;
public ElementSerializer() {
_lock = new Object();
}
public String serialize(Element element)
throws IllegalArgumentException {
synchronized (_lock) {
if (_inUse) {
String detail = "ElementSerializer instance already in use.";
throw Utils.logProgrammingError(detail);
}
_inUse = true;
}
MandatoryArgumentChecker.check("element", element);
Writer fsw = new StringWriter(512);
XMLOutputter out;
final String ENCODING = "UTF-8";
try {
out = new XMLOutputter(fsw, ENCODING);
} catch (UnsupportedEncodingException uee) {
String message = "Expected XMLOutputter to support encoding \"" + ENCODING + "\".";
throw Utils.logProgrammingError(message, uee);
}
try {
output(out, element);
} catch (IOException exception) {
throw Utils.logProgrammingError(exception);
} finally {
_inUse = false;
}
String xml = fsw.toString();
return xml;
}
public void output(XMLOutputter out, Element element)
throws NullPointerException, IOException {
String namespacePrefix = element.getNamespacePrefix();
String namespaceURI = element.getNamespaceURI();
String localName = element.getLocalName();
Map namespaces = new HashMap();
if (namespacePrefix != null) {
out.startTag(namespacePrefix + ':' + localName);
} else {
out.startTag(localName);
}
if (namespaceURI != null) {
if (namespacePrefix == null) {
out.attribute("xmlns", namespaceURI);
namespaces.put("", namespaceURI);
} else {
out.attribute("xmlns:" + namespacePrefix, namespaceURI);
namespaces.put(namespacePrefix, namespaceURI);
}
}
Map attributes = element.getAttributeMap();
Iterator entries = attributes.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Element.QualifiedName qn = (Element.QualifiedName) entry.getKey();
String attrNamespaceURI = qn.getNamespaceURI();
String attrLocalName = qn.getLocalName();
String attrNamespacePrefix = qn.getNamespacePrefix();
String attrValue = (String) entry.getValue();
if (attrValue != null &&
(!"xmlns".equals(attrNamespacePrefix) || !attrLocalName.equals(namespacePrefix))) {
if (attrNamespacePrefix != null) {
out.attribute(attrNamespacePrefix + ':' + attrLocalName, attrValue);
} else {
out.attribute(attrLocalName, attrValue);
}
if (attrNamespaceURI != null) {
if (attrNamespacePrefix == null && !namespaces.containsKey("")) {
out.attribute("xmlns", attrNamespaceURI);
namespaces.put("", namespaceURI);
} else if (!namespaces.containsKey(attrNamespacePrefix)) {
out.attribute("xmlns:" + attrNamespacePrefix, attrNamespaceURI);
namespaces.put(attrNamespacePrefix, namespaceURI);
}
}
}
}
List content = element.getChildElements();
int count = content.size();
for (int i = 0; i < count; i++) {
Object o = content.get(i);
output(out, (Element) o);
}
if (element.getText() != null) {
out.pcdata(element.getText());
}
out.endTag();
}
}