OpenASIP  2.0
XMLSerializer.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file XMLSerializer.hh
26  *
27  * Declaration of XMLSerializer class.
28  *
29  * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
30  * @note reviewed 8 Jun 2004 by tr, jm, am, ll
31  * @note rating: red
32  */
33 
34 #ifndef TTA_XML_SERIALIZER_HH
35 #define TTA_XML_SERIALIZER_HH
36 
37 #include <string>
38 
39 #include <xercesc/util/XercesVersion.hpp>
40 
41 #if XERCES_VERSION_MAJOR >= 3
42 #include <xercesc/dom/DOMLSParser.hpp>
43 #else
44 #include <xercesc/dom/DOMBuilder.hpp>
45 #endif
46 #include <xercesc/dom/DOMDocument.hpp>
47 
48 #include "Serializable.hh"
49 #include "Serializer.hh"
50 #include "Exception.hh"
51 
52 #if _XERCES_VERSION >= 20200
53 XERCES_CPP_NAMESPACE_USE
54 #endif
55 
56 class ObjectState;
57 
58 /**
59  * This class is used to read and write XML. This is a base class for
60  * different kind of XML serializers.
61  */
63 public:
64  XMLSerializer();
65  virtual ~XMLSerializer();
66 
67  void setSourceFile(const std::string& fileName);
68  void setSourceString(const std::string& source);
69 
70  void setDestinationFile(const std::string& fileName);
71  void setDestinationString(std::string& destination);
72 
73  void setSchemaFile(const std::string& fileName);
74  void setUseSchema(bool useSchema);
75 
76  void setXMLNamespace(std::string nsUri);
77 
78  virtual ObjectState* readState();
79 
80  virtual void writeState(const ObjectState* rootState);
81 
82 protected:
83  std::string sourceFile() const;
84 
85 private:
86  /// Copying forbidden.
88  /// Assignment forbidden.
90 
91  void initializeParser();
92 
93  virtual ObjectState* readFile(const std::string& fileName);
94 
95  virtual ObjectState* readString(const std::string& source);
96 
97  virtual void writeFile(
98  const std::string& fileName, const ObjectState* rootState);
99 
100  virtual void writeString(std::string& target, const ObjectState* rootState);
101 
102  DOMDocument* createDOMDocument(const ObjectState* state) const;
103  void ensureValidStream(const std::string& fileName) const;
104 
105  DOMElement* createDOM(
106  ObjectState* state,
107  DOMDocument* doc) const;
108  ObjectState* createState(const DOMNode* node) const;
109  bool hasChildElementNodes(const DOMNode* node) const;
110 
111  /// Source file path.
112  std::string sourceFile_;
113  /// Destination file path.
114  std::string destinationFile_;
115  /// Schema file path.
116  std::string schemaFile_;
117  /// Indicates if xml file is validated using schema.
119  /// The parser that checks the XML file for errors with the Schema.
120 #if XERCES_VERSION_MAJOR >= 3
121  DOMLSParser* parser_;
122 #else
123  DOMBuilder* parser_;
124 #endif
125  /// Implementation of the DOM.
126  DOMImplementation* domImplementation_;
127 
128  /// Source string to read.
129  const std::string* sourceString_;
130  /// Destination string to write.
131  std::string* destinationString_;
132 
133  /// XML namespace URI
134  std::string nsUri_;
135 };
136 
137 #endif
138 
XMLSerializer::readString
virtual ObjectState * readString(const std::string &source)
Definition: XMLSerializer.cc:301
XMLSerializer::XMLSerializer
XMLSerializer()
Definition: XMLSerializer.cc:78
Serializer.hh
XMLSerializer::writeString
virtual void writeString(std::string &target, const ObjectState *rootState)
Definition: XMLSerializer.cc:485
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
Exception.hh
XMLSerializer::schemaFile_
std::string schemaFile_
Schema file path.
Definition: XMLSerializer.hh:116
XMLSerializer::operator=
XMLSerializer & operator=(const XMLSerializer &)
Assignment forbidden.
XMLSerializer::sourceFile
std::string sourceFile() const
Definition: XMLSerializer.cc:526
XMLSerializer::sourceFile_
std::string sourceFile_
Source file path.
Definition: XMLSerializer.hh:112
ObjectState
Definition: ObjectState.hh:59
XMLSerializer::ensureValidStream
void ensureValidStream(const std::string &fileName) const
Definition: XMLSerializer.cc:587
XMLSerializer::hasChildElementNodes
bool hasChildElementNodes(const DOMNode *node) const
Definition: XMLSerializer.cc:695
XMLSerializer::writeFile
virtual void writeFile(const std::string &fileName, const ObjectState *rootState)
Definition: XMLSerializer.cc:435
XMLSerializer::readFile
virtual ObjectState * readFile(const std::string &fileName)
Definition: XMLSerializer.cc:368
XMLSerializer::initializeParser
void initializeParser()
Definition: XMLSerializer.cc:234
XMLSerializer::setSchemaFile
void setSchemaFile(const std::string &fileName)
Definition: XMLSerializer.cc:168
XMLSerializer::domImplementation_
DOMImplementation * domImplementation_
Implementation of the DOM.
Definition: XMLSerializer.hh:126
XMLSerializer::readState
virtual ObjectState * readState()
Definition: XMLSerializer.cc:200
XMLSerializer::nsUri_
std::string nsUri_
XML namespace URI.
Definition: XMLSerializer.hh:134
XMLSerializer::setDestinationFile
void setDestinationFile(const std::string &fileName)
Definition: XMLSerializer.cc:142
TCETools::Serializer
Definition: Serializer.hh:45
XMLSerializer::setUseSchema
void setUseSchema(bool useSchema)
Definition: XMLSerializer.cc:179
XMLSerializer::createDOMDocument
DOMDocument * createDOMDocument(const ObjectState *state) const
Definition: XMLSerializer.cc:537
XMLSerializer::createDOM
DOMElement * createDOM(ObjectState *state, DOMDocument *doc) const
Definition: XMLSerializer.cc:609
XMLSerializer::setXMLNamespace
void setXMLNamespace(std::string nsUri)
Definition: XMLSerializer.cc:188
XMLSerializer::parser_
DOMBuilder * parser_
The parser that checks the XML file for errors with the Schema.
Definition: XMLSerializer.hh:123
Serializable.hh
XMLSerializer::setDestinationString
void setDestinationString(std::string &destination)
Definition: XMLSerializer.cc:156
XMLSerializer::useSchema_
bool useSchema_
Indicates if xml file is validated using schema.
Definition: XMLSerializer.hh:118
XMLSerializer::sourceString_
const std::string * sourceString_
Source string to read.
Definition: XMLSerializer.hh:129
XMLSerializer::destinationFile_
std::string destinationFile_
Destination file path.
Definition: XMLSerializer.hh:114
XMLSerializer::~XMLSerializer
virtual ~XMLSerializer()
Definition: XMLSerializer.cc:101
XMLSerializer::createState
ObjectState * createState(const DOMNode *node) const
Definition: XMLSerializer.cc:653
XMLSerializer::setSourceString
void setSourceString(const std::string &source)
Definition: XMLSerializer.cc:128
XMLSerializer::destinationString_
std::string * destinationString_
Destination string to write.
Definition: XMLSerializer.hh:131
XMLSerializer
Definition: XMLSerializer.hh:62
XMLSerializer::writeState
virtual void writeState(const ObjectState *rootState)
Definition: XMLSerializer.cc:219