Skip to content

Commit

Permalink
#296: add overload for setAnnotation that does parses rdf conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergmann committed Mar 16, 2023
1 parent 12a8bcf commit 239205b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 12 deletions.
40 changes: 30 additions & 10 deletions src/sbml/SBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,11 +1249,23 @@ SBase::setIdAttribute (const std::string& sid)
}
}

int
SBase::setAnnotation(const XMLNode* annotation)
{
return SBase::setAnnotation(annotation, true);
}

int
SBase::setAnnotation(const std::string& annotation)
{
return SBase::setAnnotation(annotation, true);
}

/*
* Sets the annotation of this SBML object to a copy of annotation.
*/
int
SBase::setAnnotation (const XMLNode* annotation)
SBase::setAnnotation (const XMLNode* annotation, bool parseRdf)
{
//
// (*NOTICE*)
Expand All @@ -1274,7 +1286,7 @@ SBase::setAnnotation (const XMLNode* annotation)
delete mAnnotation;

// the annotation is an rdf annotation but the object has no metaid
if (RDFAnnotationParser::hasRDFAnnotation(annotation) == true
if (parseRdf && RDFAnnotationParser::hasRDFAnnotation(annotation) == true
&& (RDFAnnotationParser::hasCVTermRDFAnnotation(annotation) == true
|| RDFAnnotationParser::hasHistoryRDFAnnotation(annotation) == true)
&& isSetMetaId() == false)
Expand Down Expand Up @@ -1342,7 +1354,7 @@ SBase::setAnnotation (const XMLNode* annotation)
}


if(mAnnotation != NULL
if(parseRdf && mAnnotation != NULL
&& RDFAnnotationParser::hasCVTermRDFAnnotation(mAnnotation))
{
// parse mAnnotation (if any) and set mCVTerms
Expand All @@ -1351,7 +1363,7 @@ SBase::setAnnotation (const XMLNode* annotation)
mCVTermsChanged = true;
}

if(getLevel() > 2 && mAnnotation != NULL
if(parseRdf && getLevel() > 2 && mAnnotation != NULL
&& RDFAnnotationParser::hasHistoryRDFAnnotation(mAnnotation))
{
// parse mAnnotation (if any) and set mHistory
Expand All @@ -1371,7 +1383,7 @@ SBase::setAnnotation (const XMLNode* annotation)
* Sets the annotation (by string) of this SBML object to a copy of annotation.
*/
int
SBase::setAnnotation (const std::string& annotation)
SBase::setAnnotation (const std::string& annotation, bool parseRdf)
{

int success = LIBSBML_OPERATION_FAILED;
Expand Down Expand Up @@ -1404,7 +1416,7 @@ SBase::setAnnotation (const std::string& annotation)

if(annt_xmln != NULL)
{
success = setAnnotation(annt_xmln);
success = setAnnotation(annt_xmln, parseRdf);
delete annt_xmln;
}
return success;
Expand Down Expand Up @@ -4272,13 +4284,21 @@ SBase::getSBMLNamespaces() const
*/
char*
SBase::toSBML ()
{
return safe_strdup( toSBMLString().c_str() );
}

/*
* @return the partial SBML that describes this SBML object.
*/
std::string SBase::toSBMLString()
{
ostringstream os;
XMLOutputStream stream(os, "UTF-8", false);
ostringstream os;
XMLOutputStream stream(os, "UTF-8", false);

write(stream);
write(stream);

return safe_strdup( os.str().c_str() );
return os.str();
}


Expand Down
115 changes: 113 additions & 2 deletions src/sbml/SBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,54 @@ class LIBSBML_EXTERN SBase
* @see unsetAnnotation()
*/
virtual int setAnnotation (const XMLNode* annotation);

/**
* Sets the value of the "annotation" subelement of this SBML object.
*
* The content of @p annotation is copied, and any previous content of
* this object's "annotation" subelement is deleted.
*
* Whereas the SBase "notes" subelement is a container for content to be
* shown directly to humans, the "annotation" element is a container for
* optional software-generated content @em not meant to be shown to
* humans. Every object derived from SBase can have its own value for
* "annotation". The element's content type is <a target="_blank"
* href="http://www.w3.org/TR/2004/REC-xml-20040204/#elemdecls">XML type
* "any"</a>, allowing essentially arbitrary well-formed XML data
* content.
*
* SBML places a few restrictions on the organization of the content of
* annotations; these are intended to help software tools read and write
* the data as well as help reduce conflicts between annotations added by
* different tools. Please see the SBML specifications for more details.
*
* Call this method will result in any existing content of the
* "annotation" subelement to be discarded. Unless you have taken steps
* to first copy and reconstitute any existing annotations into the @p
* annotation that is about to be assigned, it is likely that performing
* such wholesale replacement is unfriendly towards other software
* applications whose annotations are discarded. An alternative may be
* to use SBase::appendAnnotation(const XMLNode* annotation) or
* SBase::appendAnnotation(const std::string& annotation).
*
* @param annotation an XML structure that is to be used as the new content
* of the "annotation" subelement of this object.
*
* @param parseRdf if true, the annotation will be parsed for RDF content.
* This should only be set, if the RDF follows the subset described in the
* SBML specification.
*
* @copydetails doc_returns_one_success_code
* @li @sbmlconstant{LIBSBML_OPERATION_SUCCESS, OperationReturnValues_t}
*
* @see getAnnotationString()
* @see isSetAnnotation()
* @see setAnnotation(const std::string& annotation)
* @see appendAnnotation(const XMLNode* annotation)
* @see appendAnnotation(const std::string& annotation)
* @see unsetAnnotation()
*/
virtual int setAnnotation(const XMLNode* annotation, bool parseRdf);


/**
Expand Down Expand Up @@ -1262,6 +1310,55 @@ class LIBSBML_EXTERN SBase
*
* @param annotation an XML string that is to be used as the content
* of the "annotation" subelement of this object.
*
* @copydetails doc_returns_success_code
* @li @sbmlconstant{LIBSBML_OPERATION_SUCCESS, OperationReturnValues_t}
* @li @sbmlconstant{LIBSBML_OPERATION_FAILED, OperationReturnValues_t}
*
* @see getAnnotationString()
* @see isSetAnnotation()
* @see setAnnotation(const XMLNode* annotation)
* @see appendAnnotation(const XMLNode* annotation)
* @see appendAnnotation(const std::string& annotation)
* @see unsetAnnotation()
*/
virtual int setAnnotation(const std::string& annotation);

/**
* Sets the value of the "annotation" subelement of this SBML object.
*
* The content of @p annotation is copied, and any previous content of
* this object's "annotation" subelement is deleted.
*
* Whereas the SBase "notes" subelement is a container for content to be
* shown directly to humans, the "annotation" element is a container for
* optional software-generated content @em not meant to be shown to
* humans. Every object derived from SBase can have its own value for
* "annotation". The element's content type is <a target="_blank"
* href="http://www.w3.org/TR/2004/REC-xml-20040204/#elemdecls">XML type
* "any"</a>, allowing essentially arbitrary well-formed XML data
* content.
*
* SBML places a few restrictions on the organization of the content of
* annotations; these are intended to help software tools read and write
* the data as well as help reduce conflicts between annotations added by
* different tools. Please see the SBML specifications for more details.
*
* Call this method will result in any existing content of the
* "annotation" subelement to be discarded. Unless you have taken steps
* to first copy and reconstitute any existing annotations into the @p
* annotation that is about to be assigned, it is likely that performing
* such wholesale replacement is unfriendly towards other software
* applications whose annotations are discarded. An alternative may be
* to use SBase::appendAnnotation(const XMLNode* annotation) or
* SBase::appendAnnotation(const std::string& annotation).
*
* @param annotation an XML string that is to be used as the content
* of the "annotation" subelement of this object.
*
* @param parseRdf if true, the annotation will be parsed for RDF content.
* This should only be set, if the RDF follows the subset described in the
* SBML specification.
*
* @copydetails doc_returns_success_code
* @li @sbmlconstant{LIBSBML_OPERATION_SUCCESS, OperationReturnValues_t}
Expand All @@ -1274,7 +1371,7 @@ class LIBSBML_EXTERN SBase
* @see appendAnnotation(const std::string& annotation)
* @see unsetAnnotation()
*/
virtual int setAnnotation (const std::string& annotation);
virtual int setAnnotation (const std::string& annotation, bool parseRdf);


/**
Expand Down Expand Up @@ -2313,7 +2410,9 @@ s.setNotes("<body xmlns='http://www.w3.org/1999/xhtml'><p>here is my note</p></b

/**
* Returns a string consisting of a partial SBML corresponding to just
* this object.
* this object.
*
* The string is owned by the caller and has to be freed manualy.
*
* @return the partial SBML that describes this SBML object.
*
Expand All @@ -2323,6 +2422,18 @@ s.setNotes("<body xmlns='http://www.w3.org/1999/xhtml'><p>here is my note</p></b
*/
char* toSBML();

/**
* Returns a string consisting of a partial SBML corresponding to just
* this object.
*
* @return the partial SBML that describes this SBML object.
*
* @warning <span class="warning">This is primarily provided for testing
* and debugging purposes. It may be removed in a future version of
* libSBML.</span>
*/
std::string toSBMLString();


/**
* Returns this element as an XMLNode.
Expand Down

0 comments on commit 239205b

Please sign in to comment.