Skip to content

Commit a4d6445

Browse files
committed
Add component validator for URDF validation against XSD
1 parent 94f077a commit a4d6445

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2020 ros2_control Development Team
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef HARDWARE_INTERFACE__COMPONENT_VALIDATOR_HPP_
16+
#define HARDWARE_INTERFACE__COMPONENT_VALIDATOR_HPP_
17+
18+
#include <gmock/gmock.h>
19+
#include <gtest/gtest.h>
20+
#include <libxml/parser.h>
21+
#include <libxml/xmlschemas.h>
22+
23+
#include <string>
24+
25+
#include <ament_index_cpp/get_package_share_directory.hpp>
26+
27+
namespace hardware_interface
28+
{
29+
30+
/// Validate URDF string against an XML Schema Definition (XSD) file.
31+
/**
32+
* \param[in] urdf string with robot's URDF
33+
* \param[in] xsd_file_path path to the XSD file
34+
* \return true if the URDF is valid according to the XSD, false otherwise
35+
*/
36+
bool validate_urdf_with_xsd(const std::string & urdf, const std::string & xsd_file_path);
37+
38+
} // namespace hardware_interface
39+
40+
#endif // HARDWARE_INTERFACE__COMPONENT_VALIDATOR_HPP_
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 ros2_control Development Team
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "ros2_control/hardware_interface/component_validator.hpp"
16+
17+
namespace hardware_interface
18+
{
19+
bool validate_urdf_with_xsd(const std::string & urdf, const std::string & xsd_file_path)
20+
{
21+
xmlDocPtr doc = xmlReadMemory(urdf.c_str(), static_cast<int>(urdf.size()), nullptr, nullptr, 0);
22+
if (!doc)
23+
{
24+
return false;
25+
}
26+
27+
xmlSchemaParserCtxtPtr schemaCtx = xmlSchemaNewParserCtxt(xsd_file_path.c_str());
28+
if (!schemaCtx)
29+
{
30+
xmlFreeDoc(doc);
31+
return false;
32+
}
33+
xmlSchemaPtr schema = xmlSchemaParse(schemaCtx);
34+
if (!schema)
35+
{
36+
xmlSchemaFreeParserCtxt(schemaCtx);
37+
xmlFreeDoc(doc);
38+
return false;
39+
}
40+
41+
xmlSchemaValidCtxtPtr validCtx = xmlSchemaNewValidCtxt(schema);
42+
if (!validCtx)
43+
{
44+
xmlSchemaFree(schema);
45+
xmlSchemaFreeParserCtxt(schemaCtx);
46+
xmlFreeDoc(doc);
47+
return false;
48+
}
49+
int ret = xmlSchemaValidateDoc(validCtx, doc);
50+
xmlSchemaFreeValidCtxt(validCtx);
51+
xmlSchemaFree(schema);
52+
xmlSchemaFreeParserCtxt(schemaCtx);
53+
xmlFreeDoc(doc);
54+
55+
return ret == 0;
56+
}
57+
} // namespace hardware_interface

0 commit comments

Comments
 (0)