CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
entity_test.cxx
Go to the documentation of this file.
1// Compare handling of elements in the physical file passed to parser
2// versus those coming from a separate file, included via entity reference
3
4#include <xercesc/dom/DOM.hpp>
5#include <xercesc/framework/LocalFileInputSource.hpp>
6#include <xercesc/framework/MemBufInputSource.hpp>
7#include <xercesc/util/XMLString.hpp>
8#include <xercesc/util/PlatformUtils.hpp>
9#include <xercesc/util/XMLUni.hpp>
10#include <xercesc/sax/ErrorHandler.hpp>
11#include <xercesc/sax/SAXParseException.hpp>
12#include <xercesc/parsers/XercesDOMParser.hpp>
13
14#include <string>
15#include <iostream>
16
17XERCES_CPP_NAMESPACE_USE
18class XmlErrorHandler : public ErrorHandler {
19public:
20 XmlErrorHandler(std::ostream& errOut=std::cerr) : m_errOut(errOut)
21 {resetErrors();}
23 void warning(const SAXParseException& exception);
24 void error(const SAXParseException& exception);
25 void fatalError(const SAXParseException& exception);
26 void resetErrors();
27
28 int getWarningCount() const {return m_nWarning;}
29 int getErrorCount() const {return m_nError;}
30 int getFatalCount() const {return m_nFatal;}
31
32private:
33 int m_nWarning, m_nError, m_nFatal;
34 std::ostream& m_errOut;
35}; // end class definition
36
37// forward declaration
38void Process(DOMDocument* doc, char* eltName);
39
40
41// Start here
42int main(int argc, char* argv[]) {
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}
104
105void Process(DOMDocument* doc, char* eltName) {
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}
157
158
159
160
161// XmlErrorHandler implementation
162void XmlErrorHandler::warning(const SAXParseException&) {
163 m_nWarning++;
164}
165
166void XmlErrorHandler::error(const SAXParseException& toCatch) {
167 char* charSyst = XMLString::transcode(toCatch.getSystemId());
168 std::string systemId(charSyst);
169 XMLString::release(&charSyst);
170 char* charMsg = XMLString::transcode(toCatch.getMessage());
171 std::string msg(charMsg);
172 XMLString::release(&charMsg);
173 m_nError++;
174 std::cerr << "Error at file \"" << systemId
175 << "\", line " << toCatch.getLineNumber()
176 << ", column " << toCatch.getColumnNumber()
177 << "\n Message: " << msg << "\n\n";
178}
179
180void XmlErrorHandler::fatalError(const SAXParseException& toCatch) {
181
182 // getMessage returns type XMLCh*
183 char* charMsg = XMLString::transcode(toCatch.getMessage());
184 std::string msg(charMsg);
185 XMLString::release(&charMsg);
186 m_nFatal++;
187 if (!(toCatch.getSystemId()) ) {
188 std::cerr << "Fatal XML parse error: no such file "
189 << "\n Message: " << msg << "\n\n";
190 }
191 else {
192 char* charSyst = XMLString::transcode(toCatch.getSystemId());
193 std::string systemId(charSyst);
194 XMLString::release(&charSyst);
195 std::cerr << "Fatal error at file \""
196 << systemId
197 << "\", line " << toCatch.getLineNumber()
198 << ", column " << toCatch.getColumnNumber()
199 << "\n Message: " << msg << "\n\n";
200 }
201}
203 m_nWarning = m_nError = m_nFatal = 0;
204}
int getWarningCount() const
void warning(const SAXParseException &exception)
XmlErrorHandler(std::ostream &errOut=std::cerr)
int getFatalCount() const
int getErrorCount() const
void fatalError(const SAXParseException &exception)
void error(const SAXParseException &exception)
void Process(DOMDocument *doc, char *eltName)
int main()