1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/** \file
* Code for handling XSLT extensions.
*/
/*
* Authors:
* Ted Gould <ted@gould.cx>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 2006-2007 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glibmm/fileutils.h>
#include "file.h"
#include "xslt.h"
#include "../extension.h"
#include "../output.h"
#include "extension/input.h"
#include "io/resource.h"
#include <unistd.h>
#include <cstring>
#include "document.h"
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
Inkscape::XML::Document * sp_repr_do_read (xmlDocPtr doc, const gchar * default_ns);
/* Namespaces */
namespace Inkscape {
namespace Extension {
namespace Implementation {
/* Real functions */
/**
\return A XSLT object
\brief This function creates a XSLT object and sets up the
variables.
*/
XSLT::XSLT() :
Implementation(),
_filename(""),
_parsedDoc(nullptr),
_stylesheet(nullptr)
{
}
Glib::ustring XSLT::solve_reldir(Inkscape::XML::Node *reprin) {
gchar const *s = reprin->attribute("reldir");
if (!s) {
Glib::ustring str = reprin->firstChild()->content();
return str;
}
Glib::ustring reldir = s;
if (reldir == "extensions") {
using namespace Inkscape::IO::Resource;
return get_filename(EXTENSIONS, reprin->firstChild()->content());
} else {
Glib::ustring str = reprin->firstChild()->content();
return str;
}
}
bool XSLT::check(Inkscape::Extension::Extension *module)
{
if (load(module)) {
unload(module);
return true;
} else {
return false;
}
}
bool XSLT::load(Inkscape::Extension::Extension *module)
{
if (module->loaded()) { return true; }
Inkscape::XML::Node *child_repr = module->get_repr()->firstChild();
while (child_repr != nullptr) {
if (!strcmp(child_repr->name(), INKSCAPE_EXTENSION_NS "xslt")) {
child_repr = child_repr->firstChild();
while (child_repr != nullptr) {
if (!strcmp(child_repr->name(), INKSCAPE_EXTENSION_NS "file")) {
_filename = solve_reldir(child_repr);
}
child_repr = child_repr->next();
}
break;
}
child_repr = child_repr->next();
}
_parsedDoc = xmlParseFile(_filename.c_str());
if (_parsedDoc == nullptr) { return false; }
_stylesheet = xsltParseStylesheetDoc(_parsedDoc);
return true;
}
void XSLT::unload(Inkscape::Extension::Extension *module)
{
if (!module->loaded()) { return; }
xsltFreeStylesheet(_stylesheet);
// No need to use xmlfreedoc(_parsedDoc), it's handled by xsltFreeStylesheet(_stylesheet);
return;
}
SPDocument * XSLT::open(Inkscape::Extension::Input */*module*/,
gchar const *filename)
{
xmlDocPtr filein = xmlParseFile(filename);
if (filein == nullptr) { return nullptr; }
const char * params[1];
params[0] = nullptr;
xmlDocPtr result = xsltApplyStylesheet(_stylesheet, filein, params);
xmlFreeDoc(filein);
Inkscape::XML::Document * rdoc = sp_repr_do_read( result, SP_SVG_NS_URI);
xmlFreeDoc(result);
if (rdoc == nullptr) {
return nullptr;
}
if (strcmp(rdoc->root()->name(), "svg:svg") != 0) {
return nullptr;
}
gchar * base = nullptr;
gchar * name = nullptr;
gchar * s = nullptr, * p = nullptr;
s = g_strdup(filename);
p = strrchr(s, '/');
if (p) {
name = g_strdup(p + 1);
p[1] = '\0';
base = g_strdup(s);
} else {
base = nullptr;
name = g_strdup(filename);
}
g_free(s);
SPDocument * doc = SPDocument::createDoc(rdoc, filename, base, name, true, nullptr);
g_free(base); g_free(name);
return doc;
}
void XSLT::save(Inkscape::Extension::Output *module, SPDocument *doc, gchar const *filename)
{
/* TODO: Should we assume filename to be in utf8 or to be a raw filename?
* See JavaFXOutput::save for discussion. */
g_return_if_fail(doc != nullptr);
g_return_if_fail(filename != nullptr);
Inkscape::XML::Node *repr = doc->getReprRoot();
std::string tempfilename_out;
int tempfd_out = 0;
try {
tempfd_out = Glib::file_open_tmp(tempfilename_out, "ink_ext_XXXXXX");
} catch (...) {
/// \todo Popup dialog here
return;
}
if (!sp_repr_save_rebased_file(repr->document(), tempfilename_out.c_str(), SP_SVG_NS_URI,
doc->getBase(), filename)) {
throw Inkscape::Extension::Output::save_failed();
}
xmlDocPtr svgdoc = xmlParseFile(tempfilename_out.c_str());
close(tempfd_out);
if (svgdoc == nullptr) {
return;
}
std::list<std::string> params;
module->paramListString(params);
const int max_parameters = params.size() * 2;
const char * xslt_params[max_parameters+1] ;
int count = 0;
for(std::list<std::string>::iterator t=params.begin(); t != params.end(); ++t) {
std::size_t pos = t->find("=");
std::ostringstream parameter;
std::ostringstream value;
parameter << t->substr(2,pos-2);
value << t->substr(pos+1);
xslt_params[count++] = g_strdup_printf("%s", parameter.str().c_str());
xslt_params[count++] = g_strdup_printf("'%s'", value.str().c_str());
}
xslt_params[count] = nullptr;
xmlDocPtr newdoc = xsltApplyStylesheet(_stylesheet, svgdoc, xslt_params);
//xmlSaveFile(filename, newdoc);
int success = xsltSaveResultToFilename(filename, newdoc, _stylesheet, 0);
xmlFreeDoc(newdoc);
xmlFreeDoc(svgdoc);
xsltCleanupGlobals();
xmlCleanupParser();
if (success < 1) {
throw Inkscape::Extension::Output::save_failed();
}
return;
}
} /* Implementation */
} /* module */
} /* Inkscape */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|