ome-common  5.4.0
Document.h
1 /*
2  * #%L
3  * OME-XERCES C++ library for working with Xerces C++.
4  * %%
5  * Copyright © 2006 - 2016 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
39 #ifndef OME_COMMON_XML_DOM_DOCUMENT_H
40 #define OME_COMMON_XML_DOM_DOCUMENT_H
41 
42 #include <ome/common/config.h>
43 
44 #include <cassert>
45 #include <functional>
46 #include <istream>
47 #include <memory>
48 #include <string>
49 #include <ostream>
50 
51 #include <boost/filesystem/path.hpp>
52 
53 #include <xercesc/dom/DOMComment.hpp>
54 #include <xercesc/dom/DOMDocument.hpp>
55 #include <xercesc/dom/DOMNode.hpp>
56 #include <xercesc/parsers/XercesDOMParser.hpp>
57 
58 #include <ome/common/filesystem.h>
59 
60 #include <ome/common/xml/dom/Element.h>
61 #include <ome/common/xml/dom/NodeList.h>
62 #include <ome/common/xml/dom/Wrapper.h>
63 #include <ome/common/xml/EntityResolver.h>
64 #include <ome/common/xml/String.h>
65 
66 namespace ome
67 {
68  namespace common
69  {
70  namespace xml
71  {
75  namespace dom
76  {
77 
85  class Document : public Wrapper<xercesc::DOMDocument, Node>
86  {
87  public:
92  Wrapper<xercesc::DOMDocument, Node>()
93  {
94  }
95 
101  Document (const Document& document):
102  Wrapper<xercesc::DOMDocument, Node>(document)
103  {
104  }
105 
112  Wrapper<xercesc::DOMDocument, Node>(base)
113  {
114  }
115 
123  bool managed):
124  Wrapper<xercesc::DOMDocument, Node>(managed ?
125  Wrapper<xercesc::DOMDocument, Node>(document, std::mem_fun(&base_element_type::release)) :
126  Wrapper<xercesc::DOMDocument, Node>(document, &ome::common::xml::dom::detail::unmanaged<base_element_type>))
127  {
128  }
129 
137  bool managed):
138  Wrapper<xercesc::DOMDocument, Node>(managed ?
139  Wrapper<xercesc::DOMDocument, Node>(base, std::mem_fun(&base_element_type::release)) :
140  Wrapper<xercesc::DOMDocument, Node>(base, &ome::common::xml::dom::detail::unmanaged<base_element_type>))
141  {
142  }
143 
146  {
147  }
148 
155  Document&
157  {
159  return *this;
160  }
161 
169  Element
170  createElementNS(const std::string& ns,
171  const std::string& name)
172  {
173  common::xml::String xns(ns);
174  common::xml::String xname(name);
175 
176  return Element((*this)->createElementNS(xns, xname), false);
177  }
178 
185  Node
186  createComment(const std::string& comment)
187  {
188  common::xml::String text(comment);
189 
190  xercesc::DOMNode *node = dynamic_cast<xercesc::DOMNode *>((*this)->createComment(text));
191  return Node(node, false);
192  }
193 
200  Element
201  createElement(const std::string& name)
202  {
203  common::xml::String xname(name);
204 
205  return Element((*this)->createElement(xname), false);
206  }
207 
213  Element
215  {
216  return Element((*this)->getDocumentElement(), false);
217  }
218 
225  NodeList
226  getElementsByTagName(const std::string& name)
227  {
228  return (*this)->getElementsByTagName(String(name));
229  }
230  };
231 
248  {
250  xercesc::XercesDOMParser::ValSchemes validationScheme;
254  bool doSchema;
261 
264  validationScheme(xercesc::XercesDOMParser::Val_Auto),
265  doNamespaces(true),
266  doSchema(true),
267  handleMultipleImports(true),
268  validationSchemaFullChecking(true),
269  createEntityReferenceNodes(true)
270  {
271  }
272  };
273 
280  Document
281  createEmptyDocument(const std::string& qualifiedName);
282 
291  Document
292  createEmptyDocument(const std::string& namespaceURI,
293  const std::string& qualifiedName);
294 
303  Document
304  createDocument(const boost::filesystem::path& file,
305  EntityResolver& resolver,
306  const ParseParameters& params = ParseParameters());
307 
317  Document
318  createDocument(const std::string& text,
319  EntityResolver& resolver,
320  const ParseParameters& params = ParseParameters(),
321  const std::string& id = "membuf");
322 
332  Document
333  createDocument(std::istream& stream,
334  EntityResolver& resolver,
335  const ParseParameters& params = ParseParameters(),
336  const std::string& id = "streambuf");
337 
354  {
360  bool comments;
366  bool entities;
378  bool validate;
383 
386  canonicalForm(false),
387  CDATASections(true),
388  comments(true),
389  datatypeNormalization(false),
390  discardDefaultContent(true),
391  entities(false),
392  namespaces(true),
393  namespaceDeclarations(true),
394  normalizeCharacters(false),
395  prettyPrint(false),
396  splitCDATASections(true),
397  validate(true),
398  whitespace(false),
399  xmlDeclaration(true)
400  {
401  }
402  };
403 
411  void
412  writeNode(xercesc::DOMNode& node,
413  const boost::filesystem::path& file,
414  const WriteParameters& params = WriteParameters());
415 
423  void
424  writeNode(xercesc::DOMNode& node,
425  std::ostream& stream,
426  const WriteParameters& params = WriteParameters());
427 
435  void
436  writeNode(xercesc::DOMNode& node,
437  std::string& text,
438  const WriteParameters& params = WriteParameters());
439 
447  void
448  writeNode(Node& node,
449  const boost::filesystem::path& file,
450  const WriteParameters& params = WriteParameters());
451 
459  void
460  writeNode(Node& node,
461  std::ostream& stream,
462  const WriteParameters& params = WriteParameters());
463 
471  void
472  writeNode(Node& node,
473  std::string& text,
474  const WriteParameters& params = WriteParameters());
475 
483  void
484  writeDocument(Document& document,
485  const boost::filesystem::path& file,
486  const WriteParameters& params = WriteParameters());
487 
495  void
496  writeDocument(Document& document,
497  std::ostream& stream,
498  const WriteParameters& params = WriteParameters());
499 
507  void
508  writeDocument(Document& document,
509  std::string& text,
510  const WriteParameters& params = WriteParameters());
511 
512  }
513  }
514  }
515 }
516 
517 #endif // OME_COMMON_XML_DOM_DOCUMENT_H
518 
519 /*
520  * Local Variables:
521  * mode:C++
522  * End:
523  */
bool discardDefaultContent
Discard defaults (discard-default-content).
Definition: Document.h:364
Node createComment(const std::string &comment)
Create Comment.
Definition: Document.h:186
bool comments
Retain comments (comments).
Definition: Document.h:360
bool doSchema
Use schemas?
Definition: Document.h:254
std::shared_ptr< base_element_type > base
Wrapped reference.
Definition: Base.h:253
element_type * wrapped
The wrapped type.
Definition: Wrapper.h:264
bool CDATASections
Retain CDATA (cdata-sections).
Definition: Document.h:358
Xerces DOM class wrapper.
Definition: Wrapper.h:72
bool whitespace
Retain whitespace (element-content-whitespace).
Definition: Document.h:380
DOM Node wrapper.
Definition: Node.h:70
bool doNamespaces
Use namespaces?
Definition: Document.h:252
DOM Document wrapper.
Definition: Document.h:85
STL namespace.
DOM Element wrapper.
Definition: Element.h:67
Document createDocument(const boost::filesystem::path &file, EntityResolver &resolver, const ParseParameters &params)
Construct a Document from the content of a file.
Definition: Document.cpp:225
bool validate
Validate if schema available (validate-if-schema).
Definition: Document.h:378
bool canonicalForm
Canonicalize document (canonical-form).
Definition: Document.h:356
bool datatypeNormalization
Datatype normalization (datatype-normalization).
Definition: Document.h:362
Node()
Construct a NULL Node.
Definition: Node.h:79
void writeDocument(Document &document, const boost::filesystem::path &file, const WriteParameters &params)
Write a Document to a file.
Definition: Document.cpp:361
bool entities
Retain entities (entities).
Definition: Document.h:366
WriteParameters()
Constructor.
Definition: Document.h:385
Element createElement(const std::string &name)
Create Element without namespace.
Definition: Document.h:201
Xerces entity resolver.
Definition: EntityResolver.h:66
Document(Wrapper< xercesc::DOMDocument, Node >::base_element_type *base, bool managed)
Construct a Document from a xercesc::DOMNode *.
Definition: Document.h:136
bool namespaces
Namespace processing (namespaces).
Definition: Document.h:368
~Document()
Destructor.
Definition: Document.h:145
bool handleMultipleImports
Handle multiple imports?
Definition: Document.h:256
bool createEntityReferenceNodes
Create entity reference nodes?
Definition: Document.h:260
Boost.Filesystem compatibility.
Document(const Wrapper< xercesc::DOMDocument, Node >::base_type &base)
Copy construct a Document.
Definition: Document.h:111
Open Microscopy Environment C++.
Definition: base64.h:48
Document(const Document &document)
Copy construct a Document.
Definition: Document.h:101
Document(Wrapper< xercesc::DOMDocument, Node >::element_type *document, bool managed)
Construct a Document from a xercesc::DOMDocument *.
Definition: Document.h:122
bool prettyPrint
Pretty-print (format-pretty-print).
Definition: Document.h:374
Element getDocumentElement()
Get the root element of this document.
Definition: Document.h:214
Document & operator=(const Document &wrapped)
Assign a Document.
Definition: Document.h:156
void writeNode(xercesc::DOMNode &node, const boost::filesystem::path &file, const WriteParameters &params)
Write a Node to a file.
Definition: Document.cpp:293
Wrapper & operator=(const Wrapper &wrapped)
Assign a Wrapper.
Definition: Wrapper.h:208
Document()
Construct a NULL Document.
Definition: Document.h:91
xercesc::DOMNode base_element_type
Base element type (root type of the wrapped type).
Definition: Base.h:82
NodeList getElementsByTagName(const std::string &name)
Get child elements with a given tag name.
Definition: Document.h:226
Xerces string wrapper.
Definition: String.h:74
ParseParameters()
Constructor.
Definition: Document.h:263
bool xmlDeclaration
Require XML declaration (xml-declaration).
Definition: Document.h:382
bool normalizeCharacters
Normalize characters.
Definition: Document.h:372
DOM NodeList wrapper.
Definition: NodeList.h:69
bool validationSchemaFullChecking
Do full checking during validation?
Definition: Document.h:258
Element createElementNS(const std::string &ns, const std::string &name)
Create Element with namespace.
Definition: Document.h:170
Parameters controlling DOM writing.
Definition: Document.h:247
bool splitCDATASections
Split CDATA sections (split-cdata-sections).
Definition: Document.h:376
Parameters controlling DOM writing.
Definition: Document.h:353
xercesc::XercesDOMParser::ValSchemes validationScheme
Validation scheme.
Definition: Document.h:250
Document createEmptyDocument(const std::string &qualifiedName)
Construct an empty Document.
Definition: Document.cpp:200
bool namespaceDeclarations
Include namespace declaration attributes (namespace-declarations).
Definition: Document.h:370