Implemented DocumentReader for GuiDocument.xml reading. Final

This commit is contained in:
AgCaliva
2023-06-30 20:50:36 -03:00
parent 0751770bc6
commit 816d4077df
20 changed files with 1036 additions and 359 deletions

View File

@@ -19,41 +19,24 @@
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
//#include "PreCompiled.h"
//#include <locale>
#include "DocumentReader.h"
//#include "Reader.h"
//#include "Base64.h"
//#include "Console.h"
#include "InputSource.h"
//#include "Persistence.h"
//#include "Sequencer.h"
//#include "Stream.h"
#include "XMLTools.h"
//#ifdef _MSC_VER
//#include <zipios++/zipios-config.h>
//#endif
//#include <zipios++/zipinputstream.h>
#include <Base/Reader.h>
#include <Base/Parameter.h>
#include "Persistence.h"
#include "Sequencer.h"
#ifdef _MSC_VER
#include <zipios++/zipios-config.h>
#endif
#include <zipios++/zipinputstream.h>
#ifndef _PreComp_
//# include <cassert>
//# include <memory>
# include <xercesc/dom/DOM.hpp>
//# include <xercesc/framework/LocalFileFormatTarget.hpp>
//# include <xercesc/framework/LocalFileInputSource.hpp>
//# include <xercesc/framework/MemBufFormatTarget.hpp>
//# include <xercesc/framework/MemBufInputSource.hpp>
# include <xercesc/parsers/XercesDOMParser.hpp>
//# include <xercesc/sax/ErrorHandler.hpp>
//# include <xercesc/sax/SAXParseException.hpp>
//# include <sstream>
//# include <string>
//# include <utility>
#endif
XERCES_CPP_NAMESPACE_USE
@@ -67,30 +50,12 @@ using namespace Base;
static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;
DocumentReader::DocumentReader()
{
gDoNamespaces = false;
gDoSchema = false;
gSchemaFullChecking = false;
gDoCreate = true;
/*
gOutputEncoding = nullptr;
gMyEOLSequence = nullptr;
gSplitCdataSections = true;
gDiscardDefaultContent = true;
gUseFilter = true;
gFormatPrettyPrint = true;
*/
}
//DocumentReader::~DocumentReader()
//{
//delete parser;
//}
//int DocumentReader::LoadDocument(const XERCES_CPP_NAMESPACE_QUALIFIER InputSource& inputSource)
//int DocumentReader::LoadDocument(std::istream& Stream,std::string filename)
int DocumentReader::LoadDocument(Base::Reader& reader)
{
FileInfo _File( reader.getFileName() );
@@ -139,7 +104,7 @@ int DocumentReader::LoadDocument(Base::Reader& reader)
return 0;
}
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* _pDocument = parser->adoptDocument();
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* _pDocument = parser->adoptDocument();
delete parser;
delete errReporter;
@@ -174,9 +139,7 @@ XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(const cha
for (DOMNode *clChild = _pGroupNode->getFirstChild(); clChild != nullptr; clChild = clChild->getNextSibling()) {
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
if (clChild->getAttributes()->getLength() > 0) {
return static_cast<DOMElement*>(clChild);
}
return static_cast<DOMElement*>(clChild);
}
}
}
@@ -191,9 +154,6 @@ XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(XERCES_CP
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
return static_cast<DOMElement*>(clChild);
//if (clChild->getAttributes()->getLength() > 0) {
//return static_cast<DOMElement*>(clChild);
//}
}
}
}
@@ -216,41 +176,22 @@ XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindNextElement(XERCE
return nullptr;
}
/*
//CONTENT:
const char * DocumentReader::GetContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl) const
{
if (!DOMEl)
return nullptr;
return StrX(DOMEl->getAttribute(XStr("Value").unicodeForm())).c_str();
}
std::string DocumentReader::GetContentASCII(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl) const
{
//return std::string(StrXUTF8(pcElem->getNodeValue()).c_str() );
//maybe its better to use getNodeValue()
if (!DOMEl)
return nullptr;
return std::string( StrXUTF8(DOMEl->getAttribute(XStr("Value").unicodeForm())).c_str() );
}
*/
long DocumentReader::ContentToInt( const char* content ) const
long DocumentReader::ContentToInt( const char* content )
{
return atol( content );
}
unsigned long DocumentReader::ContentToUnsigned(const char* content) const
unsigned long DocumentReader::ContentToUnsigned(const char* content)
{
return strtoul(content,nullptr,10);
}
double DocumentReader::ContentToFloat(const char* content) const
double DocumentReader::ContentToFloat(const char* content)
{
return atof(content);
}
bool DocumentReader::ContentToBool(const char* content) const
bool DocumentReader::ContentToBool(const char* content)
{
if (strcmp(content,"1"))
return false;
@@ -282,18 +223,16 @@ const char * DocumentReader::GetAttribute(const char* Attr) const
return nullptr;
}
const XMLCh * attr = _pGroupNode->getAttribute( xstr.unicodeForm() );
//stringLen
return strdup( StrX( attr ).c_str() );//strdup is needed since pointer from strx only exists in context where StrX() is created.
}
/*
unsigned int Base::XMLReader::getAttributeCount() const
{
return static_cast<unsigned int>(AttrMap.size());
}
*/
//Status
void Base::DocumentReader::setPartialRestore(bool on)
{
setStatus(PartialRestore, on);
setStatus(PartialRestoreInDocumentObject, on);
setStatus(PartialRestoreInProperty, on);
setStatus(PartialRestoreInObject, on);
}
void Base::DocumentReader::clearPartialRestoreProperty()
{
@@ -311,4 +250,77 @@ void Base::DocumentReader::setStatus(ReaderStatus pos, bool on)
StatusBits.set(static_cast<size_t>(pos), on);
}
const char *Base::DocumentReader::addFile(const char* Name, Base::Persistence *Object)
{
FileEntry temp;
temp.FileName = Name;
temp.Object = Object;
FileList.push_back(temp);
FileNames.push_back( temp.FileName );
return Name;
}
void Base::DocumentReader::readFiles(zipios::ZipInputStream &zipstream) const
{
// It's possible that not all objects inside the document could be created, e.g. if a module
// is missing that would know these object types. So, there may be data files inside the zip
// file that cannot be read. We simply ignore these files.
// On the other hand, however, it could happen that a file should be read that is not part of
// the zip file. This happens e.g. if a document is written without GUI up but is read with GUI
// up. In this case the associated GUI document asks for its file which is not part of the ZIP
// file, then.
// In either case it's guaranteed that the order of the files is kept.
zipios::ConstEntryPointer entry;
try {
entry = zipstream.getNextEntry();
}
catch (const std::exception&) {
// There is no further file at all. This can happen if the
// project file was created without GUI
return;
}
std::vector<FileEntry>::const_iterator it = FileList.begin();
Base::SequencerLauncher seq("Importing project files...", FileList.size());
while (entry->isValid() && it != FileList.end()) {
std::vector<FileEntry>::const_iterator jt = it;
// Check if the current entry is registered, otherwise check the next registered files as soon as
// both file names match
while (jt != FileList.end() && entry->getName() != jt->FileName)
++jt;
// If this condition is true both file names match and we can read-in the data, otherwise
// no file name for the current entry in the zip was registered.
if (jt != FileList.end()) {
try {
Base::Reader reader(zipstream, jt->FileName, FileVersion);
jt->Object->RestoreDocFile(reader);
if (reader.getLocalReader())
reader.getLocalReader()->readFiles(zipstream);
if (reader.getLocalDocReader())
reader.getLocalDocReader()->readFiles(zipstream);
}catch(...) {
// For any exception we just continue with the next file.
// It doesn't matter if the last reader has read more or
// less data than the file size would allow.
// All what we need to do is to notify the user about the
// failure.
Base::Console().Error("Reading failed from embedded file(DocumentReader): %s\n", entry->toString().c_str());
}
// Go to the next registered file name
it = jt + 1;
}
seq.next();
// In either case we must go to the next entry
try {
entry = zipstream.getNextEntry();
}
catch (const std::exception&) {
// there is no further entry
break;
}
}
}

View File

@@ -25,18 +25,16 @@
#include <bitset>
#include <map>
//#include <memory>
//#include <sstream>
#include <memory>
#include <string>
#include <vector>
//#include <xercesc/framework/XMLPScanToken.hpp>
//#include <xercesc/sax2/Attributes.hpp>
//#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/util/XercesDefs.hpp>
//#include "FileInfo.h"//reemplazado por:
//#include "FileInfo.h"//remplaced:
#include <FCGlobal.h>
//#include <Base/Parameter.h>
namespace zipios {
class ZipInputStream;
}
XERCES_CPP_NAMESPACE_BEGIN
class DOMNode;
@@ -49,7 +47,7 @@ namespace Base
class Reader;
class Persistence;
class BaseExport DocumentReader //: public ParameterManager
class BaseExport DocumentReader
{
public:
enum ReaderStatus {
@@ -66,12 +64,12 @@ public:
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElement(const char* Type) const;
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *Prev, const char* Type) const;
long ContentToASCII(const char* Content) const;
static long ContentToASCII(const char* Content);
long ContentToInt(const char* Content) const;
unsigned long ContentToUnsigned(const char* Content) const;
double ContentToFloat(const char* Content) const;
bool ContentToBool(const char* Content) const;
static long ContentToInt(const char* Content);
static unsigned long ContentToUnsigned(const char* Content);
static double ContentToFloat(const char* Content);
static bool ContentToBool(const char* Content);
const char* GetAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* DOMEl, const char* Attr) const;
const char* GetAttribute(const char* Attr) const;
@@ -80,15 +78,38 @@ public:
bool testStatus(ReaderStatus pos) const;
/// set the status bits
void setStatus(ReaderStatus pos, bool on);
/// sets simultaneously the global and local PartialRestore bits
void setPartialRestore(bool on);
void clearPartialRestoreProperty();
/// add a read request of a persistent object
const char *addFile(const char* Name, Base::Persistence *Object);
void readFiles(zipios::ZipInputStream &zipstream) const;
std::shared_ptr<Base::DocumentReader> getLocalReader() const;
struct FileEntry {
std::string FileName;
Base::Persistence *Object;
};
std::vector<FileEntry> FileList;
/// Version of the file format
int FileVersion;
protected:
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *_pGroupNode;
bool gDoNamespaces ;
bool gDoSchema ;
bool gSchemaFullChecking ;
bool gDoCreate ;
std::vector<std::string> FileNames;
std::bitset<32> StatusBits;
};

View File

@@ -34,6 +34,10 @@
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include "Persistence.h"
#ifndef _PreComp_
# include <xercesc/dom/DOM.hpp>
#endif
using namespace Base;
@@ -63,8 +67,10 @@ void Persistence::Save (Writer &/*writer*/) const
void Persistence::Restore(DocumentReader &/*reader*/)
{
// you have to implement this method in all descending classes!
assert(0);
}
void Persistence::Restore(DocumentReader &/*reader*/,XERCES_CPP_NAMESPACE_QUALIFIER DOMElement */*containerEl*/)
{
}
void Persistence::Restore(XMLReader &/*reader*/)

View File

@@ -26,6 +26,15 @@
#include "BaseClass.h"
#include <xercesc/util/XercesDefs.hpp>
XERCES_CPP_NAMESPACE_BEGIN
class DOMNode;
class DOMElement;
// class DefaultHandler;
// class SAX2XMLReader;
XERCES_CPP_NAMESPACE_END
namespace Base
{
class Reader;
@@ -79,6 +88,8 @@ public:
*/
virtual void Restore(XMLReader &/*reader*/) = 0;
virtual void Restore(DocumentReader &/*reader*/);
virtual void Restore(DocumentReader &/*reader*/,XERCES_CPP_NAMESPACE_QUALIFIER DOMElement */*containerEl*/);
/** This method is used to save large amounts of data to a binary file.
* Sometimes it makes no sense to write property data as XML. In case the
* amount of data is too big or the data type has a more effective way to
@@ -143,6 +154,8 @@ public:
* @see Base::Reader,Base::XMLReader
*/
virtual void RestoreDocFile(Reader &/*reader*/);
/// Encodes an attribute upon saving.
static std::string encodeAttribute(const std::string&);

View File

@@ -31,6 +31,7 @@
#include <locale>
#include "Reader.h"
#include "DocumentReader.h"
#include "Base64.h"
#include "Console.h"
#include "InputSource.h"
@@ -333,9 +334,11 @@ void Base::XMLReader::readFiles(zipios::ZipInputStream &zipstream) const
try {
Base::Reader reader(zipstream, jt->FileName, FileVersion);
jt->Object->RestoreDocFile(reader);
if (reader.getLocalReader())
reader.getLocalReader()->readFiles(zipstream);
if (reader.getLocalReader())
reader.getLocalReader()->readFiles(zipstream);
if (reader.getLocalDocReader())
reader.getLocalDocReader()->readFiles(zipstream);
}catch(...) {
// For any exception we just continue with the next file.
// It doesn't matter if the last reader has read more or
@@ -577,3 +580,13 @@ std::shared_ptr<Base::XMLReader> Base::Reader::getLocalReader() const
{
return(this->localreader);
}
void Base::Reader::initLocalDocReader(std::shared_ptr<Base::DocumentReader> reader)
{
this->localdocreader = reader;
}
std::shared_ptr<Base::DocumentReader> Base::Reader::getLocalDocReader() const
{
return(this->localdocreader);
}

View File

@@ -48,6 +48,7 @@ XERCES_CPP_NAMESPACE_END
namespace Base
{
class Persistence;
class DocumentReader;
/** The XML reader class
* This is an important helper class for the store and retrieval system
@@ -295,13 +296,16 @@ public:
std::string getFileName() const;
int getFileVersion() const;
void initLocalReader(std::shared_ptr<Base::XMLReader>);
void initLocalDocReader(std::shared_ptr<Base::DocumentReader>);
std::shared_ptr<Base::XMLReader> getLocalReader() const;
std::shared_ptr<Base::DocumentReader> getLocalDocReader() const;
private:
std::istream& _str;
std::string _name;
int fileVersion;
std::shared_ptr<Base::XMLReader> localreader;
std::shared_ptr<Base::DocumentReader> localdocreader;
};
}