OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
XMLSerializer Class Reference

#include <XMLSerializer.hh>

Inheritance diagram for XMLSerializer:
Inheritance graph
Collaboration diagram for XMLSerializer:
Collaboration graph

Public Member Functions

 XMLSerializer ()
 
virtual ~XMLSerializer ()
 
void setSourceFile (const std::string &fileName)
 
void setSourceString (const std::string &source)
 
void setDestinationFile (const std::string &fileName)
 
void setDestinationString (std::string &destination)
 
void setSchemaFile (const std::string &fileName)
 
void setUseSchema (bool useSchema)
 
void setXMLNamespace (std::string nsUri)
 
virtual ObjectStatereadState ()
 
virtual void writeState (const ObjectState *rootState)
 
- Public Member Functions inherited from TCETools::Serializer
virtual ~Serializer ()
 

Protected Member Functions

std::string sourceFile () const
 

Private Member Functions

 XMLSerializer (const XMLSerializer &)
 Copying forbidden.
 
XMLSerializeroperator= (const XMLSerializer &)
 Assignment forbidden.
 
void initializeParser ()
 
virtual ObjectStatereadFile (const std::string &fileName)
 
virtual ObjectStatereadString (const std::string &source)
 
virtual void writeFile (const std::string &fileName, const ObjectState *rootState)
 
virtual void writeString (std::string &target, const ObjectState *rootState)
 
DOMDocument * createDOMDocument (const ObjectState *state) const
 
void ensureValidStream (const std::string &fileName) const
 
DOMElement * createDOM (ObjectState *state, DOMDocument *doc) const
 
ObjectStatecreateState (const DOMNode *node) const
 
bool hasChildElementNodes (const DOMNode *node) const
 

Private Attributes

std::string sourceFile_
 Source file path.
 
std::string destinationFile_
 Destination file path.
 
std::string schemaFile_
 Schema file path.
 
bool useSchema_
 Indicates if xml file is validated using schema.
 
DOMBuilder * parser_
 The parser that checks the XML file for errors with the Schema.
 
DOMImplementation * domImplementation_
 Implementation of the DOM.
 
const std::string * sourceString_
 Source string to read.
 
std::string * destinationString_
 Destination string to write.
 
std::string nsUri_
 XML namespace URI.
 

Detailed Description

This class is used to read and write XML. This is a base class for different kind of XML serializers.

Definition at line 62 of file XMLSerializer.hh.

Constructor & Destructor Documentation

◆ XMLSerializer() [1/2]

XMLSerializer::XMLSerializer ( )

Constructor.

Definition at line 78 of file XMLSerializer.cc.

78 :
80 useSchema_(false), parser_(NULL), domImplementation_(NULL),
81 sourceString_(NULL), destinationString_(NULL), nsUri_("") {
82
83 XMLPlatformUtils::Initialize();
84
85 const XMLCh gLS[] = {chLatin_L, chLatin_S, chNull};
87 DOMImplementationRegistry::getDOMImplementation(gLS);
88
89#if XERCES_VERSION_MAJOR >= 3
90 parser_ = domImplementation_->createLSParser(
91 DOMImplementationLS::MODE_SYNCHRONOUS, 0);
92#else
93 parser_ = domImplementation_->createDOMBuilder(
94 DOMImplementationLS::MODE_SYNCHRONOUS, 0);
95#endif
96}
DOMImplementation * domImplementation_
Implementation of the DOM.
const std::string * sourceString_
Source string to read.
std::string schemaFile_
Schema file path.
bool useSchema_
Indicates if xml file is validated using schema.
std::string sourceFile_
Source file path.
std::string destinationFile_
Destination file path.
DOMBuilder * parser_
The parser that checks the XML file for errors with the Schema.
std::string * destinationString_
Destination string to write.
std::string nsUri_
XML namespace URI.

References domImplementation_, and parser_.

◆ ~XMLSerializer()

XMLSerializer::~XMLSerializer ( )
virtual

Destructor.

Definition at line 101 of file XMLSerializer.cc.

101 {
102 delete parser_;
103 XMLPlatformUtils::Terminate();
104}

References parser_.

◆ XMLSerializer() [2/2]

XMLSerializer::XMLSerializer ( const XMLSerializer )
private

Copying forbidden.

Member Function Documentation

◆ createDOM()

DOMElement * XMLSerializer::createDOM ( ObjectState state,
DOMDocument *  doc 
) const
private

Creates DOM tree from the given ObjectState tree.

Parameters
stateRoot node of the ObjectState tree.
docThe DOMDocument to which the created DOM tree belongs.

Definition at line 609 of file XMLSerializer.cc.

611 {
612
613 XMLCh* elemName = Conversion::toXMLCh(state->name());
614 DOMElement* rootElem =
615 doc->createElement(elemName);
616 XMLString::release(&elemName);
617
618 for (int i = 0; i < state->attributeCount(); i++) {
619 ObjectState::Attribute* attribute = state->attribute(i);
620 XMLCh* attribName = Conversion::toXMLCh(attribute->name);
621 XMLCh* attribValue = Conversion::toXMLCh(attribute->value);
622 rootElem->setAttribute(attribName, attribValue);
623 XMLString::release(&attribName);
624 XMLString::release(&attribValue);
625 }
626
627 if (state->childCount() == 0) {
628 if (state->stringValue() != "") {
629 XMLCh* text = Conversion::toXMLCh(state->stringValue());
630 DOMText* value = doc->createTextNode(text);
631 rootElem->appendChild(value);
632 XMLString::release(&text);
633 }
634 }
635
636 for (int i = 0; i < state->childCount(); i++) {
637 ObjectState* childState = state->child(i);
638 rootElem->appendChild(createDOM(childState, doc));
639 }
640
641 return rootElem;
642}
static XMLCh * toXMLCh(const std::string &string)
Attribute * attribute(int index) const
int attributeCount() const
ObjectState * child(int index) const
std::string stringValue() const
std::string name() const
int childCount() const
DOMElement * createDOM(ObjectState *state, DOMDocument *doc) const
Struct for describing an attribute of the XML element.
std::string value
Value of the attribute.
std::string name
Name of the attribute.

References ObjectState::attribute(), ObjectState::attributeCount(), ObjectState::child(), ObjectState::childCount(), createDOM(), ObjectState::Attribute::name, ObjectState::name(), ObjectState::stringValue(), Conversion::toXMLCh(), and ObjectState::Attribute::value.

Referenced by createDOM(), and createDOMDocument().

Here is the call graph for this function:

◆ createDOMDocument()

DOMDocument * XMLSerializer::createDOMDocument ( const ObjectState state) const
private

Creates a DOM tree according to the given ObjectState tree.

Parameters
stateRoot node of the ObjectState tree.
Returns
The created DOM tree.

Definition at line 537 of file XMLSerializer.cc.

537 {
538
539 XMLCh* nsUri = NULL;
540 if (!nsUri_.empty()) {
542 }
543 XMLCh* docName = Conversion::toXMLCh(state->name());
544 DOMDocument* doc = domImplementation_->createDocument(nsUri, docName, 0);
545 XMLString::release(&docName);
546 if (nsUri != NULL) {
547 XMLString::release(&nsUri);
548 nsUri = NULL;
549 }
550 DOMElement* rootElem = doc->getDocumentElement();
551
552 for (int i = 0; i < state->attributeCount(); i++) {
553 ObjectState::Attribute* attribute = state->attribute(i);
554 XMLCh* attribName = Conversion::toXMLCh(attribute->name);
555 XMLCh* attribValue = Conversion::toXMLCh(attribute->value);
556 rootElem->setAttribute(attribName, attribValue);
557 XMLString::release(&attribName);
558 XMLString::release(&attribValue);
559 }
560
561 string value = state->stringValue();
562 if (value != "") {
563 XMLCh* nodeValue = Conversion::toXMLCh(value);
564 DOMText* valueNode = doc->createTextNode(nodeValue);
565 rootElem->appendChild(valueNode);
566 XMLString::release(&nodeValue);
567 }
568
569 for (int i = 0; i < state->childCount(); i++) {
570 ObjectState* childState = state->child(i);
571 DOMElement* childElement = createDOM(childState, doc);
572 rootElem->appendChild(childElement);
573 }
574
575 return doc;
576}

References ObjectState::attribute(), ObjectState::attributeCount(), ObjectState::child(), ObjectState::childCount(), createDOM(), domImplementation_, ObjectState::Attribute::name, ObjectState::name(), nsUri_, ObjectState::stringValue(), Conversion::toXMLCh(), and ObjectState::Attribute::value.

Referenced by writeFile(), and writeString().

Here is the call graph for this function:

◆ createState()

ObjectState * XMLSerializer::createState ( const DOMNode *  node) const
private

Creates a one-to-one ObjectState object (tree) according to the given DOMNode.

Parameters
nodeDOMNode from which the ObjectState is created.
Returns
The created ObjectState instance (tree).

Definition at line 653 of file XMLSerializer.cc.

653 {
654 ObjectState* state =
655 new ObjectState(Conversion::XMLChToString(node->getNodeName()));
656 DOMNamedNodeMap* attributes = node->getAttributes();
657
658 // set the attributes
659 if (attributes != NULL) {
660 for (unsigned int i = 0; i < attributes->getLength(); i++) {
661 DOMNode* attribute = attributes->item(i);
662 state->setAttribute(
663 Conversion::XMLChToString(attribute->getNodeName()),
664 Conversion::XMLChToString(attribute->getNodeValue()));
665 }
666 }
667
668 DOMNode* child = node->getFirstChild();
669 while (child != NULL) {
670 if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
671 state->addChild(createState(child));
672 }
673 child = child->getNextSibling();
674 }
675
676 // if node has no child elements, read its value
677 if (!hasChildElementNodes(node)) {
678 if (node->getFirstChild() != NULL) {
679 state->setValue(Conversion::XMLChToString(node->getFirstChild()->
680 getNodeValue()));
681 }
682 }
683
684 return state;
685}
static std::string XMLChToString(const XMLCh *source)
void setAttribute(const std::string &name, const std::string &value)
void setValue(const std::string &value)
void addChild(ObjectState *child)
bool hasChildElementNodes(const DOMNode *node) const
ObjectState * createState(const DOMNode *node) const

References ObjectState::addChild(), createState(), hasChildElementNodes(), ObjectState::setAttribute(), ObjectState::setValue(), and Conversion::XMLChToString().

Referenced by createState(), readFile(), and readString().

Here is the call graph for this function:

◆ ensureValidStream()

void XMLSerializer::ensureValidStream ( const std::string &  fileName) const
private

Checks that the file is OK for reading.

Parameters
fileNameName of the XML file.
Exceptions
UnreachableStreamIf the given file cannot be read.

Definition at line 587 of file XMLSerializer.cc.

587 {
588 ifstream filestream;
589 filestream.open(fileName.c_str());
590 if(!filestream.is_open() || !filestream.good()) {
591 filestream.close();
592 string procName = "XMLSerializer::ensureValidStream";
593 string errorMessage = "Cannot open file \'";
594 errorMessage += fileName;
595 errorMessage += "\'.";
596 throw UnreachableStream(__FILE__, __LINE__, procName, errorMessage);
597 }
598 filestream.close();
599}

Referenced by initializeParser(), and readFile().

◆ hasChildElementNodes()

bool XMLSerializer::hasChildElementNodes ( const DOMNode *  node) const
private

Returns true if the given node has child elements.

Parameters
nodeNode.
Returns
True if the node has child elements, otherwise false.

Definition at line 695 of file XMLSerializer.cc.

695 {
696 DOMNode* child = node->getFirstChild();
697 while (child != NULL) {
698 if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
699 return true;
700 }
701 child = child->getNextSibling();
702 }
703 return false;
704}

Referenced by createState().

◆ initializeParser()

void XMLSerializer::initializeParser ( )
private

Initializes the XML parser.

Definition at line 234 of file XMLSerializer.cc.

234 {
235
236 if (useSchema_ && schemaFile_ == "") {
237 string errorMsg = "No schema file set.";
238 throw SerializerException(__FILE__, __LINE__, __func__, errorMsg);
239 }
240
241 if (useSchema_) {
242
243 try {
245 } catch (UnreachableStream& exception) {
246 SerializerException error(__FILE__, __LINE__, __func__,
247 exception.errorMessage());
248
249 error.setCause(exception);
250 throw error;
251 }
252
253 string absoluteSchemaFile = schemaFile_;
254
255 // convert the given schemaFile path to absolute path if it isn't
257 absoluteSchemaFile = FileSystem::currentWorkingDir() +
259 }
260
261 XMLCh* filePath = Conversion::toXMLCh(absoluteSchemaFile);
262#if XERCES_VERSION_MAJOR >= 3
263 parser_->getDomConfig()->setParameter(XMLUni::fgXercesSchema, true);
264 parser_->getDomConfig()->setParameter(XMLUni::fgDOMValidate, true);
265 parser_->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, true);
266 parser_->getDomConfig()->setParameter(
267 XMLUni::fgXercesSchemaFullChecking, true);
268 parser_->getDomConfig()->setParameter(
269 XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
270 filePath);
271#else
272 parser_->setFeature(XMLUni::fgXercesSchema, true);
273 parser_->setFeature(XMLUni::fgDOMValidation, true);
274 parser_->setFeature(XMLUni::fgDOMNamespaces, true);
275 parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
276 parser_->setProperty(
277 XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
278 filePath);
279#endif
280
281 XMLString::release(&filePath);
282 }
283#if XERCES_VERSION_MAJOR >= 3
284 parser_->getDomConfig()->setParameter(
285 XMLUni::fgDOMElementContentWhitespace, false);
286 parser_->getDomConfig()->setParameter(XMLUni::fgDOMComments, false);
287#else
288 parser_->setFeature(XMLUni::fgDOMWhitespaceInElementContent, false);
289 parser_->setFeature(XMLUni::fgDOMComments, false);
290#endif
291}
#define __func__
std::string errorMessage() const
Definition Exception.cc:123
static bool isRelativePath(const std::string &pathName)
static const std::string DIRECTORY_SEPARATOR
static std::string currentWorkingDir()
void ensureValidStream(const std::string &fileName) const

References __func__, FileSystem::currentWorkingDir(), FileSystem::DIRECTORY_SEPARATOR, ensureValidStream(), Exception::errorMessage(), FileSystem::isRelativePath(), parser_, schemaFile_, Exception::setCause(), Conversion::toXMLCh(), and useSchema_.

Referenced by readFile(), and readString().

Here is the call graph for this function:

◆ operator=()

XMLSerializer & XMLSerializer::operator= ( const XMLSerializer )
private

Assignment forbidden.

◆ readFile()

ObjectState * XMLSerializer::readFile ( const std::string &  sourceFile)
privatevirtual

Reads current XML file set and creates an ObjectState tree according to it.

The XML file is validated with the current schema file if the setting is on.

Returns
Root node of the created ObjectState tree.
Exceptions
SerializerExceptionIf an error occurs while reading.

Definition at line 368 of file XMLSerializer.cc.

368 {
370 DOMDocument* dom = NULL;
371
372 if (sourceFile == "") {
373 string errorMsg = "No source file set.";
374 throw SerializerException(__FILE__, __LINE__, __func__, errorMsg);
375 }
376
377 // check validity of file stream
378 try {
380 } catch (UnreachableStream& exception) {
381 throw SerializerException(__FILE__, __LINE__, __func__,
382 exception.errorMessage());
383 }
384
386#if XERCES_VERSION_MAJOR >= 3
387 parser_->getDomConfig()->setParameter(
388 XMLUni::fgDOMErrorHandler, errHandler);
389#else
390 parser_->setErrorHandler(errHandler);
391#endif
392
393 try {
394 parser_->resetDocumentPool();
395 dom = parser_->parseURI(sourceFile.c_str());
396 } catch (const ErrorInExternalFile& exception) {
397
398 string errorLog = errHandler->errorLog();
399 delete errHandler;
400
401 throw SerializerException(__FILE__, __LINE__, __func__, errorLog);
402 }
403
404 if (dom == NULL || dom->getDocumentElement() == NULL) {
405 delete errHandler;
407 __FILE__, __LINE__, __func__, "Illegal file: " + sourceFile);
408
409 }
410
411
412 int errors(errHandler->errorCount());
413
414 if (errors > 0) {
415 // if there were such errors in xml file or schema file that
416 // exception was not thrown when parseURI was called
417 string errorLog = errHandler->errorLog();
418 delete errHandler;
419 throw SerializerException(__FILE__, __LINE__, __func__, errorLog);
420 }
421
422 ObjectState* rootState = createState(dom->getDocumentElement());
423
424 delete errHandler;
425 return rootState;
426}
std::string sourceFile() const
void initializeParser()

References __func__, createState(), ensureValidStream(), DOMBuilderErrorHandler::errorCount(), DOMBuilderErrorHandler::errorLog(), Exception::errorMessage(), initializeParser(), parser_, and sourceFile().

Referenced by readState().

Here is the call graph for this function:

◆ readState()

ObjectState * XMLSerializer::readState ( )
virtual

Reads object state from a file or string.

File is read if a source file is set with setSourceFile(). String is read if a source string is set with setSourceString(). Only the last file/string set will be read.

Implements TCETools::Serializer.

Reimplemented in IPXactSerializer, GUIOptionsSerializer, BEMSerializer, IDF::IDFSerializer, and ADFSerializer.

Definition at line 200 of file XMLSerializer.cc.

200 {
201 if (sourceFile_ != "") {
202 return readFile(sourceFile_);
203 } else if (sourceString_ != NULL) {
204 return readString(*sourceString_);
205 } else {
206 string errorMsg = "No Source file or string set.";
207 throw SerializerException(__FILE__, __LINE__, __func__, errorMsg);
208 }
209}
virtual ObjectState * readString(const std::string &source)
virtual ObjectState * readFile(const std::string &fileName)

References __func__, readFile(), readString(), sourceFile_, and sourceString_.

Referenced by BlocksModel::BlocksModel(), GUIOptionsSerializer::readState(), BEMSerializer::readState(), IDF::IDFSerializer::readState(), ADFSerializer::readState(), OperationSerializer::readState(), and OSEdOptionsSerializer::readState().

Here is the call graph for this function:

◆ readString()

ObjectState * XMLSerializer::readString ( const std::string &  source)
privatevirtual

Reads object state from an xml string.

Parameters
XMLto read as an string containing the entire xml document.
Returns
Root node of the created ObjectState tree.
Exceptions
SerializerExceptionIf an error occurs while reading.

Definition at line 301 of file XMLSerializer.cc.

301 {
303 DOMDocument* dom = NULL;
305#if XERCES_VERSION_MAJOR >= 3
306 parser_->getDomConfig()->setParameter(
307 XMLUni::fgDOMErrorHandler, errHandler);
308#else
309 parser_->setErrorHandler(errHandler);
310#endif
311
312 // build the DOM object
313 try {
314 parser_->resetDocumentPool();
315
316 MemBufInputSource* buf = new MemBufInputSource(
317 (const XMLByte*)source.c_str(),
318 source.length(),
319 "sourceXML",
320 false);
321
322 Wrapper4InputSource* domBuf = new Wrapper4InputSource(buf);
323#if XERCES_VERSION_MAJOR >= 3
324 dom = parser_->parse(domBuf);
325#else
326 dom = parser_->parse(*domBuf);
327#endif
328 delete domBuf;
329 } catch(...) {
330 string errorLog = errHandler->errorLog();
331 delete errHandler;
332 throw SerializerException(__FILE__, __LINE__, __func__, errorLog);
333 }
334
335 int errors(errHandler->errorCount());
336
337 if (errors > 0) {
338 // if there were such errors in xml file or schema file that
339 // exception was not thrown when parseURI was called
340 string errorLog = errHandler->errorLog();
341 delete errHandler;
342 throw SerializerException(__FILE__, __LINE__, __func__, errorLog);
343 }
344
345 if (dom == NULL || dom->getDocumentElement() == NULL) {
346 delete errHandler;
348 __FILE__, __LINE__, __func__, "Illegal file: " + source);
349 }
350 ObjectState* rootState = createState(dom->getDocumentElement());
351 delete errHandler;
352 errHandler = NULL;
353
354 return rootState;
355}

References __func__, createState(), DOMBuilderErrorHandler::errorCount(), DOMBuilderErrorHandler::errorLog(), initializeParser(), and parser_.

Referenced by readState().

Here is the call graph for this function:

◆ setDestinationFile()

void XMLSerializer::setDestinationFile ( const std::string &  fileName)

◆ setDestinationString()

void XMLSerializer::setDestinationString ( std::string &  target)

Sets the destination string used when writeState is called.

Previously set destination file or string is unset.

Parameters
targetTarget string to write.

Definition at line 156 of file XMLSerializer.cc.

156 {
157 destinationString_ = &target;
158 destinationFile_ = "";
159}

References destinationFile_, and destinationString_.

Referenced by DSDBManager::addArchitecture(), DSDBManager::addImplementation(), and TTAMachine::Machine::hash().

◆ setSchemaFile()

void XMLSerializer::setSchemaFile ( const std::string &  fileName)

Sets the schema file used to validate xml files.

Parameters
fileNameRelative or absolute path of the schema file, e.g. /home/openasip/schema.xsd or ./schema.xsd.

Definition at line 168 of file XMLSerializer.cc.

168 {
169 schemaFile_ = fileName;
170}

References schemaFile_.

Referenced by ADFSerializer::ADFSerializer(), BEMSerializer::BEMSerializer(), IDF::IDFSerializer::IDFSerializer(), Proxim::loadOptions(), ProDe::OnInit(), OperationSerializer::setSchemaFile(), and OSEdOptionsSerializer::setSchemaFile().

◆ setSourceFile()

void XMLSerializer::setSourceFile ( const std::string &  fileName)

◆ setSourceString()

void XMLSerializer::setSourceString ( const std::string &  source)

Sets the source string used when readState is called.

Previously set source file or string is unset.

Parameters
sourceSource string to read.

Definition at line 128 of file XMLSerializer.cc.

128 {
129 sourceString_ = &source;
130 sourceFile_ = "";
131}

References sourceFile_, and sourceString_.

Referenced by DSDBManager::architecture(), DSDBManager::implementation(), DSDBManager::writeArchitectureToFile(), and DSDBManager::writeImplementationToFile().

◆ setUseSchema()

void XMLSerializer::setUseSchema ( bool  useSchema)

Sets/unsets validation of xml files using xml schema.

Parameters
useSchemaTrue sets and false unsets validation. Default value is false.

Definition at line 179 of file XMLSerializer.cc.

179 {
180 useSchema_ = useSchema;
181}

References useSchema_.

Referenced by ADFSerializer::ADFSerializer(), BEMSerializer::BEMSerializer(), IDF::IDFSerializer::IDFSerializer(), IPXactSerializer::IPXactSerializer(), Proxim::loadOptions(), ProDe::OnInit(), OperationSerializer::setUseSchema(), and OSEdOptionsSerializer::setUseSchema().

◆ setXMLNamespace()

void XMLSerializer::setXMLNamespace ( std::string  nsUri)

Sets the XML namespace URI to be used when creating a DOM document

Definition at line 188 of file XMLSerializer.cc.

188 {
189 nsUri_ = nsUri;
190}

References nsUri_.

Referenced by IPXactSerializer::writeState().

◆ sourceFile()

std::string XMLSerializer::sourceFile ( ) const
protected

Returns the source file that is set.

Returns
The source file.

Definition at line 526 of file XMLSerializer.cc.

526 {
527 return sourceFile_;
528}

References sourceFile_.

Referenced by readFile(), and IDF::IDFSerializer::readState().

◆ writeFile()

void XMLSerializer::writeFile ( const std::string &  destinationFile,
const ObjectState rootState 
)
privatevirtual

Writes the given ObjectState tree into the current XML file set.

Parameters
rootStateThe root object of the ObjectState tree.
Exceptions
SerializerExceptionIf the destination file cannot be written.

Definition at line 435 of file XMLSerializer.cc.

436 {
437#if XERCES_VERSION_MAJOR >= 3
438 DOMLSSerializer* domWriter = domImplementation_->createLSSerializer();
439 domWriter->getDomConfig()->setParameter(
440 XMLUni::fgDOMWRTFormatPrettyPrint, true);
441 DOMLSOutput* lsOutput = domImplementation_->createLSOutput();
442#else
443 DOMWriter* domWriter = domImplementation_->createDOMWriter();
444 domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
445#endif
446 DOMDocument* document = createDOMDocument(rootState);
447
448 try {
449 if (!FileSystem::fileIsCreatable(destinationFile) &&
450 !FileSystem::fileIsWritable(destinationFile)) {
451 throw "";
452 }
453#if XERCES_VERSION_MAJOR >= 3
454 LocalFileFormatTarget targetFile(destinationFile.c_str());
455 lsOutput->setByteStream(&targetFile);
456 domWriter->write(document, lsOutput);
457 domWriter->release();
458 delete lsOutput;
459#else
460 LocalFileFormatTarget targetFile(destinationFile.c_str());
461 domWriter->writeNode(&targetFile, *document);
462 domWriter->release();
463 delete document;
464#endif
465 } catch (...) {
466#if XERCES_VERSION_MAJOR >= 3
467 domWriter->release();
468 delete lsOutput;
469#else
470 delete document;
471#endif
472 string errorMessage = "Cannot write to " + destinationFile;
473 throw SerializerException(__FILE__, __LINE__, __func__,
474 errorMessage);
475 }
476}
static bool fileIsWritable(const std::string fileName)
static bool fileIsCreatable(const std::string fileName)
DOMDocument * createDOMDocument(const ObjectState *state) const

References __func__, createDOMDocument(), domImplementation_, FileSystem::fileIsCreatable(), and FileSystem::fileIsWritable().

Referenced by writeState().

Here is the call graph for this function:

◆ writeState()

void XMLSerializer::writeState ( const ObjectState state)
virtual

Writes the given object state to a file or string.

File is written if a destiantion file is set with setDestinationFile(). String is written if a destination string is set with setDestinationString(). Only the last file/string set will be written.

Implements TCETools::Serializer.

Reimplemented in IPXactSerializer, ADFSerializer, GUIOptionsSerializer, BEMSerializer, and IDF::IDFSerializer.

Definition at line 219 of file XMLSerializer.cc.

219 {
220 if (destinationFile_ != "") {
222 } else if (destinationString_ != NULL) {
224 } else {
225 string errorMsg = "No destination file or string set.";
226 throw SerializerException(__FILE__, __LINE__, __func__, errorMsg);
227 }
228}
virtual void writeString(std::string &target, const ObjectState *rootState)
virtual void writeFile(const std::string &fileName, const ObjectState *rootState)

References __func__, destinationFile_, destinationString_, writeFile(), and writeString().

Referenced by IPXactSerializer::writeState(), ADFSerializer::writeState(), GUIOptionsSerializer::writeState(), BEMSerializer::writeState(), IDF::IDFSerializer::writeState(), OperationSerializer::writeState(), OSEdOptionsSerializer::writeState(), and DataDependenceGraph::writeToXMLFile().

Here is the call graph for this function:

◆ writeString()

void XMLSerializer::writeString ( std::string &  target,
const ObjectState rootState 
)
privatevirtual

Writes the given ObjectState tree to a target string.

Parameters
rootStateThe root object of the ObjectState tree.
Exceptions
SerializerExceptionIf the destination file cannot be written.

Definition at line 485 of file XMLSerializer.cc.

485 {
486#if XERCES_VERSION_MAJOR >= 3
487 DOMLSSerializer* domWriter = domImplementation_->createLSSerializer();
488 domWriter->getDomConfig()->setParameter(
489 XMLUni::fgDOMWRTFormatPrettyPrint, true);
490 DOMLSOutput* lsOutput = domImplementation_->createLSOutput();
491#else
492 DOMWriter* domWriter = domImplementation_->createDOMWriter();
493 domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
494#endif
495 DOMDocument* document = createDOMDocument(rootState);
496
497 try {
498 target.clear();
499#if XERCES_VERSION_MAJOR >= 3
500 MemBufFormatTarget* buf = new MemBufFormatTarget();
501 lsOutput->setByteStream(buf);
502 domWriter->write(document, lsOutput);
503#else
504 MemBufFormatTarget* buf = new MemBufFormatTarget();
505 domWriter->writeNode(buf, *document);
506#endif
507 target.append((char*)(buf->getRawBuffer()));
508 domWriter->release();
509 delete buf;
510 delete document;
511 } catch (...) {
512 domWriter->release();
513 delete document;
514 string errorMessage = "Error writing xml to a string.";
515 throw SerializerException(__FILE__, __LINE__, __func__,
516 errorMessage);
517 }
518}

References __func__, createDOMDocument(), and domImplementation_.

Referenced by writeState().

Here is the call graph for this function:

Member Data Documentation

◆ destinationFile_

std::string XMLSerializer::destinationFile_
private

Destination file path.

Definition at line 114 of file XMLSerializer.hh.

Referenced by setDestinationFile(), setDestinationString(), and writeState().

◆ destinationString_

std::string* XMLSerializer::destinationString_
private

Destination string to write.

Definition at line 131 of file XMLSerializer.hh.

Referenced by setDestinationFile(), setDestinationString(), and writeState().

◆ domImplementation_

DOMImplementation* XMLSerializer::domImplementation_
private

Implementation of the DOM.

Definition at line 126 of file XMLSerializer.hh.

Referenced by createDOMDocument(), writeFile(), writeString(), and XMLSerializer().

◆ nsUri_

std::string XMLSerializer::nsUri_
private

XML namespace URI.

Definition at line 134 of file XMLSerializer.hh.

Referenced by createDOMDocument(), and setXMLNamespace().

◆ parser_

DOMBuilder* XMLSerializer::parser_
private

The parser that checks the XML file for errors with the Schema.

Definition at line 123 of file XMLSerializer.hh.

Referenced by initializeParser(), readFile(), readString(), XMLSerializer(), and ~XMLSerializer().

◆ schemaFile_

std::string XMLSerializer::schemaFile_
private

Schema file path.

Definition at line 116 of file XMLSerializer.hh.

Referenced by initializeParser(), and setSchemaFile().

◆ sourceFile_

std::string XMLSerializer::sourceFile_
private

Source file path.

Definition at line 112 of file XMLSerializer.hh.

Referenced by readState(), setSourceFile(), setSourceString(), and sourceFile().

◆ sourceString_

const std::string* XMLSerializer::sourceString_
private

Source string to read.

Definition at line 129 of file XMLSerializer.hh.

Referenced by readState(), setSourceFile(), and setSourceString().

◆ useSchema_

bool XMLSerializer::useSchema_
private

Indicates if xml file is validated using schema.

Definition at line 118 of file XMLSerializer.hh.

Referenced by initializeParser(), and setUseSchema().


The documentation for this class was generated from the following files: