void XmlElement::SetAttribute( const char * cname, const char * cvalue ) { XmlAttribute* attrib = attributeSet.FindOrCreate( cname ); if ( attrib ) { attrib->SetValue( cvalue ); } }
//=========================================== // PauseMenu::PauseMenu //=========================================== PauseMenu::PauseMenu(const XmlNode data) : Asset(internString("PauseMenu")), Entity(data.firstChild().firstChild().firstChild()), Menu(data.firstChild()) { try { AssetManager assetManager; XML_NODE_CHECK(data, PauseMenu); XmlNode node = data.nthChild(1); XML_NODE_CHECK(node, flare); XmlAttribute attr = node.firstAttribute(); XML_ATTR_CHECK(attr, ptr); long id = attr.getLong(); m_flare = boost::dynamic_pointer_cast<CSprite>(assetManager.getAssetPointer(id)); if (!m_flare) throw XmlException("Bad asset id for flare item", __FILE__, __LINE__); m_flare->addToWorld(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class PauseMenu; "); throw; } }
//=========================================== // Animation::Animation //=========================================== Animation::Animation(const XmlNode data) : Asset(internString("Animation")), m_state(STOPPED), m_frameReady(false) { try { XML_NODE_CHECK(data, Animation); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, name); m_name = internString(attr.getString()); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, duration); m_duration = attr.getFloat(); uint_t f = 0; XmlNode node = data.firstChild(); while (!node.isNull() && node.name() == "AnimFrame") { AnimFrame frame(node); frame.number = f; m_frames.push_back(frame); ++f; node = node.nextSibling(); } } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Animation; "); throw; } }
void XmlElement::SetAttribute( const std::string& name, int val ) { XmlAttribute* attrib = attributeSet.FindOrCreate( name ); if ( attrib ) { attrib->SetIntValue( val ); } }
void XmlElement::SetDoubleAttribute( const std::string& name, double val ) { XmlAttribute* attrib = attributeSet.FindOrCreate( name ); if ( attrib ) { attrib->SetDoubleValue( val ); } }
bool CXmlDocument::SetNodeAttribute(const CXmlNode& node, const CString& attributeName, const CString& value) { if(node.GetNode() == NULL) return FALSE; XmlAttribute pAttr; HRESULT hr = m_pDoc->createAttribute((_bstr_t)attributeName, &pAttr); if ( FAILED(hr) ) return FALSE; hr = pAttr->put_text((_bstr_t)value); if( FAILED(hr) ) return FALSE; XmlMap pNodeMap; hr = node.GetNode()->get_attributes(&pNodeMap); if ( FAILED(hr) ) return FALSE; XmlNode pNamedItem; hr = pNodeMap->setNamedItem(pAttr, &pNamedItem); if ( FAILED(hr) ) return FALSE; return TRUE; }
//=========================================== // CreditsMenu::CreditsMenu //=========================================== CreditsMenu::CreditsMenu(const XmlNode data) : Asset(internString("CreditsMenu")), Entity(data.firstChild().firstChild().firstChild()), Menu(data.firstChild()) { try { AssetManager assetManager; XML_NODE_CHECK(data, CreditsMenu); XmlNode node = data.nthChild(1); XML_NODE_CHECK(node, font); XmlAttribute attr = node.firstAttribute(); XML_ATTR_CHECK(attr, ptr); long id = attr.getLong(); m_font = boost::dynamic_pointer_cast<Dodge::Font>(assetManager.getAssetPointer(id)); if (!m_font) throw XmlException("Bad font asset id", __FILE__, __LINE__); node = node.nextSibling(); XML_NODE_CHECK(node, fadeInTime); m_fadeInTime = node.getFloat(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class CreditsMenu; "); throw; } init(); }
void XmlAttributeUnitTests::testEmpty() { XmlAttribute attr; CPPUNIT_ASSERT(attr.GetValue().empty()); CPPUNIT_ASSERT(false == attr.GetBoolValue(false)); CPPUNIT_ASSERT(true == attr.GetBoolValue(true)); }
void XmlElement::SetAttribute( const std::string& _name, const std::string& _value ) { XmlAttribute* attrib = attributeSet.FindOrCreate( _name ); if ( attrib ) { attrib->SetValue( _value ); } }
void XmlSchema::addSimpleVector(const XmlNode* child, String& structDefinition, size_t typeWidth, String& readingFunction, String& writingFunction) const { assert(child != NULL); XmlAttribute* type = child->findAttribute(ATTR_TYPE); if (type == NULL) { return; } String typeString = T("std::vector<"); typeString += getSimpleTypeString(type); typeString += T(">"); size_t thisWidth = typeString.size(); assert(thisWidth < typeWidth + 1); for (size_t i = 0; i < typeWidth + 1 - thisWidth; ++i) { typeString += T(" "); } structDefinition += T(" "); structDefinition += typeString; structDefinition += getPluralName(child->getName()); structDefinition += T(";\r\n"); readingFunction += T("\r\n childNode = node->findFirstChild("); readingFunction += LEFT_QUOTE; readingFunction += child->getName(); readingFunction += T("\", iter);\r\n while (childNode != NULL)\r\n {\r\n "); readingFunction += getPluralName(child->getName()); readingFunction += T(".resize("); readingFunction += getPluralName(child->getName()); readingFunction += T(".size() + 1);\r\n "); readingFunction += getPluralName(child->getName()); readingFunction += T(".back() = childNode->get"); String typeName = type->getString(); typeName[0] -= 32; readingFunction += typeName; readingFunction += T("();\r\n childNode = node->findNextChild("); readingFunction += LEFT_QUOTE; readingFunction += child->getName(); readingFunction += T("\", iter);\r\n }\r\n"); writingFunction += T("\r\n for (std::vector<"); writingFunction += getSimpleTypeString(type); writingFunction += T(">::const_iterator iter = "); writingFunction += getPluralName(child->getName()); writingFunction += T(".begin();\r\n iter != "); writingFunction += getPluralName(child->getName()); writingFunction += T(".end();\r\n ++iter)\r\n {\r\n const "); writingFunction += getSimpleTypeString(type); writingFunction += T("& value = *iter;\r\n"); writingFunction += T(" childNode = node->addChild("); writingFunction += LEFT_QUOTE; writingFunction += child->getName(); writingFunction += T("\");\r\n childNode->set"); writingFunction += typeName; writingFunction += T("(value);\r\n }\r\n"); }
XmlAttribute *XmlElement::XmlAttributeSet::FindOrCreate(const String &sName) { XmlAttribute *pAttribute = Find(sName); if (!pAttribute) { pAttribute = new XmlAttribute(); Add(*pAttribute); pAttribute->SetName(sName); } return pAttribute; }
XmlAttribute* XmlAttributeSet::FindOrCreate( const char* _name ) { XmlAttribute* attrib = Find( _name ); if ( !attrib ) { attrib = new XmlAttribute(); Add( attrib ); attrib->SetName( _name ); } return attrib; }
//=========================================== // Item::Item //=========================================== Item::Item(const XmlNode data) { try { XML_NODE_CHECK(data, Item); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, solid); m_solid = attr.getBool(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Item; "); throw; } }
//=========================================== // Item::assignData //=========================================== void Item::assignData(const XmlNode data) { try { XML_NODE_CHECK(data, Item); XmlAttribute attr = data.firstAttribute(); if (!attr.isNull() && attr.name() == "solid") m_solid = attr.getBool(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Item; "); throw; } }
//=========================================== // Box2dPhysics::Box2dPhysics //=========================================== Box2dPhysics::Box2dPhysics(Entity* entity, const XmlNode data) : EntityPhysics(entity, data), m_init(false), m_entity(entity), m_body(NULL), m_opts(false, false, 1.f, 0.3f) { init(); try { XML_NODE_CHECK(data, Box2dPhysics); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, dynamic); m_opts.dynamic = attr.getBool(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, fixedAngle); m_opts.fixedAngle = attr.getBool(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, density); m_opts.density = attr.getFloat(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, friction); m_opts.friction = attr.getFloat(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Box2dPhysics; "); throw; } }
//=========================================== // Vec2i::Vec2i //=========================================== Vec2i::Vec2i(const XmlNode data) { try { XML_NODE_CHECK(data, Vec2i); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, x); x = attr.getInt(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, y); y = attr.getInt(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Vec2i; "); throw; } }
size_t XmlSchema::getNodeMemberTypeWidth(const XmlNode* node) const { size_t maxWidth = 0; size_t width = 0; NodeIterator iter; for (const XmlNode* child = node->getFirstChild(iter); child != NULL; child = node->getNextChild(iter)) { XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE); if (child->isEmpty()) { //simple type XmlAttribute* type = child->findAttribute(ATTR_TYPE); if (type == NULL) { continue; } width = getSimpleTypeString(type).size(); if (multiple != NULL && multiple->getBool()) { width += Strlen(T("std::vector<>")); } } else { //struct type width = Strlen(child->getName()); if (multiple != NULL && multiple->getBool()) { width += Strlen(T("std::vector<>")); } } if (width > maxWidth) { maxWidth = width; } } return maxWidth; }
bool LibreOfficeInspector::_readNodeCallback(XmlNode node, void *data) { vector <XmlAttribute>* attributes; bool bIsItem; if (g_parsing_state == PropUILocale && node.GetName().compare(L"value")==0) { wstring* lang_found = (wstring *) data; *lang_found = node.GetText(); g_parsing_state = ItemOther; return false; } bIsItem = node.GetName().compare(L"item") == 0; if (bIsItem && g_parsing_state != ItemOther) { g_parsing_state = ItemOther; } attributes = node.GetAttributes(); for (unsigned int i = 0; i < attributes->size(); i++) { XmlAttribute attribute; attribute = attributes->at(i); if (g_parsing_state == ItemOther && bIsItem && attribute.GetName() == L"oor:path" && attribute.GetValue() == L"/org.openoffice.Office.Linguistic/General") { g_parsing_state = ItemLinguisticGeneral; } if (g_parsing_state == ItemLinguisticGeneral && attribute.GetName() == L"oor:name" && attribute.GetValue() == L"UILocale") { g_parsing_state = PropUILocale; } } return true; }
//[-------------------------------------------------------] //[ Protected virtual XmlTextElement functions ] //[-------------------------------------------------------] void XmlTextImage::OnParse(XmlNode &cXmlNode) { // Is this an XML element? if (cXmlNode.GetType() == XmlNode::Element) { // Destroy the previous image if (m_pImage) { delete m_pImage; m_pImage = nullptr; } // Get XML element XmlElement &cXmlElement = static_cast<XmlElement&>(cXmlNode); // Parse attributes XmlAttribute *pAttribute = cXmlElement.GetFirstAttribute(); while (pAttribute) { // Get name and value String sName = pAttribute->GetName(); String sValue = pAttribute->GetValue(); // Save attribute if (sName.CompareNoCase("Src")) { // Image filename m_sFilename = sValue; } else if (sName.CompareNoCase("Width")) { // Image width m_vSize.x = sValue.GetInt(); } if (sName.CompareNoCase("Height")) { // Image height m_vSize.y = sValue.GetInt(); } // Next attribute pAttribute = pAttribute->GetNext(); } } }
//=========================================== // TextEntity::TextEntity //=========================================== TextEntity::TextEntity(const XmlNode data) : Asset(internString("TextEntity")), Entity(data.firstChild()), m_renderer(Renderer::getInstance()), m_model(Renderer::TRIANGLES) { try { XML_NODE_CHECK(data, TextEntity); XmlNode node = data.nthChild(1); XML_NODE_CHECK(node, font); XmlAttribute attr = node.firstAttribute(); XML_ATTR_CHECK(attr, ptr); long fontId = attr.getLong(); AssetManager assetManager; pFont_t font = boost::dynamic_pointer_cast<Dodge::Font>(assetManager.getAssetPointer(fontId)); if (!font) throw XmlException("Bad asset id", __FILE__, __LINE__); m_font = font; node = node.nextSibling(); XML_NODE_CHECK(node, textSize); m_size = Vec2f(node.firstChild()); node = node.nextSibling(); XML_NODE_CHECK(node, text); setText(node.getString()); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class TextEntity; "); throw; } }
//=========================================== // MusicTrack::MusicTrack //=========================================== MusicTrack::MusicTrack(const XmlNode data) : Asset(internString("MusicTrack")) { string path; try { XML_NODE_CHECK(data, MusicTrack); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, path); stringstream ss; ss << gGetWorkingDir() << "/" << attr.getString(); path = ss.str(); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class MusicTrack; "); throw; } Audio audio; audio.newMusicTrack(this, path); }
//=========================================== // TextEntity::assignData //=========================================== void TextEntity::assignData(const XmlNode data) { try { if (data.isNull() || data.name() != "TextEntity") return; XmlNode node = data.nthChild(1); if (!node.isNull() && node.name() == "font") { XmlAttribute attr = node.firstAttribute(); if (!attr.isNull() && attr.name() == "ptr") { long fontId = attr.getLong(); AssetManager assetManager; pFont_t font = boost::dynamic_pointer_cast<Dodge::Font>(assetManager.getAssetPointer(fontId)); if (!font) throw XmlException("Bad asset id", __FILE__, __LINE__); m_font = font; } node = node.nextSibling(); } if (!node.isNull() && node.name() == "textSize") { m_size = Vec2f(node.firstChild()); node = node.nextSibling(); } XML_NODE_CHECK(node, text); setText(node.getString()); } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class TextEntity; "); throw; } }
void XmlSchema::addConstructorItem(const XmlNode* child, String& structDefinition, int& index) const { XmlAttribute* type = child->findAttribute(ATTR_TYPE); bool recursive = (child->findAttribute(ATTR_RECURSIVE) != NULL); if (type == NULL && !recursive) { return; } XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE); if (multiple != NULL && multiple->getBool()) { //don't need to construct a vector member return; } XmlAttribute* defaultAttribute = child->findAttribute(ATTR_DEFAULT); if (type != NULL && Strcmp(type->getString(), T("string")) == 0 && defaultAttribute == NULL) { //for String, need construction only when default value exist return; } if (index == 0) { structDefinition += T(" "); structDefinition += child->getParent()->getName(); structDefinition += T("()\r\n : "); } else { structDefinition += T(" , "); } if (recursive) { structDefinition += T("Child"); } else { structDefinition += child->getName(); } structDefinition += T("("); ++index; if (defaultAttribute != NULL) { if (Strcmp(type->getString(), T("string")) == 0) { structDefinition += LEFT_QUOTE; structDefinition += defaultAttribute->getString(); structDefinition += T("\""); } else { structDefinition += defaultAttribute->getString(); } } else { if (recursive) { structDefinition += T("NULL"); } else { structDefinition += getTypeDefaultValue(type->getString()); } } structDefinition += T(")\r\n"); }
void XmlSchema::addSimpleMember(const XmlNode* child, String& structDefinition, size_t typeWidth, String& readingFunction, String& writingFunction) const { assert(child != NULL); XmlAttribute* type = child->findAttribute(ATTR_TYPE); XmlAttribute* defaultAttribute = child->findAttribute(ATTR_DEFAULT); bool inAttribute = (child->findAttribute(ATTR_ATTRIBUTE) != NULL); if (type == NULL) { return; } String typeString = getSimpleTypeString(type); size_t thisWidth = typeString.size(); assert(thisWidth < typeWidth + 1); for (size_t i = 0; i < typeWidth + 1 - thisWidth; ++i) { typeString += T(" "); } structDefinition += T(" "); structDefinition += typeString; structDefinition += child->getName(); structDefinition += T(";\r\n"); readingFunction += inAttribute ? T("\r\n attribute = node->findAttribute(") : T("\r\n childNode = node->findChild("); readingFunction += LEFT_QUOTE; readingFunction += child->getName(); readingFunction += inAttribute ? T("\");\r\n if (attribute != NULL)\r\n {\r\n ") : T("\");\r\n if (childNode != NULL)\r\n {\r\n "); readingFunction += child->getName(); readingFunction += inAttribute ? T(" = attribute->get") : T(" = childNode->get"); String typeName = type->getString(); typeName[0] -= 32; readingFunction += typeName; readingFunction += T("();\r\n }\r\n"); writingFunction += T("\r\n if ("); writingFunction += child->getName(); writingFunction += T(" != "); if (defaultAttribute != NULL) { //with initialized value if (Strcmp(type->getString(), T("string")) == 0) { writingFunction += LEFT_QUOTE; } writingFunction += defaultAttribute->getString(); if (Strcmp(type->getString(), T("string")) == 0) { writingFunction += T("\""); } } else { writingFunction += getTypeDefaultValue(type->getString()); } writingFunction += inAttribute ? T(")\r\n {\r\n attribute = node->addAttribute(") : T(")\r\n {\r\n childNode = node->addChild("); writingFunction += LEFT_QUOTE; writingFunction += child->getName(); writingFunction += inAttribute ? T("\");\r\n attribute->set") : T("\");\r\n childNode->set"); typeName = type->getString(); typeName[0] -= 32; writingFunction += typeName; writingFunction += T("("); writingFunction += child->getName(); writingFunction += T(");\r\n }\r\n"); }
//=========================================== // Entity::Entity //=========================================== Entity::Entity(const XmlNode data) : Asset(internString("Entity")), m_silent(false), m_parent(NULL) { AssetManager assetManager; ShapeFactory shapeFactory; try { setSilent(true); XML_NODE_CHECK(data, Entity); XmlAttribute attr = data.firstAttribute(); XML_ATTR_CHECK(attr, type); m_type = internString(attr.getString()); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, name); m_name = internString(attr.getString()); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, x); m_transl.x = attr.getFloat(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, y); m_transl.y = attr.getFloat(); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, z); m_z = attr.getFloat(); // So that no Z values are 'exactly' equal m_z += 0.1f * static_cast<float32_t>(rand()) / static_cast<float32_t>(RAND_MAX); attr = attr.nextAttribute(); XML_ATTR_CHECK(attr, rot); float32_t rot = attr.getFloat(); XmlNode node = data.firstChild(); if (!node.isNull() && node.name() == "shape") { m_shape = unique_ptr<Shape>(shapeFactory.create(node.firstChild())); node = node.nextSibling(); } m_rot = 0; setRotation(rot); XML_NODE_CHECK(node, scale); m_scale = Vec2f(1.f, 1.f); setScale(Vec2f(node.firstChild())); node = node.nextSibling(); XML_NODE_CHECK(node, fillColour); m_fillColour = Colour(node.firstChild()); node = node.nextSibling(); XML_NODE_CHECK(node, lineColour); m_lineColour = Colour(node.firstChild()); node = node.nextSibling(); XML_NODE_CHECK(node, lineWidth); m_lineWidth = node.getInt(); node = node.nextSibling(); XML_NODE_CHECK(node, children); XmlNode node_ = node.firstChild(); while (!node_.isNull() && node_.name() == "child") { XmlAttribute attr = node_.firstAttribute(); if (!attr.isNull() && attr.name() == "ptr") { long id = attr.getLong(); pEntity_t child = boost::dynamic_pointer_cast<Entity>(assetManager.getAssetPointer(id)); if (!child) throw XmlException("Bad entity asset id", __FILE__, __LINE__); addChild(child); } node_ = node_.nextSibling(); } setSilent(false); ++m_count; } catch (XmlException& e) { e.prepend("Error parsing XML for instance of class Entity; "); throw; } recomputeBoundary(); }
//=========================================== // Application::constructAsset //=========================================== pAsset_t Application::constructAsset(const XmlNode data) { long proto = -1; bool addToWorld = false; XmlAttribute attr = data.firstAttribute(); attr = attr.nextAttribute(); if (!attr.isNull() && attr.name() == "proto") { proto = attr.getLong(); attr = attr.nextAttribute(); } if (!attr.isNull() && attr.name() == "addToWorld") { addToWorld = attr.getBool(); } XmlNode node = data.firstChild(); // Construct non-Entity assets if (node.name() == "Texture") return pAsset_t(new Texture(node)); // ... // Construct Entities pEntity_t entity; // Does the XML description reference a prototype? if (proto != -1) { entity = pEntity_t(dynamic_cast<Entity*>(m_assetManager.cloneAsset(proto))); if (!entity) throw Exception("Prototype not found", __FILE__, __LINE__); // If the prototype has an Item object (aux data), set it to point at the entity. IAuxData* p = entity->getAuxDataPtr(); if (p) { Item* item = dynamic_cast<Item*>(p); assert(item); item->setEntity(entity.get()); } // If this XML node contains the entity along with an Item bool hasAuxData = false; if (node.name() == "ExtEntity") { node = node.firstChild(); hasAuxData = true; } // The Entity node comes before the Item node entity->assignData(node); // Now we construct the Item if (hasAuxData) { node = node.nextSibling(); if (node.name() != "Item") throw Exception("Expected Item node", __FILE__, __LINE__); Item* item = new Item(node); item->setEntity(entity.get()); entity->attachAuxData(unique_ptr<Item>(item)); } } // If XML description does not reference a prototype else { bool hasAuxData = false; if (node.name() == "ExtEntity") { node = node.firstChild(); hasAuxData = true; } if (node.name() == "Player") entity = pEntity_t(new Player(node)); if (node.name() == "Soil") entity = pEntity_t(new Soil(node)); if (node.name() == "ParallaxSprite") entity = pEntity_t(new ParallaxSprite(node)); if (node.name() == "Entity") entity = pEntity_t(new Entity(node)); if (node.name() == "Sprite") entity = pEntity_t(new Sprite(node)); if (node.name() == "PhysicalEntity") entity = pEntity_t(new PhysicalEntity<Box2dPhysics>(node)); if (node.name() == "PhysicalSprite") entity = pEntity_t(new PhysicalSprite<Box2dPhysics>(node)); if (!entity) { Exception ex("Unrecognised entity type '", __FILE__, __LINE__); ex.append(node.name()); ex.append("'"); throw ex; } if (hasAuxData) { node = node.nextSibling(); if (node.name() != "Item") throw Exception("Expected Item node", __FILE__, __LINE__); Item* item = new Item(node); item->setEntity(entity.get()); entity->attachAuxData(unique_ptr<Item>(item)); } } if (addToWorld) { entity->addToWorld(); m_worldSpace.insertAndTrackEntity(entity); m_entities[entity->getName()] = entity; } return entity; }
bool XmlSchema::generateCodeForNode(const XmlNode* node, String& headerCode, String& sourceCode) const { assert(node != NULL); if (node->getType() == ELEMENT) { String structDefinition; structDefinition += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n"); structDefinition += T("struct "); structDefinition += node->getName(); structDefinition += T("\r\n{\r\n"); //constructor int index = 0; NodeIterator iter; for (XmlNode* child = node->getFirstChild(iter); child != NULL; child = node->getNextChild(iter)) { if (child->isEmpty() && child->getType() == ELEMENT) { //simple type addConstructorItem(child, structDefinition, index); } } if (index > 0) { structDefinition += T(" {\r\n }\r\n"); } //destructor for (XmlNode* child = node->getFirstChild(iter); child != NULL; child = node->getNextChild(iter)) { if (child->findAttribute(ATTR_RECURSIVE) != NULL && child->findAttribute(ATTR_MULTIPLE) == NULL) { //recursive pointer, need to delete structDefinition += T(" ~"); structDefinition += node->getName(); structDefinition += T("()\r\n {\r\n if (Child != NULL)\r\n {\r\n delete Child;\r\n"); structDefinition += T(" Child = NULL;\r\n }\r\n }\r\n"); break; //can't have more than one } } structDefinition += T(" void read(const slim::XmlNode* node);\r\n void write(slim::XmlNode* node) const;\r\n\r\n"); String readingCode; readingCode += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n"); readingCode += T("void "); readingCode += node->getName(); readingCode += T("::read(const XmlNode* node)\r\n{\r\n assert(node != NULL);\r\n"); readingCode += T("\r\n NodeIterator iter;\r\n const XmlNode* childNode = NULL;\r\n const XmlAttribute* attribute = NULL;\r\n"); String writingCode; writingCode += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n"); writingCode += T("void "); writingCode += node->getName(); writingCode += T("::write(XmlNode* node) const\r\n{\r\n assert(node != NULL);\r\n\r\n node->clearChild();\r\n node->clearAttribute();"); writingCode += T("\r\n\r\n XmlNode* childNode = NULL;\r\n XmlAttribute* attribute = NULL;\r\n"); size_t typeWidth = getNodeMemberTypeWidth(node); for (const XmlNode* child = node->getFirstChild(iter); child != NULL; child = node->getNextChild(iter)) { if (child->getType() != ELEMENT) { continue; } XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE); bool recursive = (child->findAttribute(ATTR_RECURSIVE) != NULL); if (child->isEmpty() && !recursive) { //simple type if (multiple != NULL && multiple->getBool()) { addSimpleVector(child, structDefinition, typeWidth, readingCode, writingCode); } else { addSimpleMember(child, structDefinition, typeWidth, readingCode, writingCode); } } else { //struct type if (multiple != NULL && multiple->getBool()) { addStructVector(child, structDefinition, typeWidth, readingCode, writingCode); } else { addStructMember(child, structDefinition, typeWidth, readingCode, writingCode); } } } structDefinition += T("};\r\n\r\n"); readingCode += T("}\r\n\r\n"); writingCode += T("}\r\n\r\n"); sourceCode += readingCode; sourceCode += writingCode; //add to front headerCode = structDefinition + headerCode; } NodeIterator iter; for (const XmlNode* child = node->getFirstChild(iter); child != NULL; child = node->getNextChild(iter)) { if (child->hasChild()) { if (!generateCodeForNode(child, headerCode, sourceCode)) { return false; } } } return true; }
/////////////////////////////////////////////////////////////////////////////////////////////////// //parse data schema from node //src data node //dst schema node bool XmlSchema::parseNodeStruct(XmlNode* dst, XmlNode* src) { assert(dst != NULL); assert(src != NULL); NodeIterator nodeIterator; AttributeIterator attriIterator; for (XmlAttribute* attribute = src->getFirstAttribute(attriIterator); attribute != NULL; attribute = src->getNextAttribute(attriIterator)) { XmlNode* structure = dst->findChild(attribute->getName()); if (structure == NULL) { //first time show up structure = dst->addChild(attribute->getName()); structure->addAttribute(ATTR_TYPE, guessType(attribute->getString())); structure->addAttribute(ATTR_ATTRIBUTE, T("true")); } } for (XmlNode* child = src->getFirstChild(nodeIterator); child != NULL; child = src->getNextChild(nodeIterator)) { if (child->getType() != ELEMENT) { continue; } XmlNode* structure = dst->findChild(child->getName()); if (structure == NULL) { //first time show up bool recursive = false; const XmlNode* parent = dst; while (parent != NULL) { if (Strcmp(parent->getName(), child->getName()) == 0) { recursive = true; break; } parent = parent->getParent(); } structure = dst->addChild(child->getName()); if (recursive) { structure->addAttribute(ATTR_RECURSIVE, T("true")); } else if (!child->hasChild() && !child->hasAttribute()) { //simple type, must have a type attribute structure->addAttribute(ATTR_TYPE, guessType(child->getString())); } } else if (structure->findAttribute(ATTR_ATTRIBUTE) != NULL) { //child and attribute can't have same name return false; } XmlAttribute* multiple = structure->findAttribute(ATTR_MULTIPLE); if (multiple == NULL || !multiple->getBool()) { NodeIterator iter; if (src->findFirstChild(child->getName(), iter) != NULL && src->findNextChild(child->getName(), iter) != NULL) { if (multiple == NULL) { multiple = structure->addAttribute(ATTR_MULTIPLE); } multiple->setBool(true); } } if (!structure->findAttribute(ATTR_RECURSIVE) && (child->hasChild() || child->hasAttribute())) { parseNodeStruct(structure, child); } } return true; }
bool Test::testCase4() const { if (m_verbose) cout << "\tCase 4: Move node between documents (2)\n"; /* <nodeA> <nodeB a="1" b="two" c="3.4"/> <nodeC d="true"> <nodeD> Hello World! </nodeD> <nodeE> Goodbye World! </nodeE> </nodeC> </nodeA> */ XmlDocument doc2; try { XmlDocument doc1; doc1.parse("./test4.xml"); XmlNode node = doc1.firstNode(); XML_NODE_CHECK(node, nodeA); node = node.firstChild(); XML_NODE_CHECK(node, nodeB); node = node.nextSibling(); XML_NODE_CHECK(node, nodeC); XmlNode node_ = doc2.addNode("MyNode"); node_.addNode(node); } // doc1 destroyed here catch (XmlException& e) { if (m_verbose) { cout << e.what() << "\n"; } return false; } bool pass = true; bool b = false; XmlNode node = doc2.firstNode(); SUBCASE_CHECK_CRITICAL(!node.isNull()); SUBCASE_CHECK(node.name().compare("MyNode") == 0, pass, b); XmlNode node_ = node.firstChild(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("nodeC") == 0, pass, b); XmlAttribute attr = node_.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("d") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("true") == 0, pass, b); XmlNode node__ = node_.firstChild(); SUBCASE_CHECK_CRITICAL(!node__.isNull()); SUBCASE_CHECK(node__.name().compare("nodeD") == 0, pass, b); node__ = node__.nextSibling(); SUBCASE_CHECK_CRITICAL(!node__.isNull()); SUBCASE_CHECK(node__.name().compare("nodeE") == 0, pass, b); if (m_verbose) cout << (pass ? "\tPASS\n" : "\tFAIL\n"); return pass; }
bool Test::testCase1() const { bool pass = true; bool b = false; if (m_verbose) cout << "\tCASE 1: Parsing good document and checking tree\n"; try { XmlDocument doc; doc.parse("./test1.xml"); /* <?xml version="1.0" encoding="utf-8"?> <element1 a="1" b="2"> <element2 c="3" d="true"/> <element3>Hello World!</element3> <element4 a="123"> <element5 yes="no"> <element6 str="This is a string" num="4.5"/> <element7>99.8</element7> </element5> </element4> </element1> */ XmlNode node = doc.firstNode(); node = node.nextSibling(); SUBCASE_CHECK_CRITICAL(!node.isNull()); SUBCASE_CHECK(node.name().compare("element1") == 0, pass, b); XmlAttribute attr = node.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("a") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 1, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("b") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 2, pass, b); { XmlNode node_ = node.firstChild(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element2") == 0, pass, b); XmlAttribute attr = node_.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("c") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 3, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("d") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("true") == 0, pass, b); node_ = node_.nextSibling(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element3") == 0, pass, b); SUBCASE_CHECK(node_.getString().compare("Hello World!") == 0, pass, b); node_ = node_.nextSibling(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element4") == 0, pass, b); attr = node_.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("a") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 123, pass, b); { XmlNode node__ = node_.firstChild(); SUBCASE_CHECK_CRITICAL(!node__.isNull()); SUBCASE_CHECK(node__.name().compare("element5") == 0, pass, b); XmlAttribute attr = node__.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("yes") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("no") == 0, pass, b); { XmlNode node___ = node__.firstChild(); SUBCASE_CHECK_CRITICAL(!node___.isNull()); SUBCASE_CHECK(node___.name().compare("element6") == 0, pass, b); XmlAttribute attr = node___.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("str") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("This is a string") == 0, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("num") == 0, pass, b); SUBCASE_CHECK(attr.getFloat() == 4.5, pass, b); } } } } catch (XmlException& e) { pass = false; if (m_verbose) { cout << e.what() << "\n"; } } if (m_verbose) cout << (pass ? "\tPASS\n" : "\tFAIL\n"); return pass; }