storage::VVEStorage_XMLReader Class Reference

Implementation reading an XML file. More...

#include <storage/storage.h>

Inheritance diagram for storage::VVEStorage_XMLReader:
Inheritance graph
[legend]

List of all members.

Public Types

typedef ptrdiff_t reference_t
 Type of a reference identifier.

Public Member Functions

virtual bool checkNextField (const QString &name)
 Test if there is a field with matching name and type.
virtual bool endCompound ()
 End the current compound.
virtual bool endReference ()
 Stop the current reference.
virtual QString filename () const
 Name of the file being processed.
virtual bool reader ()
virtual bool serialize (const QString &filename, Model *model)
 Actually read the file.
virtual void setLastError (int error, const QString &err)
 Change the text of the last error.
virtual bool setOption (int option, int value)
 Change an option.
virtual bool startCompound (const QString &name)
 Start a new compound.
virtual int startReference (const QString &name, const QString &ref_type, reference_t ref)
 Start a reference.
virtual bool writer ()

Protected Member Functions

template<typename From , typename To >
bool extract_value_with_error (QDomElement element, To &result)
QDomElement extractElement (const QString &name)
template<typename T >
bool get_field (const QString &name, T &result)
void setError_invalid_conversion (const QString &name, const QString &value, const QString &type_from, const QString &type_to)
void setError_invalid_value (const QString &name, const QString &type, const QString &value)
void setError_novalue (const QString &name)
bool validType (QDomElement element, TYPES type)

Protected Attributes

QString _filename
std::list< QDomElementcompound_stack
QDomDocument doc
QDomElement root
int type_checking

Detailed Description

Implementation reading an XML file.

Definition at line 87 of file storage_xml.h.


Member Typedef Documentation

Type of a reference identifier.

Reimplemented from storage::VVEStorage.

Definition at line 91 of file storage_xml.h.


Member Function Documentation

bool storage::VVEStorage_XMLReader::checkNextField ( const QString name  )  [virtual]

Test if there is a field with matching name and type.

For writer, this should always return False.

Implements storage::VVEStorage.

Definition at line 395 of file storage_xml.cpp.

00396   {
00397     QDomElement last = compound_stack.back();
00398     QDomElement child = last.firstChildElement(name);
00399     if(child.isNull())
00400       return false;
00401     return true;
00402   }

bool storage::VVEStorage_XMLReader::endCompound (  )  [virtual]

End the current compound.

Implements storage::VVEStorage.

Definition at line 416 of file storage_xml.cpp.

References storage::NO_FIELD_ERROR, and setLastError().

00417   {
00418     if(compound_stack.empty())
00419     {
00420       setLastError(NO_FIELD_ERROR, "Trying to end a compound not started");
00421       return false;
00422     }
00423     compound_stack.pop_back();
00424     return true;
00425   }

bool storage::VVEStorage_XMLReader::endReference (  )  [virtual]

Stop the current reference.

Implements storage::VVEStorage.

Definition at line 460 of file storage_xml.cpp.

References setLastError(), and storage::USER_ERROR.

00461   {
00462     if(compound_stack.empty())
00463     {
00464       setLastError(USER_ERROR, "Trying to close a reference that wasn't opened.");
00465       return false;
00466     }
00467     compound_stack.pop_back();
00468     return true;
00469   }

virtual QString storage::VVEStorage_XMLReader::filename (  )  const [inline, virtual]

Name of the file being processed.

Implements storage::VVEStorage.

Definition at line 120 of file storage_xml.h.

00120 { return _filename; }

virtual bool storage::VVEStorage_XMLReader::reader (  )  [inline, virtual]
Returns:
True is the object is a storage reader

Implements storage::VVEStorage.

Definition at line 109 of file storage_xml.h.

00109 { return true; }

bool storage::VVEStorage_XMLReader::serialize ( const QString filename,
Model model 
) [virtual]

Actually read the file.

Implements storage::VVEStorage.

Definition at line 198 of file storage_xml.cpp.

References QString::arg(), QDomElement::attribute(), storage::BAD_CONTENT, QFile::close(), storage::IO_ERROR, storage::NO__ERROR, QFile::open(), Model::serialize(), QDomDocument::setContent(), setLastError(), and Model::versionNumber().

00199   {
00200     _filename = filename;
00201     doc = QDomDocument("VVEStorage");
00202     compound_stack.clear();
00203 
00204     last_error = NO__ERROR;
00205     last_error_string = QString();
00206 
00207     QFile file(filename);
00208     if(!file.open(QIODevice::ReadOnly))
00209     {
00210       setLastError(IO_ERROR, QString("Could not open file '%1' for reading").arg(filename));
00211       return false;
00212     }
00213     if(!doc.setContent(&file))
00214     {
00215       file.close();
00216       setLastError(BAD_CONTENT, QString("File '%1' : Content is not valid XML.").arg(filename));
00217       return false;
00218     }
00219 
00220     root = doc.firstChildElement("VVEStorage");
00221     if(root.isNull())
00222     {
00223       setLastError(BAD_CONTENT, QString("File '%1' has no root element").arg(filename));
00224       return false;
00225     }
00226     file_version = QString("%1 XML").arg(root.attribute("version", ""));
00227     version_number = model->versionNumber(file_version);
00228     compound_stack.push_back(root);
00229     bool result = model->serialize(*this);
00230     _filename = QString();
00231     return result;
00232   }

void storage::VVEStorage_XMLReader::setLastError ( int  error,
const QString error_str 
) [virtual]

Change the text of the last error.

Any specific implementation might alter the text.

Reimplemented from storage::VVEStorage.

Definition at line 234 of file storage_xml.cpp.

References QString::arg(), forall, and QDomElement::tagName().

Referenced by endCompound(), endReference(), serialize(), startCompound(), and startReference().

00235   {
00236     last_error = error;
00237     last_error_string = err;
00238     last_error_string += "\nStack of elements:";
00239     QDomElement last;
00240     forall(const QDomElement& el, compound_stack)
00241     {
00242       if(el != last)
00243         last_error_string += QString("\n<%1>").arg(el.tagName());
00244       last = el;
00245     }
00246   }

bool storage::VVEStorage_XMLReader::setOption ( int  ,
int   
) [virtual]

Change an option.

The options are implementation-dependent. If the option is not supported, the function returns 0.

Reimplemented from storage::VVEStorage.

Definition at line 270 of file storage_xml.cpp.

References storage::TCO_Exact, storage::TCO_NoCheck, storage::TCO_Permissive, storage::TCO_PermissiveNoWarning, storage::TCO_Strict, and storage::TypeChecking.

00271   {
00272     switch(option)
00273     {
00274       case TypeChecking:
00275         switch(value)
00276         {
00277           case TCO_Exact:
00278           case TCO_Strict:
00279           case TCO_Permissive:
00280           case TCO_PermissiveNoWarning:
00281           case TCO_NoCheck:
00282             type_checking = value;
00283             return true;
00284           default:
00285             return false;
00286         }
00287         break;
00288       default:
00289         return false;
00290     }
00291   }

bool storage::VVEStorage_XMLReader::startCompound ( const QString name  )  [virtual]

Start a new compound.

Useful to organize complicated data structure

When writing, always returns true. When reading, returns true only if the compound exists.

Implements storage::VVEStorage.

Definition at line 404 of file storage_xml.cpp.

References storage::NO_FIELD_ERROR, and setLastError().

00405   {
00406     QDomElement new_compound = extractElement(name);
00407     if(new_compound.isNull())
00408     {
00409       setLastError(NO_FIELD_ERROR, QString("Compound '%1' not found").arg(name));
00410       return false;
00411     }
00412     compound_stack.push_back(new_compound);
00413     return true;
00414   }

int storage::VVEStorage_XMLReader::startReference ( const QString name,
const QString ref_type,
reference_t  ref 
) [virtual]

Start a reference.

All operations preceding the call to stopReference should be stored in a section reserved for references.

References should be loaded only on need.

When writing, always returns true. When reading, returns true only if the reference exists. If the reference does not exist, it will create it, so the reader has to set the fields.

Returns:
the reference identifier or -1 if no reference is available.

Implements storage::VVEStorage.

Definition at line 427 of file storage_xml.cpp.

References QDomElement::attribute(), QString::isEmpty(), storage::NO_FIELD_ERROR, storage::REFERENCE_ERROR, and setLastError().

00428   {
00429     QDomElement place_holder = extractElement(name);
00430     if(place_holder.isNull())
00431     {
00432       setLastError(NO_FIELD_ERROR, QString("Reference '%1' not found").arg(name));
00433       return -1;
00434     }
00435 
00436     QString type = place_holder.attribute("ref_type");
00437     if(type.isEmpty())
00438     {
00439       setLastError(REFERENCE_ERROR, QString("Element '%1' is not a reference but is used as one.").arg(name));
00440       return -1;
00441     }
00442 
00443     if(type != ref_type)
00444     {
00445       setLastError(REFERENCE_ERROR, QString("Element '%1' is a reference of type '%2', reference of type '%3' was expected.").arg(name, type, ref_type));
00446       return -1;
00447     }
00448 
00449     bool ok;
00450     uint ref = place_holder.attribute("ref_id", "").toUInt(&ok);
00451     if(!ok)
00452     {
00453       setLastError(REFERENCE_ERROR, QString("Reference '%1' has no id").arg(name));
00454       return -1;
00455     }
00456     compound_stack.push_back(place_holder);
00457     return (int)ref;
00458   }

virtual bool storage::VVEStorage_XMLReader::writer (  )  [inline, virtual]
Returns:
True is the object is a storage writer

Implements storage::VVEStorage.

Definition at line 110 of file storage_xml.h.

00110 { return false; }


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:38:17 2013 for VVE by  doxygen 1.6.3