CMS 3D CMS Logo

XMLProcessor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: XMLTools
4 // Class : XMLProcessor
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author:
10 // Created: Fri Sep 21 15:56:27 CEST 2007
11 //
12 
13 // system include files
14 #include <vector>
15 #include <string>
16 #include <iostream>
17 #include <sys/types.h>
18 #include <pwd.h>
19 #include <unistd.h>
20 #include <xercesc/parsers/XercesDOMParser.hpp>
21 #include <xercesc/dom/DOM.hpp>
22 #include <xercesc/sax/HandlerBase.hpp>
23 #include <xercesc/util/XMLString.hpp>
25 #include <xercesc/dom/DOMNode.hpp>
26 #include <xercesc/framework/StdOutFormatTarget.hpp>
27 #include <xercesc/framework/LocalFileFormatTarget.hpp>
28 
29 // xalan-c init
30 //#include <xalanc/Include/PlatformDefinitions.hpp>
31 //#include <xalanc/XPath/XPathEvaluator.hpp>
32 //#include <xalanc/XalanTransformer/XalanTransformer.hpp>
33 //using namespace xalanc;
34 
35 using namespace std;
37 
38 // user include files
41 
43 
45  // initializes Xerces, must be done only once
46  init();
47 }
48 
50  // terminates Xerces
51  terminate();
52 }
53 
54 //_loaderBaseConfig::_loaderBaseConfig
56  extention_table_name = "HCAL_TRIG_PRIM_LOOKUP_TABLES";
57  name = "HCAL trigger primitive lookup table";
58  run_mode = "no-run";
59  data_set_id = "-1";
60  iov_id = "1";
61  iov_begin = "0";
62  iov_end = "1";
63  tag_id = "2";
64  tag_mode = "auto";
65  tag_name = "dummy tag";
66  detector_name = "HCAL";
67  comment_description = "empty comment";
68 }
69 
71  version = "test:2";
72  subversion = "1";
73  create_timestamp = time(nullptr);
74  created_by_user = getpwuid(getuid())->pw_name;
75 }
76 
78  XMLDOMBlock* result = new XMLDOMBlock(templateFileName);
79  DOMDocument* loader = result->getDocument();
80  //DOMElement * root = loader -> getDocumentElement();
81 
82  loader->getElementsByTagName(_toXMLCh("NAME"))
83  ->item(0)
84  ->getFirstChild()
85  ->setNodeValue(_toXMLCh("HCAL LMAP for HB, HE, HF"));
86  //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
87  //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
88 
89  return result;
90 }
91 
93  DOMDocument* loader = doc->getDocument();
94  DOMElement* root = loader->getDocumentElement();
95 
96  XMLDOMBlock dataSetDoc(templateFileName);
97  DOMDocument* dataSet = dataSetDoc.getDocument();
98 
99  //Dataset
100  dataSet->getElementsByTagName(_toXMLCh("SIDE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->side));
101  dataSet->getElementsByTagName(_toXMLCh("ETA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->eta));
102  dataSet->getElementsByTagName(_toXMLCh("PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->phi));
103  dataSet->getElementsByTagName(_toXMLCh("DELTA_PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dphi));
104 
105  dataSet->getElementsByTagName(_toXMLCh("DEPTH"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->depth));
106  dataSet->getElementsByTagName(_toXMLCh("SUBDETECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->det));
107  dataSet->getElementsByTagName(_toXMLCh("RBX_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rbx));
108  dataSet->getElementsByTagName(_toXMLCh("WEDGE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->wedge));
109 
110  dataSet->getElementsByTagName(_toXMLCh("RM_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm));
111  dataSet->getElementsByTagName(_toXMLCh("HPD_PIXEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->pixel));
112  dataSet->getElementsByTagName(_toXMLCh("QIE_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->qie));
113  dataSet->getElementsByTagName(_toXMLCh("ADC"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->adc));
114  dataSet->getElementsByTagName(_toXMLCh("RM_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm_fi));
115 
116  dataSet->getElementsByTagName(_toXMLCh("FIBER_CHANNEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fi_ch));
117  dataSet->getElementsByTagName(_toXMLCh("CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->crate));
118  dataSet->getElementsByTagName(_toXMLCh("HTR_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr));
119  dataSet->getElementsByTagName(_toXMLCh("HTR_FPGA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fpga));
120 
121  dataSet->getElementsByTagName(_toXMLCh("HTR_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr_fi));
122  dataSet->getElementsByTagName(_toXMLCh("DCC_SL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc_sl));
123  dataSet->getElementsByTagName(_toXMLCh("SPIGOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->spigo));
124  dataSet->getElementsByTagName(_toXMLCh("DCC_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc));
125  dataSet->getElementsByTagName(_toXMLCh("SLB_SITE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slb));
126  dataSet->getElementsByTagName(_toXMLCh("SLB_CHANNEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slbin));
127  dataSet->getElementsByTagName(_toXMLCh("SLB_CHANNEL2"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slbin2));
128 
129  dataSet->getElementsByTagName(_toXMLCh("SLB_CABLE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->slnam));
130  dataSet->getElementsByTagName(_toXMLCh("RCT_CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctcra));
131  dataSet->getElementsByTagName(_toXMLCh("RCT_CARD"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctcar));
132  dataSet->getElementsByTagName(_toXMLCh("RCT_CONNECTOR"))
133  ->item(0)
134  ->getFirstChild()
135  ->setNodeValue(_toXMLCh(row->rctcon));
136  dataSet->getElementsByTagName(_toXMLCh("RCT_NAME"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rctnam));
137  dataSet->getElementsByTagName(_toXMLCh("FED_ID"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fedid));
138 
139  // copy the <data_set> node into the final XML
140  DOMNode* cloneDataSet = loader->importNode(dataSet->getDocumentElement(), true);
141  root->appendChild(cloneDataSet);
142 
143  return 0;
144 }
145 
147  XMLDOMBlock* result = new XMLDOMBlock(templateFileName);
148  DOMDocument* loader = result->getDocument();
149  //DOMElement * root = loader -> getDocumentElement();
150 
151  loader->getElementsByTagName(_toXMLCh("NAME"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh("HCAL LMAP for HO"));
152  //DOMElement * _tag = (DOMElement *)(loader -> getElementsByTagName( _toXMLCh( "TAG" ) ) -> item(0));
153  //_tag -> setAttribute( _toXMLCh("mode"), _toXMLCh("test_mode") );
154 
155  return result;
156 }
157 
159  DOMDocument* loader = doc->getDocument();
160  DOMElement* root = loader->getDocumentElement();
161 
162  XMLDOMBlock dataSetDoc(templateFileName);
163  DOMDocument* dataSet = dataSetDoc.getDocument();
164 
165  //Dataset
166  dataSet->getElementsByTagName(_toXMLCh("SIDE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->sideO));
167  dataSet->getElementsByTagName(_toXMLCh("ETA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->etaO));
168  dataSet->getElementsByTagName(_toXMLCh("PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->phiO));
169  dataSet->getElementsByTagName(_toXMLCh("DELTA_PHI"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dphiO));
170 
171  dataSet->getElementsByTagName(_toXMLCh("DEPTH"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->depthO));
172  dataSet->getElementsByTagName(_toXMLCh("SUBDETECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->detO));
173  dataSet->getElementsByTagName(_toXMLCh("RBX_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rbxO));
174  dataSet->getElementsByTagName(_toXMLCh("SECTOR"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->sectorO));
175 
176  dataSet->getElementsByTagName(_toXMLCh("RM_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rmO));
177  dataSet->getElementsByTagName(_toXMLCh("HPD_PIXEL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->pixelO));
178  dataSet->getElementsByTagName(_toXMLCh("QIE_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->qieO));
179  dataSet->getElementsByTagName(_toXMLCh("ADC"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->adcO));
180  dataSet->getElementsByTagName(_toXMLCh("RM_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->rm_fiO));
181  dataSet->getElementsByTagName(_toXMLCh("FIBER_CHANNEL"))
182  ->item(0)
183  ->getFirstChild()
184  ->setNodeValue(_toXMLCh(row->fi_chO));
185 
186  dataSet->getElementsByTagName(_toXMLCh("LETTER_CODE"))
187  ->item(0)
188  ->getFirstChild()
189  ->setNodeValue(_toXMLCh(row->let_codeO));
190  dataSet->getElementsByTagName(_toXMLCh("CRATE"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->crateO));
191  dataSet->getElementsByTagName(_toXMLCh("HTR_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htrO));
192  dataSet->getElementsByTagName(_toXMLCh("HTR_FPGA"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fpgaO));
193 
194  dataSet->getElementsByTagName(_toXMLCh("HTR_FIBER"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->htr_fiO));
195  dataSet->getElementsByTagName(_toXMLCh("DCC_SL"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dcc_slO));
196  dataSet->getElementsByTagName(_toXMLCh("SPIGOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->spigoO));
197  dataSet->getElementsByTagName(_toXMLCh("DCC_SLOT"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->dccO));
198  dataSet->getElementsByTagName(_toXMLCh("FED_ID"))->item(0)->getFirstChild()->setNodeValue(_toXMLCh(row->fedidO));
199 
200  // copy the <data_set> node into the final XML
201  DOMNode* cloneDataSet = loader->importNode(dataSet->getDocumentElement(), true);
202  root->appendChild(cloneDataSet);
203 
204  return 0;
205 }
206 
208  DOMDocument* loader = doc->getDocument();
209  //DOMElement * root = loader -> getDocumentElement();
210 
211  XMLCh* _t;
212  _t = serializeDOM(loader, target);
213  delete _t;
214 
215  return 0;
216 }
217 
219  //if ( init() != 0 ) return 1;
220 
221  XMLDOMBlock dataBlock("HCAL_TRIG_PRIM_LOOKUP_TABLE.datablock.template");
222 
223  DOMDocument* dataBlockDocument = dataBlock.getDocument();
224 
225  std::cout << "===> Tag length: " << dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))->getLength()
226  << std::endl;
227  std::cout << "===> Tag name: "
228  << XMLString::transcode(
229  dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))->item(0)->getNodeName())
230  << std::endl;
231  dataBlockDocument->getElementsByTagName(_toXMLCh("CREATED_BY_USER"))
232  ->item(0)
233  ->getFirstChild()
234  ->setNodeValue(_toXMLCh("kukarzev test"));
235 
236  XMLCh* _t;
237  _t = serializeDOM(dataBlockDocument);
238  delete _t;
239 
240  //terminate();
241 
242  return 0;
243 }
244 
246  XMLCh tempStr[100];
247  XMLString::transcode("LS", tempStr, 99);
248  DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
249  DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
250  DOMConfiguration* dc = theSerializer->getDomConfig();
251  dc->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
252  dc->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
253 
254  XMLFormatTarget* myFormTarget = nullptr;
255  XMLCh* _string = nullptr;
256  if (target == "stdout" || target == "string") {
257  myFormTarget = new StdOutFormatTarget();
258  }
259  //else if ( target == "memory" )
260  // {
261  // myFormTarget = new MemBufFormatTarget();
262  // }
263  else {
264  myFormTarget = new LocalFileFormatTarget(_toXMLCh(target));
265  }
266 
267  try {
268  if (target == "string") {
269  _string = theSerializer->writeToString(node);
270  } else {
271  DOMLSOutput* outputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
272  outputDesc->setByteStream(myFormTarget);
273  theSerializer->write(node, outputDesc);
274  }
275  } catch (const XMLException& toCatch) {
276  char* message = XMLString::transcode(toCatch.getMessage());
277  std::cout << "Exception message is: \n" << message << "\n";
278  XMLString::release(&message);
279  return nullptr;
280  } catch (const DOMException& toCatch) {
281  char* message = XMLString::transcode(toCatch.msg);
282  std::cout << "Exception message is: \n" << message << "\n";
283  XMLString::release(&message);
284  return nullptr;
285  } catch (...) {
286  std::cout << "Unexpected Exception \n";
287  return nullptr;
288  }
289 
290  theSerializer->release();
291  if (myFormTarget)
292  delete myFormTarget;
293  return _string;
294 }
295 
297  std::cerr << "Intializing Xerces-c...";
298  try {
300  //
301  //_____ following removed as a xalan-c component_____________________
302  //
303  //XPathEvaluator::initialize();
304  } catch (const XMLException& toCatch) {
305  std::cout << " FAILED! Exiting..." << std::endl;
306  return 1;
307  }
308  std::cerr << " done" << std::endl;
309 
310  return 0;
311 }
312 
314  //
315  //_____ following removed as a xalan-c component_____________________
316  //
317  //std::cout << "Terminating Xalan-c...";
318  //XPathEvaluator::terminate();
319  //std::cout << " done" << std::endl;
320 
321  std::cout << "Terminating Xerces-c...";
323  std::cout << " done" << std::endl;
324 
325  // Other terminations and cleanup.
326 
327  return 0;
328 }
XMLCh * serializeDOM(XERCES_CPP_NAMESPACE::DOMNode *node, std::string target="stdout")
XMLDOMBlock * createLMapHBEFXMLBase(std::string templateFileName)
Definition: XMLProcessor.cc:77
void xercesTerminate()
Definition: Xerces.cc:23
int init
Definition: HydjetWrapper.h:64
void xercesInitialize()
Definition: Xerces.cc:18
int addLMapHBEFDataset(XMLDOMBlock *doc, LMapRowHBEF *row, std::string templateFileName)
Definition: XMLProcessor.cc:92
int terminate(void)
XERCES_CPP_NAMESPACE::DOMDocument * getDocument(void)
Definition: XMLDOMBlock.cc:268
int addLMapHODataset(XMLDOMBlock *doc, LMapRowHO *row, std::string templateFileName)
virtual ~XMLProcessor()
Definition: XMLProcessor.cc:49
XMLDOMBlock * createLMapHOXMLBase(std::string templateFileName)
int init(void)
int write(XMLDOMBlock *doc, std::string target="stdout")
static XMLProcessor * instance
Definition: XMLProcessor.h:169
int test(void)