~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/model/mapping/XMLModelMappingDefinitionSerializer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: XMLModelMappingDefinitionSerializer
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2007
 
8
//
 
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.
 
13
// 
 
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.
 
18
// 
 
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.//
 
22
//
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include "XMLModelMappingDefinitionSerializer.h"
 
28
//#include "components/ogre/EmberOgrePrerequisites.h"
 
29
 
 
30
namespace EmberOgre {
 
31
 
 
32
namespace Model {
 
33
 
 
34
namespace Mapping {
 
35
 
 
36
using namespace Definitions;
 
37
 
 
38
XMLModelMappingDefinitionSerializer::XMLModelMappingDefinitionSerializer(EmberOgre::Model::Mapping::ModelMappingManager& modelMappingManager)
 
39
: mModelMappingManager(modelMappingManager)
 
40
{
 
41
}
 
42
 
 
43
 
 
44
XMLModelMappingDefinitionSerializer::~XMLModelMappingDefinitionSerializer()
 
45
{
 
46
}
 
47
 
 
48
void XMLModelMappingDefinitionSerializer::parseScript(TiXmlDocument& xmlDocument)
 
49
{
 
50
 
 
51
        TiXmlElement* rootElem = xmlDocument.RootElement();
 
52
        
 
53
        if (rootElem) {
 
54
 
 
55
                for (TiXmlElement* smElem = rootElem->FirstChildElement("modelmapping");
 
56
                                smElem != 0; smElem = smElem->NextSiblingElement("modelmapping"))
 
57
                {
 
58
                        const char* tmp =  smElem->Attribute("name");
 
59
                        if (!tmp) {
 
60
                                continue;
 
61
                        } else {
 
62
                                try {
 
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());
 
71
                                } catch (...) {
 
72
                                        //S_LOG_FAILURE("Error when reading model mapping with name " << name);
 
73
                                }
 
74
                        }
 
75
                                        
 
76
                }
 
77
                
 
78
                
 
79
                for (TiXmlElement* smElem = rootElem->FirstChildElement("nomodel");
 
80
                                smElem != 0; smElem = smElem->NextSiblingElement("nomodel"))
 
81
                {
 
82
                        const char* tmp =  smElem->Attribute("name");
 
83
                        if (!tmp) {
 
84
                                continue;
 
85
                        } else {
 
86
                                const std::string name(tmp);
 
87
                                ModelMappingDefinition* definition(0);
 
88
                                try {
 
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");
 
97
                                        
 
98
                                        caseDef.getActions().push_back(actionDef);
 
99
                                        definition->getRoot().getCases().push_back(caseDef);
 
100
                                
 
101
                                } catch (std::exception ex) {
 
102
                                        delete definition;
 
103
                                        //S_LOG_FAILURE(ex.what());
 
104
                                } catch (...) {
 
105
                                        delete definition;
 
106
                                        //S_LOG_FAILURE("Error when reading model mapping with name " << name);
 
107
                                }
 
108
                                if (definition) {
 
109
                                        mModelMappingManager.addDefinition(definition);
 
110
                                }
 
111
                        }
 
112
                }       
 
113
                
 
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"))
 
119
                {
 
120
                        const char* tmp =  smElem->Attribute("name");
 
121
                        if (!tmp) {
 
122
                                continue;
 
123
                        } else {
 
124
                                const std::string name(tmp);
 
125
                                ModelMappingDefinition* definition(0);
 
126
                                try {
 
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");
 
135
                                        
 
136
                                        ///check if a model name is set
 
137
                                        const char* tmpModelName = smElem->Attribute("modelname");
 
138
                                        if (tmpModelName) {
 
139
                                                actionDef.setValue(std::string(tmpModelName));
 
140
                                        } else {
 
141
                                                actionDef.setValue(name);
 
142
                                        }
 
143
                                        
 
144
                                        caseDef.getActions().push_back(actionDef);
 
145
                                        definition->getRoot().getCases().push_back(caseDef);
 
146
                                
 
147
                                } catch (std::exception ex) {
 
148
                                        delete definition;
 
149
                                        //S_LOG_FAILURE(ex.what());
 
150
                                } catch (...) {
 
151
                                        //S_LOG_FAILURE("Error when reading model mapping with name " << name);
 
152
                                        delete definition;
 
153
                                }
 
154
                                if (definition) {
 
155
                                        mModelMappingManager.addDefinition(definition);
 
156
                                }
 
157
                        }
 
158
                }
 
159
        }
 
160
}
 
161
 
 
162
void XMLModelMappingDefinitionSerializer::parseMatchElement(ModelMappingDefinition& definition, MatchDefinition& matchDef, TiXmlElement* element)
 
163
{
 
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";
 
171
        
 
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";
 
177
        }
 
178
        
 
179
        for (TiXmlAttribute* attribute = element->FirstAttribute();
 
180
            attribute != 0; attribute = attribute->Next())
 
181
    {
 
182
        matchDef.getProperties()[attribute->Name()] = attribute->Value();
 
183
        }
 
184
        
 
185
        if (!element->NoChildren()) {
 
186
                for (TiXmlElement* childElement = element->FirstChildElement();
 
187
                                childElement != 0; childElement = childElement->NextSiblingElement())
 
188
                {
 
189
                        CaseDefinition caseDef;
 
190
                        caseDef.setType(caseType);
 
191
                        parseCaseElement(definition, caseDef, childElement);
 
192
                        matchDef.getCases().push_back(caseDef);
 
193
                }
 
194
        }
 
195
}
 
196
 
 
197
void XMLModelMappingDefinitionSerializer::parseCaseElement(ModelMappingDefinition& definition, CaseDefinition& caseDef, TiXmlElement* element)
 
198
{
 
199
        for (TiXmlAttribute* attribute = element->FirstAttribute();
 
200
            attribute != 0; attribute = attribute->Next())
 
201
    {
 
202
        caseDef.getProperties()[attribute->Name()] = attribute->Value();
 
203
        }
 
204
        
 
205
        
 
206
        if (!element->NoChildren()) {
 
207
                for (TiXmlElement* childElement = element->FirstChildElement();
 
208
                                childElement != 0; childElement = childElement->NextSiblingElement())
 
209
                {
 
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));
 
221
                                        }
 
222
                                }
 
223
                        } else {
 
224
                                ///we'll assume it's a match 
 
225
                                MatchDefinition matchDef;
 
226
                                parseMatchElement(definition, matchDef, childElement);
 
227
                                caseDef.getMatches().push_back(matchDef);
 
228
                        }
 
229
                }
 
230
        }
 
231
}
 
232
 
 
233
void XMLModelMappingDefinitionSerializer::parseActionElement(ModelMappingDefinition& definition, ActionDefinition& actionDef, TiXmlElement* element)
 
234
{
 
235
        for (TiXmlAttribute* attribute = element->FirstAttribute();
 
236
            attribute != 0; attribute = attribute->Next())
 
237
    {
 
238
        actionDef.getProperties()[attribute->Name()] = attribute->Value();
 
239
        }
 
240
        actionDef.setType(element->Attribute("type" ));
 
241
        TiXmlNode* textNode =  element->FirstChild();
 
242
        if (textNode) {
 
243
                actionDef.setValue(textNode->Value());
 
244
        }
 
245
        
 
246
}
 
247
}
 
248
 
 
249
}
 
250
 
 
251
}