2
// C++ Implementation: XMLModelMappingDefinitionSerializer
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2007
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
27
#include "XMLModelMappingDefinitionSerializer.h"
28
//#include "components/ogre/EmberOgrePrerequisites.h"
36
using namespace Definitions;
38
XMLModelMappingDefinitionSerializer::XMLModelMappingDefinitionSerializer(EmberOgre::Model::Mapping::ModelMappingManager& modelMappingManager)
39
: mModelMappingManager(modelMappingManager)
44
XMLModelMappingDefinitionSerializer::~XMLModelMappingDefinitionSerializer()
48
void XMLModelMappingDefinitionSerializer::parseScript(TiXmlDocument& xmlDocument)
51
TiXmlElement* rootElem = xmlDocument.RootElement();
55
for (TiXmlElement* smElem = rootElem->FirstChildElement("modelmapping");
56
smElem != 0; smElem = smElem->NextSiblingElement("modelmapping"))
58
const char* tmp = smElem->Attribute("name");
63
const std::string name(tmp);
64
ModelMappingDefinition* definition = new ModelMappingDefinition();
65
definition->setName(name);
66
TiXmlElement* matchElement = smElem->FirstChildElement();
67
parseMatchElement(*definition, definition->getRoot(), matchElement);
68
mModelMappingManager.addDefinition(definition);
69
} catch (std::exception ex) {
70
//S_LOG_FAILURE(ex.what());
72
//S_LOG_FAILURE("Error when reading model mapping with name " << name);
79
for (TiXmlElement* smElem = rootElem->FirstChildElement("nomodel");
80
smElem != 0; smElem = smElem->NextSiblingElement("nomodel"))
82
const char* tmp = smElem->Attribute("name");
86
const std::string name(tmp);
87
ModelMappingDefinition* definition(0);
89
definition = new ModelMappingDefinition();
90
definition->setName(name);
91
definition->getRoot().setType("entitytype");
92
CaseDefinition caseDef;
93
caseDef.setType("entitytypecase");
94
caseDef.getCaseParameters().push_back(CaseDefinition::ParameterEntry("equals", name));
95
ActionDefinition actionDef;
96
actionDef.setType("hide-model");
98
caseDef.getActions().push_back(actionDef);
99
definition->getRoot().getCases().push_back(caseDef);
101
} catch (std::exception ex) {
103
//S_LOG_FAILURE(ex.what());
106
//S_LOG_FAILURE("Error when reading model mapping with name " << name);
109
mModelMappingManager.addDefinition(definition);
114
///Check for automodelmapping elements, which allow for a quick mapping between a entity type and a model.
115
///format: <automodelmapping name="oak">
116
///or: <automodelmapping name="oak" modelname="oak_1">
117
for (TiXmlElement* smElem = rootElem->FirstChildElement("automodelmapping");
118
smElem != 0; smElem = smElem->NextSiblingElement("automodelmapping"))
120
const char* tmp = smElem->Attribute("name");
124
const std::string name(tmp);
125
ModelMappingDefinition* definition(0);
127
definition = new ModelMappingDefinition();
128
definition->setName(name);
129
definition->getRoot().setType("entitytype");
130
CaseDefinition caseDef;
131
caseDef.setType("entitytypecase");
132
caseDef.getCaseParameters().push_back(CaseDefinition::ParameterEntry("equals", name));
133
ActionDefinition actionDef;
134
actionDef.setType("display-model");
136
///check if a model name is set
137
const char* tmpModelName = smElem->Attribute("modelname");
139
actionDef.setValue(std::string(tmpModelName));
141
actionDef.setValue(name);
144
caseDef.getActions().push_back(actionDef);
145
definition->getRoot().getCases().push_back(caseDef);
147
} catch (std::exception ex) {
149
//S_LOG_FAILURE(ex.what());
151
//S_LOG_FAILURE("Error when reading model mapping with name " << name);
155
mModelMappingManager.addDefinition(definition);
162
void XMLModelMappingDefinitionSerializer::parseMatchElement(ModelMappingDefinition& definition, MatchDefinition& matchDef, TiXmlElement* element)
164
std::string caseType("");
165
if (std::string(element->Value()) == std::string("entitymatch")) {
166
matchDef.setType("entitytype");
167
caseType = "entitytypecase";
168
} else if (std::string(element->Value()) == std::string("attributematch")) {
169
matchDef.setType("attribute");
170
caseType = "attributecase";
172
/* const char* tmp = smElem->Attribute("attribute");
173
matchDef.getProperties()["attribute"] = std::string(tmp);*/
174
} else if (std::string(element->Value()) == std::string("outfitmatch")) {
175
matchDef.setType("outfit");
176
caseType = "outfitcase";
179
for (TiXmlAttribute* attribute = element->FirstAttribute();
180
attribute != 0; attribute = attribute->Next())
182
matchDef.getProperties()[attribute->Name()] = attribute->Value();
185
if (!element->NoChildren()) {
186
for (TiXmlElement* childElement = element->FirstChildElement();
187
childElement != 0; childElement = childElement->NextSiblingElement())
189
CaseDefinition caseDef;
190
caseDef.setType(caseType);
191
parseCaseElement(definition, caseDef, childElement);
192
matchDef.getCases().push_back(caseDef);
197
void XMLModelMappingDefinitionSerializer::parseCaseElement(ModelMappingDefinition& definition, CaseDefinition& caseDef, TiXmlElement* element)
199
for (TiXmlAttribute* attribute = element->FirstAttribute();
200
attribute != 0; attribute = attribute->Next())
202
caseDef.getProperties()[attribute->Name()] = attribute->Value();
206
if (!element->NoChildren()) {
207
for (TiXmlElement* childElement = element->FirstChildElement();
208
childElement != 0; childElement = childElement->NextSiblingElement())
210
if (std::string(childElement->Value()) == std::string("action")) {
211
ActionDefinition actionDef;
212
parseActionElement(definition, actionDef, childElement);
213
caseDef.getActions().push_back(actionDef);
214
} else if (std::string(childElement->Value()) == std::string("caseparam")){
215
///it's a case parameter
216
if (const char* attributeValue = childElement->Attribute("type")) {
217
if (TiXmlNode* textNode = childElement->FirstChild()) {
218
std::string type(attributeValue);
219
std::string value(textNode->Value());
220
caseDef.getCaseParameters().push_back(std::pair<std::string, std::string>(type, value));
224
///we'll assume it's a match
225
MatchDefinition matchDef;
226
parseMatchElement(definition, matchDef, childElement);
227
caseDef.getMatches().push_back(matchDef);
233
void XMLModelMappingDefinitionSerializer::parseActionElement(ModelMappingDefinition& definition, ActionDefinition& actionDef, TiXmlElement* element)
235
for (TiXmlAttribute* attribute = element->FirstAttribute();
236
attribute != 0; attribute = attribute->Next())
238
actionDef.getProperties()[attribute->Name()] = attribute->Value();
240
actionDef.setType(element->Attribute("type" ));
241
TiXmlNode* textNode = element->FirstChild();
243
actionDef.setValue(textNode->Value());