ome-common  5.5.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/xml/dom/Element.h>
59 #include <ome/common/xml/dom/NodeList.h>
60 #include <ome/common/xml/dom/Wrapper.h>
61 #include <ome/common/xml/EntityResolver.h>
62 #include <ome/common/xml/String.h>
63 
64 namespace ome
65 {
66  namespace common
67  {
68  namespace xml
69  {
73  namespace dom
74  {
75 
83  class Document : public Wrapper<xercesc::DOMDocument, Node>
84  {
85  public:
90  Wrapper<xercesc::DOMDocument, Node>()
91  {
92  }
93 
99  Document (const Document& document):
100  Wrapper<xercesc::DOMDocument, Node>(document)
101  {
102  }
103 
110  Wrapper<xercesc::DOMDocument, Node>(base)
111  {
112  }
113 
121  bool managed):
122  Wrapper<xercesc::DOMDocument, Node>(managed ?
123  Wrapper<xercesc::DOMDocument, Node>(document, std::mem_fun(&base_element_type::release)) :
124  Wrapper<xercesc::DOMDocument, Node>(document, &ome::common::xml::dom::detail::unmanaged<base_element_type>))
125  {
126  }
127 
135  bool managed):
136  Wrapper<xercesc::DOMDocument, Node>(managed ?
137  Wrapper<xercesc::DOMDocument, Node>(base, std::mem_fun(&base_element_type::release)) :
138  Wrapper<xercesc::DOMDocument, Node>(base, &ome::common::xml::dom::detail::unmanaged<base_element_type>))
139  {
140  }
141 
144  {
145  }
146 
153  Document&
155  {
157  return *this;
158  }
159 
167  Element
168  createElementNS(const std::string& ns,
169  const std::string& name)
170  {
171  common::xml::String xns(ns);
172  common::xml::String xname(name);
173 
174  return Element((*this)->createElementNS(xns, xname), false);
175  }
176 
183  Node
184  createComment(const std::string& comment)
185  {
186  common::xml::String text(comment);
187 
188  xercesc::DOMNode *node = dynamic_cast<xercesc::DOMNode *>((*this)->createComment(text));
189  return Node(node, false);
190  }
191 
198  Element
199  createElement(const std::string& name)
200  {
201  common::xml::String xname(name);
202 
203  return Element((*this)->createElement(xname), false);
204  }
205 
211  Element
213  {
214  return Element((*this)->getDocumentElement(), false);
215  }
216 
223  NodeList
224  getElementsByTagName(const std::string& name)
225  {
226  return (*this)->getElementsByTagName(String(name));
227  }
228  };
229 
246  {
248  xercesc::XercesDOMParser::ValSchemes validationScheme;
252  bool doSchema;
259 
262  validationScheme(xercesc::XercesDOMParser::Val_Auto),
263  doNamespaces(true),
264  doSchema(true),
265  handleMultipleImports(true),
266  validationSchemaFullChecking(true),
267  createEntityReferenceNodes(true)
268  {
269  }
270  };
271 
278  Document
279  createEmptyDocument(const std::string& qualifiedName);
280 
289  Document
290  createEmptyDocument(const std::string& namespaceURI,
291  const std::string& qualifiedName);
292 
301  Document
302  createDocument(const boost::filesystem::path& file,
303  EntityResolver& resolver,
304  const ParseParameters& params = ParseParameters());
305 
315  Document
316  createDocument(const std::string& text,
317  EntityResolver& resolver,
318  const ParseParameters& params = ParseParameters(),
319  const std::string& id = "membuf");
320 
330  Document
331  createDocument(std::istream& stream,
332  EntityResolver& resolver,
333  const ParseParameters& params = ParseParameters(),
334  const std::string& id = "streambuf");
335 
352  {
358  bool comments;
364  bool entities;
376  bool validate;
381 
384  canonicalForm(false),
385  CDATASections(true),
386  comments(true),
387  datatypeNormalization(false),
388  discardDefaultContent(true),
389  entities(false),
390  namespaces(true),
391  namespaceDeclarations(true),
392  normalizeCharacters(false),
393  prettyPrint(false),
394  splitCDATASections(true),
395  validate(true),
396  whitespace(false),
397  xmlDeclaration(true)
398  {
399  }
400  };
401 
409  void
410  writeNode(xercesc::DOMNode& node,
411  const boost::filesystem::path& file,
412  const WriteParameters& params = WriteParameters());
413 
421  void
422  writeNode(xercesc::DOMNode& node,
423  std::ostream& stream,
424  const WriteParameters& params = WriteParameters());
425 
433  void
434  writeNode(xercesc::DOMNode& node,
435  std::string& text,
436  const WriteParameters& params = WriteParameters());
437 
445  void
446  writeNode(Node& node,
447  const boost::filesystem::path& file,
448  const WriteParameters& params = WriteParameters());
449 
457  void
458  writeNode(Node& node,
459  std::ostream& stream,
460  const WriteParameters& params = WriteParameters());
461 
469  void
470  writeNode(Node& node,
471  std::string& text,
472  const WriteParameters& params = WriteParameters());
473 
481  void
482  writeDocument(Document& document,
483  const boost::filesystem::path& file,
484  const WriteParameters& params = WriteParameters());
485 
493  void
494  writeDocument(Document& document,
495  std::ostream& stream,
496  const WriteParameters& params = WriteParameters());
497 
505  void
506  writeDocument(Document& document,
507  std::string& text,
508  const WriteParameters& params = WriteParameters());
509 
510  }
511  }
512  }
513 }
514 
515 #endif // OME_COMMON_XML_DOM_DOCUMENT_H
516 
517 /*
518  * Local Variables:
519  * mode:C++
520  * End:
521  */
bool discardDefaultContent
Discard defaults (discard-default-content).
Definition: Document.h:362
Node createComment(const std::string &comment)
Create Comment.
Definition: Document.h:184
bool comments
Retain comments (comments).
Definition: Document.h:358
bool doSchema
Use schemas?
Definition: Document.h:252
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:356
Xerces DOM class wrapper.
Definition: Wrapper.h:72
bool whitespace
Retain whitespace (element-content-whitespace).
Definition: Document.h:378
DOM Node wrapper.
Definition: Node.h:70
bool doNamespaces
Use namespaces?
Definition: Document.h:250
DOM Document wrapper.
Definition: Document.h:83
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:376
bool canonicalForm
Canonicalize document (canonical-form).
Definition: Document.h:354
bool datatypeNormalization
Datatype normalization (datatype-normalization).
Definition: Document.h:360
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:364
WriteParameters()
Constructor.
Definition: Document.h:383
Element createElement(const std::string &name)
Create Element without namespace.
Definition: Document.h:199
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:134
bool namespaces
Namespace processing (namespaces).
Definition: Document.h:366
~Document()
Destructor.
Definition: Document.h:143
bool handleMultipleImports
Handle multiple imports?
Definition: Document.h:254
bool createEntityReferenceNodes
Create entity reference nodes?
Definition: Document.h:258
Document(const Wrapper< xercesc::DOMDocument, Node >::base_type &base)
Copy construct a Document.
Definition: Document.h:109
Open Microscopy Environment C++.
Definition: base64.h:48
Document(const Document &document)
Copy construct a Document.
Definition: Document.h:99
Document(Wrapper< xercesc::DOMDocument, Node >::element_type *document, bool managed)
Construct a Document from a xercesc::DOMDocument *.
Definition: Document.h:120
bool prettyPrint
Pretty-print (format-pretty-print).
Definition: Document.h:372
Element getDocumentElement()
Get the root element of this document.
Definition: Document.h:212
Document & operator=(const Document &wrapped)
Assign a Document.
Definition: Document.h:154
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:89
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:224
Xerces string wrapper.
Definition: String.h:74
ParseParameters()
Constructor.
Definition: Document.h:261
bool xmlDeclaration
Require XML declaration (xml-declaration).
Definition: Document.h:380
bool normalizeCharacters
Normalize characters.
Definition: Document.h:370
DOM NodeList wrapper.
Definition: NodeList.h:69
bool validationSchemaFullChecking
Do full checking during validation?
Definition: Document.h:256
Element createElementNS(const std::string &ns, const std::string &name)
Create Element with namespace.
Definition: Document.h:168
Parameters controlling DOM writing.
Definition: Document.h:245
bool splitCDATASections
Split CDATA sections (split-cdata-sections).
Definition: Document.h:374
Parameters controlling DOM writing.
Definition: Document.h:351
xercesc::XercesDOMParser::ValSchemes validationScheme
Validation scheme.
Definition: Document.h:248
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:368