Complete new XML structure.

This commit is contained in:
Hermann Kraus 2012-03-07 19:16:41 +01:00
parent daf30ca0d1
commit c3cd50ff57
15 changed files with 145 additions and 438 deletions

View file

@ -0,0 +1,90 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_BOOLEAN_HPP
#define MAPNIK_BOOLEAN_HPP
#include <istream>
namespace mapnik
{
/** Helper for class bool */
class boolean {
public:
boolean() {}
boolean(bool b) : b_(b) {}
boolean(boolean const& b) : b_(b.b_) {}
operator bool() const
{
return b_;
}
boolean & operator = (boolean const& other)
{
b_ = other.b_;
return * this;
}
boolean & operator = (bool other)
{
b_ = other;
return * this;
}
private:
bool b_;
};
/** Special stream input operator for boolean values */
template <typename charT, typename traits>
std::basic_istream<charT, traits> &
operator >> ( std::basic_istream<charT, traits> & s, boolean & b )
{
std::string word;
s >> word;
if ( s )
{
if ( word == "true" || word == "yes" || word == "on" ||
word == "1")
{
b = true;
}
else if ( word == "false" || word == "no" || word == "off" ||
word == "0")
{
b = false;
}
else
{
s.setstate( std::ios::failbit );
}
}
return s;
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits> &
operator << ( std::basic_ostream<charT, traits> & s, boolean const& b )
{
s << ( b ? "true" : "false" );
return s;
}
}
#endif // MAPNIK_BOOLEAN_HPP

View file

@ -137,6 +137,14 @@ public:
std::string to_hex_string() const; std::string to_hex_string() const;
}; };
template <typename charT, typename traits>
std::basic_ostream<charT, traits> &
operator << ( std::basic_ostream<charT, traits> & s, mapnik::color const& c )
{
std::string hex_string( c.to_string() );
s << hex_string;
return s;
}
} }

View file

@ -23,442 +23,17 @@
#ifndef MAPNIK_PTREE_HELPERS_HPP #ifndef MAPNIK_PTREE_HELPERS_HPP
#define MAPNIK_PTREE_HELPERS_HPP #define MAPNIK_PTREE_HELPERS_HPP
// mapnik
#include <mapnik/enumeration.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/color_factory.hpp>
#include <mapnik/util/conversions.hpp>
// boost // boost
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
// stl
#include <iostream>
#include <sstream>
namespace mapnik { namespace mapnik {
template <typename T>
inline boost::optional<T> fast_cast(std::string const& value);
template <typename T>
T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute,
T const& default_value);
template <typename T>
T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute);
template <typename T>
T get_value(boost::property_tree::ptree const& node, std::string const& name);
template <typename T>
boost::optional<T> get_optional(boost::property_tree::ptree const& node, std::string const& name,
bool is_attribute);
template <typename T>
boost::optional<T> get_opt_attr( boost::property_tree::ptree const& node,
std::string const& name)
{
return get_optional<T>( node, name, true);
}
template <typename T>
boost::optional<T> get_opt_child( boost::property_tree::ptree const& node,
std::string const& name)
{
return get_optional<T>( node, name, false);
}
template <typename T>
T get_attr( boost::property_tree::ptree const& node, std::string const& name,
T const& default_value )
{
return get<T>( node, name, true, default_value);
}
template <typename T>
T get_attr( boost::property_tree::ptree const& node, std::string const& name )
{
return get<T>( node, name, true );
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits> &
operator << ( std::basic_ostream<charT, traits> & s, mapnik::color const& c )
{
std::string hex_string( c.to_string() );
s << hex_string;
return s;
}
/** Helper for class bool */
class boolean {
public:
boolean() {}
boolean(bool b) : b_(b) {}
boolean(boolean const& b) : b_(b.b_) {}
operator bool() const
{
return b_;
}
boolean & operator = (boolean const& other)
{
b_ = other.b_;
return * this;
}
boolean & operator = (bool other)
{
b_ = other;
return * this;
}
private:
bool b_;
};
/** Special stream input operator for boolean values */
template <typename charT, typename traits>
std::basic_istream<charT, traits> &
operator >> ( std::basic_istream<charT, traits> & s, boolean & b )
{
std::string word;
s >> word;
if ( s )
{
if ( word == "true" || word == "yes" || word == "on" ||
word == "1")
{
b = true;
}
else if ( word == "false" || word == "no" || word == "off" ||
word == "0")
{
b = false;
}
else
{
s.setstate( std::ios::failbit );
}
}
return s;
}
template <typename charT, typename traits>
std::basic_ostream<charT, traits> &
operator << ( std::basic_ostream<charT, traits> & s, boolean const& b )
{
s << ( b ? "true" : "false" );
return s;
}
template <typename T> template <typename T>
void set_attr(boost::property_tree::ptree & pt, std::string const& name, T const& v) void set_attr(boost::property_tree::ptree & pt, std::string const& name, T const& v)
{ {
pt.put("<xmlattr>." + name, v); pt.put("<xmlattr>." + name, v);
} }
class boolean;
template <typename T>
struct name_trait
{
static std::string name()
{
return "<unknown>";
}
// missing name_trait for type ...
// if you get here you are probably using a new type
// in the XML file. Just add a name trait for the new
// type below.
BOOST_STATIC_ASSERT( sizeof(T) == 0 );
};
#define DEFINE_NAME_TRAIT( type, type_name ) \
template <> \
struct name_trait<type> \
{ \
static std::string name() { return std::string("type ") + type_name; } \
};
DEFINE_NAME_TRAIT( double, "double")
DEFINE_NAME_TRAIT( float, "float")
DEFINE_NAME_TRAIT( unsigned, "unsigned")
DEFINE_NAME_TRAIT( boolean, "boolean")
DEFINE_NAME_TRAIT( int, "integer" )
DEFINE_NAME_TRAIT( std::string, "string" )
DEFINE_NAME_TRAIT( color, "color" )
template <typename ENUM, int MAX>
struct name_trait< mapnik::enumeration<ENUM, MAX> >
{
typedef enumeration<ENUM, MAX> Enum;
static std::string name()
{
std::string value_list("one of [");
for (unsigned i = 0; i < Enum::MAX; ++i)
{
value_list += Enum::get_string( i );
if ( i + 1 < Enum::MAX ) value_list += ", ";
}
value_list += "]";
return value_list;
}
};
template <typename T>
inline boost::optional<T> fast_cast(std::string const& value)
{
return boost::lexical_cast<T>( value );
}
template <>
inline boost::optional<int> fast_cast(std::string const& value)
{
int result;
if (mapnik::conversions::string2int(value,result))
return boost::optional<int>(result);
return boost::optional<int>();
}
template <>
inline boost::optional<double> fast_cast(std::string const& value)
{
double result;
if (mapnik::conversions::string2double(value,result))
return boost::optional<double>(result);
return boost::optional<double>();
}
template <>
inline boost::optional<float> fast_cast(std::string const& value)
{
float result;
if (mapnik::conversions::string2float(value,result))
return boost::optional<float>(result);
return boost::optional<float>();
}
template <typename T>
T get(boost::property_tree::ptree const& node,
std::string const& name,
bool is_attribute,
T const& default_value)
{
boost::optional<std::string> str;
if (is_attribute)
{
str = node.get_optional<std::string>( std::string("<xmlattr>.") + name );
}
else
{
str = node.get_optional<std::string>(name + ".<xmltext>");
}
if ( str )
{
boost::optional<T> result = fast_cast<T>(*str);
if (result)
{
return *result;
}
else
{
throw config_error(std::string("Failed to parse ") +
(is_attribute ? "attribute" : "child node") + " '" +
name + "'. Expected " + name_trait<T>::name() +
" but got '" + *str + "'");
}
}
else
{
return default_value;
}
}
template <>
inline color get(boost::property_tree::ptree const& node,
std::string const& name,
bool is_attribute,
color const& default_value)
{
boost::optional<std::string> str;
if (is_attribute)
{
str = node.get_optional<std::string>( std::string("<xmlattr>.") + name );
}
else
{
str = node.get_optional<std::string>(name + ".<xmltext>");
}
if ( str )
{
try
{
return mapnik::color_factory::from_string((*str).c_str());
}
catch (...)
{
throw config_error(std::string("Failed to parse ") +
(is_attribute ? "attribute" : "child node") + " '" +
name + "'. Expected " + name_trait<color>::name() +
" but got '" + *str + "'");
}
}
else
{
return default_value;
}
}
template <typename T>
T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute)
{
boost::optional<std::string> str;
if (is_attribute)
{
str = node.get_optional<std::string>( std::string("<xmlattr>.") + name);
}
else
{
str = node.get_optional<std::string>(name + ".<xmltext>");
}
if ( ! str )
{
throw config_error(std::string("Required ") +
(is_attribute ? "attribute " : "child node ") +
"'" + name + "' is missing");
}
boost::optional<T> result = fast_cast<T>(*str);
if (result)
{
return *result;
}
else
{
throw config_error(std::string("Failed to parse ") +
(is_attribute ? "attribute" : "child node") + " '" +
name + "'. Expected " + name_trait<T>::name() +
" but got '" + *str + "'");
}
}
template <typename T>
T get_value(boost::property_tree::ptree const& node, std::string const& name)
{
try
{
/* NOTE: get_child works as long as there is only one child with that name.
If this function is used this used this condition must always be satisfied.
*/
return node.get_child("<xmltext>").get_value<T>();
}
catch (boost::property_tree::ptree_bad_path)
{
/* If the XML parser did not find any non-empty data element the is no
<xmltext> node. But we don't want to fail here but simply return a
default constructed value of the requested type.
*/
return T();
}
catch (...)
{
throw config_error(std::string("Failed to parse ") +
name + ". Expected " + name_trait<T>::name() +
" but got '" + node.data() + "'");
}
}
template <typename T>
boost::optional<T> get_optional(boost::property_tree::ptree const& node,
std::string const& name,
bool is_attribute)
{
boost::optional<std::string> str;
if (is_attribute)
{
str = node.get_optional<std::string>( std::string("<xmlattr>.") + name);
}
else
{
str = node.get_optional<std::string>(name + ".<xmltext>");
}
boost::optional<T> result;
if ( str )
{
result = fast_cast<T>(*str);
if (!result)
{
throw config_error(std::string("Failed to parse ") +
(is_attribute ? "attribute" : "child node") + " '" +
name + "'. Expected " + name_trait<T>::name() +
" but got '" + *str + "'");
}
}
return result;
}
template <>
inline boost::optional<std::string> get_optional(boost::property_tree::ptree const& node,
std::string const& name,
bool is_attribute)
{
if (is_attribute)
{
return node.get_optional<std::string>( std::string("<xmlattr>.") + name);
}
else
{
return node.get_optional<std::string>(name + ".<xmltext>");
}
}
template <>
inline boost::optional<color> get_optional(boost::property_tree::ptree const& node,
std::string const& name,
bool is_attribute)
{
boost::optional<std::string> str;
if (is_attribute)
{
str = node.get_optional<std::string>( std::string("<xmlattr>.") + name);
}
else
{
str = node.get_optional<std::string>(name + ".<xmltext>");
}
boost::optional<color> result;
if ( str )
{
try
{
result = mapnik::color_factory::from_string((*str).c_str());
}
catch (...)
{
throw config_error(std::string("Failed to parse ") +
(is_attribute ? "attribute" : "child node") + " '" +
name + "'. Expected " + name_trait<color>::name() +
" but got '" + *str + "'");
}
}
return result;
}
static inline bool has_child(boost::property_tree::ptree const& node, std::string const& name)
{
boost::optional<std::string> str = node.get_optional<std::string>(name);
return str;
}
} // end of namespace mapnik } // end of namespace mapnik
#endif // MAPNIK_PTREE_HELPERS_HPP #endif // MAPNIK_PTREE_HELPERS_HPP

View file

@ -23,6 +23,10 @@
#ifndef MAPNIK_XML_TREE_H #ifndef MAPNIK_XML_TREE_H
#define MAPNIK_XML_TREE_H #define MAPNIK_XML_TREE_H
//mapnik
#include <mapnik/boolean.hpp>
//boost //boost
#include <boost/optional.hpp> #include <boost/optional.hpp>

View file

@ -14,7 +14,8 @@
#include <mapnik/memory_featureset.hpp> #include <mapnik/memory_featureset.hpp>
#include <mapnik/wkt/wkt_factory.hpp> #include <mapnik/wkt/wkt_factory.hpp>
#include <mapnik/util/geometry_to_ds_type.hpp> #include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/ptree_helpers.hpp> // mapnik::boolean #include <mapnik/boolean.hpp>
#include <mapnik/color_factory.hpp>
// stl // stl
#include <sstream> #include <sstream>

View file

@ -25,7 +25,7 @@
#include "gdal_featureset.hpp" #include "gdal_featureset.hpp"
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
#include <gdal_version.h> #include <gdal_version.h>

View file

@ -30,7 +30,7 @@
#include <cstdarg> #include <cstdarg>
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
// boost // boost

View file

@ -33,7 +33,7 @@
#include <cstdio> #include <cstdio>
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
// boost // boost
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View file

@ -25,7 +25,7 @@
#include "occi_featureset.hpp" #include "occi_featureset.hpp"
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/sql_utils.hpp> #include <mapnik/sql_utils.hpp>
// boost // boost

View file

@ -33,7 +33,7 @@
#include <gdal_version.h> #include <gdal_version.h>
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
// boost // boost

View file

@ -26,7 +26,7 @@
// mapnik // mapnik
#include <mapnik/global.hpp> #include <mapnik/global.hpp>
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/sql_utils.hpp> #include <mapnik/sql_utils.hpp>
#include <mapnik/util/conversions.hpp> #include <mapnik/util/conversions.hpp>

View file

@ -29,7 +29,7 @@
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
using mapnik::datasource; using mapnik::datasource;

View file

@ -26,7 +26,7 @@
#include "sqlite_utils.hpp" #include "sqlite_utils.hpp"
// mapnik // mapnik
#include <mapnik/ptree_helpers.hpp> #include <mapnik/boolean.hpp>
#include <mapnik/sql_utils.hpp> #include <mapnik/sql_utils.hpp>
#include <mapnik/util/geometry_to_ds_type.hpp> #include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/wkb.hpp> #include <mapnik/wkb.hpp>

View file

@ -45,7 +45,7 @@ void text_node::to_xml(ptree &xml) const
node_ptr text_node::from_xml(xml_node const& xml) node_ptr text_node::from_xml(xml_node const& xml)
{ {
std::string data = xml.get_text(); std::string data = xml.text();
if (data.empty()) return node_ptr(); //No text if (data.empty()) return node_ptr(); //No text
return node_ptr(new text_node(parse_expression(data, "utf8"))); return node_ptr(new text_node(parse_expression(data, "utf8")));
} }

View file

@ -27,6 +27,8 @@
#include <mapnik/color_factory.hpp> #include <mapnik/color_factory.hpp>
#include <mapnik/gamma_method.hpp> #include <mapnik/gamma_method.hpp>
#include <mapnik/line_symbolizer.hpp> #include <mapnik/line_symbolizer.hpp>
#include <mapnik/feature_type_style.hpp>
#include <mapnik/text_properties.hpp>
//boost //boost
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
@ -79,6 +81,11 @@ inline boost::optional<color> fast_cast(xml_tree const& tree, std::string const&
return mapnik::color_factory::from_string(value); return mapnik::color_factory::from_string(value);
} }
template <>
inline boost::optional<expression_ptr> fast_cast(xml_tree const& tree, std::string const& value)
{
return parse_expression(value);
}
/****************************************************************************/ /****************************************************************************/
@ -112,6 +119,7 @@ DEFINE_NAME_TRAIT( boolean, "boolean")
DEFINE_NAME_TRAIT( int, "integer" ) DEFINE_NAME_TRAIT( int, "integer" )
DEFINE_NAME_TRAIT( std::string, "string" ) DEFINE_NAME_TRAIT( std::string, "string" )
DEFINE_NAME_TRAIT( color, "color" ) DEFINE_NAME_TRAIT( color, "color" )
DEFINE_NAME_TRAIT(expression_ptr, "expression_ptr" )
template <typename ENUM, int MAX> template <typename ENUM, int MAX>
struct name_trait< mapnik::enumeration<ENUM, MAX> > struct name_trait< mapnik::enumeration<ENUM, MAX> >
@ -376,19 +384,21 @@ std::string xml_node::get_text() const
template <typename T> template <typename T>
T xml_node::get_value() const T xml_node::get_value() const
{ {
boost::optional<T> result = fast_cast<T>(get_text()); boost::optional<T> result = fast_cast<T>(tree_, get_text());
if (!result) if (!result)
{ {
throw config_error(std::string("Failed to parse value in node '") + throw config_error(std::string("Failed to parse value in node '") +
name_ + "'. Expected " + name_trait<T>::name() + name_ + "'. Expected " + name_trait<T>::name() +
" but got '" + get_text() + "'"); " but got '" + get_text() + "'");
} }
return result; return *result;
} }
#define compile_get_opt_attr(T) template boost::optional<T> xml_node::get_opt_attr<T>(std::string const&) const #define compile_get_opt_attr(T) template boost::optional<T> xml_node::get_opt_attr<T>(std::string const&) const
#define compile_get_attr(T) template T xml_node::get_attr<T>(std::string const&) const; template T xml_node::get_attr<T>(std::string const&, T const&) const
#define compile_get_value(T) template T xml_node::get_value<T>() const
//compile_get_opt_attr(boolean); compile_get_opt_attr(boolean);
compile_get_opt_attr(std::string); compile_get_opt_attr(std::string);
compile_get_opt_attr(unsigned); compile_get_opt_attr(unsigned);
compile_get_opt_attr(float); compile_get_opt_attr(float);
@ -396,4 +406,23 @@ compile_get_opt_attr(double);
compile_get_opt_attr(color); compile_get_opt_attr(color);
compile_get_opt_attr(gamma_method_e); compile_get_opt_attr(gamma_method_e);
compile_get_opt_attr(line_rasterizer_e); compile_get_opt_attr(line_rasterizer_e);
compile_get_opt_attr(line_join_e);
compile_get_opt_attr(line_cap_e);
compile_get_opt_attr(text_transform_e);
compile_get_opt_attr(label_placement_e);
compile_get_opt_attr(vertical_alignment_e);
compile_get_opt_attr(horizontal_alignment_e);
compile_get_opt_attr(justify_alignment_e);
compile_get_attr(std::string);
compile_get_attr(filter_mode_e);
compile_get_attr(point_placement_e);
compile_get_attr(marker_placement_e);
compile_get_attr(marker_type_e);
compile_get_attr(pattern_alignment_e);
compile_get_attr(line_rasterizer_e);
compile_get_attr(colorizer_mode);
compile_get_attr(double);
compile_get_value(int);
compile_get_value(double);
compile_get_value(expression_ptr);
} //ns mapnik } //ns mapnik