diff --git a/include/mapnik/config_error.hpp b/include/mapnik/config_error.hpp index 1b1e8fefa..a889bc39e 100644 --- a/include/mapnik/config_error.hpp +++ b/include/mapnik/config_error.hpp @@ -32,16 +32,16 @@ class xml_node; class config_error : public std::exception { public: - config_error() {} - - config_error(std::string const& what, xml_node const* node = 0, std::string const& filename=""); - config_error(unsigned line_number, std::string const& filename, std::string const& what); + config_error(std::string const& what); + config_error(std::string const& what, xml_node const& node); + config_error(std::string const& what, unsigned line_number, std::string const& filename); virtual ~config_error() throw() {} virtual const char * what() const throw(); - void append_context(const std::string & ctx, xml_node const* node = 0, std::string const& filename="") const; - + void append_context(const std::string & ctx) const; + void append_context(const std::string & ctx, xml_node const& node) const; + void append_context(xml_node const& node) const; protected: mutable std::string what_; mutable unsigned line_number_; diff --git a/include/mapnik/palette.hpp b/include/mapnik/palette.hpp index de58bbe10..a0db170bf 100644 --- a/include/mapnik/palette.hpp +++ b/include/mapnik/palette.hpp @@ -26,7 +26,6 @@ // mapnik #include #include -#include // boost #include diff --git a/include/mapnik/xml_node.hpp b/include/mapnik/xml_node.hpp index 3047bd9dd..f0db516e1 100644 --- a/include/mapnik/xml_node.hpp +++ b/include/mapnik/xml_node.hpp @@ -86,8 +86,9 @@ public: typedef std::map 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; + std::string const& name() const; + std::string const& text() const; + std::string const& filename() const; bool is_text() const; bool is(std::string const& name) const; @@ -128,7 +129,7 @@ private: bool text_node_; unsigned line_; mutable bool processed_; - + static std::string xml_text; }; } //ns mapnik diff --git a/include/mapnik/xml_tree.hpp b/include/mapnik/xml_tree.hpp index 4a0491223..a27a7e78a 100644 --- a/include/mapnik/xml_tree.hpp +++ b/include/mapnik/xml_tree.hpp @@ -38,7 +38,7 @@ class xml_tree public: xml_tree(std::string const& encoding="utf8"); void set_filename(std::string fn); - std::string filename() const; + std::string const& filename() const; xml_node &root(); private: xml_node node_; diff --git a/src/config_error.cpp b/src/config_error.cpp index 2adc487fb..21ab1ac63 100644 --- a/src/config_error.cpp +++ b/src/config_error.cpp @@ -3,19 +3,21 @@ namespace mapnik { -config_error::config_error(std::string const& what, xml_node const* node, std::string const& filename) - : what_( what ), line_number_(0), file_(filename), node_name_(), msg_() + +config_error::config_error(std::string const& what) + : what_(what), line_number_(0), file_(), node_name_(), msg_() { - if (node) - { - node_name_ = node->name(); - line_number_ = node->line(); - } } -config_error::config_error(unsigned line_number, std::string const& filename, std::string const& what) - : what_( what ), line_number_(line_number), file_(filename), node_name_(), msg_() +config_error::config_error(std::string const& what, xml_node const& node) + : what_(what), line_number_(node.line()), file_(node.filename()), node_name_(node.name()), msg_() +{ +} + + +config_error::config_error(std::string const& what, unsigned line_number, std::string const& filename) + : what_(what), line_number_(line_number), file_(filename), node_name_(), msg_() { } @@ -32,14 +34,22 @@ config_error::config_error(unsigned line_number, std::string const& filename, st return msg_.c_str(); } -void config_error::append_context(const std::string & ctx, xml_node const* node, std::string const& filename) const + void config_error::append_context(std::string const& ctx) const + { + what_ += " " + ctx; + } + +void config_error::append_context(std::string const& ctx, xml_node const& node) const { - what_ += " " + ctx; - if (node) - { - if (!line_number_) line_number_ = node->line(); - if (node_name_.empty()) node_name_ = node->name(); - if (file_.empty()) file_ = filename; - } + append_context(ctx); + append_context(node); } + +void config_error::append_context(xml_node const& node) const +{ + if (!line_number_) line_number_ = node.line(); + if (node_name_.empty()) node_name_ = node.name(); + if (file_.empty()) file_ = node.filename(); +} + } diff --git a/src/formatting/registry.cpp b/src/formatting/registry.cpp index cf77de4c5..bccac964a 100644 --- a/src/formatting/registry.cpp +++ b/src/formatting/registry.cpp @@ -51,7 +51,7 @@ void registry::register_name(std::string name, from_xml_function_ptr ptr, bool o node_ptr registry::from_xml(xml_node const& xml) { std::map::const_iterator itr = map_.find(xml.name()); - if (itr == map_.end()) throw config_error("Unknown element '" + xml.name() + "'", &xml); + if (itr == map_.end()) throw config_error("Unknown element '" + xml.name() + "'", xml); return itr->second(xml); } } //ns formatting diff --git a/src/libxml2_loader.cpp b/src/libxml2_loader.cpp index ffc2d6bfa..5472d29c0 100644 --- a/src/libxml2_loader.cpp +++ b/src/libxml2_loader.cpp @@ -92,7 +92,7 @@ public: os << ": " << std::endl << error->message; // remove CR std::string msg = os.str().substr(0, os.str().size() - 1); - throw config_error(error->line, error->file, msg); + throw config_error(msg, error->line, error->file); } } @@ -139,7 +139,7 @@ public: { os << ": " << std::endl << error->message; } - throw config_error(error->line, error->file, os.str()); + throw config_error(os.str(), error->line, error->file); } int iXIncludeReturn = xmlXIncludeProcessFlags(doc, options_); diff --git a/src/load_map.cpp b/src/load_map.cpp index 1fed54e94..cae90d5a5 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -51,6 +51,7 @@ #include #include #include +#include // boost #include @@ -312,7 +313,7 @@ void map_parser::parse_map(Map & map, xml_node const& pt, std::string const& bas } catch (const config_error & ex) { - ex.append_context("", &map_node, filename_); + ex.append_context(map_node); throw; } @@ -416,7 +417,7 @@ void map_parser::parse_map_include(Map & map, xml_node const& include) } } } catch (const config_error & ex) { - ex.append_context("", &include, filename_); + ex.append_context(include); throw; } @@ -447,7 +448,7 @@ void map_parser::parse_style(Map & map, xml_node const& sty) map.insert_style(name, style); } catch (const config_error & ex) { - ex.append_context(std::string("in style '") + name + "'", &sty, filename_); + ex.append_context(std::string("in style '") + name + "'", sty); throw; } } @@ -462,7 +463,7 @@ void map_parser::parse_metawriter(Map & map, xml_node const& pt) writer = metawriter_create(pt); map.insert_metawriter(name, writer); } catch (const config_error & ex) { - ex.append_context(std::string("in meta writer '") + name + "'", &pt, filename_); + ex.append_context(std::string("in meta writer '") + name + "'", pt); } } @@ -491,7 +492,7 @@ void map_parser::parse_fontset(Map & map, xml_node const& fset) // when it's parsed fontsets_.insert(pair(name, fontset)); } catch (const config_error & ex) { - ex.append_context(std::string("in FontSet '") + name + "'", &fset, filename_); + ex.append_context(std::string("in FontSet '") + name + "'", fset); throw; } } @@ -509,7 +510,7 @@ void map_parser::parse_font(font_set &fset, xml_node const& f) } else { - throw config_error("Must have 'face-name' set", &f, filename_); + throw config_error("Must have 'face-name' set", f); } } @@ -652,7 +653,7 @@ void map_parser::parse_layer(Map & map, xml_node const& lay) { if (!name.empty()) { - ex.append_context(std::string(" encountered during parsing of layer '") + name + "'"); + ex.append_context(std::string(" encountered during parsing of layer '") + name + "'", lay); } throw; } @@ -748,7 +749,7 @@ void map_parser::parse_rule(feature_type_style & style, xml_node const& r) { if (!name.empty() ) { - ex.append_context(std::string("in rule '") + name + "'"); + ex.append_context(std::string("in rule '") + name + "'", r); } throw; } @@ -844,7 +845,7 @@ void map_parser::parse_point_symbolizer(rule & rule, xml_node const & sym) } catch (const config_error & ex) { - ex.append_context("in PointSymbolizer"); + ex.append_context("in PointSymbolizer", sym); throw; } } @@ -958,7 +959,7 @@ void map_parser::parse_markers_symbolizer(rule & rule, xml_node const& sym) } catch (const config_error & ex) { - ex.append_context("in MarkersSymbolizer"); + ex.append_context("in MarkersSymbolizer", sym); throw; } } @@ -1004,7 +1005,7 @@ void map_parser::parse_line_pattern_symbolizer( rule & rule, xml_node const & sy } catch (const config_error & ex) { - ex.append_context("in LinePatternSymbolizer"); + ex.append_context("in LinePatternSymbolizer", sym); throw; } } @@ -1063,7 +1064,7 @@ void map_parser::parse_polygon_pattern_symbolizer( rule & rule, } catch (const config_error & ex) { - ex.append_context("in PolygonPatternSymbolizer"); + ex.append_context("in PolygonPatternSymbolizer", sym); throw; } } @@ -1090,7 +1091,7 @@ void map_parser::parse_text_symbolizer( rule & rule, xml_node const& sym ) } catch (const config_error & ex) { - ex.append_context("in TextSymbolizer"); + ex.append_context("in TextSymbolizer", sym); throw; } } @@ -1195,7 +1196,7 @@ void map_parser::parse_shield_symbolizer(rule & rule, xml_node const& sym ) } catch (const config_error & ex) { - ex.append_context("in ShieldSymbolizer"); + ex.append_context("in ShieldSymbolizer", sym); throw; } } @@ -1293,7 +1294,7 @@ void map_parser::parse_line_symbolizer( rule & rule, xml_node const & sym ) } catch (const config_error & ex) { - ex.append_context("in LineSymbolizer"); + ex.append_context("in LineSymbolizer", sym); throw; } } @@ -1322,7 +1323,7 @@ void map_parser::parse_polygon_symbolizer( rule & rule, xml_node const & sym ) } catch (const config_error & ex) { - ex.append_context("in PolygonSymbolizer"); + ex.append_context("in PolygonSymbolizer", sym); throw; } } @@ -1349,7 +1350,7 @@ void map_parser::parse_building_symbolizer( rule & rule, xml_node const & sym ) } catch (const config_error & ex) { - ex.append_context("in BuildingSymbolizer"); + ex.append_context("in BuildingSymbolizer", sym); throw; } } @@ -1398,13 +1399,13 @@ void map_parser::parse_raster_symbolizer( rule & rule, xml_node const & sym ) } catch (const config_error & ex) { - ex.append_context("in RasterSymbolizer"); + ex.append_context("in RasterSymbolizer", sym); throw; } } void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc, - xml_node const& node ) + xml_node const& node) { try { @@ -1484,7 +1485,7 @@ void map_parser::parse_raster_colorizer(raster_colorizer_ptr const& rc, } catch (const config_error & ex) { - ex.append_context("in RasterColorizer"); + ex.append_context("in RasterColorizer", node); throw; } } diff --git a/src/metawriter_factory.cpp b/src/metawriter_factory.cpp index ae1900e91..378cd0fd4 100644 --- a/src/metawriter_factory.cpp +++ b/src/metawriter_factory.cpp @@ -61,7 +61,7 @@ metawriter_create(xml_node const& pt) metawriter_inmem_ptr inmem = metawriter_inmem_ptr(new metawriter_inmem(properties)); writer = inmem; } else { - throw config_error(string("Unknown type '") + type + "'", &pt); + throw config_error(string("Unknown type '") + type + "'", pt); } return writer; diff --git a/src/palette.cpp b/src/palette.cpp index 29d4120d4..6125d8808 100644 --- a/src/palette.cpp +++ b/src/palette.cpp @@ -21,6 +21,7 @@ *****************************************************************************/ #include +#include namespace mapnik { diff --git a/src/text_placements/registry.cpp b/src/text_placements/registry.cpp index af6751665..321f65d18 100644 --- a/src/text_placements/registry.cpp +++ b/src/text_placements/registry.cpp @@ -48,7 +48,7 @@ void registry::register_name(std::string name, from_xml_function_ptr ptr, bool o text_placements_ptr registry::from_xml(std::string name, xml_node const& xml, fontset_map const& fontsets) { std::map::const_iterator itr = map_.find(name); - if (itr == map_.end()) throw config_error("Unknown placement-type '" + name + "'", &xml); + if (itr == map_.end()) throw config_error("Unknown placement-type '" + name + "'", xml); return itr->second(xml, fontsets); } } //ns formatting diff --git a/src/text_properties.cpp b/src/text_properties.cpp index dcb8b7b94..ea68d2d8b 100644 --- a/src/text_properties.cpp +++ b/src/text_properties.cpp @@ -267,16 +267,16 @@ void char_properties::from_xml(xml_node const& sym, fontset_map const& fontsets) fontset = itr->second; } else { - throw config_error("Unable to find any fontset named '" + *fontset_name_ + "'", &sym); + throw config_error("Unable to find any fontset named '" + *fontset_name_ + "'", sym); } } if (!face_name.empty() && !fontset.get_name().empty()) { - throw config_error("Can't have both face-name and fontset-name", &sym); + throw config_error("Can't have both face-name and fontset-name", sym); } if (face_name.empty() && fontset.get_name().empty()) { - throw config_error("Must have face-name or fontset-name", &sym); + throw config_error("Must have face-name or fontset-name", sym); } } diff --git a/src/xml_tree.cpp b/src/xml_tree.cpp index 4928760a3..9cbba8d83 100644 --- a/src/xml_tree.cpp +++ b/src/xml_tree.cpp @@ -29,6 +29,7 @@ #include #include #include +#include //boost #include @@ -171,7 +172,7 @@ void xml_tree::set_filename(std::string fn) file_ = fn; } -std::string xml_tree::filename() const +std::string const& xml_tree::filename() const { return file_; } @@ -255,20 +256,27 @@ xml_node::xml_node(xml_tree &tree, std::string name, unsigned line, bool text_no } -std::string xml_node::name() const +std::string xml_node::xml_text = ""; + +std::string const& xml_node::name() const { if (!text_node_) return name_; else - return ""; + return xml_text; } -std::string xml_node::text() const +std::string const& xml_node::text() const { if (text_node_) return name_; else - return "NOT A TEXT NODE"; //TODO: throw + throw config_error("text() called on non-text node", *this); +} + +std::string const& xml_node::filename() const +{ + return tree_.filename(); } bool xml_node::is_text() const @@ -416,9 +424,9 @@ T xml_node::get_value() const boost::optional result = fast_cast(tree_, get_text()); if (!result) { - throw config_error(std::string("Failed to parse value in node '") + - name_ + "'. Expected " + name_trait::name() + - " but got '" + get_text() + "'"); + throw config_error(std::string("Failed to parse value. Expected ") + + name_trait::name() + + " but got '" + get_text() + "'", *this); } return *result; }