-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathSDF.cc
463 lines (402 loc) · 12.3 KB
/
SDF.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "sdf/parser.hh"
#include "sdf/Assert.hh"
#include "sdf/Console.hh"
#include "sdf/Filesystem.hh"
#include "sdf/SDFImpl.hh"
#include "SDFImplPrivate.hh"
#include "sdf/sdf_config.h"
#include "EmbeddedSdf.hh"
#include <gz/utils/Environment.hh>
namespace sdf
{
inline namespace SDF_VERSION_NAMESPACE
{
// TODO(azeey) This violates the Google style guide. Change to a function that
// returns the version string when possible.
std::string SDF::version = SDF_VERSION; // NOLINT(runtime/string)
std::string sdfSharePath()
{
#ifdef SDF_SHARE_PATH
if (std::string(SDF_SHARE_PATH) != "/")
return SDF_SHARE_PATH;
#endif
return "";
}
/////////////////////////////////////////////////
void setFindCallback(std::function<std::string(const std::string &)> _cb)
{
ParserConfig::GlobalConfig().SetFindCallback(_cb);
}
/////////////////////////////////////////////////
std::string findFile(
const std::string &_filename, bool _searchLocalPath, bool _useCallback)
{
return findFile(
_filename, _searchLocalPath, _useCallback, ParserConfig::GlobalConfig());
}
/////////////////////////////////////////////////
std::string findFile(const std::string &_filename, bool _searchLocalPath,
bool _useCallback, const ParserConfig &_config)
{
// Check to see if _filename is URI. If so, resolve the URI path.
for (const auto &[uriScheme, paths] : _config.URIPathMap())
{
// Check to see if the URI in the global map is the first part of the
// given filename
if (_filename.find(uriScheme) == 0)
{
std::string suffix = _filename;
size_t index = suffix.find(uriScheme);
if (index != std::string::npos)
{
suffix.replace(index, uriScheme.length(), "");
}
// Check each path in the list.
for (const auto &path : paths)
{
// Return the path string if the path + suffix exists.
std::string pathSuffix = sdf::filesystem::append(path, suffix);
if (sdf::filesystem::exists(pathSuffix))
{
return pathSuffix;
}
}
}
}
// Strip scheme, if any
std::string filename = _filename;
std::string sep("://");
size_t idx = _filename.find(sep);
if (idx != std::string::npos)
{
filename = filename.substr(idx + sep.length());
}
// First check the local path, if the flag is set.
if (_searchLocalPath)
{
std::string path = sdf::filesystem::append(sdf::filesystem::current_path(),
filename);
if (sdf::filesystem::exists(path))
{
return path;
}
}
// Next check the install path.
std::string path = sdf::filesystem::append(sdfSharePath(), filename);
if (sdf::filesystem::exists(path))
{
return path;
}
// Next check the versioned install path.
path = sdf::filesystem::append(sdfSharePath(),
"sdformat" + std::string(SDF_MAJOR_VERSION_STR),
sdf::SDF::Version(), filename);
if (sdf::filesystem::exists(path))
{
return path;
}
// Finally check to see if the given file exists.
path = filename;
if (sdf::filesystem::exists(path))
{
return path;
}
std::string sdfPathEnv;
if(gz::utils::env("SDF_PATH", sdfPathEnv))
{
std::vector<std::string> paths = sdf::split(sdfPathEnv, ":");
for (std::vector<std::string>::iterator iter = paths.begin();
iter != paths.end(); ++iter)
{
path = sdf::filesystem::append(*iter, filename);
if (sdf::filesystem::exists(path))
{
return path;
}
}
}
// If we still haven't found the file, use the registered callback if the
// flag has been set
if (_useCallback)
{
if (!_config.FindFileCallback())
{
sdferr << "Tried to use callback in sdf::findFile(), but the callback "
"is empty. Did you call sdf::setFindCallback()?\n";
return std::string();
}
else
{
return _config.FindFileCallback()(_filename);
}
}
return std::string();
}
/////////////////////////////////////////////////
void addURIPath(const std::string &_uri, const std::string &_path)
{
ParserConfig::GlobalConfig().AddURIPath(_uri, _path);
}
/////////////////////////////////////////////////
SDF::SDF()
: dataPtr(new SDFPrivate)
{
}
/////////////////////////////////////////////////
SDF::~SDF()
{
}
/////////////////////////////////////////////////
void SDF::PrintDescription()
{
this->Root()->PrintDescription("");
}
/////////////////////////////////////////////////
void SDF::PrintValues(const PrintConfig &_config)
{
this->Root()->PrintValues("", _config);
}
/////////////////////////////////////////////////
void SDF::PrintDoc()
{
std::string html, html2;
int index = 0;
this->Root()->PrintDocLeftPane(html, 10, index);
index = 0;
this->Root()->PrintDocRightPane(html2, 10, index);
std::cout << "<!DOCTYPE HTML>\n"
<< "<html>\n"
<< "<head>\n"
<< " <link href='style.css' rel='stylesheet' type='text/css'>\n"
<< " <script type='text/javascript' src='jquery.js'></script>\n"
<< " <script type='text/javascript' src='splitter-152.js'></script>\n"
<< " <script type='text/javascript'>\n"
<< " var prevId = 0;\n"
<< " function highlight(id) {\n"
<< " var elem = document.getElementById(prevId);\n"
<< " elem.style.background = '#ffffff';\n"
<< " elem.style.color = '#da7800';\n"
<< " elem = document.getElementById(id);\n"
<< " elem.style.background = '#da7800';\n"
<< " elem.style.color = '#ffffff';\n"
<< " prevId = id;\n"
<< " }\n"
<< " $().ready(function() {\n"
<< " $('#my_splitter').splitter({\n"
<< " splitVertical: true,\n"
<< " outline: true,\n"
<< " sizeLeft: true,\n"
<< " resizeTo: window,\n"
<< " accessKey: 'I'\n"
<< " });\n"
<< " });\n"
<< " </script>\n"
<< " <style type='text/css' media='all'>\n"
<< " #my_splitter {\n"
<< " height: 500px;\n"
<< " width: 100%;\n"
<< " border: 1px solid #aaa;\n"
<< " }\n"
<< " #left_pane {\n"
<< " min-width:320px;\n"
<< " }\n"
<< " #right_pane {\n"
<< " min-width:500px;\n"
<< " }\n"
<< " </style>\n"
<< "</head>\n<body>\n";
std::cout << "<div style='padding:4px'>\n"
<< "<h1>SDF " << SDF::Version() << "</h1>\n";
std::cout << "<p>The Robot Modeling Language (SDF) is an XML file "
<< "format used to describe all the elements in a simulation "
<< "environment.\n</p>";
std::cout << "<h3>Usage</h3>\n";
std::cout << "<blockquote>";
std::cout << "<ul><li><b>Left Panel:</b> List of all the SDF elements.</li>";
std::cout << "<li><b>Right Panel:</b> Descriptions of all the SDF "
<< "elements.</li>";
std::cout << "<li><b>Selection:</b> Clicking an element in the Left Panel "
<< "moves the corresponding description to the top of the Right "
<< "Panel.</li>";
std::cout << "<li><b>Search:</b> Use your web-browser's built in 'Find' "
<< "function to locate a specific element."
<< "</li></ul>";
std::cout << "</blockquote>";
std::cout << "</br>\n";
std::cout << "<h3>Meta-Tags</h3>\n";
std::cout << "<blockquote>";
std::cout << "Meta-tags are processed by the parser before the final "
<< "SDF file is generated.";
std::cout << "<ul>";
std::cout << "<li><b><include></b>: Include an SDF model file "
<< "within the current SDF file."
<< "<ul style='margin-left:12px'>"
<< "<li><b><uri></b>: URI of SDF model file to include.</li>"
<< "<li><b><name></b>: Name of the included SDF model.</li>"
<< "<li><b><pose></b>: Pose of the included SDF model, "
<< "specified as <pose>x y z roll pitch yaw</pose>, "
<< "with x, y, and z representing a position in meters, and roll, "
<< "pitch, and yaw representing Euler angles in radians.</li>"
<< "</ul>"
<< "</li>";
std::cout << "</ul>";
std::cout << "</blockquote>";
std::cout << "</div>\n";
std::cout << "<div id='my_splitter'>\n";
std::cout << "<div id='left_pane'>\n";
std::cout << html;
std::cout << "</div>\n";
std::cout << "<div id='right_pane'>\n";
std::cout << html2;
std::cout << "</div>\n";
std::cout << "</div>\n";
std::cout << " </body> </html>\n";
}
/////////////////////////////////////////////////
void SDF::Write(const std::string &_filename)
{
std::string string = this->Root()->ToString("");
std::ofstream out(_filename.c_str(), std::ios::out);
if (!out)
{
sdferr << "Unable to open file[" << _filename << "] for writing\n";
return;
}
out << string;
out.close();
}
/////////////////////////////////////////////////
std::string SDF::ToString(const PrintConfig &_config) const
{
std::ostringstream stream;
stream << "<?xml version='1.0'?>\n";
if (this->Root()->GetName() != "sdf")
{
stream << "<sdf version='" << SDF::Version() << "'>\n";
}
stream << this->Root()->ToString("", _config);
if (this->Root()->GetName() != "sdf")
{
stream << "</sdf>";
}
return stream.str();
}
/////////////////////////////////////////////////
void SDF::SetFromString(const std::string &_sdfData)
{
sdf::initFile("root.sdf", this->Root());
if (!sdf::readString(_sdfData, this->Root()))
{
sdferr << "Unable to parse sdf string[" << _sdfData << "]\n";
}
}
/////////////////////////////////////////////////
void SDF::Clear()
{
this->dataPtr->root->Clear();
this->dataPtr->path.clear();
this->dataPtr->originalVersion.clear();
}
/////////////////////////////////////////////////
ElementPtr SDF::Root() const
{
return this->dataPtr->root;
}
/////////////////////////////////////////////////
void SDF::SetRoot(const ElementPtr _root)
{
this->dataPtr->root = _root;
}
/////////////////////////////////////////////////
void SDF::Root(const ElementPtr _root)
{
this->SetRoot(_root);
}
/////////////////////////////////////////////////
std::string SDF::FilePath() const
{
return this->dataPtr->path;
}
/////////////////////////////////////////////////
void SDF::SetFilePath(const std::string &_path)
{
this->dataPtr->path = _path;
this->dataPtr->root->SetFilePath(_path);
}
/////////////////////////////////////////////////
void SDF::SetOriginalVersion(const std::string &_version)
{
this->dataPtr->originalVersion = _version;
this->dataPtr->root->SetOriginalVersion(_version);
}
/////////////////////////////////////////////////
const std::string &SDF::OriginalVersion() const
{
return this->dataPtr->originalVersion;
}
/////////////////////////////////////////////////
std::string SDF::Version()
{
return version;
}
/////////////////////////////////////////////////
void SDF::Version(const std::string &_version)
{
version = _version;
}
/////////////////////////////////////////////////
ElementPtr SDF::WrapInRoot(const ElementPtr &_sdf)
{
ElementPtr root(new Element);
root->SetName("sdf");
std::stringstream v;
v << Version();
root->AddAttribute("version", "string", v.str(), true, "version");
root->InsertElement(_sdf->Clone());
return root;
}
/////////////////////////////////////////////////
const std::string &SDF::EmbeddedSpec(
const std::string &_filename, const bool _quiet)
{
try
{
const std::string pathname = SDF::Version() + "/" + _filename;
return GetEmbeddedSdf().at(pathname);
}
catch(const std::out_of_range &)
{
if (!_quiet)
sdferr << "Unable to find SDF filename[" << _filename << "] with "
<< "version " << SDF::Version() << "\n";
}
// An empty SDF string is returned if a query into the embeddedSdf map fails.
static const std::string emptySdfString;
return emptySdfString;
}
}
}