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 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 CLASS IMPLEMENTATION
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 
Open(Element * el)53 Element_ptr FGModelLoader::Open(Element *el)
54 {
55   Element_ptr document = el;
56   string fname = el->GetAttributeValue("file");
57 
58   if (!fname.empty()) {
59     FGXMLFileRead XMLFileRead;
60     SGPath path(SGPath::fromUtf8(fname));
61 
62     if (path.isRelative())
63       path = model->FindFullPathName(path);
64 
65     if (CachedFiles.find(path.utf8Str()) != CachedFiles.end())
66       document = CachedFiles[path.utf8Str()];
67     else {
68       document = XMLFileRead.LoadXMLDocument(path);
69       if (document == 0L) {
70         cerr << endl << el->ReadFrom()
71              << "Could not open file: " << fname << endl;
72         return NULL;
73       }
74       CachedFiles[path.utf8Str()] = document;
75     }
76 
77     if (document->GetName() != el->GetName()) {
78       document->SetParent(el);
79       el->AddChildElement(document);
80     }
81   }
82 
83   return document;
84 }
85 
CheckPathName(const SGPath & path,const SGPath & filename)86 SGPath CheckPathName(const SGPath& path, const SGPath& filename) {
87   SGPath fullName = path/filename.utf8Str();
88 
89   if (fullName.extension() != "xml")
90     fullName.concat(".xml");
91 
92   return fullName.exists() ? fullName : SGPath();
93 }
94 }
95