JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGModelLoader.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Module: FGModelLoader.cpp
4  Author: Bertrand Coconnier
5  Date started: 12/14/13
6  Purpose: Read and manage XML data for models definition
7 
8  ------------- Copyright (C) 2013 Bertrand Coconnier -------------
9 
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14 
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  details.
19 
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26 
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 This is the place where the XML data is loaded in memory for an access during
30 the models initialization.
31 
32 HISTORY
33 --------------------------------------------------------------------------------
34 12/14/13 BC Created
35 
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 
40 #include "FGJSBBase.h"
41 #include "FGModelLoader.h"
42 #include "FGXMLFileRead.h"
43 #include "models/FGModel.h"
44 
45 using namespace std;
46 
47 namespace JSBSim {
48 
49 IDENT(IdSrc, "$Id: FGModelLoader.cpp,v 1.4 2017/02/25 14:23:18 bcoconni Exp $");
50 IDENT(IdHdr, ID_MODELLOADER);
51 
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 CLASS IMPLEMENTATION
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 
56 Element_ptr FGModelLoader::Open(Element *el)
57 {
58  Element_ptr document = el;
59  string fname = el->GetAttributeValue("file");
60 
61  if (!fname.empty()) {
62  FGXMLFileRead XMLFileRead;
63  SGPath path(SGPath::fromLocal8Bit(fname.c_str()));
64 
65  if (path.isRelative())
66  path = model->FindFullPathName(path);
67 
68  if (CachedFiles.find(path.utf8Str()) != CachedFiles.end())
69  document = CachedFiles[path.utf8Str()];
70  else {
71  document = XMLFileRead.LoadXMLDocument(path);
72  if (document == 0L) {
73  cerr << endl << el->ReadFrom()
74  << "Could not open file: " << path << endl;
75  return NULL;
76  }
77  CachedFiles[path.utf8Str()] = document;
78  }
79 
80  if (document->GetName() != el->GetName()) {
81  document->SetParent(el);
82  el->AddChildElement(document);
83  }
84  }
85 
86  return document;
87 }
88 
89 SGPath CheckPathName(const SGPath& path, const SGPath& filename) {
90  SGPath fullName = path/filename.utf8Str();
91 
92  if (fullName.extension().empty())
93  fullName.concat(".xml");
94 
95  return fullName.exists() ? fullName : SGPath();
96 }
97 }
STL namespace.