47 #define BAD_CRC_IS_ONLY_WARNING 1
49 #include <xercesc/util/PlatformUtils.hpp>
50 #include <xercesc/dom/DOMNamedNodeMap.hpp>
72 #define X(str) XString(str).unicode_str()
73 #define S(str) str.c_str()
75 using namespace xercesc;
87 void writeXML(
const XString& s);
95 setg(buffer, buffer, buffer + bufferLength);
99 return gptr() - eback();
108 return egptr() - gptr();
112 char *gbegin = eback();
113 char *gend = egptr();
114 setg(gbegin, gbegin, gend);
125 setp(buffer, buffer + bufferLength);
129 return pptr() - pbase();
138 return pptr() - pbase();
142 char *pbegin = pbase();
143 char *pend = epptr();
156 <<
" hddm-xml [-n <count>] [-o <filename>] [HDDM file]\n\n"
158 <<
" -o <filename> write to <filename>.xml"
159 <<
" -n <count> limit output to <count> records"
164 int main(
int argC,
char* argV[])
170 XMLPlatformUtils::Initialize();
172 catch (
const XMLException* toCatch)
174 XString msg(toCatch->getMessage());
176 <<
"hddm-xml: Error during initialization! :\n"
177 <<
S(msg) << std::endl;
183 for (argInd = 1; argInd < argC; argInd++)
185 if (argV[argInd][0] !=
'-')
189 else if (strcmp(argV[argInd],
"-o") == 0)
191 xFilename = argV[++argInd];
193 else if (strcmp(argV[argInd],
"-n") == 0)
195 if (!sscanf(argV[++argInd],
"%d",&reqcount))
214 else if (argInd == argC - 1)
216 hddmFile =
XString(argV[argInd]);
217 ifs =
new std::ifstream(hddmFile.c_str());
227 <<
"hddm-xml: Error opening input stream " << hddmFile << std::endl;
230 std::ostringstream tmpFileStr;
231 tmpFileStr <<
"tmp" << getpid();
232 std::ofstream ofs(tmpFileStr.str().c_str());
236 <<
"hddm-xml: Error opening temp file " << tmpFileStr.str() << std::endl;
240 ofs <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
243 if (getline(*ifs,line))
245 if (line.substr(0,5) ==
"<?xml")
248 <<
"hddm-xml: Error reading input stream " << hddmFile
251 <<
"Input file appears to be an xml document!" << std::endl;
254 else if (line.substr(0,5) ==
"<HDDM")
256 xmlHeader = line +
"\n";
262 <<
"hddm-xml: Input stream does not contain valid hddm header"
270 <<
"hddm-xml: Error reading from input stream " << hddmFile
274 while (getline(*ifs,line))
277 if (line ==
"</HDDM>")
284 #if defined OLD_STYLE_XERCES_PARSER
292 <<
"hddm-xml : Error parsing HDDM document, "
293 <<
"cannot continue" << std::endl;
296 unlink(tmpFileStr.str().c_str());
298 DOMElement* rootEl = document->getDocumentElement();
299 XString rootS(rootEl->getTagName());
303 <<
"hddm-xml error: root element of input document is "
304 <<
"\"" <<
S(rootS) <<
"\", expected \"HDDM\""
310 if (xFilename.size())
312 XString fname(xFilename +
".xml");
313 builder.
xout.open(fname.c_str());
316 builder.
writeXML(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
319 int event_buffer_size;
320 char *event_buffer =
new char[event_buffer_size = 1000000];
323 int integrity_check_mode = 0;
324 int compression_mode = 0;
325 while (reqcount && ifs->good())
327 DOMNodeList* contList = rootEl->getChildNodes();
328 int contLength = contList->getLength();
330 ifs->read(event_buffer,4);
336 #ifdef VERBOSE_HDDM_LOGGING
337 XString tnameS(rootEl->getTagName());
338 std::cerr <<
"hddm-xml : tag " <<
S(tnameS)
339 <<
" found with size " << tsize
346 else if (tsize == 1) {
347 int size, format, flags;
348 ifs->read(event_buffer+4,4);
350 ifs->read(event_buffer+8,size);
351 *ifx >> format >> flags;
352 int compression_flags = flags & 0xf0;
353 int integrity_flags = flags & 0x0f;
354 std::streambuf *fin_sb = 0;
355 xstream::z::istreambuf *zin_sb = 0;
356 xstream::bz::istreambuf *bzin_sb = 0;
357 int *leftovers =
new int[100];
358 int sizeof_leftovers =
sizeof(
int[100]);
360 if (compression_flags == compression_mode) {
361 fin_sb = ifs->rdbuf();
363 else if (size == 8 && format == 0 && compression_flags == 0x10) {
364 if (compression_mode == 0x20) {
365 bzin_sb = (xstream::bz::istreambuf*)ifs->rdbuf();
367 compression_mode = compression_flags;
368 zin_sb =
new xstream::z::istreambuf(ifs->rdbuf(),
369 leftovers, sizeof_leftovers);
374 else if (size == 8 && format == 0 && compression_flags == 0x20) {
375 if (compression_mode == 0x10) {
376 zin_sb = (xstream::z::istreambuf*)ifs->rdbuf();
378 compression_mode = compression_flags;
379 bzin_sb =
new xstream::bz::istreambuf(ifs->rdbuf(),
380 leftovers, sizeof_leftovers);
386 if (compression_mode == 0x20) {
387 bzin_sb = (xstream::bz::istreambuf*)ifs->rdbuf();
388 fin_sb = bzin_sb->get_streambuf();
390 else if (compression_mode == 0x10) {
391 zin_sb = (xstream::z::istreambuf*)ifs->rdbuf();
392 fin_sb = zin_sb->get_streambuf();
394 compression_mode = compression_flags;
401 if (size == 8 && format == 0 && integrity_flags == 0x0) {
402 integrity_check_mode = 0;
404 else if (size == 8 && format == 0 && integrity_flags == 0x1) {
405 integrity_check_mode = 1;
408 std::cerr <<
"hddm-xml error: unrecognized stream modifier"
409 " encountered, this stream is no longer readable."
415 else if (tsize+4 > event_buffer_size) {
418 char *new_buffer =
new char[event_buffer_size = tsize+1000];
421 memcpy(new_buffer,event_buffer,4);
423 delete[] event_buffer;
424 event_buffer = new_buffer;
426 ifs->read(event_buffer+4,tsize);
429 if (integrity_check_mode == 1) {
433 unsigned int recorded_crc;
435 xstr >> recorded_crc;
436 xstream::digest::crc32 crc;
437 std::ostream out(&crc);
438 out.write(event_buffer,tsize+4);
440 if (crc.digest() != recorded_crc) {
441 #if BAD_CRC_IS_ONLY_WARNING
442 static int bad_crc_warning_needed =
true;
444 "WARNING: data integrity crc check failed on input.\n"
445 "This may be the result of a bug in the xstream library\n"
446 "if you are analyzing a data file that was generated by\n"
447 "code prior to svn rev 18530. If this concerns you, \n"
448 "regenerate the file using a newer build of the sim-recon\n"
449 "tools and it should go away.\n";
450 if (bad_crc_warning_needed) {
451 std::cerr << errmsg << std::endl;
452 bad_crc_warning_needed =
false;
455 std::cerr <<
"hddm-xml error: crc32 check error on input stream"
456 " encountered, this stream is no longer readable."
463 for (
int c = 0;
c < contLength;
c++)
465 DOMNode* cont = contList->item(
c);
466 short type = cont->getNodeType();
467 if (type == DOMNode::ELEMENT_NODE)
469 DOMElement* contEl = (DOMElement*) cont;
472 #ifdef VERBOSE_HDDM_LOGGING
473 XString cnameS(contEl->getTagName());
474 std::cerr <<
"hddm-xml : top-level tag " <<
S(cnameS)
475 <<
" found with size " << size
483 XString repS(contEl->getAttribute(
X(
"minOccurs")));
484 int rep = (repS ==
"")? 1 : atoi(
S(repS));
486 XString conameS(contEl->getTagName());
487 std::cerr <<
"hddm-xml warning: top-level tag " <<
S(conameS)
488 <<
" found with zero size "
489 <<
"inside an event with size " << tsize
490 <<
" continue? [y/n] ";
493 if (ans[0] !=
'y' && ans[0] !=
'Y') {
504 if (ifs != &std::cin)
506 ((std::ifstream*)ifs)->close();
508 XMLPlatformUtils::Terminate();
531 DOMElement* el,
int size,
int depth)
533 XString tagS(el->getTagName());
534 XString repS(el->getAttribute(
X(
"maxOccurs")));
535 int rep = (repS ==
"unbounded")? INT_MAX :
545 for (r = 0; r < rep && size > 0; r++)
547 for (
int d = 0; d < depth; d++)
553 DOMNamedNodeMap* attrList = el->getAttributes();
554 int listLength = attrList->getLength();
555 for (
int a = 0; a < listLength; a++)
557 XString nameS(attrList->item(a)->getNodeName());
558 XString typeS(attrList->item(a)->getNodeValue());
559 std::ostringstream attrStr;
565 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
567 else if (typeS ==
"long")
572 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
574 else if (typeS ==
"float")
579 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
581 else if (typeS ==
"double")
586 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
588 else if (typeS ==
"boolean")
593 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
595 else if (typeS ==
"Particle_t")
602 else if (typeS ==
"string" || typeS ==
"anyURI")
606 int strsize = value.size();
607 size -= strsize + 4 + ((strsize % 4)? 4-(strsize % 4) : 0);
608 attrStr <<
" " << nameS <<
"=\"" << value <<
"\"";
610 else if (nameS ==
"minOccurs" || nameS ==
"maxOccurs")
616 attrStr <<
" " << nameS <<
"=\"" << typeS <<
"\"";
618 writeXML(attrStr.str());
621 DOMNodeList* contList = el->getChildNodes();
622 int contLength = contList->getLength();
632 for (
int c = 0;
c < contLength;
c++)
634 DOMNode* cont = contList->item(
c);
635 short type = cont->getNodeType();
636 if (type == DOMNode::ELEMENT_NODE)
638 DOMElement* contEl = (DOMElement*) cont;
642 #ifdef VERBOSE_HDDM_LOGGING
643 XString cnameS(contEl->getTagName());
644 std::cerr <<
"hddm-xml : tag " <<
S(cnameS)
645 <<
" found with size " << csize
649 constructXML(ifx,contEl,csize,depth +1);
652 #ifdef VERBOSE_HDDM_LOGGING
654 XString irepS(contEl->getAttribute(
X(
"minOccurs")));
655 int irep = (irepS ==
"")? 1 : atoi(
S(irepS));
657 XString conameS(contEl->getTagName());
658 std::cerr <<
"hddm-xml warning: tag " <<
S(conameS)
659 <<
" found with zero size, "
660 <<
"continue? [y/n] ";
663 if (ans[0] !=
'y' && ans[0] !=
'Y') {
674 for (
int d = 0; d < depth; d++)
678 XString endTag(
"</"+tagS+
">\n");
683 std::cerr <<
"hddm-xml : size mismatch in tag " <<
S(tagS)
684 <<
", remainder is " << size
685 <<
", cannot continue." << std::endl;
689 std::cerr <<
"hddm-xml : repeat count mismatch in tag " <<
S(tagS)
690 <<
", expected " << rep <<
" but saw " << r
691 <<
", cannot continue." << std::endl;
C++ iostream like interface to read and write xdr streams.
xercesc::DOMDocument * buildDOMDocument(const XString &xmlFile, bool keep)
C++ objects to calculate digests of data.
static char * ParticleType(Particle_t p)
xercesc::DOMDocument * parseInputDocument(const XString &xmlFile, bool keep)
ostreambuffer(char *buffer, std::streamsize bufferLength)
void writeXML(const XString &s)
C++ streambuf interface to read and write file formats supported by Zlib.
istreambuffer(char *buffer, std::streamsize bufferLength)
C++ streambuf interface to read and write bzip2 streams.
void seekp(std::streampos pos)
void seekg(std::streampos pos)
int explicit_repeat_count
void constructXML(xstream::xdr::istream *ifx, DOMElement *el, int size, int depth)
int main(int argc, char *argv[])