xlnt/source/serialization/xml_node.cpp

162 lines
4.1 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/serialization/xml_serializer.hpp>
2015-10-31 06:54:04 +08:00
#include <detail/xml_node_impl.hpp>
2015-10-30 01:46:56 +08:00
namespace xlnt {
2015-10-31 06:54:04 +08:00
xml_node::xml_node() : d_(new detail::xml_node_impl)
2015-10-30 01:46:56 +08:00
{
}
2015-10-31 06:54:04 +08:00
xml_node::xml_node(const detail::xml_node_impl &d) : xml_node()
{
d_->node = d.node;
}
2015-10-31 06:54:04 +08:00
xml_node::~xml_node()
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
}
xml_node::xml_node(const xml_node &other) : xml_node()
{
d_->node = other.d_->node;
}
2015-10-31 06:54:04 +08:00
xml_node &xml_node::operator=(const xlnt::xml_node &other)
{
d_->node = other.d_->node;
2015-10-31 06:54:04 +08:00
return *this;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_name() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->node.name();
2015-10-30 01:46:56 +08:00
}
void xml_node::set_name(const std::string &name)
2015-10-30 01:46:56 +08:00
{
d_->node.set_name(name.c_str());
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_text() const
{
2015-10-31 06:54:04 +08:00
return d_->node.text() != nullptr;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_text() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->node.text().as_string();
2015-10-30 01:46:56 +08:00
}
void xml_node::set_text(const std::string &text)
2015-10-30 01:46:56 +08:00
{
d_->node.text().set(text.c_str());
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
const std::vector<xml_node> xml_node::get_children() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
std::vector<xml_node> children;
for (auto child : d_->node.children())
2015-10-31 06:54:04 +08:00
{
children.push_back(xml_node(detail::xml_node_impl(child)));
}
2015-10-31 06:54:04 +08:00
return children;
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
xml_node xml_node::add_child(const xml_node &child)
2015-10-30 01:46:56 +08:00
{
auto child_node = xml_node(detail::xml_node_impl(d_->node.append_child(child.get_name().c_str())));
for (auto attr : child.get_attributes())
2015-10-31 06:54:04 +08:00
{
child_node.add_attribute(attr.first, attr.second);
}
for (auto child_child : child.get_children())
2015-10-31 06:54:04 +08:00
{
child_node.add_child(child_child);
}
2015-10-31 06:54:04 +08:00
return child_node;
2015-10-30 01:46:56 +08:00
}
xml_node xml_node::add_child(const std::string &child_name)
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.append_child(child_name.c_str())));
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
const std::vector<xml_node::string_pair> xml_node::get_attributes() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
std::vector<string_pair> attributes;
for (auto attr : d_->node.attributes())
2015-10-31 06:54:04 +08:00
{
attributes.push_back(std::make_pair<std::string, std::string>(attr.name(), attr.value()));
2015-10-31 06:54:04 +08:00
}
2015-10-31 06:54:04 +08:00
return attributes;
2015-10-30 01:46:56 +08:00
}
void xml_node::add_attribute(const std::string &name, const std::string &value)
2015-10-30 01:46:56 +08:00
{
d_->node.append_attribute(name.c_str()).set_value(value.c_str());
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_attribute(const std::string &attribute_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.attribute(attribute_name.c_str()) != nullptr;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_attribute(const std::string &attribute_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.attribute(attribute_name.c_str()).value();
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_child(const std::string &child_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.child(child_name.c_str()) != nullptr;
2015-10-30 01:46:56 +08:00
}
xml_node xml_node::get_child(const std::string &child_name)
2015-10-30 07:37:07 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.child(child_name.c_str())));
2015-10-30 07:37:07 +08:00
}
const xml_node xml_node::get_child(const std::string &child_name) const
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.child(child_name.c_str())));
2015-10-31 06:54:04 +08:00
}
std::string xml_node::to_string() const
2015-10-31 06:54:04 +08:00
{
return xml_serializer::serialize_node(*this);
2015-10-30 01:46:56 +08:00
}
} // namespace xlnt