storage::VVEStorage_XMLWriter Class Reference

Implementation writing an XML file. More...

#include <storage/storage.h>

Inheritance diagram for storage::VVEStorage_XMLWriter:
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 &)
 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 save the file.
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 Types

typedef std::map< reference_t,
uint > 
ref_map_t
typedef std::map< QString,
ref_map_t > 
ref_map_typed_t

Protected Member Functions

QDomElement createElement (const QString &name, const QString &type=QString())

Protected Attributes

QString _filename
std::list< QDomElementcompound_stack
QDomDocument doc
QDomElement root
ref_map_typed_t typed_references_map
bool write_type

Detailed Description

Implementation writing an XML file.

Definition at line 28 of file storage_xml.h.


Member Typedef Documentation

Type of a reference identifier.

Reimplemented from storage::VVEStorage.

Definition at line 32 of file storage_xml.h.


Member Function Documentation

virtual bool storage::VVEStorage_XMLWriter::checkNextField ( const QString name  )  [inline, 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 57 of file storage_xml.h.

00057 { return false; }

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

End the current compound.

Implements storage::VVEStorage.

Definition at line 125 of file storage_xml.cpp.

References storage::NO_FIELD_ERROR, and storage::VVEStorage::setLastError().

00126   {
00127     if(compound_stack.empty())
00128     {
00129       setLastError(NO_FIELD_ERROR, "Trying to end a compound not started");
00130       return false;
00131     }
00132     compound_stack.pop_back();
00133     return true;
00134   }

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

Stop the current reference.

Implements storage::VVEStorage.

Definition at line 161 of file storage_xml.cpp.

References storage::VVEStorage::setLastError(), and storage::USER_ERROR.

00162   {
00163     if(compound_stack.empty())
00164     {
00165       setLastError(USER_ERROR, "Trying to close a reference that wasn't opened.");
00166       return false;
00167     }
00168     compound_stack.pop_back();
00169     return true;
00170   }

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

Name of the file being processed.

Implements storage::VVEStorage.

Definition at line 59 of file storage_xml.h.

00059 { return _filename; }

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

Implements storage::VVEStorage.

Definition at line 50 of file storage_xml.h.

00050 { return false; }

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

Actually save the file.

Implements storage::VVEStorage.

Definition at line 55 of file storage_xml.cpp.

References QString::arg(), QFile::close(), QDomDocument::createElement(), storage::IO_ERROR, QString::isEmpty(), storage::NO__ERROR, QFile::open(), Model::serialize(), QDomElement::setAttribute(), storage::VVEStorage::setLastError(), QDomDocument::toByteArray(), storage::UNKNOWN_ERROR, Model::version(), and Model::versionNumber().

00056   {
00057     _filename = filename;
00058     doc = QDomDocument("VVEStorage");
00059 
00060     last_error = NO__ERROR;
00061     last_error_string = QString();
00062 
00063     file_version = model->version();
00064 
00065     typed_references_map.clear();
00066     compound_stack.clear();
00067 
00068     root = doc.createElement("VVEStorage");
00069     if(!file_version.isEmpty())
00070       root.setAttribute("version", file_version);
00071     doc.appendChild(root);
00072     compound_stack.push_back(root);
00073 
00074     file_version = QString("%1 XML").arg(file_version);
00075     version_number = model->versionNumber(file_version);
00076 
00077     if(model->serialize(*this))
00078     {
00079       QFile file(filename);
00080       if(!file.open(QIODevice::WriteOnly))
00081       {
00082         setLastError(IO_ERROR, QString("Could not open file '%1' for writing").arg(filename));
00083         _filename = QString();
00084         return false;
00085       }
00086       file.write(doc.toByteArray());
00087       file.close();
00088       _filename = QString();
00089       return true;
00090     }
00091     _filename = QString();
00092     if(!last_error)
00093     {
00094       last_error = UNKNOWN_ERROR;
00095       last_error_string += "\nUnkown error.";
00096     }
00097     return false;
00098   }

bool storage::VVEStorage_XMLWriter::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 30 of file storage_xml.cpp.

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

00031   {
00032     switch(option)
00033     {
00034       case TypeChecking:
00035         switch(value)
00036         {
00037           case TCO_Exact:
00038           case TCO_Strict:
00039           case TCO_Permissive:
00040           case TCO_PermissiveNoWarning:
00041             write_type = true;
00042             return true;
00043           case TCO_NoCheck:
00044             write_type = false;
00045             return true;
00046           default:
00047             return false;
00048         }
00049         break;
00050       default:
00051         return false;
00052     }
00053   }

bool storage::VVEStorage_XMLWriter::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 118 of file storage_xml.cpp.

00119   {
00120     QDomElement new_compound = createElement(name);
00121     compound_stack.push_back(new_compound);
00122     return true;
00123   }

int storage::VVEStorage_XMLWriter::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 136 of file storage_xml.cpp.

References QDomElement::setAttribute().

00137   {
00138     QDomElement place_holder = createElement(name);
00139 
00140     ref_map_t& references_map = typed_references_map[ref_type];
00141 
00142     ref_map_t::const_iterator found = references_map.find(ref);
00143     if(found == references_map.end())
00144     {
00145       int id = (int)references_map.size();
00146       place_holder.setAttribute("ref_id", id);
00147       place_holder.setAttribute("ref_type", ref_type);
00148       references_map[ref] = id;
00149       compound_stack.push_back(place_holder);
00150       return id;
00151     }
00152     else
00153     {
00154       place_holder.setAttribute("ref_id", found->second);
00155       place_holder.setAttribute("ref_type", ref_type);
00156       compound_stack.push_back(place_holder);
00157       return found->second;
00158     }
00159   }

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

Implements storage::VVEStorage.

Definition at line 51 of file storage_xml.h.

00051 { return true; }


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:18 2013 for VVE by  doxygen 1.6.3