HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XmlIo.h
Go to the documentation of this file.
1 //
2 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
4 //
5 
6 #ifndef MATERIALX_XMLIO_H
7 #define MATERIALX_XMLIO_H
8 
9 /// @file
10 /// Support for the MTLX file format
11 
12 #include <MaterialXCore/Library.h>
13 
14 #include <MaterialXCore/Document.h>
15 
16 #include <MaterialXFormat/Export.h>
17 #include <MaterialXFormat/File.h>
18 
20 
21 class XmlReadOptions;
22 
23 extern MX_FORMAT_API const string MTLX_EXTENSION;
24 
25 /// A standard function that reads from an XML file into a Document, with
26 /// optional search path and read options.
27 using XmlReadFunction = std::function<void(DocumentPtr, const FilePath&, const FileSearchPath&, const XmlReadOptions*)>;
28 
29 /// @class XmlReadOptions
30 /// A set of options for controlling the behavior of XML read functions.
32 {
33  public:
36 
37  /// If true, then XML comments will be read into documents as comment elements.
38  /// Defaults to false.
40 
41  /// If true, then documents from earlier versions of MaterialX will be upgraded
42  /// to the current version. Defaults to true.
44 
45  /// If provided, this function will be invoked when an XInclude reference
46  /// needs to be read into a document. Defaults to readFromXmlFile.
48 
49  /// The vector of parent XIncludes at the scope of the current document.
50  /// Defaults to an empty vector.
52 };
53 
54 /// @class XmlWriteOptions
55 /// A set of options for controlling the behavior of XML write functions.
57 {
58  public:
61 
62  /// If true, elements with source file markings will be written as
63  /// XIncludes rather than explicit data. Defaults to true.
65 
66  /// If provided, this function will be used to exclude specific elements
67  /// (those returning false) from the write operation. Defaults to nullptr.
69 };
70 
71 /// @class ExceptionParseError
72 /// An exception that is thrown when a requested document cannot be parsed.
74 {
75  public:
77 };
78 
79 /// @class ExceptionFileMissing
80 /// An exception that is thrown when a requested file cannot be opened.
82 {
83  public:
85 };
86 
87 /// @name Read Functions
88 /// @{
89 
90 /// Read a Document as XML from the given character buffer.
91 /// @param doc The Document into which data is read.
92 /// @param buffer The character buffer from which data is read.
93 /// @param searchPath An optional sequence of file paths that will be applied
94 /// in order when searching for the given file and its includes. This
95 /// argument can be supplied either as a FileSearchPath, or as a standard
96 /// string with paths separated by the PATH_SEPARATOR character.
97 /// @param readOptions An optional pointer to an XmlReadOptions object.
98 /// If provided, then the given options will affect the behavior of the
99 /// read function. Defaults to a null pointer.
100 /// @throws ExceptionParseError if the document cannot be parsed.
101 MX_FORMAT_API void readFromXmlBuffer(DocumentPtr doc, const char* buffer, FileSearchPath searchPath = FileSearchPath(), const XmlReadOptions* readOptions = nullptr);
102 
103 /// Read a Document as XML from the given input stream.
104 /// @param doc The Document into which data is read.
105 /// @param stream The input stream from which data is read.
106 /// @param searchPath An optional sequence of file paths that will be applied
107 /// in order when searching for the given file and its includes. This
108 /// argument can be supplied either as a FileSearchPath, or as a standard
109 /// string with paths separated by the PATH_SEPARATOR character.
110 /// @param readOptions An optional pointer to an XmlReadOptions object.
111 /// If provided, then the given options will affect the behavior of the
112 /// read function. Defaults to a null pointer.
113 /// @throws ExceptionParseError if the document cannot be parsed.
114 MX_FORMAT_API void readFromXmlStream(DocumentPtr doc, std::istream& stream, FileSearchPath searchPath = FileSearchPath(), const XmlReadOptions* readOptions = nullptr);
115 
116 /// Read a Document as XML from the given filename.
117 /// @param doc The Document into which data is read.
118 /// @param filename The filename from which data is read. This argument can
119 /// be supplied either as a FilePath or a standard string.
120 /// @param searchPath An optional sequence of file paths that will be applied
121 /// in order when searching for the given file and its includes. This
122 /// argument can be supplied either as a FileSearchPath, or as a standard
123 /// string with paths separated by the PATH_SEPARATOR character.
124 /// @param readOptions An optional pointer to an XmlReadOptions object.
125 /// If provided, then the given options will affect the behavior of the read
126 /// function. Defaults to a null pointer.
127 /// @throws ExceptionParseError if the document cannot be parsed.
128 /// @throws ExceptionFileMissing if the file cannot be opened.
131  FileSearchPath searchPath = FileSearchPath(),
132  const XmlReadOptions* readOptions = nullptr);
133 
134 /// Read a Document as XML from the given string.
135 /// @param doc The Document into which data is read.
136 /// @param str The string from which data is read.
137 /// @param searchPath An optional sequence of file paths that will be applied
138 /// in order when searching for the given file and its includes. This
139 /// argument can be supplied either as a FileSearchPath, or as a standard
140 /// string with paths separated by the PATH_SEPARATOR character.
141 /// @param readOptions An optional pointer to an XmlReadOptions object.
142 /// If provided, then the given options will affect the behavior of the
143 /// read function. Defaults to a null pointer.
144 /// @throws ExceptionParseError if the document cannot be parsed.
145 MX_FORMAT_API void readFromXmlString(DocumentPtr doc, const string& str, FileSearchPath searchPath = FileSearchPath(), const XmlReadOptions* readOptions = nullptr);
146 
147 /// @}
148 /// @name Write Functions
149 /// @{
150 
151 /// Write a Document as XML to the given output stream.
152 /// @param doc The Document to be written.
153 /// @param stream The output stream to which data is written
154 /// @param writeOptions An optional pointer to an XmlWriteOptions object.
155 /// If provided, then the given options will affect the behavior of the
156 /// write function. Defaults to a null pointer.
157 MX_FORMAT_API void writeToXmlStream(DocumentPtr doc, std::ostream& stream, const XmlWriteOptions* writeOptions = nullptr);
158 
159 /// Write a Document as XML to the given filename.
160 /// @param doc The Document to be written.
161 /// @param filename The filename to which data is written. This argument can
162 /// be supplied either as a FilePath or a standard string.
163 /// @param writeOptions An optional pointer to an XmlWriteOptions object.
164 /// If provided, then the given options will affect the behavior of the
165 /// write function. Defaults to a null pointer.
166 MX_FORMAT_API void writeToXmlFile(DocumentPtr doc, const FilePath& filename, const XmlWriteOptions* writeOptions = nullptr);
167 
168 /// Write a Document as XML to a new string, returned by value.
169 /// @param doc The Document to be written.
170 /// @param writeOptions An optional pointer to an XmlWriteOptions object.
171 /// If provided, then the given options will affect the behavior of the
172 /// write function. Defaults to a null pointer.
173 /// @return The output string, returned by value
174 MX_FORMAT_API string writeToXmlString(DocumentPtr doc, const XmlWriteOptions* writeOptions = nullptr);
175 
176 /// @}
177 /// @name Edit Functions
178 /// @{
179 
180 /// Add an XInclude reference to the top of a Document, creating a generic
181 /// element to hold the reference filename.
182 /// @param doc The Document to be modified.
183 /// @param filename The filename of the XInclude reference to be added.
184 MX_FORMAT_API void prependXInclude(DocumentPtr doc, const FilePath& filename);
185 
186 /// @}
187 
189 
190 #endif
MX_FORMAT_API void readFromXmlStream(DocumentPtr doc, std::istream &stream, FileSearchPath searchPath=FileSearchPath(), const XmlReadOptions *readOptions=nullptr)
GT_API const UT_StringHolder filename
StringVec parentXIncludes
Definition: XmlIo.h:51
Definition: File.h:26
std::function< bool(ConstElementPtr)> ElementPredicate
A standard function taking an ElementPtr and returning a boolean.
Definition: Element.h:66
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
MX_FORMAT_API void writeToXmlFile(DocumentPtr doc, const FilePath &filename, const XmlWriteOptions *writeOptions=nullptr)
#define MX_FORMAT_API
Definition: Export.h:18
GLuint GLuint stream
Definition: glcorearb.h:1832
bool upgradeVersion
Definition: XmlIo.h:43
MX_FORMAT_API const string MTLX_EXTENSION
MX_FORMAT_API void readFromXmlFile(DocumentPtr doc, FilePath filename, FileSearchPath searchPath=FileSearchPath(), const XmlReadOptions *readOptions=nullptr)
MX_FORMAT_API void readFromXmlBuffer(DocumentPtr doc, const char *buffer, FileSearchPath searchPath=FileSearchPath(), const XmlReadOptions *readOptions=nullptr)
Definition: core.h:760
MX_FORMAT_API void writeToXmlStream(DocumentPtr doc, std::ostream &stream, const XmlWriteOptions *writeOptions=nullptr)
XmlReadFunction readXIncludeFunction
Definition: XmlIo.h:47
bool writeXIncludeEnable
Definition: XmlIo.h:64
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
~XmlReadOptions()
Definition: XmlIo.h:35
MX_FORMAT_API void readFromXmlString(DocumentPtr doc, const string &str, FileSearchPath searchPath=FileSearchPath(), const XmlReadOptions *readOptions=nullptr)
~XmlWriteOptions()
Definition: XmlIo.h:60
bool readComments
Definition: XmlIo.h:39
Exception(const string &msg)
Definition: Exception.h:24
MX_FORMAT_API string writeToXmlString(DocumentPtr doc, const XmlWriteOptions *writeOptions=nullptr)
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
MX_FORMAT_API void prependXInclude(DocumentPtr doc, const FilePath &filename)
std::function< void(DocumentPtr, const FilePath &, const FileSearchPath &, const XmlReadOptions *)> XmlReadFunction
Definition: XmlIo.h:27
ElementPredicate elementPredicate
Definition: XmlIo.h:68