Only one grammar object per XML tree.

This commit is contained in:
Hermann Kraus 2012-03-11 23:24:28 +01:00
parent c2f9e3b637
commit 502773bea6
21 changed files with 295 additions and 290 deletions

View file

@ -25,134 +25,26 @@
// mapnik
#include <mapnik/config.hpp>
#include <mapnik/color.hpp>
#include <mapnik/config_error.hpp>
// boost
#include <boost/utility.hpp>
#include <boost/version.hpp>
// boost 1.41 -> 1.44 compatibility, to be removed in mapnik 2.1 (dane)
#if BOOST_VERSION >= 104500
#include <mapnik/css_color_grammar.hpp>
namespace mapnik {
class color;
template <typename Iterator> struct css_color_grammar;
class MAPNIK_DECL color_factory : boost::noncopyable
{
public:
static void init_from_string(color & c, std::string const& css_color)
{
typedef std::string::const_iterator iterator_type;
typedef mapnik::css_color_grammar<iterator_type> css_color_grammar;
css_color_grammar g;
iterator_type first = css_color.begin();
iterator_type last = css_color.end();
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
c);
if (!result)
{
throw config_error(std::string("Failed to parse color value: ") +
"Expected a CSS color, but got '" + css_color + "'");
}
}
static void init_from_string(color & c, std::string const& css_color);
static bool parse_from_string(color & c, std::string const& css_color,
mapnik::css_color_grammar<std::string::const_iterator> const& g)
{
std::string::const_iterator first = css_color.begin();
std::string::const_iterator last = css_color.end();
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
c);
return result && (first == last);
}
mapnik::css_color_grammar<std::string::const_iterator> const& g);
static color from_string(std::string const& css_color)
{
color c;
init_from_string(c,css_color);
return c;
}
static color from_string(std::string const& css_color);
};
}
#else
#include <mapnik/css_color_grammar_deprecated.hpp>
namespace mapnik {
class MAPNIK_DECL color_factory : boost::noncopyable
{
public:
static bool parse_from_string(color & c, std::string const& css_color,
mapnik::css_color_grammar<std::string::const_iterator> const& g)
{
std::string::const_iterator first = css_color.begin();
std::string::const_iterator last = css_color.end();
mapnik::css css_;
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
css_);
if (result && (first == last))
{
c.set_red(css_.r);
c.set_green(css_.g);
c.set_blue(css_.b);
c.set_alpha(css_.a);
return true;
}
return false;
}
static void init_from_string(color & c, std::string const& css_color)
{
typedef std::string::const_iterator iterator_type;
typedef mapnik::css_color_grammar<iterator_type> css_color_grammar;
css_color_grammar g;
iterator_type first = css_color.begin();
iterator_type last = css_color.end();
mapnik::css css_;
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
css_);
if (!result)
{
throw config_error(std::string("Failed to parse color value: ") +
"Expected a CSS color, but got '" + css_color + "'");
}
c.set_red(css_.r);
c.set_green(css_.g);
c.set_blue(css_.b);
c.set_alpha(css_.a);
}
static color from_string(std::string const& css_color)
{
color c;
init_from_string(c,css_color);
return c;
}
};
}
#endif
#endif // MAPNIK_COLOR_FACTORY_HPP

View file

@ -26,7 +26,6 @@
// mapnik
#include <mapnik/config.hpp>
#include <mapnik/expression_node.hpp>
#include <mapnik/expression_grammar.hpp>
// stl
#include <string>
@ -35,6 +34,7 @@ namespace mapnik
{
typedef boost::shared_ptr<expr_node> expression_ptr;
template <typename Iterator> struct expression_grammar;
class expression_factory
{

View file

@ -1,6 +1,6 @@
#ifndef DUMP_XML_HPP
#define DUMP_XML_HPP
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
/* Debug dump ptree XML representation.
*/

136
include/mapnik/xml_node.hpp Normal file
View file

@ -0,0 +1,136 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 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_XML_NODE_H
#define MAPNIK_XML_NODE_H
//mapnik
#include <mapnik/boolean.hpp>
//boost
#include <boost/optional.hpp>
//stl
#include <list>
#include <string>
#include <map>
#include <exception>
namespace mapnik
{
class xml_tree;
class xml_attribute
{
public:
xml_attribute(std::string const& value);
std::string value;
mutable bool processed;
};
class node_not_found: public std::exception
{
public:
node_not_found(std::string node_name);
virtual const char* what() const throw();
~node_not_found() throw ();
private:
std::string node_name_;
};
class attribute_not_found: public std::exception
{
public:
attribute_not_found(std::string const& node_name, std::string const& attribute_name);
virtual const char* what() const throw();
~attribute_not_found() throw ();
private:
std::string node_name_;
std::string attribute_name_;
};
class more_than_one_child: public std::exception
{
public:
more_than_one_child(std::string const& node_name);
virtual const char* what() const throw();
~more_than_one_child() throw ();
private:
std::string node_name_;
};
class xml_node
{
public:
typedef std::list<xml_node>::const_iterator const_iterator;
typedef std::map<std::string, xml_attribute> attribute_map;
xml_node(xml_tree &tree, std::string name, unsigned line=0, bool text_node = false);
std::string name() const;
std::string text() const;
bool is_text() const;
bool is(std::string const& name) const;
xml_node &add_child(std::string const& name, unsigned line=0, bool text_node = false);
void add_attribute(std::string const& name, std::string const& value);
attribute_map const& get_attributes() const;
bool processed() const;
void set_processed(bool processed) const;
unsigned line() const;
const_iterator begin() const;
const_iterator end() const;
xml_node & get_child(std::string const& name);
xml_node const& get_child(std::string const& name) const;
xml_node const* get_opt_child(std::string const& name) const;
bool has_child(std::string const& name) const;
template <typename T>
boost::optional<T> get_opt_attr(std::string const& name) const;
template <typename T>
T get_attr(std::string const& name, T const& default_value) const;
template <typename T>
T get_attr(std::string const& name) const;
std::string get_text() const;
template <typename T>
T get_value() const;
private:
xml_tree &tree_;
std::string name_;
std::list<xml_node> children_;
attribute_map attributes_;
bool text_node_;
unsigned line_;
mutable bool processed_;
};
} //ns mapnik
#endif // MAPNIK_XML_NODE_H

View file

@ -22,127 +22,31 @@
#ifndef MAPNIK_XML_TREE_H
#define MAPNIK_XML_TREE_H
//mapnik
#include <mapnik/boolean.hpp>
//boost
#include <boost/optional.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/expression_grammar.hpp>
#include <mapnik/css_color_grammar.hpp>
//stl
#include <list>
#include <string>
#include <map>
#include <exception>
namespace mapnik
{
class xml_tree;
class color;
class xml_attribute
{
public:
xml_attribute(std::string const& value);
std::string value;
mutable bool processed;
};
class node_not_found: public std::exception
{
public:
node_not_found(std::string node_name);
virtual const char* what() const throw();
~node_not_found() throw ();
private:
std::string node_name_;
};
class attribute_not_found: public std::exception
{
public:
attribute_not_found(std::string const& node_name, std::string const& attribute_name);
virtual const char* what() const throw();
~attribute_not_found() throw ();
private:
std::string node_name_;
std::string attribute_name_;
};
class more_than_one_child: public std::exception
{
public:
more_than_one_child(std::string const& node_name);
virtual const char* what() const throw();
~more_than_one_child() throw ();
private:
std::string node_name_;
};
class xml_node
{
public:
typedef std::list<xml_node>::const_iterator const_iterator;
typedef std::map<std::string, xml_attribute> attribute_map;
xml_node(xml_tree &tree, std::string name, unsigned line=0, bool text_node = false);
std::string name() const;
std::string text() const;
bool is_text() const;
bool is(std::string const& name) const;
xml_node &add_child(std::string const& name, unsigned line=0, bool text_node = false);
void add_attribute(std::string const& name, std::string const& value);
attribute_map const& get_attributes() const;
bool processed() const;
void set_processed(bool processed) const;
unsigned line() const;
const_iterator begin() const;
const_iterator end() const;
xml_node & get_child(std::string const& name);
xml_node const& get_child(std::string const& name) const;
xml_node const* get_opt_child(std::string const& name) const;
bool has_child(std::string const& name) const;
template <typename T>
boost::optional<T> get_opt_attr(std::string const& name) const;
template <typename T>
T get_attr(std::string const& name, T const& default_value) const;
template <typename T>
T get_attr(std::string const& name) const;
std::string get_text() const;
template <typename T>
T get_value() const;
private:
xml_tree &tree_;
std::string name_;
std::list<xml_node> children_;
attribute_map attributes_;
bool text_node_;
unsigned line_;
mutable bool processed_;
};
class xml_tree
{
public:
xml_tree();
xml_tree(std::string const& encoding="utf8");
void set_filename(std::string fn);
std::string filename() const;
xml_node &root();
private:
xml_node node_;
std::string file_;
//TODO: Grammars
transcoder tr_;
public:
mapnik::css_color_grammar<std::string::const_iterator> color_grammar;
mapnik::expression_grammar<std::string::const_iterator> expr_grammar;
};
} //ns mapnik

View file

@ -25,11 +25,23 @@
// mapnik
#include <mapnik/color.hpp>
#include <mapnik/color_factory.hpp>
#include <mapnik/config_error.hpp>
// boost
#include <boost/format.hpp>
#include <boost/version.hpp>
// stl
#include <sstream>
// boost 1.41 -> 1.44 compatibility, to be removed in mapnik 2.1 (dane)
#if BOOST_VERSION >= 104500
#include <mapnik/css_color_grammar.hpp>
#else
#include <mapnik/css_color_grammar_deprecated.hpp>
#endif
namespace mapnik {
color::color( std::string const& css_string)
@ -81,5 +93,89 @@ std::string color::to_hex_string() const
}
}
/****************************************************************************/
void color_factory::init_from_string(color & c, std::string const& css_color)
{
typedef std::string::const_iterator iterator_type;
typedef mapnik::css_color_grammar<iterator_type> css_color_grammar;
css_color_grammar g;
iterator_type first = css_color.begin();
iterator_type last = css_color.end();
// boost 1.41 -> 1.44 compatibility, to be removed in mapnik 2.1 (dane)
#if BOOST_VERSION >= 104500
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
c);
if (!result)
{
throw config_error(std::string("Failed to parse color value: ") +
"Expected a CSS color, but got '" + css_color + "'");
}
#else
mapnik::css css_;
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
css_);
if (!result)
{
throw config_error(std::string("Failed to parse color value: ") +
"Expected a CSS color, but got '" + css_color + "'");
}
c.set_red(css_.r);
c.set_green(css_.g);
c.set_blue(css_.b);
c.set_alpha(css_.a);
#endif
}
bool color_factory::parse_from_string(color & c, std::string const& css_color,
mapnik::css_color_grammar<std::string::const_iterator> const& g)
{
std::string::const_iterator first = css_color.begin();
std::string::const_iterator last = css_color.end();
// boost 1.41 -> 1.44 compatibility, to be removed in mapnik 2.1 (dane)
#if BOOST_VERSION >= 104500
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
c);
return result && (first == last);
#else
mapnik::css css_;
bool result =
boost::spirit::qi::phrase_parse(first,
last,
g,
boost::spirit::ascii::space,
css_);
if (result && (first == last))
{
c.set_red(css_.r);
c.set_green(css_.g);
c.set_blue(css_.b);
c.set_alpha(css_.a);
return true;
}
#endif
}
color color_factory::from_string(std::string const& css_color)
{
color c;
init_from_string(c, css_color);
return c;
}
}

View file

@ -24,6 +24,7 @@
#include <mapnik/expression.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/expression_grammar.hpp>
// boost
#include <boost/algorithm/string.hpp>

View file

@ -23,7 +23,7 @@
#include <mapnik/formatting/base.hpp>
#include <mapnik/formatting/list.hpp>
#include <mapnik/formatting/registry.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
// boost
#include <boost/property_tree/ptree.hpp>

View file

@ -27,7 +27,7 @@
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/text_properties.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
// boost

View file

@ -21,7 +21,7 @@
*****************************************************************************/
#include <mapnik/formatting/format.hpp>
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
namespace mapnik {
using boost::property_tree::ptree;

View file

@ -24,7 +24,7 @@
#include <mapnik/formatting/text.hpp>
#include <mapnik/formatting/format.hpp>
#include <mapnik/formatting/expression.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp>
namespace mapnik

View file

@ -26,7 +26,7 @@
#include <mapnik/feature.hpp>
#include <mapnik/text_properties.hpp>
#include <mapnik/processed_text.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
namespace mapnik
{

View file

@ -24,7 +24,7 @@
// mapnik
#include <mapnik/libxml2_loader.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp>
// boost

View file

@ -73,7 +73,6 @@ using boost::lexical_cast;
using boost::bad_lexical_cast;
using boost::tokenizer;
using std::cerr;
using std::endl;
namespace mapnik
@ -86,11 +85,7 @@ public:
strict_( strict ),
filename_( filename ),
relative_to_xml_(true),
font_manager_(font_engine_),
color_grammar_(),
// TODO - use xml encoding?
tr_("utf8"),
expr_grammar_(tr_)
font_manager_(font_engine_)
{}
void parse_map(Map & map, xml_node const& sty, std::string const& base_path="");
@ -117,13 +112,12 @@ private:
void parse_raster_symbolizer(rule & rule, xml_node const& sym );
void parse_markers_symbolizer(rule & rule, xml_node const& sym );
void parse_raster_colorizer(raster_colorizer_ptr const& rc, xml_node const& node );
void parse_raster_colorizer(raster_colorizer_ptr const& rc, xml_node const& node);
void parse_stroke(stroke & strk, xml_node const & sym);
expression_ptr parse_expr(std::string const& expr);
void ensure_font_face( const std::string & face_name );
std::string ensure_relative_to_xml( boost::optional<std::string> opt_path );
std::string ensure_relative_to_xml(boost::optional<std::string> opt_path);
boost::optional<color> get_opt_color_attr(boost::property_tree::ptree const& node,
std::string const& name);
@ -135,16 +129,13 @@ private:
face_manager<freetype_engine> font_manager_;
std::map<std::string,std::string> file_sources_;
std::map<std::string,font_set> fontsets_;
mapnik::css_color_grammar<std::string::const_iterator> color_grammar_;
mapnik::transcoder tr_;
mapnik::expression_grammar<std::string::const_iterator> expr_grammar_;
};
#include <mapnik/internal/dump_xml.hpp>
void load_map(Map & map, std::string const& filename, bool strict)
{
xml_tree tree;
// TODO - use xml encoding?
xml_tree tree("utf8");
tree.set_filename(filename);
#ifdef HAVE_LIBXML2
read_xml2(filename, tree.root());
@ -166,7 +157,8 @@ void load_map(Map & map, std::string const& filename, bool strict)
void load_map_string(Map & map, std::string const& str, bool strict, std::string const& base_path)
{
xml_tree tree;
// TODO - use xml encoding?
xml_tree tree("utf8");
#ifdef HAVE_LIBXML2
if (!base_path.empty())
read_xml2_string(str, tree.root(), base_path); // accept base_path passed into function
@ -190,40 +182,6 @@ void load_map_string(Map & map, std::string const& str, bool strict, std::string
parser.parse_map(map, tree.root(), base_path);
}
expression_ptr map_parser::parse_expr(std::string const& str)
{
expression_ptr expr(boost::make_shared<expr_node>(true));
if (!expression_factory::parse_from_string(expr,str,expr_grammar_))
{
throw mapnik::config_error( "Failed to parse expression '" + str + "'" );
}
return expr;
}
boost::optional<color> map_parser::get_opt_color_attr(boost::property_tree::ptree const& node,
std::string const& name)
{
boost::optional<std::string> str = node.get_optional<std::string>( std::string("<xmlattr>.") + name);
boost::optional<color> result;
if (str && !str->empty())
{
mapnik::color c;
if (mapnik::color_factory::parse_from_string(c,*str,color_grammar_))
{
result.reset(c);
}
else
{
throw config_error(std::string("Failed to parse attribute ") +
name + "'. Expected color" +
" but got '" + *str + "'");
}
}
return result;
}
void map_parser::parse_map(Map & map, xml_node const& pt, std::string const& base_path)
{
try
@ -1383,8 +1341,8 @@ void map_parser::parse_building_symbolizer( rule & rule, xml_node const & sym )
optional<double> opacity = sym.get_opt_attr<double>("fill-opacity");
if (opacity) building_sym.set_opacity(*opacity);
// height
optional<std::string> height = sym.get_opt_attr<std::string>("height");
if (height) building_sym.set_height(parse_expr(*height));
optional<expression_ptr> height = sym.get_opt_attr<expression_ptr>("height");
if (height) building_sym.set_height(*height);
parse_metawriter_in_symbolizer(building_sym, sym);
rule.append(building_sym);

View file

@ -24,7 +24,7 @@
#include <mapnik/metawriter_factory.hpp>
#include <mapnik/metawriter_json.hpp>
#include <mapnik/metawriter_inmem.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/config_error.hpp>

View file

@ -24,6 +24,7 @@
#include <mapnik/svg/svg_parser.hpp>
#include <mapnik/svg/svg_path_parser.hpp>
#include <mapnik/config_error.hpp>
#include "agg_ellipse.h"
#include "agg_rounded_rect.h"

View file

@ -22,7 +22,7 @@
//mapnik
#include <mapnik/text_placements/list.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
//boost
#include <boost/make_shared.hpp>

View file

@ -23,7 +23,7 @@
// mapnik
#include <mapnik/text_placements/simple.hpp>
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
// boost
#include <boost/spirit/include/qi.hpp>

View file

@ -25,7 +25,7 @@
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/expression_string.hpp>
#include <mapnik/formatting/text.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_node.hpp>
#include <mapnik/config_error.hpp>
namespace mapnik

View file

@ -78,13 +78,28 @@ inline boost::optional<std::string> fast_cast(xml_tree const& tree, std::string
template <>
inline boost::optional<color> fast_cast(xml_tree const& tree, std::string const& value)
{
return mapnik::color_factory::from_string(value);
mapnik::color c;
if (mapnik::color_factory::parse_from_string(c, value, tree.color_grammar))
{
return c;
}
else
{
throw config_error("Failed to parse color '"+value+"'");
}
}
template <>
inline boost::optional<expression_ptr> fast_cast(xml_tree const& tree, std::string const& value)
{
return parse_expression(value);
expression_ptr expr(boost::make_shared<expr_node>(true));
if (expression_factory::parse_from_string(expr, value, tree.expr_grammar))
{
return expr;
} else
{
throw mapnik::config_error("Failed to parse expression '" + value + "'");
}
}
/****************************************************************************/
@ -142,8 +157,12 @@ struct name_trait< mapnik::enumeration<ENUM, MAX> >
/****************************************************************************/
xml_tree::xml_tree()
: node_(*this, "<root>")
xml_tree::xml_tree(std::string const& encoding)
: node_(*this, "<root>"),
file_(),
tr_(encoding),
color_grammar(),
expr_grammar(tr_)
{
}
@ -428,6 +447,7 @@ 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_opt_attr(expression_ptr);
compile_get_attr(std::string);
compile_get_attr(filter_mode_e);
compile_get_attr(point_placement_e);

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map>
<Map background-color="green" srs="+proj=latlong +datum=WGS84" minimum-version="10.0.0">
<Map background-color="green" srs="+proj=latlong +datum=WGS84" minimum-version="2.0.0">
<Layer name="layer" srs="+proj=latlong +datum=WGS84">
<StyleName>My Style</StyleName>
@ -9,9 +9,6 @@
<Parameter name="file">points.shp</Parameter>
</Datasource>
</Layer>
<!-- <FontSet>
<Font />
</FontSet>-->
<Style name="My Style">
<Rule>