CGEM BOSS 6.6.5.g
BESIII Offline Software System
Loading...
Searching...
No Matches
entity_test.cxx File Reference
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <string>
#include <iostream>

Go to the source code of this file.

Classes

class  XmlErrorHandler
 

Functions

void Process (DOMDocument *doc, char *eltName)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 42 of file entity_test.cxx.

42 {
43
44 std::string infile;
45 if (argc < 2) {
46 infile=std::string("/u/ey/jrb/dev500/vanilla/xmlBase/v5r1/xml/simpleDoc.xml");
47 }
48 else {
49 infile = std::string(argv[1]);
50 }
51
52 try {
53 XMLPlatformUtils::Initialize();
54 }
55 catch(const XMLException& toCatch)
56 { // may want to redirect in Gaudi environment
57 char* charMsg = XMLString::transcode(toCatch.getMessage());
58 std::string msg = std::string(charMsg);
59 XMLString::release(&charMsg);
60 // std::string msg(Dom::transToChar(toCatch.getMessage()));
61 std::cerr << "Error during Xerces-c Initialization.\n"
62 << " Exception message:" << msg << std::endl;
63 return 0;
64 }
65
66 const char* inChars = infile.c_str();
67 XMLCh* xmlchPath = XMLString::transcode(inChars);
68 XercesDOMParser* parser = new XercesDOMParser();
69
70 XmlErrorHandler* errorHandler = new XmlErrorHandler();
71 parser->setErrorHandler(errorHandler);
72
73 // The following made no difference in the output
74 // parser->useScanner(XMLUni::fgDGXMLScanner);
75 // According to documentation we shouldn't need this, but
76 // can't hurt
77 parser->setValidationScheme(AbstractDOMParser::Val_Auto);
78
79
80 std::cout << "create entity reference flag has default (true) value" <<
81 std::endl;
82 parser->parse(xmlchPath);
83 DOMDocument* doc = parser->getDocument();
84 std::cout << "Document successfully parsed" << std::endl;
85 std::cout << "processing local elements.. " << std::endl;
86 Process(doc, "const");
87 std::cout << std::endl << "processing external elements.. " << std::endl;
88 Process(doc, "extConst");
89
90 parser->reset();
91 parser->setCreateEntityReferenceNodes(false);
92
93 std::cout << std::endl << std::endl \
94 << "create entity reference flag has value false" << std::endl;
95 parser->parse(xmlchPath);
96 DOMDocument* expandDoc = parser->getDocument();
97 std::cout << "Document successfully parsed" << std::endl;
98 std::cout << "processing local elements.. " << std::endl;
99 Process(expandDoc, "const");
100 std::cout << "processing external elements.. " << std::endl;
101 Process(expandDoc, "extConst");
102
103}
void Process(DOMDocument *doc, char *eltName)

◆ Process()

void Process ( DOMDocument *  doc,
char *  eltName 
)

Definition at line 105 of file entity_test.cxx.

105 {
106 XMLCh* xmlchEltName = XMLString::transcode(eltName);
107 XMLCh* xmlchName = XMLString::transcode("name");
108 XMLCh* xmlchValue = XMLString::transcode("value");
109 XMLCh* xmlchModified = XMLString::transcode("modified");
110 if (!doc) {
111 std::cerr << "invalid document " << std::endl;
112 return;
113 }
114
115 // Find all elements with supplied tag name
116 DOMNodeList* constList = doc->getElementsByTagName(xmlchEltName);
117 unsigned int nElt = constList->getLength();
118 for (unsigned int iElt = 0; iElt < nElt; iElt++) {
119 try {
120 bool mismatch = false;
121 DOMNode* item = constList->item(iElt);
122 DOMElement* itemElt = dynamic_cast<DOMElement *>(item);
123 DOMElement* byIdElt;
124 std::cout << std::endl << eltName << " #" << iElt
125 << " Address as node: "
126 << item << " and as element: " << itemElt << std::endl;
127 const XMLCh* xmlchNamevalue = itemElt->getAttribute(xmlchName);
128 if (XMLString::stringLen(xmlchNamevalue) > 0 ) {
129 char* namevalue = XMLString::transcode(xmlchNamevalue);
130 std::cout << "element has name " << namevalue << std::endl;
131 byIdElt = doc->getElementById(xmlchNamevalue);
132 std::cout << "Address from getElementById: " << byIdElt << std::endl;
133 if (byIdElt != itemElt) {
134 mismatch = true;
135 std::cout << "**** Address mismatch " << std::endl << std::endl;
136 }
137 XMLString::release(&namevalue);
138 }
139 std::cout << "Modifying value attribute using DOM_Element address" << std::endl;
140 itemElt->setAttribute(xmlchValue, xmlchModified);
141 if (mismatch) {
142 std::cout << "Modifying value attribute using looked-up address"
143 << std::endl;
144 byIdElt->setAttribute(xmlchValue, xmlchModified);
145
146 }
147 }
148 catch (DOMException ex) {
149 int code = ex.code;
150 std::cout << "***** Processing failed for element #" << iElt
151 << " with DOMException, code = "
152 << code << std::endl << std::endl;
153 }
154 }
155
156}

Referenced by main().