// create the sbmlNamespace variable
String sbmlNamespace = JSBML.getNamespaceFrom(sbase.getLevel(), sbase.getVersion());
SMNamespace namespace = element.getContext().getNamespace(sbmlNamespace);
namespace.setPreferredPrefix("");
Annotation annotation = sbase.getAnnotation();
SMOutputElement annotationElement;
String whiteSpaces = createIndentationString(indent);
if (xmlFragment) {
annotationElement = element.addElement("annotation");
} else {
element.addCharacters("\n");
element.setIndentation(whiteSpaces, indent, indentCount);
annotationElement = element.addElement(namespace, "annotation");
}
annotationElement.setIndentation(whiteSpaces, indent, indentCount);
if ((annotation.getNonRDFannotation() != null)
&& (annotation.getNonRDFannotation().length() > 0)) {
StringBuffer annotationBeginning = StringTools.concat(whiteSpaces,
"<annotation");
// Adding the name spaces of the annotation element
Map<String, String> otherNamespaces = annotation
.getAnnotationNamespaces();
for (String namespacePrefix : otherNamespaces.keySet()) {
StringTools.append(annotationBeginning, " ", namespacePrefix,
"=\"", otherNamespaces.get(namespacePrefix), "\"");
annotationElement.addAttribute(namespacePrefix, otherNamespaces.get(namespacePrefix));
}
boolean allNamespacesDefined = true;
// Adding the name spaces of the sbml element
if (sbase.getSBMLDocument() != null) {
Map<String, String> sbmlDocumentNamespaces = sbase.getSBMLDocument().getSBMLDocumentNamespaces();
for (String namespacePrefix : sbmlDocumentNamespaces.keySet()) {
// Checking if the namespace declaration is not done twice
// in the SBMLDocument and the annotation element.
if (otherNamespaces.get(namespacePrefix) == null) {
StringTools.append(annotationBeginning, " ", namespacePrefix,
"=\"", sbmlDocumentNamespaces.get(namespacePrefix), "\"");
}
}
} else {
// Can happen when displaying the annotation from an SBase object
// that is not yet linked to a SBMLDocument.
allNamespacesDefined = false;
}
// TODO: to be able to write broken annotations where the namespace declaration is missing
// we need to add dummy namespace here !! Or remove the namespace awareness on the writer !!
StringTools.append(annotationBeginning, Character.valueOf('>'),
Character.valueOf('\n'), annotation.getNonRDFannotation(),
whiteSpaces, "</annotation>", Character.valueOf('\n'));
DOMConverter converter = new DOMConverter();
String annotationString = annotationBeginning.toString()
.replaceAll("&", "&");
// here indent gets lost.
Document domDocument = null;
boolean domConversionDone = false;
try {
domDocument = JAXPFacade.getInstance().create(
new BufferedReader(new StringReader(annotationString)), allNamespacesDefined);
domConversionDone = true;
} catch (SAXException e) {
e.printStackTrace();
// TODO: log error or send SBMLException
logger.warn("Cannot parse the following XML\n@" + annotationString + "@");
logger.warn("NonRDFannotation =\n@" + annotation.getNonRDFannotation() + "@");
// trying to read the XML without namespace awareness as the XML can be wrong and we still want to
// write it back as it is.
if (allNamespacesDefined) {
try {
domDocument = JAXPFacade.getInstance().create(
new BufferedReader(new StringReader(annotationString)), false);
domConversionDone = true;
} catch (SAXException e2) {
e.printStackTrace();
// TODO: log error or send SBMLException
}
}
} finally {
if (domConversionDone) {
converter.writeFragment(domDocument.getFirstChild()
.getChildNodes(), writer);
}
}
} else {
writer.writeCharacters("\n");
}
// if the given SBase is not a model and the level is smaller than 3,
// no history can be written.
// Annotation cannot be written without metaid tag.
if (sbase.getAnnotation().isSetRDFannotation()) {
if (sbase.isSetMetaId()) {
if (!annotation.isSetAbout()) {
// add required missing tag
annotation.setAbout('#' + sbase.getMetaId());
}
writeRDFAnnotation(annotation, annotationElement, writer, indent
+ indentCount);
} else {
/*