Merge pull request #9521 from AgCaliva/User/Document/Feature_level_units_selection_#7746
Adding "ProjectUnitSystem" support to project files.
This commit is contained in:
@@ -278,6 +278,7 @@ SET(FreeCADBase_CPP_SRCS
|
||||
Writer.cpp
|
||||
XMLTools.cpp
|
||||
ZipHeader.cpp
|
||||
DocumentReader.cpp
|
||||
)
|
||||
|
||||
SET(SWIG_HEADERS
|
||||
|
||||
351
src/Base/DocumentReader.cpp
Normal file
351
src/Base/DocumentReader.cpp
Normal file
@@ -0,0 +1,351 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
#include "PreCompiled.h"
|
||||
#include "DocumentReader.h"
|
||||
#include "InputSource.h"
|
||||
#include "XMLTools.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 <xercesc/dom/DOM.hpp>
|
||||
# include <xercesc/parsers/XercesDOMParser.hpp>
|
||||
# include <xercesc/dom/DOMException.hpp>
|
||||
# include <xercesc/dom/DOMElement.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define strdup _strdup
|
||||
#endif
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE
|
||||
|
||||
//using namespace std;
|
||||
using namespace Base;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DocumentReader: Constructors and Destructor
|
||||
// ---------------------------------------------------------------------------
|
||||
static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;
|
||||
DocumentReader::DocumentReader()
|
||||
{
|
||||
gDoNamespaces = false;
|
||||
gDoSchema = false;
|
||||
gSchemaFullChecking = false;
|
||||
gDoCreate = true;
|
||||
}
|
||||
|
||||
int DocumentReader::LoadDocument(Base::Reader& reader)
|
||||
{
|
||||
FileInfo _File( reader.getFileName() );
|
||||
StdInputSource inputSource(reader, _File.filePath().c_str());
|
||||
|
||||
//
|
||||
// Create our parser, then attach an error handler to the parser.
|
||||
// The parser will call back to methods of the ErrorHandler if it
|
||||
// discovers errors during the course of parsing the XML document.
|
||||
//
|
||||
XercesDOMParser *parser = new XercesDOMParser;
|
||||
parser->setValidationScheme(gValScheme);
|
||||
parser->setDoNamespaces(gDoNamespaces);
|
||||
parser->setDoSchema(gDoSchema);
|
||||
parser->setValidationSchemaFullChecking(gSchemaFullChecking);
|
||||
parser->setCreateEntityReferenceNodes(gDoCreate);
|
||||
|
||||
DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
|
||||
parser->setErrorHandler(errReporter);
|
||||
//
|
||||
// Parse the XML file, catching any XML exceptions that might propagate
|
||||
// out of it.
|
||||
//
|
||||
bool errorsOccured = false;
|
||||
try {
|
||||
parser->parse(inputSource);
|
||||
}
|
||||
catch (const XMLException& e) {
|
||||
std::cerr << "An error occurred during parsing\n Message: "
|
||||
<< StrX(e.getMessage()) << std::endl;
|
||||
errorsOccured = true;
|
||||
}
|
||||
catch (const DOMException& e) {
|
||||
std::cerr << "A DOM error occurred during parsing\n DOMException code: "
|
||||
<< e.code << std::endl;
|
||||
errorsOccured = true;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "An error occurred during parsing\n " << std::endl;
|
||||
errorsOccured = true;
|
||||
}
|
||||
|
||||
if (errorsOccured) {
|
||||
delete parser;
|
||||
delete errReporter;
|
||||
return 0;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* _pDocument = parser->adoptDocument();
|
||||
delete parser;
|
||||
delete errReporter;
|
||||
|
||||
if (!_pDocument)
|
||||
throw XMLBaseException("Malformed Parameter document: Invalid document");
|
||||
|
||||
DOMElement* rootElem = _pDocument->getDocumentElement();
|
||||
if (!rootElem)
|
||||
throw XMLBaseException("Malformed Parameter document: Root group not found");
|
||||
|
||||
_pGroupNode = rootElem;
|
||||
|
||||
if (!_pGroupNode){
|
||||
throw XMLBaseException("Malformed document.");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::GetRootElement() const
|
||||
{
|
||||
//if (!_pGroupNode)
|
||||
//return nullptr;
|
||||
return _pGroupNode;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(const char* Type) const
|
||||
{
|
||||
if(!Type)
|
||||
return nullptr;
|
||||
|
||||
for (DOMNode *clChild = _pGroupNode->getFirstChild(); clChild != nullptr; clChild = clChild->getNextSibling()) {
|
||||
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
|
||||
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
|
||||
return static_cast<DOMElement*>(clChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElementByField(
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start,
|
||||
const char* TypeEl,const char* field_name,const char* field_value) const
|
||||
{
|
||||
if(!TypeEl || !field_name ||!field_value)
|
||||
return nullptr;
|
||||
for (DOMNode *clChild = Start; clChild != nullptr; clChild = clChild->getNextSibling()) {
|
||||
//auto cast = static_cast<DOMElement*>(clChild);
|
||||
const char* attr = GetAttribute( static_cast<DOMElement*>(clChild), field_name );
|
||||
if(attr){
|
||||
if( !strcmp( attr, field_value ) ){
|
||||
return static_cast<DOMElement*>(clChild);;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, const char* Type) const
|
||||
{
|
||||
if(!Start || !Type)
|
||||
return nullptr;
|
||||
for (DOMNode *clChild = Start->getFirstChild(); clChild != nullptr; clChild = clChild->getNextSibling()) {
|
||||
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
|
||||
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
|
||||
return static_cast<DOMElement*>(clChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DocumentReader::FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *Prev, const char* Type) const
|
||||
{
|
||||
if (!Prev || !Type)
|
||||
return nullptr;
|
||||
DOMNode *clChild = Prev;
|
||||
while ((clChild = clChild->getNextSibling()) != nullptr) {
|
||||
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
|
||||
// the right node Type
|
||||
if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
|
||||
return static_cast<DOMElement*>(clChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
long DocumentReader::ContentToInt( const char* content )
|
||||
{
|
||||
return atol( content );
|
||||
}
|
||||
|
||||
unsigned long DocumentReader::ContentToUnsigned(const char* content)
|
||||
{
|
||||
return strtoul(content,nullptr,10);
|
||||
}
|
||||
|
||||
double DocumentReader::ContentToFloat(const char* content)
|
||||
{
|
||||
return atof(content);
|
||||
}
|
||||
|
||||
bool DocumentReader::ContentToBool(const char* content)
|
||||
{
|
||||
if (strcmp(content,"1"))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
//ATTRIBUTE:
|
||||
const char * DocumentReader::GetAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *DOMEl, const char* Attr) const
|
||||
{
|
||||
if(!Attr)
|
||||
return nullptr;
|
||||
XStr xstr( Attr );
|
||||
bool hasAttr = DOMEl->hasAttribute(xstr.unicodeForm());
|
||||
if (!hasAttr){
|
||||
return nullptr;
|
||||
}
|
||||
const XMLCh * attr = DOMEl->getAttribute( xstr.unicodeForm() );
|
||||
return strdup( StrX( attr ).c_str() );
|
||||
}
|
||||
|
||||
const char * DocumentReader::GetAttribute(const char* Attr) const
|
||||
{
|
||||
if(!Attr)
|
||||
return nullptr;
|
||||
XStr xstr( Attr );
|
||||
bool hasAttr = _pGroupNode->hasAttribute(xstr.unicodeForm());
|
||||
if (!hasAttr){
|
||||
return nullptr;
|
||||
}
|
||||
const XMLCh * attr = _pGroupNode->getAttribute( xstr.unicodeForm() );
|
||||
return strdup( StrX( attr ).c_str() );//strdup is needed since pointer from strx only exists in context where StrX() is created.
|
||||
}
|
||||
//Status
|
||||
void Base::DocumentReader::setPartialRestore(bool on)
|
||||
{
|
||||
setStatus(PartialRestore, on);
|
||||
setStatus(PartialRestoreInDocumentObject, on);
|
||||
setStatus(PartialRestoreInProperty, on);
|
||||
setStatus(PartialRestoreInObject, on);
|
||||
}
|
||||
|
||||
void Base::DocumentReader::clearPartialRestoreProperty()
|
||||
{
|
||||
setStatus(PartialRestoreInProperty, false);
|
||||
setStatus(PartialRestoreInObject, false);
|
||||
}
|
||||
|
||||
bool Base::DocumentReader::testStatus(ReaderStatus pos) const
|
||||
{
|
||||
return StatusBits.test(static_cast<size_t>(pos));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
121
src/Base/DocumentReader.h
Normal file
121
src/Base/DocumentReader.h
Normal file
@@ -0,0 +1,121 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef BASE_DOCUMENTREADER_H
|
||||
#define BASE_DOCUMENTREADER_H
|
||||
|
||||
#include <bitset>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <FCGlobal.h>
|
||||
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
|
||||
namespace zipios {
|
||||
class ZipInputStream;
|
||||
}
|
||||
|
||||
XERCES_CPP_NAMESPACE_BEGIN
|
||||
class DOMNode;
|
||||
class DOMElement;
|
||||
XERCES_CPP_NAMESPACE_END
|
||||
namespace Base
|
||||
{
|
||||
class Reader;
|
||||
class Persistence;
|
||||
|
||||
class BaseExport DocumentReader
|
||||
{
|
||||
public:
|
||||
enum ReaderStatus {
|
||||
PartialRestore = 0, // This bit indicates that a partial restore took place somewhere in this Document
|
||||
PartialRestoreInDocumentObject = 1, // This bit is local to the DocumentObject being read indicating a partial restore therein
|
||||
PartialRestoreInProperty = 2, // Local to the Property
|
||||
PartialRestoreInObject = 3 // Local to the object partially restored itself
|
||||
};
|
||||
DocumentReader();
|
||||
int LoadDocument(Base::Reader& reader);
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *GetRootElement() const;
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, const char* Type) const;
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElement(const char* Type) const;
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindElementByField(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start,
|
||||
const char* TypeEl,const char* field_name, const char* field_value) const;
|
||||
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *Prev, const char* Type) const;
|
||||
|
||||
static long ContentToASCII(const char* Content);
|
||||
|
||||
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;
|
||||
|
||||
/// return the status bits
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -33,7 +33,6 @@
|
||||
# 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>
|
||||
@@ -64,49 +63,49 @@ using namespace Base;
|
||||
//**************************************************************************
|
||||
//**************************************************************************
|
||||
// private classes declaration:
|
||||
// - DOMTreeErrorReporter
|
||||
// - StrX
|
||||
// - DOMPrintFilter
|
||||
// - DOMPrintErrorHandler
|
||||
// - XStr
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
DOMTreeErrorReporter::DOMTreeErrorReporter():
|
||||
fSawErrors(false) {
|
||||
}
|
||||
|
||||
|
||||
class DOMTreeErrorReporter : public ErrorHandler
|
||||
void DOMTreeErrorReporter::warning(const SAXParseException&)
|
||||
{
|
||||
public:
|
||||
// -----------------------------------------------------------------------
|
||||
// Constructors and Destructor
|
||||
// -----------------------------------------------------------------------
|
||||
DOMTreeErrorReporter() = default;
|
||||
//
|
||||
// Ignore all warnings.
|
||||
//
|
||||
}
|
||||
|
||||
~DOMTreeErrorReporter() override = default;
|
||||
void DOMTreeErrorReporter::error(const SAXParseException& toCatch)
|
||||
{
|
||||
fSawErrors = true;
|
||||
std::cerr << "Error at file \"" << StrX(toCatch.getSystemId())
|
||||
<< "\", line " << toCatch.getLineNumber()
|
||||
<< ", column " << toCatch.getColumnNumber()
|
||||
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
|
||||
}
|
||||
|
||||
void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch)
|
||||
{
|
||||
fSawErrors = true;
|
||||
std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId())
|
||||
<< "\", line " << toCatch.getLineNumber()
|
||||
<< ", column " << toCatch.getColumnNumber()
|
||||
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Implementation of the error handler interface
|
||||
// -----------------------------------------------------------------------
|
||||
void warning(const SAXParseException& toCatch) override;
|
||||
void error(const SAXParseException& toCatch) override;
|
||||
void fatalError(const SAXParseException& toCatch) override;
|
||||
void resetErrors() override;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Getter methods
|
||||
// -----------------------------------------------------------------------
|
||||
bool getSawErrors() const;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Private data members
|
||||
//
|
||||
// fSawErrors
|
||||
// This is set if we get any errors, and is queryable via a getter
|
||||
// method. Its used by the main code to suppress output if there are
|
||||
// errors.
|
||||
// -----------------------------------------------------------------------
|
||||
bool fSawErrors{false};
|
||||
};
|
||||
void DOMTreeErrorReporter::resetErrors()
|
||||
{
|
||||
// No-op in this case
|
||||
}
|
||||
|
||||
inline bool DOMTreeErrorReporter::getSawErrors() const
|
||||
{
|
||||
return fSawErrors;
|
||||
}
|
||||
|
||||
class DOMPrintFilter : public DOMLSSerializerFilter
|
||||
{
|
||||
@@ -150,14 +149,6 @@ public:
|
||||
void operator=(const DOMErrorHandler&) = delete;
|
||||
|
||||
};
|
||||
|
||||
|
||||
inline bool DOMTreeErrorReporter::getSawErrors() const
|
||||
{
|
||||
return fSawErrors;
|
||||
}
|
||||
|
||||
|
||||
//**************************************************************************
|
||||
//**************************************************************************
|
||||
// ParameterManager
|
||||
@@ -1833,42 +1824,6 @@ void ParameterManager::CheckDocument() const
|
||||
}
|
||||
|
||||
|
||||
//**************************************************************************
|
||||
//**************************************************************************
|
||||
// DOMTreeErrorReporter
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
void DOMTreeErrorReporter::warning(const SAXParseException&)
|
||||
{
|
||||
//
|
||||
// Ignore all warnings.
|
||||
//
|
||||
}
|
||||
|
||||
void DOMTreeErrorReporter::error(const SAXParseException& toCatch)
|
||||
{
|
||||
fSawErrors = true;
|
||||
std::cerr << "Error at file \"" << StrX(toCatch.getSystemId())
|
||||
<< "\", line " << toCatch.getLineNumber()
|
||||
<< ", column " << toCatch.getColumnNumber()
|
||||
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
|
||||
}
|
||||
|
||||
void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch)
|
||||
{
|
||||
fSawErrors = true;
|
||||
std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId())
|
||||
<< "\", line " << toCatch.getLineNumber()
|
||||
<< ", column " << toCatch.getColumnNumber()
|
||||
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
|
||||
}
|
||||
|
||||
void DOMTreeErrorReporter::resetErrors()
|
||||
{
|
||||
// No-op in this case
|
||||
}
|
||||
|
||||
|
||||
//**************************************************************************
|
||||
//**************************************************************************
|
||||
// DOMPrintFilter
|
||||
|
||||
@@ -53,6 +53,7 @@ using PyObject = struct _object;
|
||||
#include <vector>
|
||||
#include <boost_signals2.hpp>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
#include <xercesc/sax/ErrorHandler.hpp>
|
||||
|
||||
#include "Handle.h"
|
||||
#include "Observer.h"
|
||||
@@ -65,7 +66,6 @@ using PyObject = struct _object;
|
||||
# pragma warning( disable : 4275 )
|
||||
#endif
|
||||
|
||||
|
||||
XERCES_CPP_NAMESPACE_BEGIN
|
||||
class DOMNode;
|
||||
class DOMElement;
|
||||
@@ -269,6 +269,7 @@ protected:
|
||||
~ParameterGrp() override;
|
||||
/// helper function for GetGroup
|
||||
Base::Reference<ParameterGrp> _GetGroup(const char* Name);
|
||||
|
||||
bool ShouldRemove() const;
|
||||
|
||||
void _Reset();
|
||||
@@ -421,11 +422,43 @@ private:
|
||||
bool gUseFilter ;
|
||||
bool gFormatPrettyPrint ;
|
||||
|
||||
private:
|
||||
ParameterManager();
|
||||
~ParameterManager() override;
|
||||
};
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE
|
||||
|
||||
class DOMTreeErrorReporter : public ErrorHandler
|
||||
{
|
||||
public:
|
||||
// -----------------------------------------------------------------------
|
||||
// Constructors and Destructor
|
||||
// -----------------------------------------------------------------------
|
||||
DOMTreeErrorReporter();
|
||||
// -----------------------------------------------------------------------
|
||||
// Implementation of the error handler interface
|
||||
// -----------------------------------------------------------------------
|
||||
void warning(const SAXParseException& toCatch) override;
|
||||
void error(const SAXParseException& toCatch) override;
|
||||
void fatalError(const SAXParseException& toCatch) override;
|
||||
void resetErrors() override;
|
||||
// -----------------------------------------------------------------------
|
||||
// Getter methods
|
||||
// -----------------------------------------------------------------------
|
||||
bool getSawErrors() const;
|
||||
private:
|
||||
// -----------------------------------------------------------------------
|
||||
// Private data members
|
||||
//
|
||||
// fSawErrors
|
||||
// This is set if we get any errors, and is queryable via a getter
|
||||
// method. Its used by the main code to suppress output if there are
|
||||
// errors.
|
||||
// -----------------------------------------------------------------------
|
||||
bool fSawErrors;
|
||||
};
|
||||
|
||||
|
||||
/** python wrapper function
|
||||
*/
|
||||
BaseExport PyObject* GetPyObject( const Base::Reference<ParameterGrp> &hcParamGrp);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -61,6 +65,20 @@ void Persistence::Save (Writer &/*writer*/) const
|
||||
assert(0);
|
||||
}
|
||||
|
||||
|
||||
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*/)
|
||||
{
|
||||
// you have to implement this method in all descending classes!
|
||||
assert(0);
|
||||
}
|
||||
|
||||
void Persistence::Restore(XMLReader &/*reader*/)
|
||||
{
|
||||
// you have to implement this method in all descending classes!
|
||||
|
||||
@@ -26,11 +26,21 @@
|
||||
|
||||
#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;
|
||||
class Writer;
|
||||
class XMLReader;
|
||||
class DocumentReader;
|
||||
|
||||
/// Persistence class and root of the type system
|
||||
class BaseExport Persistence : public BaseClass
|
||||
@@ -77,6 +87,9 @@ public:
|
||||
* \endcode
|
||||
*/
|
||||
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
|
||||
@@ -141,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&);
|
||||
|
||||
|
||||
@@ -26,11 +26,13 @@
|
||||
#ifndef _PreComp_
|
||||
#include <memory>
|
||||
# include <xercesc/sax2/XMLReaderFactory.hpp>
|
||||
# include <xercesc/dom/DOM.hpp>
|
||||
#endif
|
||||
|
||||
#include <locale>
|
||||
|
||||
#include "Reader.h"
|
||||
#include "DocumentReader.h"
|
||||
#include "Base64.h"
|
||||
#include "Console.h"
|
||||
#include "InputSource.h"
|
||||
@@ -45,7 +47,6 @@
|
||||
#include <zipios++/zipinputstream.h>
|
||||
#include <boost/iostreams/filtering_stream.hpp>
|
||||
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE
|
||||
|
||||
using namespace std;
|
||||
@@ -382,12 +383,14 @@ void Base::XMLReader::readFiles(zipios::ZipInputStream &zipstream) const
|
||||
// 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())
|
||||
Base::Reader reader(zipstream, jt->FileName, FileVersion);
|
||||
jt->Object->RestoreDocFile(reader);
|
||||
if (reader.getLocalReader())
|
||||
reader.getLocalReader()->readFiles(zipstream);
|
||||
}
|
||||
catch(...) {
|
||||
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.
|
||||
@@ -628,3 +631,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);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,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
|
||||
@@ -321,13 +322,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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -26,15 +26,15 @@
|
||||
#include "XMLTools.h"
|
||||
|
||||
using namespace Base;
|
||||
XERCES_CPP_NAMESPACE_USE
|
||||
|
||||
std::unique_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> XMLTools::transcoder;
|
||||
std::unique_ptr<XMLTranscoder> XMLTools::transcoder;
|
||||
|
||||
void XMLTools::initialize()
|
||||
{
|
||||
XERCES_CPP_NAMESPACE_USE;
|
||||
if (!transcoder.get()) {
|
||||
XMLTransService::Codes res{};
|
||||
transcoder.reset(XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XERCES_CPP_NAMESPACE_QUALIFIER XMLRecognizer::UTF_8, res, 4096, XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgMemoryManager));
|
||||
transcoder.reset(XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XMLRecognizer::UTF_8, res, 4096, XMLPlatformUtils::fgMemoryManager));
|
||||
if (res != XMLTransService::Ok)
|
||||
throw Base::UnicodeError("Can\'t create transcoder");
|
||||
}
|
||||
@@ -44,7 +44,6 @@ std::string XMLTools::toStdString(const XMLCh* const toTranscode)
|
||||
{
|
||||
std::string str;
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE;
|
||||
initialize();
|
||||
|
||||
//char outBuff[128];
|
||||
@@ -74,8 +73,7 @@ std::basic_string<XMLCh> XMLTools::toXMLString(const char* const fromTranscode)
|
||||
std::basic_string<XMLCh> str;
|
||||
if (!fromTranscode)
|
||||
return str;
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE;
|
||||
|
||||
initialize();
|
||||
|
||||
static XMLCh outBuff[128];
|
||||
|
||||
@@ -28,16 +28,10 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <xercesc/util/TransService.hpp>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
|
||||
#include <Base/Exception.h>
|
||||
|
||||
|
||||
XERCES_CPP_NAMESPACE_BEGIN
|
||||
class DOMNode;
|
||||
class DOMElement;
|
||||
class DOMDocument;
|
||||
XERCES_CPP_NAMESPACE_END
|
||||
|
||||
XERCES_CPP_NAMESPACE_USE
|
||||
// Helper class
|
||||
class BaseExport XMLTools
|
||||
{
|
||||
@@ -48,7 +42,7 @@ public:
|
||||
static void terminate();
|
||||
|
||||
private:
|
||||
static std::unique_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> transcoder;
|
||||
static std::unique_ptr<XMLTranscoder> transcoder;
|
||||
};
|
||||
|
||||
//**************************************************************************
|
||||
@@ -79,12 +73,12 @@ inline std::ostream& operator<<(std::ostream& target, const StrX& toDump)
|
||||
inline StrX::StrX(const XMLCh* const toTranscode)
|
||||
{
|
||||
// Call the private transcoding method
|
||||
fLocalForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
|
||||
fLocalForm = XMLString::transcode(toTranscode);
|
||||
}
|
||||
|
||||
inline StrX::~StrX()
|
||||
{
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fLocalForm);
|
||||
XMLString::release(&fLocalForm);
|
||||
}
|
||||
|
||||
|
||||
@@ -158,12 +152,12 @@ private :
|
||||
|
||||
inline XStr::XStr(const char* const toTranscode)
|
||||
{
|
||||
fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
|
||||
fUnicodeForm = XMLString::transcode(toTranscode);
|
||||
}
|
||||
|
||||
inline XStr::~XStr()
|
||||
{
|
||||
XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm);
|
||||
XMLString::release(&fUnicodeForm);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user