00001 #include "xercesc/framework/LocalFileFormatTarget.hpp"
00002 #include "xercesc/framework/MemBufInputSource.hpp"
00003 #include "xercesc/sax/SAXParseException.hpp"
00004 #include "xercesc/sax/EntityResolver.hpp"
00005 #include "xercesc/sax/InputSource.hpp"
00006 #include "xercesc/parsers/XercesDOMParser.hpp"
00007 #include "xercesc/util/PlatformUtils.hpp"
00008 #include "xercesc/util/XercesDefs.hpp"
00009 #include "xercesc/util/XMLUni.hpp"
00010 #include "xercesc/util/XMLURL.hpp"
00011 #include "xercesc/util/XMLString.hpp"
00012 #include "xercesc/dom/DOM.hpp"
00013
00014 #include "GaudiKernel/MsgStream.h"
00015 #include "GaudiKernel/Service.h"
00016 #include "Reflex/PluginService.h"
00017
00018 #include "XMLFileCatalog.h"
00019
00020 #include <fstream>
00021 #include <iostream>
00022 #include <stdexcept>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include "uuid/uuid.h"
00026
00027 #include <boost/format.hpp>
00028
00029 using namespace xercesc;
00030 using namespace Gaudi;
00031 using namespace std;
00032
00033 #if _XERCES_VERSION <= 30000
00034
00035 #define setIdAttribute(a, b) setIdAttribute(a)
00036 #endif
00037
00038 PLUGINSVC_FACTORY(XMLFileCatalog,IInterface*(std::string, IMessageSvc*))
00039
00040 namespace {
00041
00042 typedef const string& CSTR;
00043 inline string _toString(const XMLCh *toTranscode) {
00044 char * buff = XMLString::transcode(toTranscode);
00045 string tmp(buff==0 ? "" : buff);
00046 XMLString::release(&buff);
00047 return tmp;
00048 }
00049 struct __Init {
00050 __Init() {
00051 try { XMLPlatformUtils::Initialize(); }
00052 catch (const XMLException& e) {
00053 cout << "Xerces-c error in initialization:" << _toString(e.getMessage()) << endl;
00054 }
00055 }
00056 ~__Init() {
00057 XMLPlatformUtils::Terminate();
00058 }
00059 };
00060 __Init __In__;
00061
00062 struct XMLStr {
00063 XMLCh* m_xml;
00064 XMLStr(CSTR c) { m_xml=XMLString::transcode(c.c_str()); }
00065 ~XMLStr() { if (m_xml) XMLString::release(&m_xml); }
00066 operator const XMLCh*() const { return m_xml; }
00067 };
00068 struct XMLTag : public XMLStr {
00069 string m_str;
00070 XMLTag(CSTR s) : XMLStr(s), m_str(s) { }
00071 ~XMLTag() { }
00072 operator CSTR () const { return m_str; }
00073 };
00074
00075 bool operator==(CSTR c, const XMLTag& b) { return c==b.m_str; }
00076 struct XMLCollection {
00077 DOMElement* m_node;
00078 XMLCollection(DOMNode* n, bool use_children=true) : m_node((DOMElement*)n) {
00079 if ( use_children ) {
00080 if ( m_node ) m_node = (DOMElement*)m_node->getFirstChild();
00081 if ( m_node && m_node->getNodeType() != DOMNode::ELEMENT_NODE ) ++(*this);
00082 }
00083 }
00084 operator bool() const { return 0 != m_node; }
00085 operator DOMNode* () const { return m_node; }
00086 operator DOMElement* () const { return m_node; }
00087 DOMElement* operator->() const { return m_node; }
00088 string attr(const XMLTag& tag) const { return _toString(m_node->getAttribute(tag));}
00089 string attr(CSTR tag) const { return attr(XMLTag(tag)); }
00090 string tag() const { return _toString(m_node->getTagName()); }
00091 void operator++() {
00092 while(m_node) {
00093 m_node = (DOMElement*)m_node->getNextSibling();
00094 if ( m_node && m_node->getNodeType() == DOMNode::ELEMENT_NODE ) {
00095 return;
00096 }
00097 }
00098 }
00099 };
00100 struct ErrHandler : public ErrorHandler {
00102 IMessageSvc* m_msg;
00104 ErrHandler(IMessageSvc* m) : m_msg(m) {}
00106 void resetErrors() { }
00108 void warning(const SAXParseException& ) { }
00110 void error(const SAXParseException& e);
00112 void fatalError(const SAXParseException& e);
00113 };
00114 struct DTDRedirect : public EntityResolver {
00115 InputSource* resolveEntity(const XMLCh* const , const XMLCh* const ) {
00116 static const char* dtdID = "redirectinmem.dtd";
00117 static const char* dtd = \
00118 "\
00119 <!ELEMENT POOLFILECATALOG (META*,File*)>\
00120 <!ELEMENT META EMPTY>\
00121 <!ELEMENT File (physical,logical,metadata*)>\
00122 <!ATTLIST META name CDATA #REQUIRED>\
00123 <!ATTLIST META type CDATA #REQUIRED>\
00124 <!ELEMENT physical (pfn)+>\
00125 <!ELEMENT logical (lfn)*>\
00126 <!ELEMENT metadata EMPTY>\
00127 <!ELEMENT lfn EMPTY>\
00128 <!ELEMENT pfn EMPTY>\
00129 <!ATTLIST File ID ID #REQUIRED>\
00130 <!ATTLIST pfn name ID #REQUIRED>\
00131 <!ATTLIST pfn filetype CDATA #IMPLIED>\
00132 <!ATTLIST lfn name ID #REQUIRED>\
00133 <!ATTLIST metadata att_name CDATA #REQUIRED>\
00134 <!ATTLIST metadata att_value CDATA #REQUIRED>\
00135 ";
00136 static const size_t len = strlen(dtd);
00137 return new MemBufInputSource((const XMLByte*)dtd,len,dtdID,false);
00138 }
00139 };
00140
00141 void ErrHandler::error(const SAXParseException& e) {
00142 string m(_toString(e.getMessage()));
00143 if (m.find("The values for attribute 'name' must be names or name tokens")!=string::npos ||
00144 m.find("The values for attribute 'ID' must be names or name tokens")!=string::npos ||
00145 m.find("for attribute 'name' must be Name or Nmtoken")!=string::npos ||
00146 m.find("for attribute 'ID' must be Name or Nmtoken")!=string::npos ||
00147 m.find("for attribute 'name' is invalid Name or NMTOKEN value")!=string::npos ||
00148 m.find("for attribute 'ID' is invalid Name or NMTOKEN value")!=string::npos )
00149 return;
00150 string sys(_toString(e.getSystemId()));
00151 MsgStream log(m_msg,"XMLCatalog");
00152 log << MSG::ERROR << "Error at file \"" << sys
00153 << "\", line " << e.getLineNumber() << ", column " << e.getColumnNumber() << endmsg
00154 << "Message: " << m << endmsg;
00155 }
00156 void ErrHandler::fatalError(const SAXParseException& e) {
00157 MsgStream log(m_msg,"XMLCatalog");
00158 string m(_toString(e.getMessage()));
00159 string sys(_toString(e.getSystemId()));
00160 log << MSG::ERROR << "Fatal Error at file \"" << sys
00161 << "\", line " << e.getLineNumber() << ", column " << e.getColumnNumber() << endmsg
00162 << "Message: " << m << endmsg;
00163 throw runtime_error( "Standard pool exception : Fatal Error on the DOM Parser" );
00164 }
00165
00166 const XMLTag EmptyCatalog("<!-- Edited By POOL -->\n"
00167 "<!DOCTYPE POOLFILECATALOG SYSTEM \"InMemory\">\n"
00168 "<POOLFILECATALOG>\n"
00169 "</POOLFILECATALOG>\n");
00170 const XMLTag PFNCOLL ("physical");
00171 const XMLTag LFNCOLL ("logical");
00172 const XMLTag PFNNODE ( "pfn");
00173 const XMLTag LFNNODE ( "lfn");
00174 const XMLTag Attr_type ( "type");
00175 const XMLTag Attr_ID ( "ID");
00176 const XMLTag Attr_name ( "name");
00177 const XMLTag Attr_ftype ( "filetype");
00178 const XMLTag MetaNode ( "metadata");
00179 const XMLTag Attr_metaName ( "att_name");
00180 const XMLTag Attr_metaValue ( "att_value");
00181 }
00182
00184 std::string Gaudi::createGuidAsString() {
00185 uuid_t uuid;
00186 ::uuid_generate_time(uuid);
00187 struct Guid {
00188 unsigned int Data1;
00189 unsigned short Data2;
00190 unsigned short Data3;
00191 unsigned char Data4[8];
00192 } *g = (Guid*)&uuid;
00193
00194 boost::format text("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X");
00195 text % g->Data1 % g->Data2 % g->Data3;
00196 for (int i = 0; i < 8; ++i)
00197 text % (unsigned short)g->Data4[i];
00198 return text.str();
00199 }
00200
00201 XMLFileCatalog::XMLFileCatalog(CSTR uri, IMessageSvc* m)
00202 : m_rdOnly(false),m_update(false),m_doc(0),m_parser(0),m_errHdlr(0),
00203 m_file(uri), m_msgSvc(m)
00204 {
00205 }
00206
00207 XMLFileCatalog::~XMLFileCatalog() {
00208 if (m_parser) delete m_parser;
00209 m_parser = 0;
00210 if (m_errHdlr) delete m_errHdlr;
00211 m_errHdlr = 0;
00212 m_doc = 0;
00213 }
00214
00216 std::string XMLFileCatalog::createFID() const {
00217 return createGuidAsString();
00218 }
00219
00220 DOMDocument* XMLFileCatalog::getDoc(bool throw_if_no_exists) const {
00221 if ( !m_doc && throw_if_no_exists )
00222 printError("The XML catalog was not started.",true);
00223 return m_doc;
00224 }
00225
00226 void XMLFileCatalog::printError(CSTR msg, bool rethrow) const {
00227 MsgStream log(m_msgSvc,"XMLCatalog");
00228 log << MSG::FATAL << msg << endmsg;
00229 if ( rethrow ) {
00230 throw runtime_error("XMLFileCatalog> "+msg);
00231 }
00232 }
00233
00234 void XMLFileCatalog::init() {
00235 string xmlFile = getfile(false);
00236 try{
00237 if ( m_parser ) delete m_parser;
00238 m_parser = new XercesDOMParser;
00239 m_parser->setValidationScheme(XercesDOMParser::Val_Auto);
00240 m_parser->setDoNamespaces(false);
00241 DTDRedirect dtdinmem;
00242 m_parser->setEntityResolver(&dtdinmem);
00243 if ( ! m_errHdlr ) m_errHdlr = new ErrHandler(m_msgSvc);
00244 m_parser->setErrorHandler(m_errHdlr);
00245 if ( !xmlFile.empty() ) {
00246 m_parser->parse(xmlFile.c_str());
00247 }
00248 else {
00249 const std::string& s = EmptyCatalog;
00250 MemBufInputSource src((const XMLByte*)s.c_str(),s.length(),"MemCatalog");
00251 m_parser->parse(src);
00252 }
00253 m_doc = m_parser->getDocument();
00254 }
00255 catch (const XMLException& e) {
00256 printError("XML parse error["+xmlFile+"]: "+_toString(e.getMessage()));
00257 }
00258 catch (const DOMException& e) {
00259 printError("XML parse error["+xmlFile+"]: "+_toString(e.getMessage()));
00260 }
00261 catch (...) {
00262 printError("UNKNOWN XML parse error in file "+xmlFile);
00263 }
00264 }
00265
00266 string XMLFileCatalog::lookupFID(const std::string& fid) const {
00267 std::string result;
00268 DOMNode* e = element(fid,false);
00269 e = e ? e->getParentNode() : 0;
00270 e = e ? e->getParentNode() : 0;
00271 if ( e ) {
00272 if ( e->getAttributes() ) {
00273 char* nam = XMLString::transcode(((DOMElement*)e)->getAttribute(Attr_ID));
00274 if ( nam ) result = nam;
00275 XMLString::release(&nam);
00276 }
00277 }
00278 return result;
00279 }
00280
00281 void XMLFileCatalog::getFID(Strings& fids) const {
00282 fids.clear();
00283 DOMNode* fde = getDoc(true)->getElementsByTagName(XMLStr("*"))->item(0);
00284 for(XMLCollection c(child(fde,"File"), false); c; ++c)
00285 fids.push_back(c.attr(Attr_ID));
00286 }
00287
00288 void XMLFileCatalog::getPFN(CSTR fid, Files& files) const {
00289 files.clear();
00290 for(XMLCollection c(child(child(element(fid,false),PFNCOLL),PFNNODE), false); c; ++c)
00291 files.push_back(make_pair(c.attr(Attr_name),c.attr(Attr_ftype)));
00292 }
00293
00294 void XMLFileCatalog::getLFN(CSTR fid, Files& files) const {
00295 files.clear();
00296 for(XMLCollection c(child(child(element(fid,false),LFNCOLL),LFNNODE), false); c; ++c)
00297 files.push_back(make_pair(c.attr(Attr_name),fid));
00298 }
00299
00300 void XMLFileCatalog::getMetaData(CSTR fid, Attributes& attr) const {
00301 attr.clear();
00302 for(XMLCollection c(child(element(fid),MetaNode), false); c; ++c)
00303 attr.push_back(make_pair(c.attr(Attr_metaName),c.attr(Attr_metaValue)));
00304 if ( attr.size() > 0 )
00305 attr.push_back(make_pair("guid",fid));
00306 }
00307
00308 DOMNode* XMLFileCatalog::child(DOMNode* par,CSTR tag,CSTR attr,CSTR val) const {
00309 for(XMLCollection c(par); c; ++c ) {
00310 if( c.tag() == tag ) {
00311 if( !attr.empty() && c.attr(attr) != val) continue;
00312 return c;
00313 }
00314 }
00315 return 0;
00316 }
00317
00318 void XMLFileCatalog::setMetaData(CSTR fid, CSTR attr, CSTR val) const {
00319 if ( !readOnly() ) {
00320 DOMNode* node = element(fid);
00321 DOMElement* mnod = (DOMElement*)child(node,MetaNode,Attr_metaName,attr);
00322 if (!mnod){
00323 mnod = getDoc(true)->createElement(MetaNode);
00324 node->appendChild(mnod);
00325 mnod->setAttribute(Attr_metaName,XMLStr(attr));
00326 }
00327 mnod->setAttribute(Attr_metaValue,XMLStr(val));
00328 m_update = true;
00329 return;
00330 }
00331 printError("Cannot update readonly catalog!");
00332 }
00333
00334 string XMLFileCatalog::getMetaDataItem(CSTR fid,CSTR attr) const {
00335 XMLCollection c(child(getDoc(true)->getElementById(XMLStr(fid)),MetaNode,Attr_metaName,attr));
00336 return c ? c.attr(attr) : string("");
00337 }
00338
00339 void XMLFileCatalog::dropMetaData(CSTR fid,CSTR attr) const {
00340 vector<DOMNode*> gbc;
00341 DOMNode* fn = getDoc(true)->getElementById(XMLStr(fid));
00342 for(XMLCollection c(child(fn,MetaNode)); c; ++c)
00343 if ( attr[0]=='*' || !c.attr(attr).empty() ) gbc.push_back(c);
00344 for(vector<DOMNode*>::iterator i=gbc.begin(); i != gbc.end(); i++)
00345 fn->removeChild(*i);
00346 }
00347
00348 DOMNode* XMLFileCatalog::element(CSTR element_name,bool print_err) const {
00349 DOMNode* node = getDoc(true)->getElementById(XMLStr(element_name));
00350 if ( !node && print_err ) printError("Cannot find element:"+element_name);
00351 return node;
00352 }
00353
00354 void XMLFileCatalog::deleteFID(CSTR fid) const {
00355 DOMNode *pn = 0, *fn = element(fid);
00356 if ( fn ) pn = fn->getParentNode();
00357 if ( pn ) pn->removeChild(fn);
00358 }
00359
00360 void XMLFileCatalog::registerFID(CSTR fid) const {
00361 if ( !fid.empty() ) {
00362 std::pair<DOMElement*, DOMElement*> res = i_registerFID(fid);
00363 if ( res.first == 0 || res.second == 0 ) {
00364 printError("Failed to register FID:"+fid);
00365 }
00366 return;
00367 }
00368 throw runtime_error("XMLFileCatalog> Cannot register LFN for invalid FID:"+fid);
00369 }
00370
00371 std::pair<DOMElement*,DOMElement*> XMLFileCatalog::i_registerFID(CSTR fid) const {
00372 if ( !readOnly() ) {
00374 DOMElement *file = (DOMElement*)element(fid,false), *phyelem = 0, *logelem = 0;
00375 DOMDocument* doc = getDoc(true);
00376 if ( !file ) {
00377 DOMNode* fde = doc->getElementsByTagName(XMLStr("*"))->item(0);
00378 file = m_doc->createElement(XMLStr("File"));
00379 file->setAttribute(Attr_ID, XMLStr(fid));
00380 file->setIdAttribute(Attr_ID, true);
00381 fde->appendChild(file);
00382 m_update = true;
00383 }
00384 for(XMLCollection c1(file); c1; ++c1 ) {
00385 char* nam = XMLString::transcode(c1->getNodeName());
00386 if ( nam == PFNCOLL ) phyelem = c1;
00387 if ( nam == LFNCOLL ) logelem = c1;
00388 XMLString::release(&nam);
00389 }
00390 if ( !phyelem ) {
00391 phyelem = doc->createElement(PFNCOLL);
00392 file->appendChild(phyelem);
00393 m_update = true;
00394 }
00395 if ( !logelem ) {
00396 logelem = doc->createElement(LFNCOLL);
00397 file->appendChild(logelem);
00398 m_update = true;
00399 }
00400 return std::make_pair(logelem,phyelem);
00401 }
00402 printError("Cannot update readonly catalog!");
00403 return std::pair<DOMElement*, DOMElement*>(0,0);
00404 }
00405
00406 void XMLFileCatalog::registerPFN(CSTR fid, CSTR pfn, CSTR ftype) const {
00407 if ( !fid.empty() ) {
00408 std::pair<DOMElement*,DOMElement*> res = i_registerFID(fid);
00409 DOMElement* phyelem = res.second, *fnelem = 0;
00410 for(XMLCollection c(phyelem); c; ++c ) {
00411 char* nam = XMLString::transcode(c->getNodeName());
00412 if ( nam == PFNNODE ) {
00413 XMLString::release(&nam);
00414 nam = XMLString::transcode(c->getAttribute(Attr_name));
00415 if ( nam == pfn ) {
00416 XMLString::release(&nam);
00417 fnelem = c;
00418 break;
00419 }
00420 }
00421 XMLString::release(&nam);
00422 }
00423 if ( !fnelem ) {
00424 fnelem = getDoc(true)->createElement(PFNNODE);
00425 phyelem->appendChild(fnelem);
00426 fnelem->setAttribute(Attr_ftype,XMLStr(ftype));
00427 fnelem->setAttribute(Attr_name,XMLStr(pfn));
00428 fnelem->setIdAttribute(Attr_name, true);
00429 m_update = true;
00430 }
00431 return;
00432 }
00433 throw runtime_error("XMLFileCatalog> Cannot register PFN for invalid FID:"+fid);
00434 }
00435
00436 void XMLFileCatalog::registerLFN(CSTR fid, CSTR lfn) const {
00437 if ( !fid.empty() ) {
00438 std::pair<DOMElement*, DOMElement*> res = i_registerFID(fid);
00439 DOMElement* logelem = res.first, *fnelem = 0;
00440 for(XMLCollection c(logelem); c; ++c ) {
00441 char* nam = XMLString::transcode(c->getNodeName());
00442 if ( nam == LFNNODE ) {
00443 XMLString::release(&nam);
00444 nam = XMLString::transcode(c->getAttribute(Attr_name));
00445 if ( nam == lfn ) {
00446 XMLString::release(&nam);
00447 fnelem = c;
00448 break;
00449 }
00450 }
00451 }
00452 if ( !fnelem ) {
00453 fnelem = getDoc(true)->createElement(LFNNODE);
00454 logelem->appendChild(fnelem);
00455 fnelem->setAttribute(Attr_name,XMLStr(lfn));
00456 fnelem->setIdAttribute(Attr_name, true);
00457 m_update = true;
00458 }
00459 return;
00460 }
00461 throw runtime_error("XMLFileCatalog> Cannot register LFN for invalid FID:"+fid);
00462 }
00463
00464 void XMLFileCatalog::commit() {
00465 try {
00466 if ( dirty() && !readOnly() ) {
00467 string xmlfile = getfile(true);
00468 XMLStr ii("LS");
00469 DOMImplementation *imp = DOMImplementationRegistry::getDOMImplementation(ii);
00470 XMLFormatTarget *tar = new LocalFileFormatTarget(xmlfile.c_str());
00471 #if _XERCES_VERSION <= 30000
00472 DOMWriter *wr = imp->createDOMWriter();
00473 wr->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00474 wr->writeNode(tar, *m_doc);
00475 wr->release();
00476 #else
00477 DOMLSOutput *output = imp->createLSOutput();
00478 output->setByteStream(tar);
00479 DOMLSSerializer *wr = imp->createLSSerializer();
00480 wr->getDomConfig()->setParameter(XMLStr("format-pretty-print"), true);
00481 wr->write(m_doc, output);
00482 output->release();
00483 wr->release();
00484 #endif
00485 delete tar;
00486 }
00487 }
00488 catch ( exception& e ) {
00489 printError(string("Cannot open output file:")+e.what());
00490 }
00491 catch (...) {
00492 printError("Unknown IO rrror.");
00493 }
00494 }
00495
00496 string XMLFileCatalog::getfile(bool create) {
00497 string protocol, path;
00498 XMLURL xerurl;
00499 try{
00500 xerurl = (const XMLCh*)XMLStr(m_file);
00501 protocol = _toString(xerurl.getProtocolName());
00502 path = _toString(xerurl.getPath());
00503 }
00504 catch (const XMLException& e ) {
00505 printError(_toString(e.getMessage()));
00506 }
00507 if ( protocol.empty() ) {
00508 printError("Missing protocol.");
00509 }
00510 else if ( protocol == "http" || protocol == "ftp" ) {
00511 m_rdOnly = true;
00512 }
00513 else if ( protocol == "file" ) {
00514 m_rdOnly = false;
00515 struct stat buff;
00516 int exist = ::stat(path.c_str(),&buff) != -1;
00517 if ( create && !exist ) {
00518 MsgStream log(m_msgSvc,"XMLCatalog");
00519 log << MSG::INFO << "File '" << path << "' does not exist. New file created." << endmsg;
00520 ofstream out(path.c_str());
00521 if( !m_rdOnly && out.is_open() ) {
00522 out << (CSTR)EmptyCatalog << endl;
00523 }
00524 else {
00525 printError("Problem creating file "+path);
00526 }
00527 out.close();
00528 }
00529 else if ( exist ) {
00530 return path;
00531 }
00532 else if ( !create ) {
00533 return "";
00534 }
00535 }
00536 else {
00537 printError(protocol + ": protocol not supported.");
00538 }
00539 return path;
00540 }