void XMLEntityMappingDefinitionSerializer::parseCaseElement(EntityMappingDefinition& definition, CaseDefinition& caseDef, TiXmlElement* element)
{
	for (TiXmlAttribute* attribute = element->FirstAttribute();
            attribute != 0; attribute = attribute->Next())
    {
    	caseDef.getProperties()[attribute->Name()] = attribute->Value();
	}


	if (!element->NoChildren()) {
		for (TiXmlElement* childElement = element->FirstChildElement();
				childElement != 0; childElement = childElement->NextSiblingElement())
		{
			if (std::string(childElement->Value()) == std::string("action")) {
				ActionDefinition actionDef;
				parseActionElement(definition, actionDef, childElement);
				caseDef.getActions().push_back(actionDef);
			} else if (std::string(childElement->Value()) == std::string("caseparam")){
				//it's a case parameter
				if (const char* attributeValue = childElement->Attribute("type")) {
					if (TiXmlNode* textNode =  childElement->FirstChild()) {
						std::string type(attributeValue);
						std::string value(textNode->Value());
						caseDef.getCaseParameters().push_back(std::pair<std::string, std::string>(type, value));
					}
				}
			} else {
				//we'll assume it's a match
				MatchDefinition matchDef;
				parseMatchElement(definition, matchDef, childElement);
				caseDef.getMatches().push_back(matchDef);
			}
		}
	}
}
void XMLEntityMappingDefinitionSerializer::parseMatchElement(EntityMappingDefinition& definition, MatchDefinition& matchDef, TiXmlElement* element)
{
	std::string caseType("");
	if (std::string(element->Value()) == std::string("entitymatch")) {
		matchDef.setType("entitytype");
		caseType = "entitytypecase";
	} else if (std::string(element->Value()) == std::string("attributematch")) {
		matchDef.setType("attribute");
		caseType = "attributecase";

/*		const char* tmp =  smElem->Attribute("attribute");
		matchDef.getProperties()["attribute"] = std::string(tmp);*/
	} else if (std::string(element->Value()) == std::string("outfitmatch")) {
		matchDef.setType("outfit");
		caseType = "outfitcase";
	}

	for (TiXmlAttribute* attribute = element->FirstAttribute();
            attribute != 0; attribute = attribute->Next())
    {
    	matchDef.getProperties()[attribute->Name()] = attribute->Value();
	}

	if (!element->NoChildren()) {
		for (TiXmlElement* childElement = element->FirstChildElement();
				childElement != 0; childElement = childElement->NextSiblingElement())
		{
			CaseDefinition caseDef;
			caseDef.setType(caseType);
			parseCaseElement(definition, caseDef, childElement);
			matchDef.getCases().push_back(caseDef);
		}
	}
}
Example #3
0
EntityMapping* EntityMappingManager::createMapping(Eris::Entity& entity, IActionCreator& actionCreator, Eris::View* view) {
	if (mTypeService) {
		EntityMappingDefinition* definition = nullptr;
		if (entity.hasAttr("present")) {
			auto mappingElement = entity.valueOfAttr("present");
			if (mappingElement.isString() && !mappingElement.String().empty()) {
				auto I = mDefinitions.find(mappingElement.String());
				if (I != mDefinitions.end()) {
					definition = I->second;
				}
			}
		}

		if (definition) {
			EntityMappingCreator creator(*definition, entity, actionCreator, *mTypeService, view);
			return creator.create();
		} else {
			auto mapping = new EntityMapping(entity);

			auto attributeMatch = new Matches::SingleAttributeMatch("present");
			auto* attributeCase = new Cases::AttributeCase(new Cases::AttributeComparers::StringComparerWrapper(new Cases::AttributeComparers::StringNotEmptyComparer()));
			Matches::Observers::MatchAttributeObserver* observer = new Matches::Observers::MatchAttributeObserver(attributeMatch, "present");
			attributeMatch->setMatchAttributeObserver(observer);

			attributeMatch->addCase(attributeCase);
			CaseDefinition caseDefinition;
			ActionDefinition actionDefinition;
			actionDefinition.setType("present");
			caseDefinition.getActions().emplace_back(std::move(actionDefinition));
			actionCreator.createActions(*mapping, attributeCase, caseDefinition);
			mapping->getBaseCase().addMatch(attributeMatch);

			mapping->getBaseCase().setEntity(&entity);

			return mapping;
		}
	}
	return nullptr;
}
void XMLEntityMappingDefinitionSerializer::parseScript(TiXmlDocument& xmlDocument)
{

	TiXmlElement* rootElem = xmlDocument.RootElement();

	if (rootElem) {

		for (TiXmlElement* smElem = rootElem->FirstChildElement("entitymapping");
				smElem != 0; smElem = smElem->NextSiblingElement("entitymapping"))
		{
			const char* tmp =  smElem->Attribute("name");
			if (!tmp) {
				continue;
			} else {
				EntityMappingDefinition* definition(0);
				try {
					const std::string name(tmp);
					definition = new EntityMappingDefinition();
					definition->setName(name);
					TiXmlElement* matchElement = smElem->FirstChildElement();
					parseMatchElement(*definition, definition->getRoot(), matchElement);
					mEntityMappingManager.addDefinition(definition);
				} catch (const std::exception& ex) {
					S_LOG_FAILURE("Error when reading model mapping with name '" << tmp << "'." << ex);
					delete definition;
				} catch (...) {
					S_LOG_FAILURE("Error when reading model mapping with name '" << tmp << "'.");
					delete definition;
				}
			}

		}


		for (TiXmlElement* smElem = rootElem->FirstChildElement("nomodel");
				smElem != 0; smElem = smElem->NextSiblingElement("nomodel"))
		{
			const char* tmp =  smElem->Attribute("name");
			if (!tmp) {
				continue;
			} else {
				const std::string name(tmp);
				EntityMappingDefinition* definition(0);
				try {
					definition = new EntityMappingDefinition();
					definition->setName(name);
					definition->getRoot().setType("entitytype");
					CaseDefinition caseDef;
					caseDef.setType("entitytypecase");
					caseDef.getCaseParameters().push_back(CaseDefinition::ParameterEntry("equals", name));
					ActionDefinition actionDef;
					actionDef.setType("hide-model");

					caseDef.getActions().push_back(actionDef);
					definition->getRoot().getCases().push_back(caseDef);

				} catch (const std::exception& ex) {
					delete definition;
					//S_LOG_FAILURE(ex.what());
				} catch (...) {
					delete definition;
					//S_LOG_FAILURE("Error when reading model mapping with name " << name);
				}
				if (definition) {
					mEntityMappingManager.addDefinition(definition);
				}
			}
		}

		//Check for autoentitymapping elements, which allow for a quick mapping between a entity type and a model.
		//format: <autoentitymapping name="oak">
		//or: <autoentitymapping name="oak" modelname="oak_1">
		for (TiXmlElement* smElem = rootElem->FirstChildElement("autoentitymapping");
				smElem != 0; smElem = smElem->NextSiblingElement("autoentitymapping"))
		{
			const char* tmp =  smElem->Attribute("name");
			if (!tmp) {
				continue;
			} else {
				const std::string name(tmp);
				EntityMappingDefinition* definition(0);
				try {
					definition = new EntityMappingDefinition();
					definition->setName(name);
					definition->getRoot().setType("entitytype");
					CaseDefinition caseDef;
					caseDef.setType("entitytypecase");
					caseDef.getCaseParameters().push_back(CaseDefinition::ParameterEntry("equals", name));
					ActionDefinition actionDef;
					actionDef.setType("display-model");

					//check if a model name is set
					const char* tmpModelName = smElem->Attribute("modelname");
					if (tmpModelName) {
						actionDef.setValue(std::string(tmpModelName));
					} else {
						actionDef.setValue(name);
					}

					caseDef.getActions().push_back(actionDef);
					definition->getRoot().getCases().push_back(caseDef);

				} catch (const std::exception& ex) {
					delete definition;
					//S_LOG_FAILURE(ex.what());
				} catch (...) {
					//S_LOG_FAILURE("Error when reading model mapping with name " << name);
					delete definition;
				}
				if (definition) {
					mEntityMappingManager.addDefinition(definition);
				}
			}
		}
	}
}