Code_TYMPAN  4.4.0
Industrial site acoustic simulation
xml_project_util.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #include <cstring>
17 
18 #include <boost/foreach.hpp>
19 
20 #include "Tympan/core/logging.h"
21 #include "Tympan/core/exceptions.h"
22 #include "Tympan/core/config.h"
24 #include "xml_project_util.h"
25 
26 namespace tympan
27 {
28 
29 LPTYProjet load_project(const char* filename)
30 {
31  TYXMLManager xmlManager;
32  LPTYElementArray elements;
33  LPTYProjet project;
34 
35  // Load XML file
36  int is_loaded = xmlManager.load(filename, elements);
37  if (is_loaded != 1)
38  {
39  throw tympan::invalid_data("XML Project file could not be loaded ")
40  << tympan_source_loc << boost::errinfo_file_name(filename);
41  }
42 
43  // Retrieve project
44  BOOST_FOREACH (LPTYElement& elt, elements)
45  {
46  if (std::strcmp(elt->getClassName(), "TYProjet") == 0)
47  {
48  project = TYProjet::safeDownCast(elt);
49  break;
50  }
51  }
52 
53  if (!project)
54  {
55  throw tympan::invalid_data("No project was found in the XML file")
56  << boost::errinfo_file_name(filename);
57  }
58  return project;
59 }
60 
61 void save_project(const char* filename, const LPTYProjet& project)
62 {
63  if (!project)
64  {
65  throw tympan::invalid_data("A NULL project can not be saved");
66  }
67 
68  TYXMLManager xmlManager;
69  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
70  xmlManager.addElement(project);
71  if (xmlManager.save(filename) != 0)
72  {
73  throw tympan::invalid_data("Project could not be saved") << boost::errinfo_file_name(filename);
74  }
75 }
76 
77 std::vector<LPTYElement> load_elements(const char* filename)
78 {
79  TYXMLManager xmlManager;
80  LPTYElementArray elements;
81  TYElement::setRegenerateID(true); // Force le renouvellement de l'ID des elements charges
82 
83  // Load XML file
84  int is_loaded = xmlManager.load(filename, elements);
85  if (is_loaded != 1)
86  {
87  throw tympan::invalid_data("XML file could not be loaded ")
88  << tympan_source_loc << boost::errinfo_file_name(filename);
89  }
90 
92  return elements;
93 }
94 
95 } /* namespace tympan */
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:345
virtual const char * getClassName() const
Definition: TYElement.h:249
static OPrototype * safeDownCast(OPrototype *pObject)
Definition: TYElement.cpp:71
static void setRegenerateID(const bool &bRegenerate)
Definition: TYElement.h:923
int save(QString fileName)
int load(const QString &fileName, LPTYElementArray &eltCollection)
void createDoc(QString docName, QString version)
int addElement(TYElement *pElt)
Utilities to handle exceptions and to pretty-print value.
#define tympan_source_loc
This macro build a source_loc object to be attached to a tympan::Exception.
Definition: exceptions.h:76
void save_project(const char *filename, const LPTYProjet &project)
save a project into an XML file
LPTYProjet load_project(const char *filename)
load an XML project file
std::vector< LPTYElement > load_elements(const char *filename)
load one or several objects from an xml file
The base exception class for errors due to invalid data.
Definition: exceptions.h:60
Utilities to load a project and a solver.