Happy new year
[metaproxy-moved-to-github.git] / src / url_recipe.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20
21 #include <boost/regex.hpp>
22 #include <metaproxy/xmlutil.hpp>
23
24 #include <string.h>
25
26 namespace mp_xml = metaproxy_1::xml;
27
28 std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe)
29 {
30     std::string result;
31     if (recipe.length() == 0)
32         return result;
33
34     const xmlNode *ptr1 = xmlDocGetRootElement(doc);
35     while (ptr1 && ptr1->type != XML_ELEMENT_NODE)
36         ptr1 = ptr1->next;
37     if (ptr1)
38         ptr1 = ptr1->children;
39
40     size_t p0 = 0;
41     for (;;)
42     {
43         size_t p1 = recipe.find_first_of("${", p0);
44         if (p1 == std::string::npos)
45         {
46             result += recipe.substr(p0);
47             break;
48         }
49         result += recipe.substr(p0, p1 - p0);
50         p0 = p1+2;
51
52         int step = 0;  // 0=variable, 1=pattern, 2=replacement, 3=mode
53         std::string variable;
54         std::string pattern;
55         std::string replacement;
56         std::string mode;
57         int c_prev = 0;
58         while (p0 < recipe.length() && step < 5)
59         {
60             char c = recipe[p0];
61             int c_check = c;
62             if (c_prev == '\\')
63                 c_check = 0;
64             
65             if (c_check == '}')
66                 step = 5;
67             else if (step == 0)
68             {
69                 if (c_check == '[')
70                     step = 1;
71                 else
72                     variable += c;
73             }
74             else if (c_check == ']')
75                 step = 4;
76             else if (step == 1)
77             {
78                 if (c_check == '/')
79                     step = 2;
80                 else
81                     pattern += c;
82             }
83             else if (step == 2)
84             {
85                 if (c_check == '/')
86                     step = 3;
87                 else
88                     replacement += c;
89             }
90             else if (step == 3)
91             {
92                 mode += c;
93             }
94             c_prev = c;
95             p0++;
96         }
97         if (variable.length())
98         {
99             std::string text;
100             size_t offset = 0;
101             size_t md_pos = variable.find_first_of("md-");
102             if (md_pos == 0)
103                 offset = 3;
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"))
108                 {
109                     const _xmlAttr *attr = ptr->properties;
110                     for (; attr; attr = attr->next)
111                         if (!strcmp((const char *) attr->name, "type")
112                             && attr->children
113                             && !strcmp((const char *) attr->children->content,
114                                        variable.c_str() + offset))
115                         {
116                             text = mp_xml::get_text(ptr);
117                             break;
118                         }
119                 }
120             if (pattern.length() == 0)
121                 result += text;
122             else
123             {
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);
132             }
133         }
134     }
135     return result;
136 }
137
138
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * c-file-style: "Stroustrup"
143  * indent-tabs-mode: nil
144  * End:
145  * vim: shiftwidth=4 tabstop=8 expandtab
146  */
147