Skip to content

Commit

Permalink
Removes blob inheritance from OriginalXmlLabel (#4316)
Browse files Browse the repository at this point in the history
* Initial refactor of OriginalLabel class

* Addressed PR feedback

* Fixed error message

* Remove blob inheritance from originalxmllabel

* Removed commented / old code

* Added byte order specification
  • Loading branch information
AustinSanders authored Mar 1, 2021
1 parent 0138808 commit 54dff2b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 75 deletions.
3 changes: 1 addition & 2 deletions isis/src/base/apps/topds4/topds4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
30 changes: 30 additions & 0 deletions isis/src/base/objs/Cube/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions isis/src/base/objs/Cube/Cube.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace Isis {
class Histogram;
class History;
class OriginalLabel;
class OriginalXmlLabel;
class ImagePolygon;

/**
Expand Down Expand Up @@ -253,11 +254,13 @@ namespace Isis {
const std::vector<PvlKeyword> keywords = std::vector<PvlKeyword>()) 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);
Expand Down
120 changes: 52 additions & 68 deletions isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}


Expand All @@ -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) +
Expand All @@ -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();
}


Expand All @@ -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;
}
}
11 changes: 6 additions & 5 deletions isis/src/base/objs/OriginalXmlLabel/OriginalXmlLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@ 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.
};
};

#endif

0 comments on commit 54dff2b

Please sign in to comment.