CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends

DDXMLElement Class Reference

This is a base class for processing XML elements in the DDD. More...

#include <DDXMLElement.h>

Inheritance diagram for DDXMLElement:
DDLAlgoPosPart DDLAlgorithm DDLDivision DDLLogicalPart DDLMap DDLMaterial DDLNumeric DDLPosPart DDLRotationAndReflection DDLRotationByAxis DDLSolid DDLSpecPar DDLString DDLVector

List of all members.

Public Member Functions

void appendText (const std::string &inText)
 append to the current (i.e. most recently added)
virtual std::vector
< DDXMLAttribute >
::const_iterator 
begin (void)
virtual void clear (void)
 clear this element's contents.
 DDXMLElement (DDLElementRegistry *myreg, const bool &clearme)
 Constructor for autoClear element.
 DDXMLElement (DDLElementRegistry *myreg)
 Constructor.
virtual std::vector
< DDXMLAttribute >
::const_iterator 
end (void)
virtual const std::string & get (const std::string &name, size_t aIndex=0) const
 Returns a specific value from the aIndex set of attributes.
virtual const std::string & getAttribute (const std::string &name) const
 Access to attributes by name.
virtual const DDXMLAttributegetAttributeSet (size_t aIndex=0) const
 Get a "row" of attributes, i.e. one attribute set.
virtual const DDName getDDName (const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
const std::string getText (size_t tindex=0) const
 retrieve the text blob.
virtual std::vector< std::string > getVectorAttribute (const std::string &name)
 Returns a set of values as a std::vector of strings, given the attribute name.
virtual bool gotText (void) const
 gotText()? kind of like gotMilk? Yes = text has already been encountered.
virtual bool isEmpty (void) const
 Have any elements of this type been encountered but not processed?
void loadAttributes (const std::string &elemName, const std::vector< std::string > &names, const std::vector< std::string > &values, const std::string &nmspace, DDCompactView &cpv)
 Load the element attributes.
void loadText (const std::string &inText)
 Used to load both text and XML comments into this object.
std::vector< DDXMLAttribute >
::const_iterator & 
operator++ (int inc)
 Allow the elements of this type to be iterated over using ++ operator.
const std::string & parent (void) const
 access to parent element name
virtual void preProcessElement (const std::string &name, const std::string &nmspace, DDCompactView &cpv)
 Called by loadAttributes AFTER attributes are loaded.
virtual void processElement (const std::string &name, const std::string &nmspace, DDCompactView &cpv)
 Processing the element.
void setParent (const std::string &pename)
 Set parent element name to central list of names.
void setSelf (const std::string &sename)
 Set self element name to central list of names.
virtual size_t size (void) const
 Number of elements accumulated.
virtual void stream (std::ostream &os) const
 Allow for the elements to have their own streaming method, but also provide a default.
void throwError (const std::string &keyMessage) const
 format std::string for throw an error.
virtual ~DDXMLElement (void)
 Destructor.

Static Public Member Functions

static std::string itostr (int i)
 WARNING: abused by other classes in this system: yet another conversion from int to std::string...

Protected Attributes

DDLElementRegistrymyRegistry_

Private Member Functions

void appendAttributes (std::vector< std::string > &tv, const std::string &name)
 behind the scenes appending to pAttributes...

Private Attributes

AttrAccumType attributeAccumulator_
std::vector< DDXMLAttributeattributes_
bool autoClear_
std::string myElement_
std::vector< DDXMLAttribute >
::const_iterator 
myIter_
std::string parentElement_
std::vector< std::string > text_

Friends

std::ostream & operator<< (std::ostream &os, const DDXMLElement &element)

Detailed Description

This is a base class for processing XML elements in the DDD.

DDXMLElement

Component of DDL XML Parsing

A DDXMLElement stores all the attributes and text of an XML element. It is designed to accumulate this information unless cleared. In other words, it accumulates sets of attributes, and allows the appending of text indefinitely, as opposed to, say, creating another class which is designed to hold a std::vector of single element information. This is contrary to the way that XML normally defines an element, but for DDL, this works fine.

One of the things that one needs to build in to each subclass is when an element needs to be cleared. For some, emptying the std::vectors should happen at the end (of the processElement method). For some, clearing is ONLY done by the parent. For example, SpecPar and its child PartSelector. or Polyhedra and its child ZSection. In some cases elements can be in one or more parent elements as well as on their own (Vector, Map). For these the processing currently depends on the parent so one must clear only as appropriate.

Definition at line 59 of file DDXMLElement.h.


Constructor & Destructor Documentation

DDXMLElement::DDXMLElement ( DDLElementRegistry myreg)

Constructor.

Definition at line 18 of file DDXMLElement.cc.

  : myRegistry_( myreg ),
    attributes_(),
    text_(),
    autoClear_( false )
{}
DDXMLElement::DDXMLElement ( DDLElementRegistry myreg,
const bool &  clearme 
)

Constructor for autoClear element.

Definition at line 25 of file DDXMLElement.cc.

  : myRegistry_( myreg ),
    attributes_(),
    text_(),
    autoClear_( clearme )
{}
DDXMLElement::~DDXMLElement ( void  ) [virtual]

Destructor.

Definition at line 32 of file DDXMLElement.cc.

{}

Member Function Documentation

void DDXMLElement::appendAttributes ( std::vector< std::string > &  tv,
const std::string &  name 
) [private]

behind the scenes appending to pAttributes...

Definition at line 285 of file DDXMLElement.cc.

References attributes_, end(), and i.

Referenced by getVectorAttribute().

{
  for (size_t i = tv.size(); i < attributes_.size(); ++i)
  {
    DDXMLAttribute::const_iterator itnv = attributes_[i].find(name);
    if (itnv != attributes_[i].end())
      tv.push_back(itnv->second);
    else
      tv.push_back("");
  }  
}
void DDXMLElement::appendText ( const std::string &  inText)

append to the current (i.e. most recently added)

Definition at line 233 of file DDXMLElement.cc.

References runTheMatrix::msg, text_, and throwError().

Referenced by DDLSAX2FileHandler::characters().

{
  static const std::string cr("\n");
  if (text_.size() > 0) {
    text_[text_.size() - 1] += cr;
    text_[text_.size() - 1] += inText ;
  } else
  {
    std::string msg = "DDXMLElement::appendText could not append to non-existent text.";
    throwError(msg);
  }
}
std::vector< DDXMLAttribute >::const_iterator DDXMLElement::begin ( void  ) [virtual]

Definition at line 306 of file DDXMLElement.cc.

References attributes_, and myIter_.

{
  myIter_ = attributes_.begin();
  return attributes_.begin();
}
void DDXMLElement::clear ( void  ) [virtual]
std::vector< DDXMLAttribute >::const_iterator DDXMLElement::end ( void  ) [virtual]

Definition at line 313 of file DDXMLElement.cc.

References attributes_, and myIter_.

Referenced by appendAttributes(), and get().

{
  myIter_ = attributes_.end();
  return attributes_.end();
}
const std::string & DDXMLElement::get ( const std::string &  name,
size_t  aIndex = 0 
) const [virtual]

Returns a specific value from the aIndex set of attributes.

Definition at line 159 of file DDXMLElement.cc.

References attributes_, DCOUT_V, end(), itostr(), runTheMatrix::msg, AlCaRecoCosmics_cfg::name, and throwError().

{
  static const std::string sts;
  if (aIndex < attributes_.size())
  {
    DDXMLAttribute::const_iterator it = attributes_[aIndex].find(name);
    if (attributes_[aIndex].end() == it)
    {
      DCOUT_V('P', "WARNING: DDXMLElement::get did not find the requested attribute: "  << name << std::endl << *this);
      return sts;
    }
    else
      return (it->second);
  }
  std::string msg = "DDXMLElement:get failed.  It was asked for attribute " + name;
  msg += " in position " + itostr(int(aIndex)) + " when there are only ";
  msg += itostr(int(attributes_.size())) + " in the element storage.\n";
  throwError(msg);
  // meaningless...
  return sts;
}
const std::string & DDXMLElement::getAttribute ( const std::string &  name) const [virtual]

Access to attributes by name.

Definition at line 74 of file DDXMLElement.cc.

References attributes_.

{
  static const std::string ldef;
  if (attributes_.size())
    return get(name, attributes_.size() - 1);
  return ldef;
}
const DDXMLAttribute & DDXMLElement::getAttributeSet ( size_t  aIndex = 0) const [virtual]
const DDName DDXMLElement::getDDName ( const std::string &  defaultNS,
const std::string &  attname = std::string( "name" ),
size_t  aIndex = 0 
) [virtual]

Definition at line 91 of file DDXMLElement.cc.

References attributes_, itostr(), runTheMatrix::msg, AlCaRecoCosmics_cfg::name, and throwError().

Referenced by DDLRotationAndReflection::isLeftHanded(), DDLBooleanSolid::processElement(), DDLRotationSequence::processElement(), DDLSpecPar::processElement(), DDLTrapezoid::processElement(), DDLVector::processElement(), DDLCone::processElement(), DDLDivision::processElement(), DDLCompositeMaterial::processElement(), DDLEllipsoid::processElement(), DDLShapelessSolid::processElement(), DDLLogicalPart::processElement(), DDLReflectionSolid::processElement(), DDLRotationAndReflection::processElement(), DDLOrb::processElement(), DDLPseudoTrap::processElement(), DDLString::processElement(), DDLNumeric::processElement(), DDLPosPart::processElement(), DDLRotationByAxis::processElement(), DDLAlgorithm::processElement(), DDLElementaryMaterial::processElement(), DDLTubs::processElement(), DDLBox::processElement(), DDLAlgoPosPart::processElement(), DDLParallelepiped::processElement(), DDLPolyGenerator::processElement(), DDLEllipticalTube::processElement(), DDLSphere::processElement(), DDLTorus::processElement(), and DDLMap::processElement().

{
  if (aIndex < attributes_.size()
      && attributes_[aIndex].find(attname) != attributes_[aIndex].end()) { 
    std::string ns = defaultNS;
    // For the user to fully control namespaces they must provide for 
    // all name attributes something of the form, for example:
    //        <Solid name="ns:name" ...
    // If defaultNS is "!" (magic I don't like) then find and set
    // the namespace properly.
    if ( defaultNS == "!" ) {
      ns = "";
    } 
    const std::string & name = attributes_[aIndex].find(attname)->second;
    std::string rn = name;
    size_t foundColon= name.find(':');
    if (foundColon != std::string::npos) {
      ns = name.substr(0,foundColon);
      rn = name.substr(foundColon+1);

    }
    //    std::cout << "Name: " << rn << " Namespace: " << ns << std::endl;
    return DDName(rn, ns);
  }
  //  std::cout << "no " << attname <<  " default namespace: " << defaultNS << " at index " << aIndex << std::endl;
  std::string msg = "DDXMLElement:getDDName failed.  It was asked to make ";
  msg += "a DDName using attribute: " + attname;
  msg += " in position: " + itostr(int(aIndex)) + ".  There are ";
  msg += itostr(int(attributes_.size())) + " entries in the element.";
  throwError(msg);
  return DDName("justToCompile", "justToCompile"); // used to make sure it compiles
} 
const std::string DDXMLElement::getText ( size_t  tindex = 0) const

retrieve the text blob.

Definition at line 247 of file DDXMLElement.cc.

References runTheMatrix::msg, text_, and throwError().

Referenced by DDLVector::processElement(), and DDLMap::processElement().

{
  if (tindex > text_.size()) {
    std::string msg = "DDXMLElement::getText tindex is greater than text_.size()).";
    throwError(msg);
  }
  return text_[tindex];
}
std::vector< std::string > DDXMLElement::getVectorAttribute ( const std::string &  name) [virtual]

Returns a set of values as a std::vector of strings, given the attribute name.

Definition at line 184 of file DDXMLElement.cc.

References appendAttributes(), attributeAccumulator_, attributes_, DCOUT_V, AlCaRecoCosmics_cfg::name, and size().

{
  //  The idea here is that the attributeAccumulator_ is a cache of
  //  on-the-fly generation from the std::vector<DDXMLAttribute> and the 
  //  reason is simply to speed things up if it is requested more than once.
  std::vector<std::string> tv;
  AttrAccumType::const_iterator ita = attributeAccumulator_.find(name);
  if (ita != attributeAccumulator_.end())
  {
    tv = attributeAccumulator_[name];
    if (tv.size() < attributes_.size())
    {
      appendAttributes(tv, name);
    }
    DCOUT_V('P', "DDXMLElement::getAttribute found attribute named " << name << " in a map of size " << size());
  }
  else
  {
    if (attributes_.size())
    {
      appendAttributes(tv, name);
    }
    else
    {
      DCOUT_V('P', "DDXMLAttributeAccumulator::getAttribute was asked to provide a std::vector of values for an attribute named " << name << " but there was no such attribute.");
      //      throw DDException(msg);
    }
  } 
  return tv;
}
bool DDXMLElement::gotText ( void  ) const [virtual]

gotText()? kind of like gotMilk? Yes = text has already been encountered.

Definition at line 257 of file DDXMLElement.cc.

References text_.

Referenced by DDLSAX2FileHandler::characters().

{
  if (text_.size() != 0)
    return true;
  return false;
}
bool DDXMLElement::isEmpty ( void  ) const [virtual]

Have any elements of this type been encountered but not processed?

Definition at line 354 of file DDXMLElement.cc.

References attributes_.

{
  return (attributes_.size() == 0 ? true : false);
}
std::string DDXMLElement::itostr ( int  i) [static]
void DDXMLElement::loadAttributes ( const std::string &  elemName,
const std::vector< std::string > &  names,
const std::vector< std::string > &  values,
const std::string &  nmspace,
DDCompactView cpv 
)

Load the element attributes.

The loadAttributes method loads the attributes of the element into a std::map<std::string, std::string> which is used to store Name-Value pairs. It takes as input two std::vectors of strings containing "synchronized" names and values.

In the SAX2 based calling process, this is done on a startElement event.

Definition at line 44 of file DDXMLElement.cc.

References attributes_, DCOUT_V, i, and preProcessElement().

Referenced by DDLRotationByAxis::processElement(), DDLSolid::setReference(), DDLMaterial::setReference(), and DDLSAX2FileHandler::startElement().

{
  attributes_.resize(attributes_.size()+1);
  DDXMLAttribute & tAttributes =  attributes_.back();
  
  // adds attributes
  for (size_t i = 0; i < names.size(); ++i)
  {
    //      tAttributes[ names[i] ] = values[i];
    tAttributes.insert(std::make_pair(names[i], values[i]));
  }

  preProcessElement( elemName, nmspace, cpv );
  DCOUT_V('P', "DDXMLElement::loadAttributes completed. " << *this);
}
void DDXMLElement::loadText ( const std::string &  inText)

Used to load both text and XML comments into this object.

At the current time this is done simply as a way for the user of this class to accumulate text and/or comments as std::vector of strings, each one matching the std::vector of attributes. Therefore loadText starts a new text storage.

Definition at line 226 of file DDXMLElement.cc.

References text_.

Referenced by DDLSAX2FileHandler::characters(), processElement(), and DDLSAX2FileHandler::startElement().

{
  text_.push_back(inText);
  //  std::cout << "just put a std::string using loadText. size is now: " << text_.size() << std::endl;
}
std::vector< DDXMLAttribute >::const_iterator & DDXMLElement::operator++ ( int  inc)

Allow the elements of this type to be iterated over using ++ operator.

Definition at line 320 of file DDXMLElement.cc.

References myIter_.

{
  myIter_ = myIter_ + inc;
  return myIter_;
}
const std::string & DDXMLElement::parent ( void  ) const
void DDXMLElement::preProcessElement ( const std::string &  name,
const std::string &  nmspace,
DDCompactView cpv 
) [virtual]

Called by loadAttributes AFTER attributes are loaded.

The preProcessElement method can assume that the attributes are loaded and perform any code that is necessary at the start of an element.

This would allow users to call their own code to setup anything necessary for the continued processing of the child elements.

Reimplemented in DDLAlgorithm, DDLBooleanSolid, DDLCompositeMaterial, DDLDivision, DDLLogicalPart, DDLMap, DDLNumeric, DDLPolyGenerator, DDLPosPart, DDLReflectionSolid, DDLRotationByAxis, DDLRotationSequence, DDLShapelessSolid, DDLString, and DDLVector.

Definition at line 37 of file DDXMLElement.cc.

References DCOUT_V.

Referenced by loadAttributes().

{
  DCOUT_V('P', "DDXMLElement::preProcessElementBase default, do nothing) started-completed.");
}
void DDXMLElement::processElement ( const std::string &  name,
const std::string &  nmspace,
DDCompactView cpv 
) [virtual]

Processing the element.

The processElement method completes any necessary work to process the XML element.

For example, this can be used to call the DDCore to make the geometry in memory. There is a default for this so that if not declared in the inheriting class, no processing is done.

Reimplemented in DDLAlgoPosPart, DDLAlgorithm, DDLBooleanSolid, DDLBox, DDLCompositeMaterial, DDLCone, DDLDivision, DDLElementaryMaterial, DDLEllipsoid, DDLEllipticalTube, DDLLogicalPart, DDLMap, DDLNumeric, DDLOrb, DDLParallelepiped, DDLPolyGenerator, DDLPosPart, DDLPseudoTrap, DDLReflectionSolid, DDLRotationAndReflection, DDLRotationByAxis, DDLRotationSequence, DDLShapelessSolid, DDLSpecPar, DDLSphere, DDLString, DDLTorus, DDLTrapezoid, DDLTubs, and DDLVector.

Definition at line 217 of file DDXMLElement.cc.

References autoClear_, clear(), DCOUT_V, and loadText().

Referenced by DDLSAX2FileHandler::endElement().

{
  DCOUT_V('P', "DDXMLElement::processElementBase (default, do nothing) started-completed");
  loadText(std::string());
  if ( autoClear_ ) clear(); 
  
}
void DDXMLElement::setParent ( const std::string &  pename)

Set parent element name to central list of names.

Definition at line 333 of file DDXMLElement.cc.

References parentElement_.

Referenced by DDLSAX2FileHandler::endElement().

{
  parentElement_ = pename;
}
void DDXMLElement::setSelf ( const std::string &  sename)

Set self element name to central list of names.

Definition at line 339 of file DDXMLElement.cc.

References myElement_.

Referenced by DDLSAX2FileHandler::endElement().

{
  myElement_ = sename;
}
size_t DDXMLElement::size ( void  ) const [virtual]
void DDXMLElement::stream ( std::ostream &  os) const [virtual]

Allow for the elements to have their own streaming method, but also provide a default.

Definition at line 198 of file AlgoPos.cc.

References AlgoPos::end_, AlgoPos::incr_, AlgoPos::ParE_, AlgoPos::ParS_, and AlgoPos::start_.

Referenced by operator<<().

{
  os <<  "start=" << start_ << " end=" << end_ << " incr=" << incr_ << std::endl;
  parE_type::const_iterator eit = ParE_.begin();
  for (; eit != ParE_.end() ; ++eit) {
    std::vector<double>::const_iterator sit = eit->second.begin();
    os << "parE name=" << eit->first;
    for (; sit != eit->second.end(); ++sit) {
       os << " val=" << *sit << std::endl;
    }
  }
  parS_type::const_iterator stit = ParS_.begin();
  for (; stit != ParS_.end() ; ++stit) {
    std::vector<std::string>::const_iterator sit = stit->second.begin();
    os << "parS name=" << stit->first;
    for (; sit != stit->second.end(); ++sit) {
       os << " val=" << *sit << std::endl;
    }
  }
}
void DDXMLElement::throwError ( const std::string &  keyMessage) const

format std::string for throw an error.

Definition at line 360 of file DDXMLElement.cc.

References runTheMatrix::msg, and myElement_.

Referenced by appendText(), DDLMap::errorOut(), DDLVector::errorOut(), get(), getDDName(), getText(), DDLDivision::makeDivider(), DDLBooleanSolid::processElement(), DDLTrapezoid::processElement(), DDLDivision::processElement(), DDLCompositeMaterial::processElement(), DDLRotationAndReflection::processElement(), DDLTubs::processElement(), DDLAlgoPosPart::processElement(), DDLPolyGenerator::processElement(), and DDLRotationByAxis::processOne().

{
  std::string msg = keyMessage + "\n";
  //    if (myElement_) {
  msg += " Element " + myElement_ +"\n";
  //    }
  //    msg += " File " + DDLParser::instance()->getCurrFileName() + ".\n";
  throw DDException(msg);
}

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const DDXMLElement element 
) [friend]

Definition at line 264 of file DDXMLElement.cc.

{
  element.stream(os);
  return os;
}

Member Data Documentation

Definition at line 195 of file DDXMLElement.h.

Referenced by clear(), and getVectorAttribute().

std::vector<DDXMLAttribute> DDXMLElement::attributes_ [private]
bool DDXMLElement::autoClear_ [private]

Definition at line 196 of file DDXMLElement.h.

Referenced by processElement().

std::string DDXMLElement::myElement_ [private]

Definition at line 198 of file DDXMLElement.h.

Referenced by setSelf(), and throwError().

std::vector<DDXMLAttribute>::const_iterator DDXMLElement::myIter_ [private]

Definition at line 197 of file DDXMLElement.h.

Referenced by begin(), end(), and operator++().

std::string DDXMLElement::parentElement_ [private]

Definition at line 199 of file DDXMLElement.h.

Referenced by parent(), and setParent().

std::vector<std::string> DDXMLElement::text_ [private]

Definition at line 194 of file DDXMLElement.h.

Referenced by appendText(), clear(), getText(), gotText(), and loadText().