Skip to content

Commit

Permalink
avoid adding extra newlines when writing user.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 10, 2019
1 parent 2736597 commit 9ed08a8
Showing 1 changed file with 92 additions and 2 deletions.
94 changes: 92 additions & 2 deletions src/Base/Parameter.cpp
Expand Up @@ -27,6 +27,7 @@

#ifndef _PreComp_
# include <assert.h>
# include <memory>
# include <xercesc/util/PlatformUtils.hpp>
# include <xercesc/util/XercesVersion.hpp>
# include <xercesc/dom/DOM.hpp>
Expand Down Expand Up @@ -158,6 +159,34 @@ class DOMPrintFilter : public DOMWriterFilter
unsigned long fWhatToShow;

};
#else
class DOMPrintFilter : public DOMLSSerializerFilter
{
public:

/** @name Constructors */
DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL);
//@{

/** @name Destructors */
~DOMPrintFilter() {}
//@{

/** @ interface from DOMWriterFilter */
virtual FilterAction acceptNode(const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode*) const;
//@{

virtual ShowType getWhatToShow() const {
return fWhatToShow;
}

private:
// unimplemented copy ctor and assignment operator
DOMPrintFilter(const DOMPrintFilter&);
DOMPrintFilter & operator = (const DOMPrintFilter&);

ShowType fWhatToShow;
};
#endif
class DOMPrintErrorHandler : public DOMErrorHandler
{
Expand Down Expand Up @@ -1054,7 +1083,7 @@ ParameterManager::ParameterManager()

gSplitCdataSections = true;
gDiscardDefaultContent = true;
gUseFilter = false;
gUseFilter = true;
gFormatPrettyPrint = true;
}

Expand Down Expand Up @@ -1339,6 +1368,8 @@ void ParameterManager::SaveDocument(XMLFormatTarget* pFormatTarget) const
}
#else
try {
std::unique_ptr<DOMPrintFilter> myFilter;

// get a serializer, an instance of DOMWriter
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
Expand All @@ -1356,11 +1387,20 @@ void ParameterManager::SaveDocument(XMLFormatTarget* pFormatTarget) const
if (_pDocument) {
DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
theOutput->setEncoding(gOutputEncoding);

if (gUseFilter) {
myFilter.reset(new DOMPrintFilter(DOMNodeFilter::SHOW_ELEMENT |
DOMNodeFilter::SHOW_ATTRIBUTE |
DOMNodeFilter::SHOW_DOCUMENT_TYPE
));
theSerializer->setFilter(myFilter.get());
}

theOutput->setByteStream(pFormatTarget);
theSerializer->write(_pDocument, theOutput);
}

delete theSerializer;
theSerializer->release();
}
catch (XMLException& e) {
std::cerr << "An error occurred during creation of output transcoder. Msg is:"
Expand Down Expand Up @@ -1507,6 +1547,56 @@ short DOMPrintFilter::acceptNode(const DOMNode* node) const

return DOMNodeFilter::FILTER_ACCEPT;
}
#else
DOMPrintFilter::DOMPrintFilter(ShowType whatToShow)
: fWhatToShow(whatToShow)
{
}

DOMPrintFilter::FilterAction DOMPrintFilter::acceptNode(const DOMNode* node) const
{
if (XMLString::compareString(node->getNodeName(), XStr("FCParameters").unicodeForm()) == 0) {
// This node is supposed to have a single FCParamGroup and two text nodes.
// Over time it can happen that the text nodes collect extra newlines.
const DOMNodeList* children = node->getChildNodes();
for (XMLSize_t i=0; i<children->getLength(); i++) {
DOMNode* child = children->item(i);
if (child->getNodeType() == DOMNode::TEXT_NODE) {
child->setNodeValue(XStr("\n").unicodeForm());
}
}
}

switch (node->getNodeType()) {
case DOMNode::ELEMENT_NODE: {
return DOMNodeFilter::FILTER_ACCEPT;

break;
}
case DOMNode::COMMENT_NODE: {
return DOMNodeFilter::FILTER_ACCEPT;
break;
}
case DOMNode::TEXT_NODE: {
return DOMNodeFilter::FILTER_ACCEPT;
break;
}
case DOMNode::DOCUMENT_TYPE_NODE: {
return DOMNodeFilter::FILTER_REJECT; // no effect
break;
}
case DOMNode::DOCUMENT_NODE: {
return DOMNodeFilter::FILTER_REJECT; // no effect
break;
}
default : {
return DOMNodeFilter::FILTER_ACCEPT;
break;
}
}

return DOMNodeFilter::FILTER_ACCEPT;
}
#endif

//**************************************************************************
Expand Down

0 comments on commit 9ed08a8

Please sign in to comment.