System.setProperty("javax.xml.stream.XMLEventFactory", "com.ctc.wstx.stax.WstxEventFactory");
initializePackageParsers();
SMOutputFactory smFactory = new SMOutputFactory(XMLOutputFactory.newInstance());
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 %s version %s on %s with jsbml version %s. ";
outputDocument.addComment(
String.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());
}