metaproxy 1.22.1
url_recipe.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "config.hpp"
20
21#include <yaz/srw.h>
22#include <boost/regex.hpp>
23#include <metaproxy/xmlutil.hpp>
24
25#include <string.h>
26
27namespace mp_xml = metaproxy_1::xml;
28
29std::string mp_xml::url_recipe_handle(xmlDoc *doc, std::string recipe)
30{
31 std::string result;
32 if (recipe.length() == 0)
33 return result;
34
35 const xmlNode *ptr1 = xmlDocGetRootElement(doc);
36 while (ptr1 && ptr1->type != XML_ELEMENT_NODE)
37 ptr1 = ptr1->next;
38 if (ptr1)
39 ptr1 = ptr1->children;
40
41 size_t p0 = 0;
42 while (p0 < recipe.length())
43 {
44 bool uri_encode = false;
45 if (p0 < recipe.length() -1 && recipe[p0] == '%'
46 && recipe[p0 + 1] == '{')
47 {
48 p0++;
49 p0++;
50 uri_encode = true;
51 }
52 else if (p0 < recipe.length() -1 && recipe[p0] == '$'
53 && recipe[p0 + 1] == '{')
54 {
55 p0++;
56 p0++;
57 }
58 else
59 {
60 result += recipe[p0];
61 p0++;
62 continue;
63 }
64 int step = 0; // 0=variable, 1=pattern, 2=replacement, 3=mode
65 std::string variable;
66 std::string pattern;
67 std::string replacement;
68 std::string mode;
69 int c_prev = 0;
70 while (p0 < recipe.length() && step < 5)
71 {
72 char c = recipe[p0];
73 int c_check = c;
74 if (c_prev == '\\')
75 c_check = 0;
76
77 if (c_check == '}')
78 step = 5;
79 else if (step == 0)
80 {
81 if (c_check == '[')
82 step = 1;
83 else
84 variable += c;
85 }
86 else if (c_check == ']')
87 step = 4;
88 else if (step == 1)
89 {
90 if (c_check == '/')
91 step = 2;
92 else
93 pattern += c;
94 }
95 else if (step == 2)
96 {
97 if (c_check == '/')
98 step = 3;
99 else
100 replacement += c;
101 }
102 else if (step == 3)
103 {
104 mode += c;
105 }
106 c_prev = c;
107 p0++;
108 }
109 if (variable.length())
110 {
111 std::string text;
112 size_t offset = 0;
113 size_t md_pos = variable.find_first_of("md-");
114 if (md_pos == 0)
115 offset = 3;
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"))
120 {
121 const _xmlAttr *attr = ptr->properties;
122 for (; attr; attr = attr->next)
123 if (!strcmp((const char *) attr->name, "type")
124 && attr->children
125 && !strcmp((const char *) attr->children->content,
126 variable.c_str() + offset))
127 {
128 text = mp_xml::get_text(ptr);
129 break;
130 }
131 }
132 if (pattern.length() > 0)
133 {
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);
142 }
143 if (!uri_encode)
144 result += text;
145 else
146 {
147 char src[2];
148 char dst[4];
149 size_t i;
150 for (i = 0; i < text.length(); i++)
151 {
152 src[0] = text[i];
153 src[1] = '\0';
154 yaz_encode_uri_component(dst, src);
155 result += dst;
156 }
157 }
158 }
159 }
160 return result;
161}
162
163
164/*
165 * Local variables:
166 * c-basic-offset: 4
167 * c-file-style: "Stroustrup"
168 * indent-tabs-mode: nil
169 * End:
170 * vim: shiftwidth=4 tabstop=8 expandtab
171 */
172