1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <boost/regex.hpp>
22 #include <metaproxy/xmlutil.hpp>
26 namespace mp_xml = metaproxy_1::xml;
28 std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe)
31 if (recipe.length() == 0)
34 const xmlNode *ptr1 = xmlDocGetRootElement(doc);
35 while (ptr1 && ptr1->type != XML_ELEMENT_NODE)
38 ptr1 = ptr1->children;
43 size_t p1 = recipe.find_first_of("${", p0);
44 if (p1 == std::string::npos)
46 result += recipe.substr(p0);
49 result += recipe.substr(p0, p1 - p0);
52 int step = 0; // 0=variable, 1=pattern, 2=replacement, 3=mode
55 std::string replacement;
58 while (p0 < recipe.length() && step < 5)
74 else if (c_check == ']')
97 if (variable.length())
101 size_t md_pos = variable.find_first_of("md-");
104 const xmlNode *ptr = ptr1;
105 for (; ptr; ptr = ptr->next)
106 if (ptr->type == XML_ELEMENT_NODE
107 && !strcmp((const char *) ptr->name, "metadata"))
109 const _xmlAttr *attr = ptr->properties;
110 for (; attr; attr = attr->next)
111 if (!strcmp((const char *) attr->name, "type")
113 && !strcmp((const char *) attr->children->content,
114 variable.c_str() + offset))
116 text = mp_xml::get_text(ptr);
120 if (pattern.length() == 0)
124 boost::regex::flag_type b_mode = boost::regex::perl;
125 if (mode.find_first_of('i') != std::string::npos)
126 b_mode |= boost::regex::icase;
127 boost::regex e(pattern, b_mode);
128 boost::match_flag_type match_mode = boost::format_first_only;
129 if (mode.find_first_of('g') != std::string::npos)
130 match_mode = boost::format_all;
131 result += regex_replace(text, e, replacement, match_mode);
142 * c-file-style: "Stroustrup"
143 * indent-tabs-mode: nil
145 * vim: shiftwidth=4 tabstop=8 expandtab