1 // Copyright (C) 2013-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Frederic Pons (OpenCascade)
21 #include <libxml/parser.h>
23 #include "XAO_XaoExporter.hxx"
24 #include "XAO_Xao.hxx"
25 #include "XAO_Geometry.hxx"
26 #include "XAO_Group.hxx"
27 #include "XAO_Field.hxx"
28 #include "XAO_Step.hxx"
29 #include "XAO_XaoUtils.hxx"
32 # define _separator_ '\\'
34 # define _separator_ '/'
39 const xmlChar* C_TAG_XAO = (xmlChar*)"XAO";
40 const xmlChar* C_ATTR_XAO_AUTHOR = (xmlChar*)"author";
41 const xmlChar* C_ATTR_XAO_VERSION = (xmlChar*)"version";
43 const xmlChar* C_TAG_GEOMETRY = (xmlChar*)"geometry";
44 const xmlChar* C_ATTR_GEOMETRY_NAME = (xmlChar*)"name";
46 const xmlChar* C_TAG_SHAPE = (xmlChar*)"shape";
47 const xmlChar* C_ATTR_SHAPE_FORMAT = (xmlChar*)"format";
48 const xmlChar* C_ATTR_SHAPE_FILE = (xmlChar*)"file";
50 const xmlChar* C_TAG_TOPOLOGY = (xmlChar*)"topology";
51 const xmlChar* C_TAG_VERTICES = (xmlChar*)"vertices";
52 const xmlChar* C_TAG_VERTEX = (xmlChar*)"vertex";
53 const xmlChar* C_TAG_EDGES = (xmlChar*)"edges";
54 const xmlChar* C_TAG_EDGE = (xmlChar*)"edge";
55 const xmlChar* C_TAG_FACES = (xmlChar*)"faces";
56 const xmlChar* C_TAG_FACE = (xmlChar*)"face";
57 const xmlChar* C_TAG_SOLIDS = (xmlChar*)"solids";
58 const xmlChar* C_TAG_SOLID = (xmlChar*)"solid";
59 const xmlChar* C_ATTR_COUNT = (xmlChar*)"count";
60 const xmlChar* C_ATTR_ELT_INDEX = (xmlChar*)"index";
61 const xmlChar* C_ATTR_ELT_NAME = (xmlChar*)"name";
62 const xmlChar* C_ATTR_ELT_REFERENCE = (xmlChar*)"reference";
64 const xmlChar* C_TAG_GROUPS = (xmlChar*)"groups";
65 const xmlChar* C_TAG_GROUP = (xmlChar*)"group";
66 const xmlChar* C_ATTR_GROUP_NAME = (xmlChar*)"name";
67 const xmlChar* C_ATTR_GROUP_DIM = (xmlChar*)"dimension";
69 const xmlChar* C_TAG_ELEMENT = (xmlChar*)"element";
70 const xmlChar* C_ATTR_ELEMENT_INDEX = (xmlChar*)"index";
71 const xmlChar* C_TAG_VALUE = (xmlChar*)"value";
72 const xmlChar* C_ATTR_VALUE_COMPONENT = (xmlChar*)"component";
74 const xmlChar* C_TAG_FIELDS = (xmlChar*)"fields";
75 const xmlChar* C_TAG_FIELD = (xmlChar*)"field";
76 const xmlChar* C_ATTR_FIELD_NAME = (xmlChar*)"name";
77 const xmlChar* C_ATTR_FIELD_TYPE = (xmlChar*)"type";
78 const xmlChar* C_ATTR_FIELD_DIMENSION = (xmlChar*)"dimension";
79 const xmlChar* C_TAG_COMPONENTS = (xmlChar*)"components";
80 const xmlChar* C_TAG_COMPONENT = (xmlChar*)"component";
81 const xmlChar* C_ATTR_COMPONENT_COLUMN = (xmlChar*)"column";
82 const xmlChar* C_ATTR_COMPONENT_NAME = (xmlChar*)"name";
84 const xmlChar* C_TAG_STEPS = (xmlChar*)"steps";
85 const xmlChar* C_TAG_STEP = (xmlChar*)"step";
86 const xmlChar* C_ATTR_STEP_NUMBER = (xmlChar*)"number";
87 const xmlChar* C_ATTR_STEP_STAMP = (xmlChar*)"stamp";
93 xmlDocPtr exportXMLDoc(Xao* xaoObject, const std::string& shapeFileName);
94 void exportGeometry(Geometry* xaoGeometry, xmlDocPtr doc, xmlNodePtr xao, const std::string& shapeFileName);
95 void exportGeometricElements(Geometry* xaoGeometry, xmlNodePtr topology,
96 XAO::Dimension dim, const xmlChar* colTag, const xmlChar* eltTag);
97 void exportGroups(Xao* xaoObject, xmlNodePtr xao);
98 void exportFields(Xao* xaoObject, xmlNodePtr xao);
99 void exportStep(Step* step, Field* field, xmlNodePtr nodeSteps);
101 void parseXMLDoc(xmlDocPtr doc, Xao* xaoObject);
102 void parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode, Xao* xaoObject);
103 void parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode, Xao* xaoObject);
104 void parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode, Geometry* geometry);
105 void parseTopologyNode(xmlNodePtr topologyNode, Geometry* geometry);
106 void parseVerticesNode(xmlNodePtr verticesNode, Geometry* geometry);
107 void parseEdgesNode(xmlNodePtr edgesNode, Geometry* geometry);
108 void parseFacesNode(xmlNodePtr facesNode, Geometry* geometry);
109 void parseSolidsNode(xmlNodePtr solidsNode, Geometry* geometry);
110 void parseGroupsNode(xmlNodePtr groupsNode, Xao* xaoObject);
111 void parseGroupNode(xmlNodePtr groupNode, Xao* xaoObject);
113 void parseFieldsNode(xmlNodePtr fieldsNode, Xao* xaoObject);
114 void parseFieldNode(xmlNodePtr fieldNode, Xao* xaoObject);
115 void parseStepNode(xmlNodePtr stepNode, Field* field);
116 void parseStepElementNode(xmlNodePtr eltNode, Step* step);
118 std::string readStringProp(xmlNodePtr node, const xmlChar* attribute,
119 const bool& required, const std::string& defaultValue,
120 const std::string& exception = std::string(""));
121 int readIntegerProp(xmlNodePtr node, const xmlChar* attribute,
122 const bool& required, const int& defaultValue,
123 const std::string& exception = std::string(""));
125 std::string readStringProp(xmlNodePtr node, const xmlChar* attribute,
126 const bool& required, const std::string& defaultValue,
127 const std::string& exception /*= std::string() */)
129 xmlChar* strAttr = xmlGetProp(node, attribute);
134 if (exception.size() > 0)
135 throw XAO_Exception(exception.c_str());
137 throw XAO_Exception(MsgBuilder() << "Line " << node->line << ": "
138 << "Property " << (char*)attribute << " is required.");
144 std::string res = (char*)strAttr;
149 int readIntegerProp(xmlNodePtr node, const xmlChar* attribute,
150 const bool& required, const int& defaultValue,
151 const std::string& exception /*= std::string() */)
153 xmlChar* strAttr = xmlGetProp(node, attribute);
158 if (exception.size() > 0)
159 throw XAO_Exception(exception.c_str());
161 throw XAO_Exception(MsgBuilder() << "Line " << node->line << ": "
162 << "Property " << (char*)attribute << " is required.");
168 int res = XaoUtils::stringToInt((char*)strAttr);
173 xmlDocPtr exportXMLDoc(Xao* xaoObject, const std::string& shapeFileName)
175 // Creating the Xml document
176 xmlDocPtr masterDocument = xmlNewDoc(BAD_CAST "1.0");
177 xmlNodePtr xao = xmlNewNode(0, C_TAG_XAO);
178 xmlDocSetRootElement(masterDocument, xao);
180 xmlNewProp(xao, C_ATTR_XAO_VERSION, BAD_CAST xaoObject->getVersion().c_str());
181 xmlNewProp(xao, C_ATTR_XAO_AUTHOR, BAD_CAST xaoObject->getAuthor().c_str());
183 if (xaoObject->getGeometry() != NULL)
185 exportGeometry(xaoObject->getGeometry(), masterDocument, xao, shapeFileName);
188 exportGroups(xaoObject, xao);
189 exportFields(xaoObject, xao);
191 return masterDocument;
194 void exportGeometricElements(Geometry* xaoGeometry,
195 xmlNodePtr topology, XAO::Dimension dim, const xmlChar* colTag, const xmlChar* eltTag)
197 xmlNodePtr vertices = xmlNewChild(topology, 0, colTag, 0);
198 xmlNewProp(vertices, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoGeometry->countElements(dim)).c_str());
199 GeometricElementList::iterator it = xaoGeometry->begin(dim);
200 for (; it != xaoGeometry->end(dim); it++)
202 int index = it->first;
203 GeometricElement elt = it->second;
204 xmlNodePtr vertex = xmlNewChild(vertices, 0, eltTag, 0);
205 xmlNewProp(vertex, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(index).c_str());
206 xmlNewProp(vertex, C_ATTR_ELT_NAME, BAD_CAST elt.getName().c_str());
207 xmlNewProp(vertex, C_ATTR_ELT_REFERENCE, BAD_CAST elt.getReference().c_str());
211 void exportGeometry(Geometry* xaoGeometry, xmlDocPtr doc, xmlNodePtr xao,
212 const std::string& shapeFileName)
215 xmlNodePtr geometry = xmlNewChild(xao, 0, C_TAG_GEOMETRY, 0);
216 xmlNewProp(geometry, C_ATTR_GEOMETRY_NAME, BAD_CAST xaoGeometry->getName().c_str());
218 xmlNodePtr shape = xmlNewChild(geometry, 0, C_TAG_SHAPE, 0);
219 xmlNewProp(shape, C_ATTR_SHAPE_FORMAT, BAD_CAST XaoUtils::shapeFormatToString(xaoGeometry->getFormat()).c_str());
221 if (shapeFileName == "")
223 // export the shape in the XAO file
224 std::string txtShape = xaoGeometry->getShapeString();
225 xmlNodePtr cdata = xmlNewCDataBlock(doc, BAD_CAST txtShape.c_str(), txtShape.size());
226 xmlAddChild(shape, cdata);
230 // export the shape in an external file
231 xmlNewProp(shape, C_ATTR_SHAPE_FILE, BAD_CAST shapeFileName.c_str());
232 xaoGeometry->writeShapeFile(shapeFileName);
235 xmlNodePtr topology = xmlNewChild(geometry, 0, C_TAG_TOPOLOGY, 0);
237 exportGeometricElements(xaoGeometry, topology, XAO::VERTEX, C_TAG_VERTICES, C_TAG_VERTEX);
238 exportGeometricElements(xaoGeometry, topology, XAO::EDGE, C_TAG_EDGES, C_TAG_EDGE);
239 exportGeometricElements(xaoGeometry, topology, XAO::FACE, C_TAG_FACES, C_TAG_FACE);
240 exportGeometricElements(xaoGeometry, topology, XAO::SOLID, C_TAG_SOLIDS, C_TAG_SOLID);
243 void exportGroups(Xao* xaoObject, xmlNodePtr xao)
245 xmlNodePtr groups = xmlNewChild(xao, 0, C_TAG_GROUPS, 0);
246 xmlNewProp(groups, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoObject->countGroups()).c_str());
248 for (int i = 0; i < xaoObject->countGroups(); i++)
250 //Group* grp = (*it);
251 Group* grp = xaoObject->getGroup(i);
252 xmlNodePtr group = xmlNewChild(groups, 0, C_TAG_GROUP, 0);
253 xmlNewProp(group, C_ATTR_GROUP_NAME, BAD_CAST grp->getName().c_str());
254 xmlNewProp(group, C_ATTR_GROUP_DIM, BAD_CAST XaoUtils::dimensionToString(grp->getDimension()).c_str());
255 xmlNewProp(group, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(grp->count()).c_str());
257 for (std::set<int>::iterator it = grp->begin(); it != grp->end(); ++it)
260 xmlNodePtr elt = xmlNewChild(group, 0, C_TAG_ELEMENT, 0);
261 xmlNewProp(elt, C_ATTR_ELEMENT_INDEX, BAD_CAST XaoUtils::intToString(grpElt).c_str());
266 void exportFields(Xao* xaoObject, xmlNodePtr xao)
268 xmlNodePtr fields = xmlNewChild(xao, 0, C_TAG_FIELDS, 0);
269 xmlNewProp(fields, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoObject->countFields()).c_str());
271 for (int i = 0; i < xaoObject->countFields(); i++)
273 Field* field = xaoObject->getField(i);
274 xmlNodePtr nodeField = xmlNewChild(fields, 0, C_TAG_FIELD, 0);
275 xmlNewProp(nodeField, C_ATTR_FIELD_NAME, BAD_CAST field->getName().c_str());
276 xmlNewProp(nodeField, C_ATTR_FIELD_TYPE, BAD_CAST XaoUtils::fieldTypeToString(field->getType()).c_str());
277 xmlNewProp(nodeField, C_ATTR_FIELD_DIMENSION, BAD_CAST XaoUtils::dimensionToString(field->getDimension()).c_str());
279 int nbComponents = field->countComponents();
280 xmlNodePtr components = xmlNewChild(nodeField, 0, C_TAG_COMPONENTS, 0);
281 xmlNewProp(components, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(nbComponents).c_str());
283 for (int j = 0; j < nbComponents; j++)
285 xmlNodePtr nodeComponent = xmlNewChild(components, 0, C_TAG_COMPONENT, 0);
286 xmlNewProp(nodeComponent, C_ATTR_COMPONENT_COLUMN, BAD_CAST XaoUtils::intToString(j).c_str());
287 xmlNewProp(nodeComponent, C_ATTR_COMPONENT_NAME, BAD_CAST field->getComponentName(j).c_str());
290 int nbSteps = field->countSteps();
291 xmlNodePtr nodeSteps = xmlNewChild(nodeField, 0, C_TAG_STEPS, 0);
292 xmlNewProp(nodeSteps, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(nbSteps).c_str());
293 for (stepIterator itStep = field->begin(); itStep != field->end(); itStep++)
295 Step* step = *itStep;
296 exportStep(step, field, nodeSteps);
301 void exportStep(Step* step, Field* field, xmlNodePtr nodeSteps)
303 xmlNodePtr nodeStep = xmlNewChild(nodeSteps, 0, C_TAG_STEP, 0);
304 xmlNewProp(nodeStep, C_ATTR_STEP_NUMBER, BAD_CAST XaoUtils::intToString(step->getStep()).c_str());
305 if (step->getStamp() >= 0)
307 xmlNewProp(nodeStep, C_ATTR_STEP_STAMP, BAD_CAST XaoUtils::intToString(step->getStamp()).c_str());
310 for(int i = 0; i < step->countElements(); ++i)
312 xmlNodePtr nodeElt = xmlNewChild(nodeStep, 0, C_TAG_ELEMENT, 0);
313 xmlNewProp(nodeElt, C_ATTR_ELEMENT_INDEX, BAD_CAST XaoUtils::intToString(i).c_str());
315 for (int j = 0; j < step->countComponents(); ++j)
317 std::string content = step->getStringValue(i, j);
318 xmlNodePtr nodeValue = xmlNewChild(nodeElt, NULL, C_TAG_VALUE, BAD_CAST content.c_str());
319 xmlNewProp(nodeValue, C_ATTR_VALUE_COMPONENT, BAD_CAST XaoUtils::intToString(j).c_str());
324 void parseXMLDoc(xmlDocPtr doc, Xao* xaoObject)
326 // Get the root element node
327 xmlNodePtr root = xmlDocGetRootElement(doc);
328 if (xmlStrcmp(root->name , C_TAG_XAO) != 0)
329 throw XAO_Exception("Cannot read XAO file: invalid format XAO node not found");
331 parseXaoNode(doc, root, xaoObject);
333 xmlFreeDoc(doc); // free document
334 //xmlCleanupParser(); // free globals //vsr: xmlCleanupParser should not be called from the application
337 void parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode, Xao* xaoObject)
339 std::string version = readStringProp(xaoNode, C_ATTR_XAO_VERSION, false, "");
341 xaoObject->setVersion(version);
343 std::string author = readStringProp(xaoNode, C_ATTR_XAO_AUTHOR, false, "");
344 xaoObject->setAuthor(author);
346 for (xmlNodePtr node = xaoNode->children; node; node = node->next)
348 if (xmlStrcmp(node->name, C_TAG_GEOMETRY) == 0)
349 parseGeometryNode(doc, node, xaoObject);
350 else if (xmlStrcmp(node->name, C_TAG_GROUPS) == 0)
351 parseGroupsNode(node, xaoObject);
352 else if (xmlStrcmp(node->name, C_TAG_FIELDS) == 0)
353 parseFieldsNode(node, xaoObject);
357 void parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode, Xao* xaoObject)
359 // get the shape and topo nodes
360 xmlNodePtr shapeNode = NULL;
361 xmlNodePtr topoNode = NULL;
362 for (xmlNodePtr node = geometryNode->children; node; node = node->next)
364 if (xmlStrcmp(node->name, C_TAG_SHAPE) == 0)
366 else if (xmlStrcmp(node->name, C_TAG_TOPOLOGY) == 0)
370 std::string name = readStringProp(geometryNode, C_ATTR_GEOMETRY_NAME, false, "");
371 std::string strFormat = readStringProp(shapeNode, C_ATTR_SHAPE_FORMAT, true, "");
372 XAO::Format shapeFormat = XaoUtils::stringToShapeFormat(strFormat);
373 Geometry* geometry = Geometry::createGeometry(shapeFormat, name);
375 parseShapeNode(doc, shapeNode, geometry);
376 parseTopologyNode(topoNode, geometry);
378 xaoObject->setGeometry(geometry);
381 void parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode, Geometry* geometry)
383 if (geometry->getFormat() == XAO::BREP)
385 std::string strFile = readStringProp(shapeNode, C_ATTR_SHAPE_FILE, false, "");
388 geometry->readShapeFile(strFile);
392 // read brep from node content
393 xmlChar* data = xmlNodeGetContent(shapeNode->children);
395 throw XAO_Exception("Missing BREP");
396 geometry->setShapeString((char*)data);
402 throw XAO_Exception(MsgBuilder() << "Shape format not supported: "
403 << XaoUtils::shapeFormatToString(geometry->getFormat()));
407 void parseTopologyNode(xmlNodePtr topologyNode, Geometry* geometry)
409 for (xmlNodePtr node = topologyNode->children; node; node = node->next)
411 if (xmlStrcmp(node->name, C_TAG_VERTICES) == 0)
412 parseVerticesNode(node, geometry);
413 else if (xmlStrcmp(node->name, C_TAG_EDGES) == 0)
414 parseEdgesNode(node, geometry);
415 else if (xmlStrcmp(node->name, C_TAG_FACES) == 0)
416 parseFacesNode(node, geometry);
417 else if (xmlStrcmp(node->name, C_TAG_SOLIDS) == 0)
418 parseSolidsNode(node, geometry);
422 void parseVerticesNode(xmlNodePtr verticesNode, Geometry* geometry)
424 int count = readIntegerProp(verticesNode, C_ATTR_COUNT, true, -1);
425 geometry->setCountVertices(count);
427 for (xmlNodePtr node = verticesNode->children; node; node = node->next)
429 if (xmlStrcmp(node->name, C_TAG_VERTEX) == 0)
431 int index = readIntegerProp(node, C_ATTR_ELT_INDEX, true, -1);
432 std::string name = readStringProp(node, C_ATTR_ELT_NAME, false, "");
433 std::string reference = readStringProp(node, C_ATTR_ELT_REFERENCE, true, "");
435 geometry->setVertex(index, name, reference);
440 void parseEdgesNode(xmlNodePtr edgesNode, Geometry* geometry)
442 int count = readIntegerProp(edgesNode, C_ATTR_COUNT, true, -1);
443 geometry->setCountEdges(count);
445 for (xmlNodePtr node = edgesNode->children; node; node = node->next)
447 if (xmlStrcmp(node->name, C_TAG_EDGE) == 0)
449 int index = readIntegerProp(node, C_ATTR_ELT_INDEX, true, -1);
450 std::string name = readStringProp(node, C_ATTR_ELT_NAME, false, "");
451 std::string reference = readStringProp(node, C_ATTR_ELT_REFERENCE, true, "");
453 geometry->setEdge(index, name, reference);
458 void parseFacesNode(xmlNodePtr facesNode, Geometry* geometry)
460 int count = readIntegerProp(facesNode, C_ATTR_COUNT, true, -1);
461 geometry->setCountFaces(count);
463 for (xmlNodePtr node = facesNode->children; node; node = node->next)
465 if (xmlStrcmp(node->name, C_TAG_FACE) == 0)
467 int index = readIntegerProp(node, C_ATTR_ELT_INDEX, true, -1);
468 std::string name = readStringProp(node, C_ATTR_ELT_NAME, false, "");
469 std::string reference = readStringProp(node, C_ATTR_ELT_REFERENCE, true, "");
471 geometry->setFace(index, name, reference);
476 void parseSolidsNode(xmlNodePtr solidsNode, Geometry* geometry)
478 int count = readIntegerProp(solidsNode, C_ATTR_COUNT, true, -1);
479 geometry->setCountSolids(count);
481 for (xmlNodePtr node = solidsNode->children; node; node = node->next)
483 if (xmlStrcmp(node->name, C_TAG_SOLID) == 0)
485 int index = readIntegerProp(node, C_ATTR_ELT_INDEX, true, -1);
486 std::string name = readStringProp(node, C_ATTR_ELT_NAME, false, "");
487 std::string reference = readStringProp(node, C_ATTR_ELT_REFERENCE, true, "");
489 geometry->setSolid(index, name, reference);
494 void parseGroupsNode(xmlNodePtr groupsNode, Xao* xaoObject)
496 for (xmlNodePtr node = groupsNode->children; node; node = node->next)
498 if (xmlStrcmp(node->name, C_TAG_GROUP) == 0)
500 parseGroupNode(node, xaoObject);
505 void parseGroupNode(xmlNodePtr groupNode, Xao* xaoObject)
507 std::string strDimension = readStringProp(groupNode, C_ATTR_GROUP_DIM, true, "");
508 XAO::Dimension dim = XaoUtils::stringToDimension(strDimension);
509 Group* group = xaoObject->addGroup(dim);
511 std::string name = readStringProp(groupNode, C_ATTR_GROUP_NAME, false, "");
512 group->setName(name);
514 for (xmlNodePtr node = groupNode->children; node; node = node->next)
516 if (xmlStrcmp(node->name, C_TAG_ELEMENT) == 0)
518 int index = readIntegerProp(node, C_ATTR_ELEMENT_INDEX, true, -1);
524 void parseFieldsNode(xmlNodePtr fieldsNode, Xao* xaoObject)
526 for (xmlNodePtr node = fieldsNode->children; node; node = node->next)
528 if (xmlStrcmp(node->name, C_TAG_FIELD) == 0)
530 parseFieldNode(node, xaoObject);
535 void parseFieldNode(xmlNodePtr fieldNode, Xao* xaoObject)
537 std::string strDimension = readStringProp(fieldNode, C_ATTR_FIELD_DIMENSION, true, "");
538 XAO::Dimension dim = XaoUtils::stringToDimension(strDimension);
540 std::string strType = readStringProp(fieldNode, C_ATTR_FIELD_TYPE, true, "");
541 XAO::Type type = XaoUtils::stringToFieldType(strType);
543 // we need to get the number of components first to create the field
544 xmlNodePtr componentsNode = NULL;
545 xmlNodePtr stepsNode = NULL;
547 for (xmlNodePtr node = fieldNode->children; node; node = node->next)
549 if (xmlStrcmp(node->name, C_TAG_COMPONENTS) == 0)
550 componentsNode = node;
551 else if (xmlStrcmp(node->name, C_TAG_STEPS) == 0)
555 // ensure that the components node is defined
556 if (componentsNode == NULL)
558 throw XAO_Exception(MsgBuilder() << "Line " << fieldNode->line << ": "
559 << "No components defined for field.");
563 int nbComponents = readIntegerProp(componentsNode, C_ATTR_COUNT, true, -1);
564 Field* field = xaoObject->addField(type, dim, nbComponents);
566 // parse the components
567 for (xmlNodePtr compNode = componentsNode->children; compNode; compNode = compNode->next)
569 std::string compName= readStringProp(compNode, C_ATTR_COMPONENT_NAME, false, "");
570 if (compName.size() > 0)
572 int col = readIntegerProp(compNode, C_ATTR_COMPONENT_COLUMN, true, -1);
573 field->setComponentName(col, compName);
578 std::string name = readStringProp(fieldNode, C_ATTR_FIELD_NAME, false, "");
579 if (name.size() > 0) field->setName(name);
584 for (xmlNodePtr stepNode = stepsNode->children; stepNode; stepNode = stepNode->next)
586 if (xmlStrcmp(stepNode->name, C_TAG_STEP) == 0)
588 parseStepNode(stepNode, field);
594 void parseStepNode(xmlNodePtr stepNode, Field* field)
596 int stepNumber = readIntegerProp(stepNode, C_ATTR_STEP_NUMBER, true, -1);
597 Step* step = field->addNewStep(stepNumber);
599 int stepStamp = readIntegerProp(stepNode, C_ATTR_STEP_STAMP, false, -1);
602 step->setStamp(stepStamp);
605 for (xmlNodePtr eltNode = stepNode->children; eltNode; eltNode = eltNode->next)
607 if (xmlStrcmp(eltNode->name, C_TAG_ELEMENT) == 0)
609 parseStepElementNode(eltNode, step);
614 void parseStepElementNode(xmlNodePtr eltNode, Step* step)
616 int index = readIntegerProp(eltNode, C_ATTR_ELT_INDEX, true, -1);
618 for (xmlNodePtr valNode = eltNode->children; valNode; valNode = valNode->next)
620 if (xmlStrcmp(valNode->name, C_TAG_VALUE) == 0)
622 int component = readIntegerProp(valNode, C_ATTR_VALUE_COMPONENT, true, -1);
623 xmlChar* data = xmlNodeGetContent(valNode->children);
630 else if (step->getType() != XAO::STRING)
632 throw XAO_Exception(MsgBuilder() << "Line " << valNode->line << ": no content for value.");
635 step->setStringValue(index, component, value);
641 const bool XaoExporter::saveToFile(Xao* xaoObject, const std::string& fileName, const std::string& shapeFileName)
642 throw (XAO_Exception)
644 xmlDocPtr doc = exportXMLDoc(xaoObject, shapeFileName);
645 xmlSaveFormatFileEnc(fileName.c_str(), doc, "UTF-8", 1); // format = 1 for node indentation
651 const std::string XaoExporter::saveToXml(Xao* xaoObject)
652 throw (XAO_Exception)
654 xmlDocPtr doc = exportXMLDoc(xaoObject, "");
658 xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1); // format = 1 for node indentation
662 return (char*)xmlbuff;
665 const bool XaoExporter::readFromFile(const std::string& fileName, Xao* xaoObject)
666 throw (XAO_Exception)
668 // parse the file and get the DOM
669 int options = XML_PARSE_HUGE | XML_PARSE_NOCDATA;
670 xmlDocPtr doc = xmlReadFile(fileName.c_str(), NULL, options);
673 throw XAO_Exception("Cannot read XAO file");
676 parseXMLDoc(doc, xaoObject);
680 const bool XaoExporter::setXML(const std::string& xml, Xao* xaoObject)
681 throw (XAO_Exception)
683 int options = XML_PARSE_HUGE | XML_PARSE_NOCDATA;
684 xmlDocPtr doc = xmlReadDoc(BAD_CAST xml.c_str(), "", NULL, options);
687 throw XAO_Exception("Cannot read XAO stream");
690 parseXMLDoc(doc, xaoObject);