string attribute(string name, AttList& atts, string default_val /* = "" */) { for(AttList::iterator it = atts.begin(), end = atts.end(); it!=end; ++it) if( it->first == name ) return it->second; return default_val; }
// ---------------------------------------------------------------------- /// Expat C-style callback funcion void XMLCALL saxreader_start(void *userdata, const char *name, const char **atts) { SAXReader* ptr = (SAXReader*) userdata; AttList a; for(; *atts; atts += 2) a.insert( pair<string, string>( atts[0], atts[1] ) ); ptr->handle_start_element(name, a); }
// Methode qui permet de construire la liste des balises DTD de notre // classe DtdDocument void DtdDocument::setBalises( std::list<DtdBalise *> * balises ) // Algorithme : // -Parcourt la std::list de balise DTD passee en parametre a l'aide des // iterator de la STL // -Fait un dynamic_cast en AttList* de chaque element de la liste // -Si ce cast reussi, ajoute l'element dans la map <mElementsByName> puis // fait la correspondance entre ce nom d'element et la definition de l'a- // ttribut. { mBalises = balises; std::list<DtdBalise *>::iterator it = mBalises->begin(); for( ; it != mBalises->end(); it++ ) { AttList * attList = dynamic_cast<AttList *>( * it ); if( attList ) { std::map<std::string, DtdElement *>::iterator element; // Cherche dans la map <mElementsByName> le nom d'element // correspond a celui de <attList> et met a jour ce nom // le cas echeant. element = mElementsByName.find( attList->mElementName ); if( element != mElementsByName.end() ) attList->mElement = element->second; // Cherche dans la liste des definitions d'attributs une // correspondance entre l'element de reference de l'attribut // et le nom d'<element>. const std::list<AttDef *> * attDefs = attList->getAttDefs(); std::list<AttDef *>::const_iterator it2 = attDefs->begin(); for( ; it2 != attDefs->end(); it2++ ) { element = mElementsByName.find( (*it2)->mElementName ); if( element != mElementsByName.end() ) (*it2)->mElement = element->second; } } } }//----- fin de setBalises
std::string VisitorBuild::build(ElementComplexe* elementComplexe) { std::string result; result+="<"; string name(elementComplexe->getElementName()->second); result+=name; //Récupérer la liste des attributs AttList *attList = elementComplexe->getAttList(); //Parcours de la liste et affichage des attributs for(AttList::iterator it = attList->begin(); it != attList->end(); ++it) { result+=" " + (*it)->first+" = \""+ (*it)->second+"\""; } result+=">"; //récupérer les fils list<Element*> *elementList(elementComplexe->getElements()); for(list<Element*> ::iterator it = elementList->begin(); it != elementList->end(); ++it) { result+=(*it)->build(this); } //Fermer la balise de l'élément result+="</"+name+">"; return result; }
static Document & getDoc() { if (singleton) { return *singleton; } singleton = new Document; Document & doc = *singleton; doc.setDoctype(Doctype("html", "html.dtd")); AttList attributes; list<Content*> childs; ProcessingInstruction* se = new ProcessingInstruction(ElementName("", "xml")); attributes.push_back(Attribut("version", "2.0")); attributes.push_back(Attribut("encoding", "utf8")); se->setAttList(attributes); doc.setXmlProlog(se); Element* root = new Element; root->setName(ElementName("", "html")); doc.setRoot(root); Element* head = new Element; head->setName(ElementName("", "head")); list<Content*> headChildren; Element* title = new Element; title->setName(ElementName("", "title")); list<Content*> titleChild; Data* titleContent = new Data("Bienvenue"); titleChild.push_back(titleContent); title->setChildren(titleChild); headChildren.push_back(title); head->setChildren(headChildren); childs.push_back(head); Element* body = new Element; body->setName(ElementName("", "body")); childs.push_back(body); root->setChildren(childs); Element* element = new Element; element->setName(ElementName("", "a")); attributes.clear(); attributes.push_back(Attribut("alt", "Google.fr")); attributes.push_back(Attribut("href", "http://www.google.fr")); element->setAttList(attributes); EmptyElement* img = new EmptyElement; img->setName(ElementName("", "img")); attributes.clear(); attributes.push_back(Attribut("src", "http://www.google.fr/img.jpg")); img->setAttList(attributes); childs.clear(); childs.push_back(img); Data* data = new Data("Mon petit lien"); childs.push_back(data); Comment* comment = new Comment("<!-- Ceci est un commentaire. -->"); childs.push_back(comment); element->setChildren(childs); childs.clear(); childs.push_back(element); body->setChildren(childs); return doc; }