| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
#ifndef APP_PROJECTFILE_H |
| 25 |
#define APP_PROJECTFILE_H |
| 26 |
|
| 27 |
#include <Base/Type.h> |
| 28 |
#include <zipios++/zipfile.h> |
| 29 |
#include <sstream> |
| 30 |
#include <list> |
| 31 |
#include <map> |
| 32 |
#include <string> |
| 33 |
#include <xercesc/util/XercesDefs.hpp> |
| 34 |
|
| 35 |
#ifndef XERCES_CPP_NAMESPACE_BEGIN |
| 36 |
#define XERCES_CPP_NAMESPACE_QUALIFIER |
| 37 |
using namespace XERCES_CPP_NAMESPACE; |
| 38 |
namespace XERCES_CPP_NAMESPACE |
| 39 |
{ |
| 40 |
class DOMNode; |
| 41 |
class DOMElement; |
| 42 |
class DOMDocument; |
| 43 |
} // namespace XERCES_CPP_NAMESPACE |
| 44 |
#else |
| 45 |
XERCES_CPP_NAMESPACE_BEGIN |
| 46 |
class DOMDocument; |
| 47 |
class DOMElement; |
| 48 |
class DOMNode; |
| 49 |
XERCES_CPP_NAMESPACE_END |
| 50 |
#endif |
| 51 |
|
| 52 |
namespace App |
| 53 |
{ |
| 54 |
|
| 55 |
class PropertyContainer; |
| 56 |
class Property; |
| 57 |
|
| 58 |
/** |
| 59 |
* @code |
| 60 |
* ProjectFile proj(project.fcstd); |
| 61 |
* if (proj.loadDocument()) { |
| 62 |
* std::list<std::string> names = proj.getObjects(); |
| 63 |
* std::list<std::string> files = proj.getInputFiles(names.front().name); |
| 64 |
* std::string meshFile = proj.extractInputFile(files.front()); |
| 65 |
* std::ifstream str(meshFile.c_str(), std::ios::in | std::ios::binary); |
| 66 |
* Mesh::MeshObject mesh; |
| 67 |
* mesh.load(str); |
| 68 |
* } |
| 69 |
* @endcode |
| 70 |
*/ |
| 71 |
class AppExport ProjectFile |
| 72 |
{ |
| 73 |
public: |
| 74 |
struct Object |
| 75 |
{ |
| 76 |
std::string name; |
| 77 |
Base::Type type; |
| 78 |
}; |
| 79 |
struct PropertyFile |
| 80 |
{ |
| 81 |
std::string file; |
| 82 |
std::string name; |
| 83 |
Base::Type type; |
| 84 |
}; |
| 85 |
struct Metadata |
| 86 |
{ |
| 87 |
std::string comment; |
| 88 |
std::string company; |
| 89 |
std::string createdBy; |
| 90 |
std::string creationDate; |
| 91 |
std::string label; |
| 92 |
std::string lastModifiedBy; |
| 93 |
std::string lastModifiedDate; |
| 94 |
std::string license; |
| 95 |
std::string licenseURL; |
| 96 |
std::string programVersion; |
| 97 |
std::string uuid; |
| 98 |
}; |
| 99 |
/** |
| 100 |
* Default constructor |
| 101 |
*/ |
| 102 |
ProjectFile(); |
| 103 |
/** |
| 104 |
* Creates an instance of ProjectFile pointing to a project file. |
| 105 |
* You have to call @ref loadDocument() afterwards. |
| 106 |
*/ |
| 107 |
explicit ProjectFile(std::string zipArchive); |
| 108 |
/** |
| 109 |
* Destructor |
| 110 |
*/ |
| 111 |
~ProjectFile(); |
| 112 |
/** |
| 113 |
* Copy/move constructor & assignment |
| 114 |
*/ |
| 115 |
ProjectFile(const ProjectFile&) = delete; |
| 116 |
ProjectFile(ProjectFile&&) = delete; |
| 117 |
ProjectFile& operator=(const ProjectFile&) = delete; |
| 118 |
ProjectFile& operator=(ProjectFile&&) = delete; |
| 119 |
/** |
| 120 |
* Assigns the file name of another project file. This clears any internal data, thus |
| 121 |
* @ref loadDocument() needs to be called afterwards. |
| 122 |
*/ |
| 123 |
void setProjectFile(const std::string& zipArchive); |
| 124 |
/** |
| 125 |
* Loads the embedded document file of the project file. This is needed to query any information |
| 126 |
* about objects, their type ids or any referenced input files. |
| 127 |
*/ |
| 128 |
bool loadDocument(); |
| 129 |
/** |
| 130 |
* Return the meta data of the loaded document. |
| 131 |
*/ |
| 132 |
Metadata getMetadata() const; |
| 133 |
/** |
| 134 |
* Returns a list of all object names with their type ids which are part of the project file. |
| 135 |
*/ |
| 136 |
std::list<Object> getObjects() const; |
| 137 |
/** |
| 138 |
* Returns a list of object names of a given type id which are part of the project file. |
| 139 |
*/ |
| 140 |
std::list<std::string> getObjectsOfType(const Base::Type&) const; |
| 141 |
/** |
| 142 |
* Restores the object identified by its @a name and loads all properties of @a prop |
| 143 |
* that match with the saved properties. |
| 144 |
*/ |
| 145 |
bool restoreObject(const std::string& name, App::PropertyContainer*, bool verbose = true); |
| 146 |
/** |
| 147 |
* Retrieves the type id of a given object name. |
| 148 |
*/ |
| 149 |
Base::Type getTypeId(const std::string& name) const; |
| 150 |
/** |
| 151 |
* Retrieves a list of input file names and their associated property names and type ids |
| 152 |
* to the given object name. |
| 153 |
* @see getInputFiles(). |
| 154 |
*/ |
| 155 |
std::list<PropertyFile> getPropertyFiles(const std::string& name) const; |
| 156 |
/** |
| 157 |
* If the project file contains the file \a name true is returned and |
| 158 |
* false otherwise |
| 159 |
*/ |
| 160 |
bool containsFile(const std::string& name) const; |
| 161 |
/** |
| 162 |
* Retrieves a list of input file names referenced to the given object name. |
| 163 |
* This method does the same as @ref getPropertyFiles() unless that it only |
| 164 |
* returns the file names. |
| 165 |
*/ |
| 166 |
std::list<std::string> getInputFiles(const std::string& name) const; |
| 167 |
/** |
| 168 |
* Extracts an input file of @a name and saves it as file to the disk. |
| 169 |
* It's up to the client code to delete the file if needed. |
| 170 |
*/ |
| 171 |
std::string extractInputFile(const std::string& name); |
| 172 |
/** |
| 173 |
* Extracts, via a temporary file the content of an input file of @a name. |
| 174 |
*/ |
| 175 |
void readInputFile(const std::string& name, std::ostream& str); |
| 176 |
/** |
| 177 |
* Directly extracts the content of an input file of @a name. |
| 178 |
*/ |
| 179 |
void readInputFileDirect(const std::string& name, std::ostream& str); |
| 180 |
/** |
| 181 |
* Replaces the input file @a name with the content of the given @a stream. |
| 182 |
* The method returns the file name of the newly created project file. |
| 183 |
*/ |
| 184 |
std::string replaceInputFile(const std::string& name, std::istream& inp); |
| 185 |
/** |
| 186 |
* Replaces the input files with the streams of @a inp. |
| 187 |
* The method returns the file name of the newly created project file. |
| 188 |
* If you want to replace the original project file you must call the |
| 189 |
* method @ref replaceProjectFile() with the returned file name as argument. |
| 190 |
*/ |
| 191 |
std::string replaceInputFiles(const std::map<std::string, std::istream*>& inp); |
| 192 |
/** |
| 193 |
* Replaces the property files with the content of the properties in @a props. |
| 194 |
* The method returns the file name of the newly created project file. |
| 195 |
* If you want to replace the original project file you must call the |
| 196 |
* method @ref replaceProjectFile() with the returned file name as argument. |
| 197 |
*/ |
| 198 |
std::string replacePropertyFiles(const std::map<std::string, App::Property*>& props); |
| 199 |
/** |
| 200 |
* Replaces the project file with the given @a name. If @a keepfile is true |
| 201 |
* then the renamed original file won't be deleted. |
| 202 |
* Returns true if the replacement succeeded and false otherwise. |
| 203 |
* If you want to replace the original project file you must call the |
| 204 |
* method @ref replaceProjectFile() with the returned file name as argument. |
| 205 |
*/ |
| 206 |
bool replaceProjectFile(const std::string& name, bool keepfile = false); |
| 207 |
|
| 208 |
private: |
| 209 |
void findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode*, std::list<std::string>&) const; |
| 210 |
void findFiles(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode*, std::list<PropertyFile>&) const; |
| 211 |
|
| 212 |
private: |
| 213 |
std::string stdFile; |
| 214 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* xmlDocument; |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
} // namespace App |
| 219 |
|
| 220 |
#endif // APP_PROJECTFILE_H |