New Xml Converters General presentation of Xml converters

  • Slides: 14
Download presentation
New Xml Converters General presentation of Xml converters The old way SAX interface Consequences

New Xml Converters General presentation of Xml converters The old way SAX interface Consequences on efficiency The new way DOM interface What we gain How to write a converter Overview (general case and specific detector element case) Real life examples References and documentation New Xml Converters S. Ponce, 21 May 2001 Slide 1

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

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 May 2001 Slide 2

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

The SAX Interface (1) SAX is an interface to the XML parser based on streaming and call-backs You first 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 Parse() Converter Parser Start. Element (. . . ) End. Element (. . . ) New Xml Converters S. Ponce, 21 May 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 May 2001 Slide 4

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

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 May 2001 Slide 5

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

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_Attr : the xml attributes DOM_Text : the bunches of text in XML 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 Converter New Xml Converters Parse(. . . ) S. Ponce, 21 May 2001 Parser 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 May 2001 Slide 7

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

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. Still better, the file is not fully parsed if not necessary 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 May 2001 Slide 8

Writing a converter Xml. Generic. Cnv implements the whole machinery of looking for files,

Writing a converter 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 can be made 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 May 2001 Slide 9

Xml. Surface. Cnv (1) // Instantiation of a static factory class used by clients

Xml. Surface. Cnv (1) // Instantiation of a static factory class used by clients to create instances of this service static Cnv. Factory<Xml. Surface. Cnv> s_Factory. Xml. Surface. Cnv; const ICnv. Factory& Xml. Surface. Cnv. Factory = s_Factory. Xml. Surface. Cnv; // Empty Constructor Xml. Surface. Cnv: : Xml. Surface. Cnv (ISvc. Locator* svc) : Xml. Generic. Cnv (svc, class. ID()) {}; Status. Code Xml. Surface. Cnv: : i_create. Obj (DOM_Element element, Data. Object*& refp. Object) { // Object creation std: : string element. Name = dom 2 Std (element. get. Attribute ("name")); Surface* data. Obj= new Surface (element. Name); refp. Object = data. Obj; // model attribute const std: : string value = dom 2 Std (element. get. Attribute ("model")); const double v_value = xml. Svc()->eval(value, false); data. Obj->set. Model (v_value); . . . } // end i_create. Obj New Xml Converters S. Ponce, 21 May 2001 Slide 10

Xml. Surface. Cnv (2) Status. Code Xml. Surface. Cnv: : i_fill. Obj (DOM_Element child.

Xml. Surface. Cnv (2) Status. Code Xml. Surface. Cnv: : i_fill. Obj (DOM_Element child. Element, Data. Object* refp. Object) { // gets the object Surface* data. Obj = dynamic_cast<Surface*> (refp. Object); // gets the element's name std: : string tag. Name = dom 2 Std (child. Element. get. Node. Name()); // dispatches, based on the name if ("tabprops" == tag. Name) { const std: : string address = dom 2 Std (child. Element. get. Attribute ("address")); long link. ID = data. Obj->add. Link(address, 0); . . . } else {. . . } } New Xml Converters S. Ponce, 21 May 2001 Slide 11

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

Writing a specific Det. Elem Converter Detector elements can be extended by users (tag <specific>) To minimize the work, a templated class called Xml. User. Det. Elem. Cnv<a. Type> has been created. It implements the whole 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 May 2001 Slide 12

Xml. Muon. Station. Cnv // Instantiation of a static factory class used by clients

Xml. Muon. Station. Cnv // Instantiation of a static factory class used by clients to create instances of this service Static Cnv. Factory<Xml. Muon. Station. Cnv> muonst_factory; const ICnv. Factory& Xml. Muon. Station. Cnv. Factory = muonst_factory; // Empty Constructor 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) { // gets the element's name std: : string tag. Name = dom 2 Std (child. Element. get. Node. Name()); if ("Al_plate_thickness" == tag. Name) { // get a value of the 'value' attribute const std: : string value = dom 2 Std (child. Element. get. Attribute ("value")); if (!value. empty()) { data. Obj->set. Thickness (xml. Svc()->eval(value)); } } else {. . . } } New Xml Converters S. Ponce, 21 May 2001 Slide 13

Documentation This presentation The xerces API (http: //xml. apache. org/xerces-c/api. Docs/index. xml) The Gaudi

Documentation 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 The Ex/Det. Desc. Example package where you can find some user specific detector element converters. New Xml Converters S. Ponce, 21 May 2001 Slide 14