XMLStreamWriter2 streamWriter = smFactory.createStax2Writer(stream);
// For this to work, the elements need to be completely empty (no whitespace or line return)
streamWriter.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS, Boolean.TRUE);
SMOutputDocument outputDocument = SMOutputFactory.createOutputDocument(
streamWriter, "1.0", "UTF-8", false);
// to have the automatic indentation working, we should probably only be using StaxMate classes and not directly StAX
// outputDocument.setIndentation("\n ", 1, 1);
String SBMLNamespace = JSBML.getNamespaceFrom(sbmlDocument.getLevel(),
sbmlDocument.getVersion());
SMOutputContext context = outputDocument.getContext();
context.setIndentation("\n" + createIndentationString(indentCount), 1, 2);
SMNamespace namespace = context.getNamespace(SBMLNamespace);
namespace.setPreferredPrefix("");
outputDocument.addCharacters("\n");
/*
* Write a comment to track which program created this SBML file and
* which version of JSBML was used for this purpose.
*/
if ((programName != null) && (programName.length() > 0)) {
String date = String.format("%1$tY-%1$tm-%1$td %1$tR", Calendar.getInstance().getTime());
String msg = " Created by {0} version {1} on {2} with JSBML version {3}. ";
outputDocument.addComment(
MessageFormat.format(msg, programName, (programVersion != null)
&& (programVersion.length() > 0) ? programVersion
: "?", date, JSBML.getJSBMLDottedVersion()));
outputDocument.addCharacters("\n");
}
SMOutputElement smOutputElement = outputDocument.addElement(namespace,
sbmlDocument.getElementName());
SBMLObjectForXML xmlObject = new SBMLObjectForXML();
xmlObject.setName(sbmlDocument.getElementName());
xmlObject.setNamespace(SBMLNamespace);
xmlObject.addAttributes(sbmlDocument.writeXMLAttributes());
// register all the name spaces of the SBMLDocument to the writer
Iterator<Map.Entry<String, String>> it = sbmlDocument
.getSBMLDocumentNamespaces().entrySet().iterator();
logger.debug(" SBML name spaces size = "
+ sbmlDocument.getSBMLDocumentNamespaces().size());
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
if (!entry.getKey().equals("xmlns")) {
logger.debug(" SBML name spaces: " + entry.getKey() + " = "
+ entry.getValue());
String namespacePrefix = entry.getKey().substring(
entry.getKey().indexOf(":") + 1);
streamWriter.setPrefix(namespacePrefix, entry.getValue());
logger.debug(" SBML namespaces: " + namespacePrefix + " = "
+ entry.getValue());
}
}
if (sbmlDocument.getDeclaredNamespaces().size() > 0) {
logger.debug(" SBML declared namespaces size = "
+ sbmlDocument.getDeclaredNamespaces().size());
xmlObject.addAttributes(sbmlDocument.getDeclaredNamespaces());
for (String prefix : sbmlDocument.getDeclaredNamespaces().keySet()) {
if (!prefix.equals("xmlns")) {
String namespaceURI = sbmlDocument.getDeclaredNamespaces().get(prefix);
logger.debug(" SBML name spaces: " + prefix + " = " + namespaceURI);
String namespacePrefix = prefix.substring(prefix.indexOf(":") + 1);
streamWriter.setPrefix(namespacePrefix, namespaceURI);
logger.debug(" SBML namespaces: " + namespacePrefix + " = " + namespaceURI);
}
}
}
it = xmlObject.getAttributes().entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
smOutputElement.addAttribute(entry.getKey(), entry.getValue());
}
int indent = indentCount;
if (sbmlDocument.isSetNotes()) {
writeNotes(sbmlDocument, smOutputElement, streamWriter,
SBMLNamespace, indent);
}
if (sbmlDocument.isSetAnnotation()) {
writeAnnotation(sbmlDocument, smOutputElement, streamWriter,
indent, false);
}
smOutputElement.addCharacters("\n");
writeSBMLElements(xmlObject, smOutputElement, streamWriter,
sbmlDocument, indent);
outputDocument.closeRoot();
}