/* * Writes the given Sed document to the output stream. * * @return true on success and false if one of the underlying parser * components fail (rare). */ bool SedWriter::writeSedML (const SedDocument* d, std::ostream& stream) { bool result = false; try { stream.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit); XMLOutputStream xos(stream, "UTF-8", true, mProgramName, mProgramVersion); d->write(xos); stream << endl; result = true; } catch (ios_base::failure&) { SedErrorLog *log = (const_cast<SedDocument *>(d))->getErrorLog(); log->logError(XMLFileOperationError); } return result; }
/* * Reads the expected attributes into the member data variables */ void SedRepeatedTask::readAttributes( const LIBSBML_CPP_NAMESPACE_QUALIFIER XMLAttributes& attributes, const LIBSBML_CPP_NAMESPACE_QUALIFIER ExpectedAttributes& expectedAttributes) { unsigned int level = getLevel(); unsigned int version = getVersion(); unsigned int numErrs; bool assigned = false; SedErrorLog* log = getErrorLog(); SedTask::readAttributes(attributes, expectedAttributes); numErrs = log->getNumErrors(); for (int n = numErrs-1; n >= 0; n--) { if (log->getError(n)->getErrorId() == SedUnknownCoreAttribute) { const std::string details = log->getError(n)->getMessage(); log->remove(SedUnknownCoreAttribute); log->logError(SedmlSedRepeatedTaskAllowedAttributes, level, version, details); } } // // range SIdRef (use = "optional" ) // assigned = attributes.readInto("range", mRangeId); if (assigned == true) { if (mRangeId.empty() == true) { logEmptyString(mRangeId, level, version, "<SedRepeatedTask>"); } else if (SyntaxChecker::isValidSBMLSId(mRangeId) == false) { logError(SedmlRepeatedTaskRangeIdMustBeRange, level, version, "The " "attribute range='" + mRangeId + "' does not conform to the syntax."); } } // // resetModel bool (use = "optional" ) // mIsSetResetModel = attributes.readInto("resetModel", mResetModel); }
/* * Writes the given Sed document to filename. * * If the filename ends with @em .gz, the file will be compressed by @em gzip. * Similary, if the filename ends with @em .zip or @em .bz2, the file will be * compressed by @em zip or @em bzip2, respectively. Otherwise, the fill will be * uncompressed. * If the filename ends with @em .zip, a filename that will be added to the * zip archive file will end with @em .xml or @em .sbml. For example, the filename * in the zip archive will be @em test.xml if the given filename is @em test.xml.zip * or @em test.zip. Also, the filename in the archive will be @em test.sbml if the * given filename is @em test.sbml.zip. * * @note To create a gzip/zip file, underlying libSed needs to be linked with zlib at * compile time. Also, underlying libSed needs to be linked with bzip2 to create a * bzip2 file. * File unwritable error will be logged and @c false will be returned if a compressed * file name is given and underlying libSed is not linked with the corresponding * required library. * SedWriter::hasZlib() and SedWriter::hasBzip2() can be used to check whether * underlying libSed is linked with the library. * * @return true on success and false if the filename could not be opened * for writing. */ bool SedWriter::writeSedML (const SedDocument* d, const std::string& filename) { std::ostream* stream = NULL; try { // open an uncompressed XML file. if ( string::npos != filename.find(".xml", filename.length() - 4) ) { stream = new(std::nothrow) std::ofstream(filename.c_str()); } // open a gzip file else if ( string::npos != filename.find(".gz", filename.length() - 3) ) { stream = OutputCompressor::openGzipOStream(filename); } // open a bz2 file else if ( string::npos != filename.find(".bz2", filename.length() - 4) ) { stream = OutputCompressor::openBzip2OStream(filename); } // open a zip file else if ( string::npos != filename.find(".zip", filename.length() - 4) ) { std::string filenameinzip = filename.substr(0, filename.length() - 4); if ( ( string::npos == filenameinzip.find(".xml", filenameinzip.length() - 4) ) && ( string::npos == filenameinzip.find(".sbml", filenameinzip.length() - 5) ) ) { filenameinzip += ".xml"; } #if defined(WIN32) && !defined(CYGWIN) char sepr = '\\'; #else char sepr = '/'; #endif size_t spos = filenameinzip.rfind(sepr, filenameinzip.length() - 1); if( spos != string::npos ) { filenameinzip = filenameinzip.substr(spos + 1, filenameinzip.length() - 1); } stream = OutputCompressor::openZipOStream(filename, filenameinzip); } else { stream = new(std::nothrow) std::ofstream(filename.c_str()); } } catch ( ZlibNotLinked& ) { // libSed is not linked with zlib. XMLErrorLog *log = (const_cast<SedDocument *>(d))->getErrorLog(); std::ostringstream oss; oss << "Tried to write " << filename << ". Writing a gzip/zip file is not enabled because " << "underlying libSed is not linked with zlib."; log->add(XMLError( XMLFileUnwritable, oss.str(), 0, 0) ); return false; } catch ( Bzip2NotLinked& ) { // libSed is not linked with bzip2. XMLErrorLog *log = (const_cast<SedDocument *>(d))->getErrorLog(); std::ostringstream oss; oss << "Tried to write " << filename << ". Writing a bzip2 file is not enabled because " << "underlying libSed is not linked with bzip2."; log->add(XMLError( XMLFileUnwritable, oss.str(), 0, 0) ); return false; } if ( stream == NULL || stream->fail() || stream->bad()) { SedErrorLog *log = (const_cast<SedDocument *>(d))->getErrorLog(); log->logError(XMLFileUnwritable); return false; } bool result = writeSedML(d, *stream); delete stream; return result; }
/* * Reads the expected attributes into the member data variables */ void SedSimulation::readAttributes( const LIBSBML_CPP_NAMESPACE_QUALIFIER XMLAttributes& attributes, const LIBSBML_CPP_NAMESPACE_QUALIFIER ExpectedAttributes& expectedAttributes) { unsigned int level = getLevel(); unsigned int version = getVersion(); unsigned int numErrs; bool assigned = false; SedErrorLog* log = getErrorLog(); if (static_cast<SedListOfSimulations*>(getParentSedObject())->size() < 2) { numErrs = log->getNumErrors(); for (int n = numErrs-1; n >= 0; n--) { if (log->getError(n)->getErrorId() == SedUnknownCoreAttribute) { const std::string details = log->getError(n)->getMessage(); log->remove(SedUnknownCoreAttribute); log->logError(SedmlLOSimulationsAllowedCoreAttributes, level, version, details); } } } SedBase::readAttributes(attributes, expectedAttributes); numErrs = log->getNumErrors(); for (int n = numErrs-1; n >= 0; n--) { if (log->getError(n)->getErrorId() == SedUnknownCoreAttribute) { const std::string details = log->getError(n)->getMessage(); log->remove(SedUnknownCoreAttribute); log->logError(SedmlSedSimulationAllowedAttributes, level, version, details); } } // // id SId (use = "required" ) // assigned = attributes.readInto("id", mId); if (assigned == true) { if (mId.empty() == true) { logEmptyString(mId, level, version, "<SedSimulation>"); } else if (SyntaxChecker::isValidSBMLSId(mId) == false) { logError(SedmlIdSyntaxRule, level, version, "The id '" + mId + "' does " "not conform to the syntax."); } } else { std::string message = "Sedml attribute 'id' is missing from the " "<SedSimulation> element."; log->logError(SedmlSimulationAllowedAttributes, level, version, message); } // // name string (use = "optional" ) // assigned = attributes.readInto("name", mName); if (assigned == true) { if (mName.empty() == true) { logEmptyString(mName, level, version, "<SedSimulation>"); } } }
/* * Reads the expected attributes into the member data variables */ void SedSetValue::readAttributes( const LIBSBML_CPP_NAMESPACE_QUALIFIER XMLAttributes& attributes, const LIBSBML_CPP_NAMESPACE_QUALIFIER ExpectedAttributes& expectedAttributes) { unsigned int level = getLevel(); unsigned int version = getVersion(); unsigned int numErrs; bool assigned = false; SedErrorLog* log = getErrorLog(); if (static_cast<SedListOfSetValues*>(getParentSedObject())->size() < 2) { numErrs = log->getNumErrors(); for (int n = numErrs-1; n >= 0; n--) { if (log->getError(n)->getErrorId() == SedUnknownCoreAttribute) { const std::string details = log->getError(n)->getMessage(); log->remove(SedUnknownCoreAttribute); log->logError(SedmlLOSetValuesAllowedCoreAttributes, level, version, details); } } } SedBase::readAttributes(attributes, expectedAttributes); numErrs = log->getNumErrors(); for (int n = numErrs-1; n >= 0; n--) { if (log->getError(n)->getErrorId() == SedUnknownCoreAttribute) { const std::string details = log->getError(n)->getMessage(); log->remove(SedUnknownCoreAttribute); log->logError(SedmlSedSetValueAllowedAttributes, level, version, details); } } // // modelReference SIdRef (use = "required" ) // assigned = attributes.readInto("modelReference", mModelReference); if (assigned == true) { if (mModelReference.empty() == true) { logEmptyString(mModelReference, level, version, "<SedSetValue>"); } else if (SyntaxChecker::isValidSBMLSId(mModelReference) == false) { logError(SedmlSetValueModelReferenceMustBeModel, level, version, "The " "attribute modelReference='" + mModelReference + "' does not conform to " "the syntax."); } } else { std::string message = "Sedml attribute 'modelReference' is missing from the " "<SedSetValue> element."; log->logError(SedmlSetValueAllowedAttributes, level, version, message); } // // symbol string (use = "optional" ) // assigned = attributes.readInto("symbol", mSymbol); if (assigned == true) { if (mSymbol.empty() == true) { logEmptyString(mSymbol, level, version, "<SedSetValue>"); } } // // target string (use = "optional" ) // assigned = attributes.readInto("target", mTarget); if (assigned == true) { if (mTarget.empty() == true) { logEmptyString(mTarget, level, version, "<SedSetValue>"); } } // // range SIdRef (use = "optional" ) // assigned = attributes.readInto("range", mRange); if (assigned == true) { if (mRange.empty() == true) { logEmptyString(mRange, level, version, "<SedSetValue>"); } else if (SyntaxChecker::isValidSBMLSId(mRange) == false) { logError(SedmlSetValueRangeMustBeRange, level, version, "The attribute " "range='" + mRange + "' does not conform to the syntax."); } } }