OpenASIP  2.0
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. More...
 
XMLSerializeroperator= (const XMLSerializer &)
 Assignment forbidden. More...
 
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. More...
 
std::string destinationFile_
 Destination file path. More...
 
std::string schemaFile_
 Schema file path. More...
 
bool useSchema_
 Indicates if xml file is validated using schema. More...
 
DOMBuilder * parser_
 The parser that checks the XML file for errors with the Schema. More...
 
DOMImplementation * domImplementation_
 Implementation of the DOM. More...
 
const std::string * sourceString_
 Source string to read. More...
 
std::string * destinationString_
 Destination string to write. More...
 
std::string nsUri_
 XML namespace URI. More...
 

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 }

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 }

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

Referenced by 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()) {
541  nsUri = Conversion::toXMLCh(nsUri_);
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 }

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

Referenced by 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 }

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;
406  throw SerializerException(
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 }

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 GUIOptionsSerializer, IPXactSerializer, ADFSerializer, IDF::IDFSerializer, and BEMSerializer.

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 }

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

Referenced by BlocksModel::BlocksModel(), BEMSerializer::readState(), IDF::IDFSerializer::readState(), OSEdOptionsSerializer::readState(), OperationSerializer::readState(), ADFSerializer::readState(), and GUIOptionsSerializer::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;
347  throw SerializerException(
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(), TTAMachine::Machine::hash(), and TDGen::writeBackendCode().

◆ 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(), OSEdOptionsSerializer::setSchemaFile(), and OperationSerializer::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(), llvm::TCETargetMachine::createMachine(), DSDBManager::implementation(), llvm::TCEStubTargetMachine::TCEStubTargetMachine(), 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(), OSEdOptionsSerializer::setUseSchema(), and OperationSerializer::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 }

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 IDF::IDFSerializer, BEMSerializer, GUIOptionsSerializer, ADFSerializer, and IPXactSerializer.

Definition at line 219 of file XMLSerializer.cc.

219  {
220  if (destinationFile_ != "") {
221  writeFile(destinationFile_, state);
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 }

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

Referenced by IPXactSerializer::writeState(), OSEdOptionsSerializer::writeState(), BEMSerializer::writeState(), IDF::IDFSerializer::writeState(), ADFSerializer::writeState(), OperationSerializer::writeState(), GUIOptionsSerializer::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:
XMLSerializer::readString
virtual ObjectState * readString(const std::string &source)
Definition: XMLSerializer.cc:301
ObjectState::attribute
Attribute * attribute(int index) const
Definition: ObjectState.cc:228
ObjectState::Attribute
Struct for describing an attribute of the XML element.
Definition: ObjectState.hh:62
XMLSerializer::writeString
virtual void writeString(std::string &target, const ObjectState *rootState)
Definition: XMLSerializer.cc:485
UnreachableStream
Definition: Exception.hh:171
ObjectState::Attribute::name
std::string name
Name of the attribute.
Definition: ObjectState.hh:63
ObjectState::Attribute::value
std::string value
Value of the attribute.
Definition: ObjectState.hh:64
FileSystem::isRelativePath
static bool isRelativePath(const std::string &pathName)
Definition: FileSystem.cc:252
XMLSerializer::schemaFile_
std::string schemaFile_
Schema file path.
Definition: XMLSerializer.hh:116
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
FileSystem::fileIsCreatable
static bool fileIsCreatable(const std::string fileName)
Definition: FileSystem.cc:123
DOMBuilderErrorHandler
Definition: DOMBuilderErrorHandler.hh:47
XMLSerializer::readFile
virtual ObjectState * readFile(const std::string &fileName)
Definition: XMLSerializer.cc:368
XMLSerializer::initializeParser
void initializeParser()
Definition: XMLSerializer.cc:234
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
XMLSerializer::domImplementation_
DOMImplementation * domImplementation_
Implementation of the DOM.
Definition: XMLSerializer.hh:126
XMLSerializer::nsUri_
std::string nsUri_
XML namespace URI.
Definition: XMLSerializer.hh:134
Conversion::toXMLCh
static XMLCh * toXMLCh(const std::string &string)
__func__
#define __func__
Definition: Application.hh:67
DOMBuilderErrorHandler::errorLog
std::string errorLog() const
Definition: DOMBuilderErrorHandler.cc:102
TCETools::Serializer
Definition: Serializer.hh:45
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
ObjectState::childCount
int childCount() const
SerializerException
Definition: Exception.hh:675
ObjectState::name
std::string name() const
XMLSerializer::createDOMDocument
DOMDocument * createDOMDocument(const ObjectState *state) const
Definition: XMLSerializer.cc:537
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
XMLSerializer::createDOM
DOMElement * createDOM(ObjectState *state, DOMDocument *doc) const
Definition: XMLSerializer.cc:609
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
XMLSerializer::parser_
DOMBuilder * parser_
The parser that checks the XML file for errors with the Schema.
Definition: XMLSerializer.hh:123
Conversion::XMLChToString
static std::string XMLChToString(const XMLCh *source)
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
ObjectState::stringValue
std::string stringValue() const
XMLSerializer::createState
ObjectState * createState(const DOMNode *node) const
Definition: XMLSerializer.cc:653
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
DOMBuilderErrorHandler::errorCount
int errorCount() const
Definition: DOMBuilderErrorHandler.cc:91
XMLSerializer::destinationString_
std::string * destinationString_
Destination string to write.
Definition: XMLSerializer.hh:131
ObjectState::setValue
void setValue(const std::string &value)
ErrorInExternalFile
Definition: Exception.hh:387
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
ObjectState::attributeCount
int attributeCount() const