Gaudi Framework, version v22r0

Home   Generated: 9 Feb 2011

Gaudi::XMLFileCatalog Class Reference

This class constitutes the core of the XML based FileCatalog API for POOL. More...

#include <XMLFileCatalog.h>

Inheritance diagram for Gaudi::XMLFileCatalog:
Inheritance graph
[legend]
Collaboration diagram for Gaudi::XMLFileCatalog:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 XMLFileCatalog (CSTR url, IMessageSvc *m)
 Create a catalog file, initialization of XercesC.
virtual ~XMLFileCatalog ()
 Destructor,.
virtual std::string createFID () const
 Catalog interface.
virtual CSTR connectInfo () const
 Access to connect string.
virtual void init ()
 Parse the DOM tree of the XML catalog.
virtual void commit ()
 Save DOM catalog to file.
virtual void rollback ()
 Save DOM catalog to file.
virtual bool readOnly () const
 Check if the catalog is read-only.
virtual bool dirty () const
 Check if the catalog should be updated.
virtual bool existsPFN (CSTR pfn) const
 Return the status of a physical file name.
virtual std::string lookupPFN (CSTR fid) const
 Lookup file identifier by physical file name.
virtual bool existsLFN (CSTR lfn) const
 Return the status of a logical file name.
virtual std::string lookupLFN (CSTR lfn) const
 Lookup file identifier by logical file name.
virtual bool existsFID (CSTR fid) const
 Return the status of a FileID.
virtual void getPFN (CSTR fid, Files &files) const
 Dump all physical file names of the catalog and their attributes associate to the FileID.
virtual void getLFN (CSTR fid, Files &files) const
 Dump all logical file names of the catalog associate to the FileID.
virtual void getFID (Strings &fids) const
 Dump all file Identifiers.
virtual void deleteFID (CSTR FileID) const
 Delete FileID Node from the catalog.
virtual void registerPFN (CSTR fid, CSTR pfn, CSTR ftype) const
 Create a FileID and Node of the physical file name with all the attributes.
virtual void registerLFN (CSTR fid, CSTR lfn) const
 Create a FileID and Node of the logical file name with all the attributes.
virtual void registerFID (CSTR fid) const
 Create a FileID and Node.
virtual void getMetaData (CSTR fid, Attributes &attr) const
 Dump all MetaData of the catalog for a given file ID.
virtual std::string getMetaDataItem (CSTR fid, CSTR name) const
 Access metadata item.
virtual void setMetaData (CSTR fid, CSTR name, CSTR value) const
 Insert/update metadata item.
virtual void dropMetaData (CSTR fid) const
 Drop all metadata of one FID.
virtual void dropMetaData (CSTR fid, CSTR attr) const
 Drop specified metadata item.

Protected Types

typedef const std::stringCSTR

Private Member Functions

xercesc::DOMDocument * getDoc (bool throw_if_no_exists=true) const
std::string getfile (bool create)
void printError (CSTR msg, bool throw_exc=true) const
std::string lookupFID (CSTR lfn) const
xercesc::DOMNode * element (CSTR fid, bool print_err=true) const
xercesc::DOMNode * child (xercesc::DOMNode *par, CSTR tag, CSTR attr="", CSTR val="") const
std::pair< xercesc::DOMElement
*, xercesc::DOMElement * > 
i_registerFID (CSTR fid) const

Private Attributes

bool m_rdOnly
bool m_update
xercesc::DOMDocument * m_doc
xercesc::XercesDOMParser * m_parser
xercesc::ErrorHandler * m_errHdlr
std::string m_file
IMessageSvcm_msgSvc

Detailed Description

This class constitutes the core of the XML based FileCatalog API for POOL.

It uses the DOM model and the external XercesC library for parsing.

Definition at line 25 of file XMLFileCatalog.h.


Member Typedef Documentation

typedef const std::string& Gaudi::XMLFileCatalog::CSTR [protected]

Definition at line 27 of file XMLFileCatalog.h.


Constructor & Destructor Documentation

XMLFileCatalog::XMLFileCatalog ( CSTR  url,
IMessageSvc m 
)

Create a catalog file, initialization of XercesC.

Definition at line 199 of file XMLFileCatalog.cpp.

00200 : m_rdOnly(false),m_update(false),m_doc(0),m_parser(0),m_errHdlr(0),
00201   m_file(uri), m_msgSvc(m)
00202 {
00203 }

XMLFileCatalog::~XMLFileCatalog (  )  [virtual]

Destructor,.

Definition at line 205 of file XMLFileCatalog.cpp.

00205                                   {
00206   if (m_parser) delete m_parser;
00207   m_parser = 0;
00208   if (m_errHdlr) delete m_errHdlr;
00209   m_errHdlr = 0;
00210   m_doc = 0;
00211 }


Member Function Documentation

xercesc::DOMNode* Gaudi::XMLFileCatalog::child ( xercesc::DOMNode *  par,
CSTR  tag,
CSTR  attr = "",
CSTR  val = "" 
) const [private]
void XMLFileCatalog::commit (  )  [virtual]

Save DOM catalog to file.

Definition at line 462 of file XMLFileCatalog.cpp.

00462                                {
00463   try {
00464     if ( dirty() && !readOnly() )  {
00465       string xmlfile = getfile(true);
00466       XMLStr ii("LS");
00467       DOMImplementation *imp = DOMImplementationRegistry::getDOMImplementation(ii);
00468       XMLFormatTarget   *tar = new LocalFileFormatTarget(xmlfile.c_str());
00469 #if _XERCES_VERSION <= 30000
00470       DOMWriter         *wr  = imp->createDOMWriter();
00471       wr->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00472       wr->writeNode(tar, *m_doc);
00473       wr->release();
00474 #else
00475       DOMLSOutput       *output = imp->createLSOutput();
00476       output->setByteStream(tar);
00477       DOMLSSerializer   *wr     = imp->createLSSerializer();
00478       wr->getDomConfig()->setParameter(XMLStr("format-pretty-print"), true);
00479       wr->write(m_doc, output);
00480       output->release();
00481       wr->release();
00482 #endif
00483       delete  tar;
00484     }
00485   }
00486   catch ( exception& e )  {
00487     printError(string("Cannot open output file:")+e.what());
00488   }
00489   catch (...)  {
00490     printError("Unknown IO rrror.");
00491   }
00492 }

virtual CSTR Gaudi::XMLFileCatalog::connectInfo (  )  const [inline, virtual]

Access to connect string.

Definition at line 39 of file XMLFileCatalog.h.

00039 { return m_file;                 }

std::string XMLFileCatalog::createFID (  )  const [virtual]

Catalog interface.

Create file identifier using UUID mechanism.

Create file identifier using UUID mechanism

Definition at line 214 of file XMLFileCatalog.cpp.

00214                                            {
00215   return createGuidAsString();
00216 }

void XMLFileCatalog::deleteFID ( CSTR  FileID  )  const [virtual]

Delete FileID Node from the catalog.

Definition at line 352 of file XMLFileCatalog.cpp.

00352                                              {
00353   DOMNode *pn = 0, *fn = element(fid);
00354   if ( fn ) pn = fn->getParentNode();
00355   if ( pn ) pn->removeChild(fn);
00356 }

virtual bool Gaudi::XMLFileCatalog::dirty (  )  const [inline, virtual]

Check if the catalog should be updated.

Definition at line 49 of file XMLFileCatalog.h.

00049 { return m_update;                }

void XMLFileCatalog::dropMetaData ( CSTR  fid,
CSTR  attr 
) const [virtual]

Drop specified metadata item.

Definition at line 337 of file XMLFileCatalog.cpp.

00337                                                            {
00338   vector<DOMNode*> gbc;
00339   DOMNode* fn = getDoc(true)->getElementById(XMLStr(fid));
00340   for(XMLCollection c(child(fn,MetaNode)); c; ++c)
00341     if ( attr[0]=='*' || !c.attr(attr).empty() ) gbc.push_back(c);
00342   for(vector<DOMNode*>::iterator i=gbc.begin(); i != gbc.end(); i++)
00343     fn->removeChild(*i);
00344 }

virtual void Gaudi::XMLFileCatalog::dropMetaData ( CSTR  fid  )  const [inline, virtual]

Drop all metadata of one FID.

Definition at line 81 of file XMLFileCatalog.h.

00081 {   dropMetaData(fid,"*");   }

DOMNode * XMLFileCatalog::element ( CSTR  fid,
bool  print_err = true 
) const [private]

Definition at line 346 of file XMLFileCatalog.cpp.

00346                                                                        {
00347   DOMNode* node = getDoc(true)->getElementById(XMLStr(element_name));
00348   if ( !node && print_err ) printError("Cannot find element:"+element_name);
00349   return node;
00350 }

virtual bool Gaudi::XMLFileCatalog::existsFID ( CSTR  fid  )  const [inline, virtual]

Return the status of a FileID.

Definition at line 59 of file XMLFileCatalog.h.

00059 { return element(fid,false) != 0; }

virtual bool Gaudi::XMLFileCatalog::existsLFN ( CSTR  lfn  )  const [inline, virtual]

Return the status of a logical file name.

Definition at line 55 of file XMLFileCatalog.h.

00055 { return element(lfn,false) != 0; }

virtual bool Gaudi::XMLFileCatalog::existsPFN ( CSTR  pfn  )  const [inline, virtual]

Return the status of a physical file name.

Definition at line 51 of file XMLFileCatalog.h.

00051 { return element(pfn,false) != 0; }

DOMDocument * XMLFileCatalog::getDoc ( bool  throw_if_no_exists = true  )  const [private]

Definition at line 218 of file XMLFileCatalog.cpp.

00218                                                                    {
00219   if ( !m_doc && throw_if_no_exists )
00220     printError("The XML catalog was not started.",true);
00221   return m_doc;
00222 }

void XMLFileCatalog::getFID ( Strings &  fids  )  const [virtual]

Dump all file Identifiers.

Definition at line 279 of file XMLFileCatalog.cpp.

00279                                                {
00280   fids.clear();
00281   DOMNode* fde = getDoc(true)->getElementsByTagName(XMLStr("*"))->item(0);
00282   for(XMLCollection c(child(fde,"File"), false); c; ++c)
00283     fids.push_back(c.attr(Attr_ID));
00284 }

string XMLFileCatalog::getfile ( bool  create  )  [private]

Definition at line 494 of file XMLFileCatalog.cpp.

00494                                             {
00495   string protocol, path;
00496   XMLURL xerurl;
00497   try{
00498     xerurl   = (const XMLCh*)XMLStr(m_file);
00499     protocol = _toString(xerurl.getProtocolName());
00500     path     = _toString(xerurl.getPath());
00501   }
00502   catch (const XMLException& e ) {
00503     printError(_toString(e.getMessage()));
00504   }
00505   if ( protocol.empty() )    {
00506     printError("Missing protocol.");
00507   }
00508   else if ( protocol == "http" || protocol == "ftp" )  {
00509     m_rdOnly = true;
00510   }
00511   else if ( protocol == "file" ) {
00512     m_rdOnly = false;
00513     struct stat buff;
00514     int exist = ::stat(path.c_str(),&buff) != -1;
00515     if ( create && !exist )  {
00516       MsgStream log(m_msgSvc,"XMLCatalog");
00517       log << MSG::INFO << "File '" << path << "' does not exist. New file created." << endmsg;
00518       ofstream out(path.c_str());
00519       if( !m_rdOnly && out.is_open() ) {
00520         out << (CSTR)EmptyCatalog << endl;
00521       }
00522       else     {
00523         printError("Problem creating file "+path);
00524       }
00525       out.close();
00526     }
00527     else if ( exist )  {
00528       return path;
00529     }
00530     else if ( !create )  {
00531       return "";
00532     }
00533   }
00534   else  {
00535     printError(protocol + ": protocol not supported.");
00536   }
00537   return path;
00538 }

void XMLFileCatalog::getLFN ( CSTR  fid,
Files &  files 
) const [virtual]

Dump all logical file names of the catalog associate to the FileID.

Definition at line 292 of file XMLFileCatalog.cpp.

00292                                                          {
00293   files.clear();
00294   for(XMLCollection c(child(child(element(fid,false),LFNCOLL),LFNNODE), false); c; ++c)
00295     files.push_back(make_pair(c.attr(Attr_name),fid));
00296 }

void XMLFileCatalog::getMetaData ( CSTR  fid,
Attributes &  attr 
) const [virtual]

Dump all MetaData of the catalog for a given file ID.

Definition at line 298 of file XMLFileCatalog.cpp.

00298                                                                    {
00299   attr.clear();
00300   for(XMLCollection c(child(element(fid),MetaNode), false); c; ++c)
00301     attr.push_back(make_pair(c.attr(Attr_metaName),c.attr(Attr_metaValue)));
00302   if ( attr.size() > 0 )
00303     attr.push_back(make_pair("guid",fid));
00304 }

string XMLFileCatalog::getMetaDataItem ( CSTR  fid,
CSTR  name 
) const [virtual]

Access metadata item.

Definition at line 332 of file XMLFileCatalog.cpp.

00332                                                                 {
00333   XMLCollection c(child(getDoc(true)->getElementById(XMLStr(fid)),MetaNode,Attr_metaName,attr));
00334   return c ? c.attr(attr) : string("");
00335 }

void XMLFileCatalog::getPFN ( CSTR  fid,
Files &  files 
) const [virtual]

Dump all physical file names of the catalog and their attributes associate to the FileID.

Definition at line 286 of file XMLFileCatalog.cpp.

00286                                                          {
00287   files.clear();
00288   for(XMLCollection c(child(child(element(fid,false),PFNCOLL),PFNNODE), false); c; ++c)
00289     files.push_back(make_pair(c.attr(Attr_name),c.attr(Attr_ftype)));
00290 }

std::pair< DOMElement *, DOMElement * > XMLFileCatalog::i_registerFID ( CSTR  fid  )  const [private]

It creates a new node File with name = fid in the XML file catalog

Definition at line 369 of file XMLFileCatalog.cpp.

00369                                                                              {
00370   if ( !readOnly() )  {
00372     DOMElement *file = (DOMElement*)element(fid,false), *phyelem = 0, *logelem = 0;
00373     DOMDocument* doc = getDoc(true);
00374     if ( !file )  {
00375       DOMNode* fde = doc->getElementsByTagName(XMLStr("*"))->item(0);
00376       file = m_doc->createElement(XMLStr("File"));
00377       file->setAttribute(Attr_ID, XMLStr(fid));
00378       file->setIdAttribute(Attr_ID, true);
00379       fde->appendChild(file);
00380       m_update = true;
00381     }
00382     for(XMLCollection c1(file); c1; ++c1 )  {
00383       char* nam = XMLString::transcode(c1->getNodeName());
00384       if ( nam == PFNCOLL ) phyelem = c1;
00385       if ( nam == LFNCOLL ) logelem = c1;
00386       XMLString::release(&nam);
00387     }
00388     if ( !phyelem )  {
00389       phyelem = doc->createElement(PFNCOLL);
00390       file->appendChild(phyelem);
00391       m_update = true;
00392     }
00393     if ( !logelem )  {
00394       logelem = doc->createElement(LFNCOLL);
00395       file->appendChild(logelem);
00396       m_update = true;
00397     }
00398     return std::make_pair(logelem,phyelem);
00399   }
00400   printError("Cannot update readonly catalog!");
00401   return std::pair<DOMElement*, DOMElement*>(0,0);
00402 }

void XMLFileCatalog::init (  )  [virtual]

Parse the DOM tree of the XML catalog.

Definition at line 232 of file XMLFileCatalog.cpp.

00232                             {
00233   string xmlFile = getfile(false);
00234   try{
00235     if ( m_parser ) delete m_parser;
00236     m_parser = new XercesDOMParser;
00237     m_parser->setValidationScheme(XercesDOMParser::Val_Auto);
00238     m_parser->setDoNamespaces(false);
00239     DTDRedirect dtdinmem;
00240     m_parser->setEntityResolver(&dtdinmem);
00241     if ( ! m_errHdlr ) m_errHdlr = new ErrHandler(m_msgSvc);
00242     m_parser->setErrorHandler(m_errHdlr);
00243     if ( !xmlFile.empty() )  {
00244       m_parser->parse(xmlFile.c_str());
00245     }
00246     else  {
00247       const std::string& s = EmptyCatalog;
00248       MemBufInputSource src((const XMLByte*)s.c_str(),s.length(),"MemCatalog");
00249       m_parser->parse(src);
00250     }
00251     m_doc = m_parser->getDocument();
00252   }
00253   catch (const XMLException& e) {
00254     printError("XML parse error["+xmlFile+"]: "+_toString(e.getMessage()));
00255   }
00256   catch (const DOMException& e) {
00257     printError("XML parse error["+xmlFile+"]: "+_toString(e.getMessage()));
00258   }
00259   catch (...)  {
00260     printError("UNKNOWN XML parse error in file "+xmlFile);
00261   }
00262 }

std::string Gaudi::XMLFileCatalog::lookupFID ( CSTR  lfn  )  const [private]
virtual std::string Gaudi::XMLFileCatalog::lookupLFN ( CSTR  lfn  )  const [inline, virtual]

Lookup file identifier by logical file name.

Definition at line 57 of file XMLFileCatalog.h.

00057 { return lookupFID(lfn);          }

virtual std::string Gaudi::XMLFileCatalog::lookupPFN ( CSTR  fid  )  const [inline, virtual]

Lookup file identifier by physical file name.

Definition at line 53 of file XMLFileCatalog.h.

00053 { return lookupFID(fid);          }

void XMLFileCatalog::printError ( CSTR  msg,
bool  throw_exc = true 
) const [private]

Definition at line 224 of file XMLFileCatalog.cpp.

00224                                                               {
00225   MsgStream log(m_msgSvc,"XMLCatalog");
00226   log << MSG::FATAL << msg << endmsg;
00227   if ( rethrow )  {
00228     throw runtime_error("XMLFileCatalog> "+msg);
00229   }
00230 }

virtual bool Gaudi::XMLFileCatalog::readOnly (  )  const [inline, virtual]

Check if the catalog is read-only.

Definition at line 47 of file XMLFileCatalog.h.

00047 { return m_rdOnly;                }

void XMLFileCatalog::registerFID ( CSTR  fid  )  const [virtual]

Create a FileID and Node.

Definition at line 358 of file XMLFileCatalog.cpp.

00358                                                {
00359   if ( !fid.empty() ) {
00360     std::pair<DOMElement*, DOMElement*> res = i_registerFID(fid);
00361     if ( res.first == 0 || res.second == 0 )  {
00362       printError("Failed to register FID:"+fid);
00363     }
00364     return;
00365   }
00366   throw runtime_error("XMLFileCatalog> Cannot register LFN for invalid FID:"+fid);
00367 }

void XMLFileCatalog::registerLFN ( CSTR  fid,
CSTR  lfn 
) const [virtual]

Create a FileID and Node of the logical file name with all the attributes.

Definition at line 434 of file XMLFileCatalog.cpp.

00434                                                          {
00435   if ( !fid.empty() )  {
00436     std::pair<DOMElement*, DOMElement*> res = i_registerFID(fid);
00437     DOMElement* logelem = res.first, *fnelem = 0;
00438     for(XMLCollection c(logelem); c; ++c )  {
00439       char* nam = XMLString::transcode(c->getNodeName());
00440       if ( nam == LFNNODE )  {
00441         XMLString::release(&nam);
00442         nam = XMLString::transcode(c->getAttribute(Attr_name));
00443         if ( nam == lfn )  {
00444           XMLString::release(&nam);
00445           fnelem = c;
00446           break;
00447         }
00448       }
00449     }
00450     if ( !fnelem )  {
00451       fnelem = getDoc(true)->createElement(LFNNODE);
00452       logelem->appendChild(fnelem);
00453       fnelem->setAttribute(Attr_name,XMLStr(lfn));
00454       fnelem->setIdAttribute(Attr_name, true);
00455       m_update = true;
00456     }
00457     return;
00458   }
00459   throw runtime_error("XMLFileCatalog> Cannot register LFN for invalid FID:"+fid);
00460 }

void XMLFileCatalog::registerPFN ( CSTR  fid,
CSTR  pfn,
CSTR  ftype 
) const [virtual]

Create a FileID and Node of the physical file name with all the attributes.

Definition at line 404 of file XMLFileCatalog.cpp.

00404                                                                      {
00405   if ( !fid.empty() )  {
00406     std::pair<DOMElement*,DOMElement*> res = i_registerFID(fid);
00407     DOMElement* phyelem = res.second, *fnelem = 0;
00408     for(XMLCollection c(phyelem); c; ++c )  {
00409       char* nam = XMLString::transcode(c->getNodeName());
00410       if ( nam == PFNNODE )  {
00411         XMLString::release(&nam);
00412         nam = XMLString::transcode(c->getAttribute(Attr_name));
00413         if ( nam == pfn )  {
00414           XMLString::release(&nam);
00415           fnelem = c;
00416           break;
00417         }
00418       }
00419       XMLString::release(&nam);
00420     }
00421     if ( !fnelem )  {
00422       fnelem = getDoc(true)->createElement(PFNNODE);
00423       phyelem->appendChild(fnelem);
00424       fnelem->setAttribute(Attr_ftype,XMLStr(ftype));
00425       fnelem->setAttribute(Attr_name,XMLStr(pfn));
00426       fnelem->setIdAttribute(Attr_name, true);
00427       m_update = true;
00428     }
00429     return;
00430   }
00431   throw runtime_error("XMLFileCatalog> Cannot register PFN for invalid FID:"+fid);
00432 }

virtual void Gaudi::XMLFileCatalog::rollback (  )  [inline, virtual]

Save DOM catalog to file.

Definition at line 45 of file XMLFileCatalog.h.

00045 { if ( dirty() )  init();        }

void XMLFileCatalog::setMetaData ( CSTR  fid,
CSTR  name,
CSTR  value 
) const [virtual]

Insert/update metadata item.

Definition at line 316 of file XMLFileCatalog.cpp.

00316                                                                       {
00317   if ( !readOnly() )  {
00318     DOMNode*    node = element(fid);
00319     DOMElement* mnod = (DOMElement*)child(node,MetaNode,Attr_metaName,attr);
00320     if (!mnod){
00321       mnod = getDoc(true)->createElement(MetaNode);
00322       node->appendChild(mnod);
00323       mnod->setAttribute(Attr_metaName,XMLStr(attr));
00324     }
00325     mnod->setAttribute(Attr_metaValue,XMLStr(val));
00326     m_update = true;
00327     return;
00328   }
00329   printError("Cannot update readonly catalog!");
00330 }


Member Data Documentation

xercesc::DOMDocument* Gaudi::XMLFileCatalog::m_doc [private]

Definition at line 94 of file XMLFileCatalog.h.

xercesc::ErrorHandler* Gaudi::XMLFileCatalog::m_errHdlr [private]

Definition at line 96 of file XMLFileCatalog.h.

std::string Gaudi::XMLFileCatalog::m_file [private]

Definition at line 97 of file XMLFileCatalog.h.

IMessageSvc* Gaudi::XMLFileCatalog::m_msgSvc [private]

Definition at line 98 of file XMLFileCatalog.h.

xercesc::XercesDOMParser* Gaudi::XMLFileCatalog::m_parser [private]

Definition at line 95 of file XMLFileCatalog.h.

bool Gaudi::XMLFileCatalog::m_rdOnly [private]

Definition at line 92 of file XMLFileCatalog.h.

bool Gaudi::XMLFileCatalog::m_update [mutable, private]

Definition at line 93 of file XMLFileCatalog.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Wed Feb 9 16:33:08 2011 for Gaudi Framework, version v22r0 by Doxygen version 1.6.2 written by Dimitri van Heesch, © 1997-2004