Gaudi Framework, version v21r4

Home   Generated: 7 Sep 2009

Gaudi::XMLFileCatalog Class Reference

#include <XMLFileCatalog.h>

Inheritance diagram for Gaudi::XMLFileCatalog:

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

Collaboration graph
[legend]

List of all members.


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.


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

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 192 of file XMLFileCatalog.cpp.

00193 : m_rdOnly(false),m_update(false),m_doc(0),m_parser(0),m_errHdlr(0),
00194   m_file(uri), m_msgSvc(m)
00195 {
00196 }

XMLFileCatalog::~XMLFileCatalog (  )  [virtual]

Destructor,.

Definition at line 198 of file XMLFileCatalog.cpp.

00198                                   {
00199   if (m_parser) delete m_parser;
00200   m_parser = 0;
00201   if (m_errHdlr) delete m_errHdlr;
00202   m_errHdlr = 0;
00203   m_doc = 0;
00204 }


Member Function Documentation

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

Catalog interface.

Create file identifier using UUID mechanism.

Create file identifier using UUID mechanism

Definition at line 207 of file XMLFileCatalog.cpp.

00207                                            {
00208   return createGuidAsString();
00209 }

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

Access to connect string.

Definition at line 39 of file XMLFileCatalog.h.

00039 { return m_file;                 }

void XMLFileCatalog::init (  )  [virtual]

Parse the DOM tree of the XML catalog.

Definition at line 225 of file XMLFileCatalog.cpp.

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

void XMLFileCatalog::commit (  )  [virtual]

Save DOM catalog to file.

Definition at line 455 of file XMLFileCatalog.cpp.

00455                                {
00456   try {
00457     if ( dirty() && !readOnly() )  {
00458       string xmlfile = getfile(true);
00459       XMLStr ii("LS");
00460       DOMImplementation *imp = DOMImplementationRegistry::getDOMImplementation(ii);
00461       DOMWriter         *wr  = ((DOMImplementationLS*)imp)->createDOMWriter();
00462       XMLFormatTarget   *tar = new LocalFileFormatTarget(xmlfile.c_str());
00463       wr->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00464       wr->writeNode(tar, *m_doc);
00465       wr->release();
00466       delete  tar;
00467     }
00468   }
00469   catch ( exception& e )  {
00470     printError(string("Cannot open output file:")+e.what());
00471   }
00472   catch (...)  {
00473     printError("Unknown IO rrror.");
00474   }
00475 }

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

Save DOM catalog to file.

Definition at line 45 of file XMLFileCatalog.h.

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

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;                }

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;                }

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; }

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);          }

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 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 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; }

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 279 of file XMLFileCatalog.cpp.

00279                                                          {
00280   files.clear();
00281   for(XMLCollection c(child(child(element(fid,false),PFNCOLL),PFNNODE), false); c; ++c)
00282     files.push_back(make_pair(c.attr(Attr_name),c.attr(Attr_ftype)));
00283 }

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

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

Definition at line 285 of file XMLFileCatalog.cpp.

00285                                                          {
00286   files.clear();
00287   for(XMLCollection c(child(child(element(fid,false),LFNCOLL),LFNNODE), false); c; ++c)
00288     files.push_back(make_pair(c.attr(Attr_name),fid));
00289 }

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

Dump all file Identifiers.

Definition at line 272 of file XMLFileCatalog.cpp.

00272                                                {
00273   fids.clear();
00274   DOMNode* fde = getDoc(true)->getElementsByTagName(XMLStr("*"))->item(0);
00275   for(XMLCollection c(child(fde,"File"), false); c; ++c)
00276     fids.push_back(c.attr(Attr_ID));
00277 }

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

Delete FileID Node from the catalog.

Definition at line 345 of file XMLFileCatalog.cpp.

00345                                              {
00346   DOMNode *pn = 0, *fn = element(fid);
00347   if ( fn ) pn = fn->getParentNode();
00348   if ( pn ) pn->removeChild(fn);
00349 }

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 397 of file XMLFileCatalog.cpp.

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

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 427 of file XMLFileCatalog.cpp.

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

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

Create a FileID and Node.

Definition at line 351 of file XMLFileCatalog.cpp.

00351                                                {
00352   if ( !fid.empty() ) {
00353     std::pair<DOMElement*, DOMElement*> res = i_registerFID(fid);
00354     if ( res.first == 0 || res.second == 0 )  {
00355       printError("Failed to register FID:"+fid);
00356     }
00357     return;
00358   }
00359   throw runtime_error("XMLFileCatalog> Cannot register LFN for invalid FID:"+fid);
00360 }

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

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

Definition at line 291 of file XMLFileCatalog.cpp.

00291                                                                    {
00292   attr.clear();
00293   for(XMLCollection c(child(element(fid),MetaNode), false); c; ++c)
00294     attr.push_back(make_pair(c.attr(Attr_metaName),c.attr(Attr_metaValue)));
00295   if ( attr.size() > 0 )
00296     attr.push_back(make_pair("guid",fid));
00297 }

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

Access metadata item.

Definition at line 325 of file XMLFileCatalog.cpp.

00325                                                                 {
00326   XMLCollection c(child(getDoc(true)->getElementById(XMLStr(fid)),MetaNode,Attr_metaName,attr));
00327   return c ? c.attr(attr) : string("");
00328 }

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

Insert/update metadata item.

Definition at line 309 of file XMLFileCatalog.cpp.

00309                                                                       {
00310   if ( !readOnly() )  {
00311     DOMNode*    node = element(fid);
00312     DOMElement* mnod = (DOMElement*)child(node,MetaNode,Attr_metaName,attr);
00313     if (!mnod){
00314       mnod = getDoc(true)->createElement(MetaNode);
00315       node->appendChild(mnod);
00316       mnod->setAttribute(Attr_metaName,XMLStr(attr));
00317     }
00318     mnod->setAttribute(Attr_metaValue,XMLStr(val));
00319     m_update = true;
00320     return;
00321   }
00322   printError("Cannot update readonly catalog!");
00323 }

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,"*");   }

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

Drop specified metadata item.

Definition at line 330 of file XMLFileCatalog.cpp.

00330                                                            {
00331   vector<DOMNode*> gbc;
00332   DOMNode* fn = getDoc(true)->getElementById(XMLStr(fid));
00333   for(XMLCollection c(child(fn,MetaNode)); c; ++c)
00334     if ( attr[0]=='*' || !c.attr(attr).empty() ) gbc.push_back(c);
00335   for(vector<DOMNode*>::iterator i=gbc.begin(); i != gbc.end(); i++)
00336     fn->removeChild(*i);
00337 }

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

Definition at line 211 of file XMLFileCatalog.cpp.

00211                                                                    {
00212   if ( !m_doc && throw_if_no_exists )
00213     printError("The XML catalog was not started.",true);
00214   return m_doc;
00215 }

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

Definition at line 477 of file XMLFileCatalog.cpp.

00477                                             {
00478   string protocol, path;
00479   XMLURL xerurl;
00480   try{
00481     xerurl   = (const XMLCh*)XMLStr(m_file);
00482     protocol = _toString(xerurl.getProtocolName());
00483     path     = _toString(xerurl.getPath());
00484   }
00485   catch (const XMLException& e ) {
00486     printError(_toString(e.getMessage()));
00487   }
00488   if ( protocol.empty() )    {
00489     printError("Missing protocol.");
00490   }
00491   else if ( protocol == "http" || protocol == "ftp" )  {
00492     m_rdOnly = true;
00493   }
00494   else if ( protocol == "file" ) {
00495     m_rdOnly = false;
00496     struct stat buff;
00497     int exist = ::stat(path.c_str(),&buff) != -1;
00498     if ( create && !exist )  {
00499       MsgStream log(m_msgSvc,"XMLCatalog");
00500       log << MSG::INFO << "File '" << path << "' does not exist. New file created." << endmsg;
00501       ofstream out(path.c_str());
00502       if( !m_rdOnly && out.is_open() ) {
00503         out << (CSTR)EmptyCatalog << endl;
00504       }
00505       else     {
00506         printError("Problem creating file "+path);
00507       }
00508       out.close();
00509     }
00510     else if ( exist )  {
00511       return path;
00512     }
00513     else if ( !create )  {
00514       return "";
00515     }
00516   }
00517   else  {
00518     printError(protocol + ": protocol not supported.");
00519   }
00520   return path;
00521 }

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

Definition at line 217 of file XMLFileCatalog.cpp.

00217                                                               {
00218   MsgStream log(m_msgSvc,"XMLCatalog");
00219   log << MSG::FATAL << msg << endmsg;
00220   if ( rethrow )  {
00221     throw runtime_error("XMLFileCatalog> "+msg);
00222   }
00223 }

std::string Gaudi::XMLFileCatalog::lookupFID ( CSTR  lfn  )  const [private]

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

Definition at line 339 of file XMLFileCatalog.cpp.

00339                                                                        {
00340   DOMNode* node = getDoc(true)->getElementById(XMLStr(element_name));
00341   if ( !node && print_err ) printError("Cannot find element:"+element_name);
00342   return node;
00343 }

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

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 362 of file XMLFileCatalog.cpp.

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


Member Data Documentation

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.

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

Definition at line 94 of file XMLFileCatalog.h.

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

Definition at line 95 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.


The documentation for this class was generated from the following files:

Generated at Mon Sep 7 18:25:59 2009 for Gaudi Framework, version v21r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004