// Adding an attribute to an element sets the attr's element. TEST(TestMeiElement, TestAttributeLink) { MeiElement *p = new MeiElement("note"); MeiAttribute *a = new MeiAttribute("pname", "c"); p->addAttribute(a); ASSERT_EQ(p, a->getElement()); // And when adding many attrs MeiAttribute *b = new MeiAttribute("pname", "d"); MeiAttribute *c = new MeiAttribute("stem.dir", "down"); vector<MeiAttribute*> atts; atts.push_back(b); atts.push_back(c); p->setAttributes(atts); ASSERT_EQ(2, p->getAttributes().size()); ASSERT_EQ(p, b->getElement()); ASSERT_EQ(p, c->getElement()); }
TEST(TestMeiElement, TestGetSetHasAttributes) { MeiElement *p = new MeiElement("note"); MeiAttribute *attr1 = new MeiAttribute("pname", "c"); MeiAttribute *attr2 = new MeiAttribute("stem.dir", "down"); vector<MeiAttribute*> attrs; attrs.push_back(attr1); attrs.push_back(attr2); p->setAttributes(attrs); ASSERT_EQ(2, p->getAttributes().size()); ASSERT_TRUE(p->hasAttribute("pname")); ASSERT_TRUE(p->hasAttribute("stem.dir")); ASSERT_EQ("c", p->getAttribute("pname")->getValue()); // Adding a new attribute to the initial list doesn't // affect the attributes in the element attrs.push_back(new MeiAttribute("oct", "4")); ASSERT_EQ(2, p->getAttributes().size()); }
MeiElement* XmlImportImpl::xmlNodeToMeiElement(xmlNode *el) { string id = ""; vector<MeiAttribute*> attributes; // XML attributes and children. Text nodes will never have these. if (el->properties) { xmlAttr *curattr = NULL; for (curattr = el->properties; curattr; curattr = curattr->next) { if (curattr->atype == XML_ATTRIBUTE_ID) { /* we store the ID on the element, not as an attribute. This will be serialized out * on export */ id = (const char*)curattr->children->content; } else { string attrname = (const char*)curattr->name; // values are rendered as children of the attribute *facepalm* string attrvalue = (const char*)curattr->children->content; MeiNamespace* meins = NULL; if (curattr->ns) { if (!this->meiDocument->hasNamespace(string((const char*)curattr->ns->href))) { string prefix = (const char*)curattr->ns->prefix; string href = (const char*)curattr->ns->href; meins = new MeiNamespace(prefix, href); } else { meins = this->meiDocument->getNamespace(string((const char*)curattr->ns->href)); } } MeiAttribute *a = new MeiAttribute(meins, attrname, attrvalue); attributes.push_back(a); } } } MeiElement *obj = MeiFactory::createInstance((const char*)el->name, id); obj->setAttributes(attributes); MeiElement *lastElement = NULL; xmlNodePtr child = el->children; while (child != NULL) { if (child->type == XML_ELEMENT_NODE) { MeiElement* ch = xmlNodeToMeiElement(child); obj->addChild(ch); lastElement = ch; } else if (child->type == XML_TEXT_NODE) { if (lastElement) { const char *content = (const char*)child->content; if (content) { lastElement->setTail(content); } } else { const char *content = (const char*)child->content; if (content) { obj->setValue(content); } } } else if (child->type == XML_COMMENT_NODE) { MeiElement *comment = new MeiCommentNode(); comment->setValue(string((const char*)child->content)); obj->addChild(comment); } child = child->next; } return obj; }