1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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
22 #include <boost/regex.hpp>
23 #include <metaproxy/xmlutil.hpp>
27 namespace mp_xml = metaproxy_1::xml;
29 std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe)
32 if (recipe.length() == 0)
35 const xmlNode *ptr1 = xmlDocGetRootElement(doc);
36 while (ptr1 && ptr1->type != XML_ELEMENT_NODE)
39 ptr1 = ptr1->children;
42 while (p0 < recipe.length())
44 bool uri_encode = false;
45 if (p0 < recipe.length() -1 && recipe[p0] == '%'
46 && recipe[p0 + 1] == '{')
52 else if (p0 < recipe.length() -1 && recipe[p0] == '$'
53 && recipe[p0 + 1] == '{')
64 int step = 0; // 0=variable, 1=pattern, 2=replacement, 3=mode
67 std::string replacement;
70 while (p0 < recipe.length() && step < 5)
86 else if (c_check == ']')
109 if (variable.length())
113 size_t md_pos = variable.find_first_of("md-");
116 const xmlNode *ptr = ptr1;
117 for (; ptr; ptr = ptr->next)
118 if (ptr->type == XML_ELEMENT_NODE
119 && !strcmp((const char *) ptr->name, "metadata"))
121 const _xmlAttr *attr = ptr->properties;
122 for (; attr; attr = attr->next)
123 if (!strcmp((const char *) attr->name, "type")
125 && !strcmp((const char *) attr->children->content,
126 variable.c_str() + offset))
128 text = mp_xml::get_text(ptr);
132 if (pattern.length() > 0)
134 boost::regex::flag_type b_mode = boost::regex::perl;
135 if (mode.find_first_of('i') != std::string::npos)
136 b_mode |= boost::regex::icase;
137 boost::regex e(pattern, b_mode);
138 boost::match_flag_type match_mode = boost::format_first_only;
139 if (mode.find_first_of('g') != std::string::npos)
140 match_mode = boost::format_all;
141 text = regex_replace(text, e, replacement, match_mode);
150 for (i = 0; i < text.length(); i++)
154 yaz_encode_uri_component(dst, src);
167 * c-file-style: "Stroustrup"
168 * indent-tabs-mode: nil
170 * vim: shiftwidth=4 tabstop=8 expandtab