New Xml Converters General presentation of Xml converters

  • Slides: 15
Download presentation
New Xml Converters Ø General presentation of Xml converters Ø The old way Ø

New Xml Converters Ø General presentation of Xml converters Ø The old way Ø SAX interface Ø Consequences on efficiency Ø The new way Ø DOM interface Ø The gain Ø How to write a converter Ø Overview of the general case Ø The specific detector element case Ø Why you don't need all that (thanks to Olivier and Andrei) Ø Real life examples Ø References and documentation New Xml Converters S. Ponce, 21 June 2001 Slide 1

Overview of Xml Converters Ø Ø One converter per object type Ø Det. Elem

Overview of Xml Converters Ø Ø One converter per object type Ø Det. Elem Ø Log. Vol Ø Isotope Ø Muon. Station Ø Vertex. Detector Ø. . . 4 main methods in IConverter interface to be implemented Ø create. Obj, update. Obj, create. Ref, update. Ref Ø Only create. Obj is actually implemented An underlying XML parser is used, namely xerces C++ The actual code does a (quasi) 1 to 1 mapping between XML elements and C++ objects and between XML attributes and C++ object members. New Xml Converters S. Ponce, 21 June 2001 Slide 2

The SAX Interface (1) Ø SAX is an interface to the XML parser based

The SAX Interface (1) Ø SAX is an interface to the XML parser based on streaming and call-backs Ø You need to implement the Handler. Base interface : Østart. Document, end. Document Østart. Element, end. Element Øcharacters Øwarning, error, fatal. Error Ø You should then give a pointer to your interface to the parser Ø Then you call parse New Xml Converters S. Ponce, 21 June 2001 Slide 3

The SAX Interface (2) Start. Document() start. Element(A) start. Element(B 1) start. Element(C) end.

The SAX Interface (2) Start. Document() start. Element(A) start. Element(B 1) start. Element(C) end. Element(B 1) characters("blabla") start. Element(B 2) end. Element(A) end. Document() <A> <B 1> <C/> </B 1> blabla <B 2/> </A> XML File New Xml Converters SAX calls S. Ponce, 21 June 2001 Slide 4

SAX pro and contra Ø CONTRA Ø The file has to be parsed entirely

SAX pro and contra Ø CONTRA Ø The file has to be parsed entirely to access any node. Thus, getting the 10 nodes included in a catalog ended up in parsing 10 times the same file. Ø Poor navigation abilities : no way to get easily the children of a given node or the list of "B" nodes Ø Made converters difficult to implement since the state of the parsing had to be handled by the user Ø PRO Ø Low memory needs since the XML file is never entirely in memory Ø Can deal with XML streams New Xml Converters S. Ponce, 21 June 2001 Slide 5

The DOM Interface (1) Ø Ø DOM is an interface to the XML parser

The DOM Interface (1) Ø Ø DOM is an interface to the XML parser based on tree representation of XML files One single method to parse files : parse. It returns a DOM_Document, the top node of the tree representing your file This tree is essentially made of : Ø DOM_Element : the xml tags Ø DOM_Text : the bunches of text in XML Ø Comments, Attributes, . . . You can navigate the tree with : Ø get. Attribute, get. Attribute. Node, get. Attributes Ø get. Child. Nodes, get. First. Child, get. Last. Child, get. Parent. Node Ø get. Node. Name, get. Node. Value Ø Get. Elements. By. Tag. Name, get. Element. By. Id New Xml Converters S. Ponce, 21 June 2001 Slide 6

The DOM Interface (2) Document A (Element) B 1 (Element) C (Element) "blabla" (Text)

The DOM Interface (2) Document A (Element) B 1 (Element) C (Element) "blabla" (Text) B 2 (Element) <A> <B 1> <C/> </B 1> blabla <B 2/> </A> XML File New Xml Converters DOM Tree S. Ponce, 21 June 2001 Slide 7

DOM pro and contra Ø PRO Ø The file is parsed only once if

DOM pro and contra Ø PRO Ø The file is parsed only once if you cache the DOM_Documents. A XMLParser. Svc was created to encapsulate parsing and caching. Ø The file is not even fully parsed due to parse on demand implementation in the xerces parser. Ø High navigation abilities : this is the aim of the DOM design Ø Converters implementation very natural. No more state. Ø CONTRA Ø More memory needed since the XML tree is in memory New Xml Converters S. Ponce, 21 June 2001 Slide 8

Writing a converter (General case) Ø Xml. Generic. Cnv implements the whole machinery of

Writing a converter (General case) Ø Xml. Generic. Cnv implements the whole machinery of looking for files, parsing them and getting the right DOM_Element from the tree. Ø By inheriting from it, you only need to implement 4 methods : Ø i_create. Obj (DOM_Element, Data. Object*&) : creation of the C++ object (new) Ø i_fill. Obj (DOM_Element, Data. Object*) : called for each child of the DOM_Element that is also a DOM_Element Ø i_fill. Obj (DOM_Text, Data. Object*) : called for each child of the DOM_Element that is a DOM_Text Ø i_process. Obj (Data. Object*) : for computation Ø In addition one should use dom 2 Std to convert DOM_String to std: : string. DOM_String: : transcode() converts DOM_String ot char* but allocates memory Ø Xml. Generic. Cnv provides you the member xml. Svc that provides you an expression evaluator New Xml Converters S. Ponce, 21 June 2001 Slide 9

Writing a specific Det. Elem Converter Ø Detector elements can be extended by users

Writing a specific Det. Elem Converter Ø Detector elements can be extended by users (tag <specific>) -> specific converters should be implemented Ø To minimize the work, a templated class called Xml. User. Det. Elem. Cnv<a. Type> has been created. It implements the conversion of a regular detector element. Ø By inheriting from it, you only need to implement 1 method : Ø i_fill. Specific. Obj (DOM_Element, a. Type*) : called for each child of the <specific> tag that is also a DOM_Element New Xml Converters S. Ponce, 21 June 2001 Slide 10

Xml. Muon. Station. Cnv usage <detelem class. ID="9990" name="MStation 01"> <geometryinfo lvname=". . .

Xml. Muon. Station. Cnv usage <detelem class. ID="9990" name="MStation 01"> <geometryinfo lvname=". . . " rpath="1" support=". . . "/> <specific> <Al_plate_thickness value="1. 2222*mm"/> </specific> </detelem> class De. Muon. Station : public Detector. Element { public: De. Muon. Station(); ~De. Muon. Station(); Smart. Data. Ptr<De. Muon. Station> station (det. Svc(), double thickness(); "/dd/Structure/LHCb/Muon/Stations/MStation 01"); void set. Thickness( double t ); if (!station) {. . . } log << MSG: : INFO << "Aluminium plate thickness: " private: << station->thickness() << endreq; double m_thickness; }; New Xml Converters S. Ponce, 21 June 2001 Slide 11

Xml. Muon. Station. Cnv Static Cnv. Factory<Xml. Muon. Station. Cnv> muonst_factory; const Icnv. Factory&

Xml. Muon. Station. Cnv Static Cnv. Factory<Xml. Muon. Station. Cnv> muonst_factory; const Icnv. Factory& Xml. Muon. Station. Cnv. Factory = muonst_factory; Xml. Muon. Station. Cnv: : Xml. Muon. Station. Cnv(ISvc. Locator* svc) : Xml. User. Det. Elem. Cnv<De. Muon. Station> (svc) {} Status. Code Xml. Muon. Station. Cnv: : i_fill. Specific. Obj (DOM_Element child. Element, De. Muon. Station* data. Obj) { std: : string tag. Name = dom 2 Std (child. Element. get. Node. Name()); if ("Al_plate_thickness" == tag. Name) { const std: : string value = dom 2 Std (child. Element. get. Attribute ("value")); if (!value. empty()) { data. Obj->set. Thickness (xml. Svc()->eval(value)); } } return Status. Code: : SUCESS; } New Xml Converters S. Ponce, 21 June 2001 Slide 12

Avoiding writing a converter Ø 99% of the extensions of detector element are addition

Avoiding writing a converter Ø 99% of the extensions of detector element are addition of parameters. Ø Tags user. Parameter and user. Parameter. Vector were added : Ø Attributes are name, type and comment Ø value is in plain text Ø This is converted by the default converter Xml. Detector. Element. Cnv Ø Parameters are accessible in regular interface IDetector. Element via methods : Ø string user. Parameter(Vector)Type (string name) Ø string user. Parameter(Vector)Comment (string name) Ø string user. Parameter(Vector)Value (string name) Ø double user. Parameter(Vector) (string name) New Xml Converters S. Ponce, 21 June 2001 Slide 13

Muon without converter <detelem name="MStation 01"> <geometryinfo lvname=". . . " rpath="1" support=". .

Muon without converter <detelem name="MStation 01"> <geometryinfo lvname=". . . " rpath="1" support=". . . "/> <user. Parameter name="Al_plate_thickness" type="double"> 1. 2222*mm </user. Parameter> </detelem> Smart. Data. Ptr<IDetector. Element> station (det. Svc(), "/dd/Structure/LHCb/Muon/Stations/MStation 01"); if (!station) {. . . } log << MSG: : INFO << "Aluminium plate thickness: " << station->user. Parameter("Al_plate_thickness") << endreq; New Xml Converters S. Ponce, 21 June 2001 Slide 14

Documentation / References Ø This presentation Ø The xerces API (http: //xml. apache. org/xerces-c/api.

Documentation / References Ø This presentation Ø The xerces API (http: //xml. apache. org/xerces-c/api. Docs/index. xml) Ø The Gaudi documentation : http: //proj-gaudi. web. cern. ch/projgaudi/Doxygen/v 7/doc/html/index. html and http: //lhcbsoft. web. cern. ch/LHCb. Soft/LHCb/v 7/doc/html/index. html. L Ø Last versions of Det/Det. Desc (v 7 -8) and Det/Xml. DDDB (v 6) packages. Especially the new TEST subdirectory of Xml. DDDB. Ø Ex/Det. Desc. Example package where you will find some user specific detector element converters. New Xml Converters S. Ponce, 21 June 2001 Slide 15