Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:51:52

0001 #ifndef DD_XMLElement_H
0002 #define DD_XMLElement_H
0003 
0004 #include <cstddef>
0005 #include <iosfwd>
0006 #include <map>
0007 #include <string>
0008 #include <vector>
0009 
0010 #include "DetectorDescription/Core/interface/DDCompactView.h"
0011 #include "DetectorDescription/Core/interface/DDName.h"
0012 #include "DetectorDescription/Parser/interface/DDLElementRegistry.h"
0013 
0014 class DDCompactView;
0015 class DDLElementRegistry;
0016 
0017 /// This is a base class for processing XML elements in the DDD
0018 /** \class DDXMLElement
0019  *                                                                         
0020  *           DDXMLElement                       
0021  *
0022  *  Component of DDL XML Parsing                                
0023  *                                                                         
0024  *  A DDXMLElement stores all the attributes and text of an XML element.
0025  *  It is designed to accumulate this information unless cleared.  In other
0026  *  words, it accumulates sets of attributes, and allows the appending of
0027  *  text indefinitely, as opposed to, say, creating another class which
0028  *  is designed to hold a std::vector of single element information. This is 
0029  *  contrary to the way that XML normally defines an element, but for
0030  *  DDL, this works fine.
0031  *
0032  *  One of the things that one needs to build in to each subclass is when
0033  *  an element needs to be cleared.  For some, emptying the std::vectors should
0034  *  happen at the end (of the processElement method).  For some, clearing
0035  *  is ONLY done by the parent.  For example, SpecPar and its child PartSelector.
0036  *  or Polyhedra and its child ZSection.  In some cases elements can be in one
0037  *  or more parent elements as well as on their own (Vector, Map).  For these
0038  *  the processing currently depends on the parent so one must clear only as
0039  *  appropriate.
0040  *
0041  *
0042  *                                                                         
0043  */
0044 
0045 typedef std::map<std::string, std::string> DDXMLAttribute;
0046 typedef std::map<std::string, std::vector<std::string> > AttrAccumType;
0047 
0048 class DDXMLElement {
0049   friend std::ostream& operator<<(std::ostream& os, const DDXMLElement& element);
0050 
0051 public:
0052   /// Constructor.
0053   DDXMLElement(DDLElementRegistry* myreg);
0054 
0055   /// Constructor for autoClear element.
0056   DDXMLElement(DDLElementRegistry* myreg, const bool& clearme);
0057 
0058   /// Destructor
0059   virtual ~DDXMLElement(void) = default;  // inline
0060 
0061   /// Load the element attributes.
0062   /**
0063     * The loadAttributes method loads the attributes of the element into a
0064     * std::map<std::string, std::string> which is used to store Name-Value pairs.  It takes
0065     * as input two std::vectors of strings containing "synchronized" names and
0066     * values.
0067     * 
0068     * In the SAX2 based calling process, this is done on a startElement event.
0069     *
0070     */
0071   void loadAttributes(const std::string& elemName,
0072                       const std::vector<std::string>& names,
0073                       const std::vector<std::string>& values,
0074                       const std::string& nmspace,
0075                       DDCompactView& cpv);
0076 
0077   /// Used to load both text and XML comments into this object
0078   /**
0079     *
0080     * At the current time this is done simply as a way for the user of this
0081     * class to accumulate text and/or comments as std::vector of strings, each one
0082     * matching the std::vector of attributes.  Therefore loadText starts a new 
0083     * text storage.
0084     *
0085     */
0086   void loadText(const std::string& inText);
0087 
0088   /// append to the current (i.e. most recently added)
0089   void appendText(const std::string& inText);
0090 
0091   /// retrieve the text blob.
0092   const std::string getText(size_t tindex = 0) const;
0093 
0094   /// gotText()? kind of like gotMilk?  Yes = text has already been encountered.
0095   virtual bool gotText(void) const;
0096 
0097   /// clear this element's contents.
0098   virtual void clear(void);
0099 
0100   /// Access to attributes by name.
0101   virtual const std::string& getAttribute(const std::string& name) const;
0102 
0103   /// Get a "row" of attributes, i.e. one attribute set
0104   virtual const DDXMLAttribute& getAttributeSet(size_t aIndex = 0) const;
0105 
0106   const virtual DDName getDDName(const std::string& defaultNS,
0107                                  const std::string& attname = std::string("name"),
0108                                  size_t aIndex = 0);
0109 
0110   /*   /// Gets the value of the name part of an attribute of the form ns:name. */
0111   /*   const virtual std::string getName(const std::string& attname, size_t aIndex = 0); */
0112 
0113   /*   /// Gets the namespace of an attribute of the form ns:name. */
0114   /*   virtual std::string getNameSpace(const std::string& defaultNS, const std::string& attname, size_t aIndex = 0); */
0115 
0116   /// Returns a specific value from the aIndex set of attributes.
0117   virtual const std::string& get(const std::string& name, size_t aIndex = 0) const;
0118 
0119   /// Returns a set of values as a std::vector of strings, given the attribute name.
0120   virtual std::vector<std::string> getVectorAttribute(const std::string& name);
0121 
0122   /// Number of elements accumulated.
0123   virtual size_t size(void) const;
0124 
0125   virtual std::vector<DDXMLAttribute>::const_iterator begin(void);
0126 
0127   virtual std::vector<DDXMLAttribute>::const_iterator end(void);
0128 
0129   /// Set parent element name to central list of names.
0130   void setParent(const std::string& pename);
0131 
0132   /// Set self element name to central list of names.
0133   void setSelf(const std::string& sename);
0134 
0135   /// access to parent element name
0136   const std::string& parent(void) const;
0137 
0138   /// Processing the element.
0139   /** 
0140     * The processElement method completes any necessary work to process the XML
0141     * element.
0142     *
0143     * For example, this can be used to call the DDCore to make the geometry in
0144     * memory.  There is a default for this so that if not declared in the 
0145     * inheriting class, no processing is done.
0146     */
0147   virtual void processElement(const std::string& name, const std::string& nmspace, DDCompactView& cpv);
0148 
0149   /// Called by loadAttributes AFTER attributes are loaded.
0150   /** 
0151     * The preProcessElement method can assume that the attributes are loaded and
0152     * perform any code that is necessary at the start of an element.
0153     *
0154     * This would allow users to call their own code to setup anything necessary
0155     * for the continued processing of the child elements.
0156     *
0157     */
0158   virtual void preProcessElement(const std::string& name, const std::string& nmspace, DDCompactView& cpv);
0159 
0160   /// Allow for the elements to have their own streaming method, but also provide a default.
0161   virtual void stream(std::ostream& os) const;
0162 
0163   /// Allow the elements of this type to be iterated over using ++ operator.
0164   std::vector<DDXMLAttribute>::const_iterator& operator++(int inc);
0165 
0166   /// Have any elements of this type been encountered but not processed?
0167   virtual bool isEmpty(void) const;
0168 
0169   /// format std::string for throw an error.
0170   void throwError(const std::string& keyMessage) const;
0171 
0172 protected:
0173   DDLElementRegistry* myRegistry_;
0174 
0175 private:
0176   /// behind the scenes appending to pAttributes...
0177   void appendAttributes(std::vector<std::string>& tv, const std::string& name);
0178 
0179   std::vector<DDXMLAttribute>
0180       attributes_;                      // std::vector of name-value std::map (i.e. multiple elements of the same type.
0181   std::vector<std::string> text_;       // accumulates text.. one per element of this type.
0182   AttrAccumType attributeAccumulator_;  // temporary holder for most recent accessed attributes_... remove later!
0183   bool autoClear_;
0184   std::vector<DDXMLAttribute>::const_iterator myIter_;
0185   std::string myElement_;
0186   std::string parentElement_;
0187 };
0188 
0189 #endif