Skip to content

Commit b999425

Browse files
authored
Merge pull request #1294 from gazebosim/sdf_error_parser
Update functions in parser.cc to use sdf::Errors output.
2 parents e55378e + fe2c105 commit b999425

File tree

9 files changed

+656
-243
lines changed

9 files changed

+656
-243
lines changed

include/sdf/Error.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ namespace sdf
219219
/// \return Error message.
220220
public: std::string Message() const;
221221

222+
/// \brief Sets the message associated with this error.
223+
/// \param [in] _message Message that describes this error.
224+
public: void SetMessage(const std::string &_message);
225+
222226
/// \brief Get the file path associated with this error.
223227
/// \return Returns the path of the file that this error is related to,
224228
/// nullopt otherwise.

include/sdf/parser.hh

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ namespace sdf
5555
SDFORMAT_VISIBLE
5656
bool init(SDFPtr _sdf, const ParserConfig &_config);
5757

58+
/// \brief Initialize the SDF interface from the embedded root spec file
59+
/// \param[out] _errors Vector of errors.
60+
/// \param[out] _sdf Pointer to an SDF object.
61+
/// \param[in] _config Custom parser configuration
62+
/// \return True if successful.
63+
SDFORMAT_VISIBLE
64+
bool init(sdf::Errors &_errors, SDFPtr _sdf, const ParserConfig &_config);
65+
5866
/// \brief Initialize the SDF interface using a file
5967
/// \param[in] _filename Name of the SDF file
6068
/// \param[out] _sdf Pointer to an SDF object.
@@ -71,9 +79,20 @@ namespace sdf
7179
bool initFile(
7280
const std::string &_filename, const ParserConfig &_config, SDFPtr _sdf);
7381

82+
/// \brief Initialize the SDF interface using a file
83+
/// \param[in] _filename Name of the SDF file
84+
/// \param[in] _config Custom parser configuration
85+
/// \param[out] _sdf Pointer to an SDF object.
86+
/// \param[out] _errors Vector of errors.
87+
/// \return True if successful.
88+
SDFORMAT_VISIBLE
89+
bool initFile(
90+
const std::string &_filename, const ParserConfig &_config, SDFPtr _sdf,
91+
sdf::Errors &_errors);
92+
7493
/// \brief Initialize an SDF Element interface using a file
7594
/// \param[in] _filename Name of the SDF file
76-
/// \param[in] _sdf Pointer to an SDF Element object.
95+
/// \param[out] _sdf Pointer to an SDF Element object.
7796
/// \return True if successful.
7897
SDFORMAT_VISIBLE
7998
bool initFile(const std::string &_filename, ElementPtr _sdf);
@@ -87,6 +106,16 @@ namespace sdf
87106
bool initFile(const std::string &_filename, const ParserConfig &_config,
88107
ElementPtr _sdf);
89108

109+
/// \brief Initialize an SDFElement interface using a file
110+
/// \param[in] _filename Name of the SDF file
111+
/// \param[in] _config Custom parser configuration
112+
/// \param[out] _sdf Pointer to an SDF Element object.
113+
/// \param[out] _errors Vector of errors.
114+
/// \return True if successful.
115+
SDFORMAT_VISIBLE
116+
bool initFile(const std::string &_filename, const ParserConfig &_config,
117+
ElementPtr _sdf, sdf::Errors &_errors);
118+
90119
/// \brief Initialize the SDF interface using a string
91120
/// \param[in] _xmlString XML string to be parsed.
92121
/// \param[out] _sdf Pointer to an SDF object.
@@ -103,6 +132,16 @@ namespace sdf
103132
bool initString(
104133
const std::string &_xmlString, const ParserConfig &_config, SDFPtr _sdf);
105134

135+
/// \brief Initialize the SDF interface using a string
136+
/// \param[in] _xmlString XML string to be parsed.
137+
/// \param[in] _config Custom parser configuration
138+
/// \param[out] _sdf Pointer to an SDF object.
139+
/// \param[out] _errors Vector of errors.
140+
/// \return True if successful.
141+
SDFORMAT_VISIBLE
142+
bool initString(const std::string &_xmlString, const ParserConfig &_config,
143+
SDFPtr _sdf, sdf::Errors &_errors);
144+
106145
/// \brief Populate the SDF values from a file
107146
///
108147
/// This populates a new SDF pointer from a file. If the file is a URDF
@@ -317,6 +356,16 @@ namespace sdf
317356
SDFORMAT_VISIBLE
318357
std::string getModelFilePath(const std::string &_modelDirPath);
319358

359+
/// \brief Get the file path to the model file
360+
/// \param[out] _errors Vector of errors.
361+
/// \param[in] _modelDirPath directory system path of the model
362+
/// \return string with the full filesystem path to the best version (greater
363+
/// SDF protocol supported by this sdformat version) of the .sdf
364+
/// model files hosted by _modelDirPath.
365+
SDFORMAT_VISIBLE
366+
std::string getModelFilePath(sdf::Errors &_errors,
367+
const std::string &_modelDirPath);
368+
320369
/// \brief Convert an SDF file to a specific SDF version.
321370
/// \param[in] _filename Name of the SDF file to convert.
322371
/// \param[in] _version Version to convert _filename to.
@@ -387,6 +436,17 @@ namespace sdf
387436
SDFORMAT_VISIBLE
388437
bool checkCanonicalLinkNames(const sdf::Root *_root);
389438

439+
/// \brief Check that for each model, the canonical_link attribute value
440+
/// matches the name of a link in the model if the attribute is set and
441+
/// not empty.
442+
/// This checks recursively and should check the files exhaustively
443+
/// rather than terminating early when the first error is found.
444+
/// \param[out] _errors Vector of errors.
445+
/// \param[in] _root SDF Root object to check recursively.
446+
/// \return True if all models have valid canonical_link attributes.
447+
SDFORMAT_VISIBLE
448+
bool checkCanonicalLinkNames(sdf::Errors &_errors, const sdf::Root *_root);
449+
390450
/// \brief For the world and each model, check that the attached_to graphs
391451
/// build without errors and have no cycles.
392452
/// Confirm that following directed edges from each vertex in the graph
@@ -398,6 +458,18 @@ namespace sdf
398458
SDFORMAT_VISIBLE
399459
bool checkFrameAttachedToGraph(const sdf::Root *_root);
400460

461+
/// \brief For the world and each model, check that the attached_to graphs
462+
/// build without errors and have no cycles.
463+
/// Confirm that following directed edges from each vertex in the graph
464+
/// leads to a model, link, or world frame.
465+
/// This checks recursively and should check the files exhaustively
466+
/// rather than terminating early when the first error is found.
467+
/// \param[out] _errors Vector of errors.
468+
/// \param[in] _root SDF Root object to check recursively.
469+
/// \return True if all attached_to graphs are valid.
470+
SDFORMAT_VISIBLE
471+
bool checkFrameAttachedToGraph(sdf::Errors &_errors, const sdf::Root *_root);
472+
401473
/// \brief Check that for each frame, the attached_to attribute value
402474
/// does not match its own frame name but does match the name of a
403475
/// link, joint, or other frame in the model if the attribute is set and
@@ -409,6 +481,18 @@ namespace sdf
409481
SDFORMAT_VISIBLE
410482
bool checkFrameAttachedToNames(const sdf::Root *_root);
411483

484+
/// \brief Check that for each frame, the attached_to attribute value
485+
/// does not match its own frame name but does match the name of a
486+
/// link, joint, or other frame in the model if the attribute is set and
487+
/// not empty.
488+
/// This checks recursively and should check the files exhaustively
489+
/// rather than terminating early when the first error is found.
490+
/// \param[out] _errors Vector of errors.
491+
/// \param[in] _root SDF Root object to check recursively.
492+
/// \return True if all frames have valid attached_to attributes.
493+
SDFORMAT_VISIBLE
494+
bool checkFrameAttachedToNames(sdf::Errors &_errors, const sdf::Root *_root);
495+
412496
/// \brief Check that all joints in contained models specify parent
413497
/// and child link names that match the names of sibling links.
414498
/// This checks recursively and should check the files exhaustively
@@ -460,6 +544,18 @@ namespace sdf
460544
SDFORMAT_VISIBLE
461545
bool checkPoseRelativeToGraph(const sdf::Root *_root);
462546

547+
/// \brief For the world and each model, check that the attached_to graphs
548+
/// build without errors and have no cycles.
549+
/// Confirm that following directed edges from each vertex in the graph
550+
/// leads to a model, link, or world frame.
551+
/// This checks recursively and should check the files exhaustively
552+
/// rather than terminating early when the first error is found.
553+
/// \param[out] _errors Vector of errors.
554+
/// \param[in] _root SDF Root object to check recursively.
555+
/// \return True if all attached_to graphs are valid.
556+
SDFORMAT_VISIBLE
557+
bool checkPoseRelativeToGraph(sdf::Errors &_errors, const sdf::Root *_root);
558+
463559
/// \brief Check that all sibling elements of the same type have unique names.
464560
/// This checks recursively and should check the files exhaustively
465561
/// rather than terminating early when the first duplicate name is found.
@@ -469,6 +565,17 @@ namespace sdf
469565
SDFORMAT_VISIBLE
470566
bool recursiveSameTypeUniqueNames(sdf::ElementPtr _elem);
471567

568+
/// \brief Check that all sibling elements of the same type have unique names.
569+
/// This checks recursively and should check the files exhaustively
570+
/// rather than terminating early when the first duplicate name is found.
571+
/// \param[out] _errors Vector of errors.
572+
/// \param[in] _elem SDF Element to check recursively.
573+
/// \return True if all contained elements have do not share a name with
574+
/// sibling elements of the same type.
575+
SDFORMAT_VISIBLE
576+
bool recursiveSameTypeUniqueNames(sdf::Errors &_errors,
577+
sdf::ElementPtr _elem);
578+
472579
/// \brief Check that all sibling elements of the any type have unique names.
473580
/// This checks recursively and should check the files exhaustively
474581
/// rather than terminating early when the first duplicate name is found.
@@ -478,6 +585,16 @@ namespace sdf
478585
SDFORMAT_VISIBLE
479586
bool recursiveSiblingUniqueNames(sdf::ElementPtr _elem);
480587

588+
/// \brief Check that all sibling elements of the any type have unique names.
589+
/// This checks recursively and should check the files exhaustively
590+
/// rather than terminating early when the first duplicate name is found.
591+
/// \param[out] _errors Vector of errors.
592+
/// \param[in] _elem SDF Element to check recursively.
593+
/// \return True if all contained elements have do not share a name with
594+
/// sibling elements of any type.
595+
SDFORMAT_VISIBLE
596+
bool recursiveSiblingUniqueNames(sdf::Errors &_errors, sdf::ElementPtr _elem);
597+
481598
/// \brief Check that all sibling elements do not contain the delimiter
482599
/// double colons '::' in element names, which is reserved for forming scopes
483600
/// in SDFormat 1.8. This checks recursively and should check the files
@@ -488,6 +605,18 @@ namespace sdf
488605
SDFORMAT_VISIBLE
489606
bool recursiveSiblingNoDoubleColonInNames(sdf::ElementPtr _elem);
490607

608+
/// \brief Check that all sibling elements do not contain the delimiter
609+
/// double colons '::' in element names, which is reserved for forming scopes
610+
/// in SDFormat 1.8. This checks recursively and should check the files
611+
/// exhaustively rather than terminating early when the first name
612+
/// containing '::' is found.
613+
/// \param[out] _errors Vector of errors.
614+
/// \param[in] _elem SDF Element to check recursively.
615+
/// \return True if all contained element names do not have the delimiter '::'
616+
SDFORMAT_VISIBLE
617+
bool recursiveSiblingNoDoubleColonInNames(sdf::Errors &_errors,
618+
sdf::ElementPtr _elem);
619+
491620
/// \brief Check whether the element should be validated. If this returns
492621
/// false, validators such as the unique name and reserve name checkers should
493622
/// skip this element and its descendants.

src/Error.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ std::string Error::Message() const
8484
return this->dataPtr->message;
8585
}
8686

87+
/////////////////////////////////////////////////
88+
void Error::SetMessage(const std::string &_message)
89+
{
90+
this->dataPtr->message = _message;
91+
}
92+
8793
/////////////////////////////////////////////////
8894
std::optional<std::string> Error::FilePath() const
8995
{

src/Root.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Errors Root::Load(const std::string &_filename, const ParserConfig &_config)
197197
if (!sdfParsed)
198198
{
199199
errors.push_back(
200-
{ErrorCode::FILE_READ, "Unable to read file:" + _filename});
200+
{ErrorCode::FILE_READ, "Unable to read file: [" + _filename + "]"});
201201
return errors;
202202
}
203203

0 commit comments

Comments
 (0)