From 54dff2be3b019281eff391ced254c4a6b28dc1cf Mon Sep 17 00:00:00 2001 From: AustinSanders Date: Mon, 1 Mar 2021 09:16:49 -0700 Subject: [PATCH] Removes blob inheritance from OriginalXmlLabel (#4316) * Initial refactor of OriginalLabel class * Addressed PR feedback * Fixed error message * Remove blob inheritance from originalxmllabel * Removed commented / old code * Added byte order specification --- isis/src/base/apps/topds4/topds4.cpp | 3 +- isis/src/base/objs/Cube/Cube.cpp | 30 +++++ isis/src/base/objs/Cube/Cube.h | 3 + .../OriginalXmlLabel/OriginalXmlLabel.cpp | 120 ++++++++---------- .../objs/OriginalXmlLabel/OriginalXmlLabel.h | 11 +- 5 files changed, 92 insertions(+), 75 deletions(-) diff --git a/isis/src/base/apps/topds4/topds4.cpp b/isis/src/base/apps/topds4/topds4.cpp index 58d8bc3691..f4ded56442 100644 --- a/isis/src/base/apps/topds4/topds4.cpp +++ b/isis/src/base/apps/topds4/topds4.cpp @@ -70,8 +70,7 @@ namespace Isis { dataSource["OriginalLabel"].update(pvlToJSON(pvlOrigLabel)); } else if (cubeLabel.hasObject("OriginalXmlLabel")) { - OriginalXmlLabel origXmlBlob; - icube->read(origXmlBlob); + OriginalXmlLabel origXmlBlob = icube->readOriginalXmlLabel(); QDomDocument doc = origXmlBlob.ReturnLabels(); dataSource["OriginalLabel"].update(xmlToJson(doc)); } diff --git a/isis/src/base/objs/Cube/Cube.cpp b/isis/src/base/objs/Cube/Cube.cpp index 4972592eeb..9649a6def3 100644 --- a/isis/src/base/objs/Cube/Cube.cpp +++ b/isis/src/base/objs/Cube/Cube.cpp @@ -32,6 +32,7 @@ find files of those names at the top level of this repository. **/ #include "LineManager.h" #include "Message.h" #include "OriginalLabel.h" +#include "OriginalXmlLabel.h" #include "Preference.h" #include "ProgramLauncher.h" #include "Projection.h" @@ -878,6 +879,23 @@ namespace Isis { return origLabel; } + + /** + * This method will read an OriginalXmlLabel from a cube. + */ + OriginalXmlLabel Cube::readOriginalXmlLabel() const { + Blob origXmlLabelBlob("IsisCube", "OriginalXmlLabel"); + try { + origXmlLabelBlob.Read(fileName()); + } + catch (IException &){ + QString msg = "Unable to locate OriginalXmlLabel in " + fileName(); + throw IException(IException::User, msg, _FILEINFO_); + } + OriginalXmlLabel origXmlLabel(origXmlLabelBlob); + return origXmlLabel; + } + /** * This method will write a blob of data (e.g. History, Table, etc) * to the cube as specified by the contents of the Blob object. @@ -958,6 +976,18 @@ namespace Isis { write(*(lab.toBlob())); } + + /** + * This method will write an OriginalXmlLabel object. + * to the cube as specified by the contents of the Blob object. + * + * @param Original xml label data to be written + */ + void Cube::write(OriginalXmlLabel lab) { + write(*(lab.toBlob())); + } + + void Cube::write(const Table &table) { Blob tableBlob = table.toBlob(); write(tableBlob); diff --git a/isis/src/base/objs/Cube/Cube.h b/isis/src/base/objs/Cube/Cube.h index c80deb34a5..a5674510bf 100644 --- a/isis/src/base/objs/Cube/Cube.h +++ b/isis/src/base/objs/Cube/Cube.h @@ -40,6 +40,7 @@ namespace Isis { class Histogram; class History; class OriginalLabel; + class OriginalXmlLabel; class ImagePolygon; /** @@ -253,11 +254,13 @@ namespace Isis { const std::vector keywords = std::vector()) const; void read(Buffer &rbuf) const; OriginalLabel readOriginalLabel() const; + OriginalXmlLabel readOriginalXmlLabel() const; History readHistory(const QString &name = "IsisCube") const; ImagePolygon readFootprint() const; void write(Blob &blob, bool overwrite=true); void write(const Table &table); void write(OriginalLabel lab); + void write(OriginalXmlLabel lab); void write(Buffer &wbuf); void setBaseMultiplier(double base, double mult); diff --git a/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.cpp b/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.cpp index 2c8873a2d3..7856701a90 100644 --- a/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.cpp +++ b/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.cpp @@ -20,8 +20,7 @@ namespace Isis { /** * Constructors a default OriginalXmlLabel with an empty label. */ - OriginalXmlLabel::OriginalXmlLabel() : Isis::Blob("IsisCube", "OriginalXmlLabel") { - p_blobPvl += Isis::PvlKeyword("ByteOrder", "NULL"); + OriginalXmlLabel::OriginalXmlLabel() { } @@ -30,70 +29,38 @@ namespace Isis { * * @param file Xml file to read labels from */ - OriginalXmlLabel::OriginalXmlLabel(const QString &file) : - Isis::Blob("IsisCube", "OriginalXmlLabel") { - p_blobPvl += Isis::PvlKeyword("ByteOrder", "NULL"); - Blob::Read(file); + OriginalXmlLabel::OriginalXmlLabel(const QString &file) { + Blob blob = Blob("IsisCube", "OriginalXmlLabel"); + blob.Read(file); + fromBlob(blob); } - /** - * Destructor + * Constructs an OriginalXmlLabel from a blob + * + * @param blob Blob from which to create the OriginalXmlLabel */ - OriginalXmlLabel::~OriginalXmlLabel() { + OriginalXmlLabel::OriginalXmlLabel(Isis::Blob &blob) { + fromBlob(blob); } /** - * Read the original label from an Xml file. - * - * @param FileName The Xml file containing the original label. - * - * @throws IException::Io "Could not open label file." - * @throws IException::Unknown "XML read/parse error in file." + * Destructor */ - void OriginalXmlLabel::readFromXmlFile(const FileName &xmlFileName) { - QFile xmlFile(xmlFileName.expanded()); - if ( !xmlFile.open(QIODevice::ReadOnly) ) { - QString msg = "Could not open label file [" + xmlFileName.expanded() + - "]."; - throw IException(IException::Io, msg, _FILEINFO_); - } - - QString errmsg; - int errline, errcol; - if ( !m_originalLabel.setContent(&xmlFile, false, &errmsg, &errline, &errcol) ) { - xmlFile.close(); - QString msg = "XML read/parse error in file [" + xmlFileName.expanded() - + "] at line [" + toString(errline) + "], column [" + toString(errcol) - + "], message: " + errmsg; - throw IException(IException::Unknown, msg, _FILEINFO_); - } - - xmlFile.close(); + OriginalXmlLabel::~OriginalXmlLabel() { } - /** - * Read the xml file data from an input stream. - * - * @param stream The input stream to read from. - * - * @throws IException::Unknown "XML read/parse error when parsing original label." - * - * @see Blob::Read(const Pvl &pvl, std::istream &is) + /* + * Load blob data into m_originalLabel */ - void OriginalXmlLabel::ReadData(std::istream &stream) { - // Use Blob's ReadData to fill p_buffer - Blob::ReadData(stream); - - // Setup variables for error reproting in QT's xml parser + void OriginalXmlLabel::fromBlob(Isis::Blob blob) { QString errorMessage; int errorLine; int errorColumn; - // Attempt to use QT's xml parser to internalize the label - if ( !m_originalLabel.setContent( QByteArray(p_buffer, p_nbytes) ) ) { + if ( !m_originalLabel.setContent( QByteArray(blob.getBuffer(), blob.Size()) ) ) { QString msg = "XML read/parse error when parsing original label. " "Error at line [" + toString(errorLine) + "], column [" + toString(errorColumn) + @@ -103,33 +70,50 @@ namespace Isis { } - /** - * Prepare to write the label out. - * - * @see Blob::Write - */ - void OriginalXmlLabel::WriteInit() { - p_nbytes = m_originalLabel.toByteArray(0).size(); - + Blob *OriginalXmlLabel::toBlob() { + std::stringstream sstream; + sstream << m_originalLabel.toString(); + string orglblStr = sstream.str(); + Isis::Blob *blob = new Blob("IsisCube", "OriginalXmlLabel"); + blob->setData((char*)orglblStr.data(), orglblStr.length()); + blob->Label() += Isis::PvlKeyword("ByteOrder", "NULL"); if (Isis::IsLsb()) { - p_blobPvl["ByteOrder"] = Isis::ByteOrderName(Isis::Lsb); + blob->Label()["ByteOrder"] = Isis::ByteOrderName(Isis::Lsb); } else { - p_blobPvl["ByteOrder"] = Isis::ByteOrderName(Isis::Msb); + blob->Label()["ByteOrder"] = Isis::ByteOrderName(Isis::Msb); } + return blob; } /** - * Write the label out to a stream. - * - * @param os The stream to write the label out to. - * - * @see Blob::Write + * Read the original label from an Xml file. + * + * @param FileName The Xml file containing the original label. + * + * @throws IException::Io "Could not open label file." + * @throws IException::Unknown "XML read/parse error in file." */ - void OriginalXmlLabel::WriteData(std::fstream &os) { - QByteArray labelByteArray = m_originalLabel.toByteArray(0); - os.write( labelByteArray.data(), labelByteArray.size() ); + void OriginalXmlLabel::readFromXmlFile(const FileName &xmlFileName) { + QFile xmlFile(xmlFileName.expanded()); + if ( !xmlFile.open(QIODevice::ReadOnly) ) { + QString msg = "Could not open label file [" + xmlFileName.expanded() + + "]."; + throw IException(IException::Io, msg, _FILEINFO_); + } + + QString errmsg; + int errline, errcol; + if ( !m_originalLabel.setContent(&xmlFile, false, &errmsg, &errline, &errcol) ) { + xmlFile.close(); + QString msg = "XML read/parse error in file [" + xmlFileName.expanded() + + "] at line [" + toString(errline) + "], column [" + toString(errcol) + + "], message: " + errmsg; + throw IException(IException::Unknown, msg, _FILEINFO_); + } + + xmlFile.close(); } @@ -138,7 +122,7 @@ namespace Isis { * * @return @b QDomDocument The parsed original label */ - const QDomDocument &OriginalXmlLabel::ReturnLabels() const { + const QDomDocument &OriginalXmlLabel::ReturnLabels(){ return m_originalLabel; } } diff --git a/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.h b/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.h index de085b07f4..1bf87bb503 100644 --- a/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.h +++ b/isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.h @@ -29,19 +29,21 @@ namespace Isis { * OriginalLabel. Fixes #4584. * */ - class OriginalXmlLabel : public Isis::Blob { + class OriginalXmlLabel { public: OriginalXmlLabel(); OriginalXmlLabel(const QString &file); + OriginalXmlLabel(Blob &blob); ~OriginalXmlLabel(); + Blob *toBlob(); + + void fromBlob(Isis::Blob blob); void readFromXmlFile(const FileName &xmlFileName); - const QDomDocument &ReturnLabels() const; + const QDomDocument &ReturnLabels(); protected: void ReadData(std::istream &stream); - void WriteData(std::fstream &os); - void WriteInit(); private: QDomDocument m_originalLabel; //!< Original Xml Label. @@ -49,4 +51,3 @@ namespace Isis { }; #endif -