summaryrefslogtreecommitdiffstats
path: root/src/io/stream/xsltstream.cpp
blob: 882db30b2c7f20b5cf268333f76770d752477f37 (plain)
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
245
246
247
248
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * XSL Transforming input and output classes
 *
 * Authors:
 *   Bob Jamison <ishmalius@gmail.com>
 *
 * Copyright (C) 2004-2008 Inkscape.org
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */


#include "xsltstream.h"
#include "stringstream.h"
#include <libxslt/transform.h>




namespace Inkscape
{
namespace IO
{

//#########################################################################
//# X S L T    S T Y L E S H E E T
//#########################################################################

/**
 *
 */
XsltStyleSheet::XsltStyleSheet(InputStream &xsltSource)
              
                   : stylesheet(nullptr)
{
    if (!read(xsltSource)) {
        throw StreamException("read failed");
        }
}

/**
 *
 */
XsltStyleSheet::XsltStyleSheet()
                    : stylesheet(nullptr)
{
}



/**
 *
 */
bool XsltStyleSheet::read(InputStream &xsltSource)
{
    StringOutputStream outs;
    pipeStream(xsltSource, outs);
    std::string strBuf = outs.getString().raw();
    xmlDocPtr doc = xmlParseMemory(strBuf.c_str(), strBuf.size());
    stylesheet = xsltParseStylesheetDoc(doc);
    //following not necessary.  handled by xsltFreeStylesheet(stylesheet);
    //xmlFreeDoc(doc);
    if (!stylesheet)
        return false;
    return true;
}


/**
 *
 */ 
XsltStyleSheet::~XsltStyleSheet()
{
    if (stylesheet)
        xsltFreeStylesheet(stylesheet);
}



//#########################################################################
//# X S L T    I N P U T    S T R E A M
//#########################################################################


/**
 *
 */ 
XsltInputStream::XsltInputStream(InputStream &xmlSource, XsltStyleSheet &sheet)
                        : BasicInputStream(xmlSource), stylesheet(sheet)
{
    //Load the data
    StringOutputStream outs;
    pipeStream(source, outs);
    std::string strBuf = outs.getString().raw();
    
    //Do the processing
    const char *params[1];
    params[0] = nullptr;
    xmlDocPtr srcDoc = xmlParseMemory(strBuf.c_str(), strBuf.size());
    xmlDocPtr resDoc = xsltApplyStylesheet(stylesheet.stylesheet, srcDoc, params);
    xmlDocDumpFormatMemory(resDoc, &outbuf, &outsize, 1);
    outpos = 0;
        
    //Free our mem
    xmlFreeDoc(resDoc);
    xmlFreeDoc(srcDoc);
}

/**
 *
 */ 
XsltInputStream::~XsltInputStream()
{
    xmlFree(outbuf);
}

/**
 * Returns the number of bytes that can be read (or skipped over) from
 * this input stream without blocking by the next caller of a method for
 * this input stream.
 */ 
int XsltInputStream::available()
{
    return outsize - outpos;
}

    
/**
 *  Closes this input stream and releases any system resources
 *  associated with the stream.
 */ 
void XsltInputStream::close()
{
    closed = true;
}
    
/**
 * Reads the next byte of data from the input stream.  -1 if EOF
 */ 
int XsltInputStream::get()
{
    if (closed)
        return -1;
    if (outpos >= outsize)
        return -1;
    int ch = (int) outbuf[outpos++];
    return ch;
}
   





//#########################################################################
//#  X S L T     O U T P U T    S T R E A M
//#########################################################################

/**
 *
 */ 
XsltOutputStream::XsltOutputStream(OutputStream &dest, XsltStyleSheet &sheet)
                     : BasicOutputStream(dest), stylesheet(sheet)
{
    flushed = false;
}

/**
 *
 */ 
XsltOutputStream::~XsltOutputStream()
{
    //do not automatically close
}

/**
 * Closes this output stream and releases any system resources
 * associated with this stream.
 */ 
void XsltOutputStream::close()
{
    flush();
    destination.close();
}
    
/**
 *  Flushes this output stream and forces any buffered output
 *  bytes to be written out.
 */ 
void XsltOutputStream::flush()
{
    if (flushed)
        {
        destination.flush();
        return;
        }
        
    //Do the processing
    xmlChar *resbuf;
    int resSize;
    const char *params[1];
    params[0] = nullptr;
    xmlDocPtr srcDoc = xmlParseMemory(outbuf.raw().c_str(), outbuf.size());
    xmlDocPtr resDoc = xsltApplyStylesheet(stylesheet.stylesheet, srcDoc, params);
    xmlDocDumpFormatMemory(resDoc, &resbuf, &resSize, 1);
    /*
    xmlErrorPtr err = xmlGetLastError();
    if (err)
        {
        throw StreamException(err->message);
        }
    */

    for (int i=0 ; i<resSize ; i++)
        {
        char ch = resbuf[i];
        destination.put(ch);
        }
        
    //Free our mem
    xmlFree(resbuf);
    xmlFreeDoc(resDoc);
    xmlFreeDoc(srcDoc);
    destination.flush();
    flushed = true;
}
    
/**
 * Writes the specified byte to this output stream.
 */ 
int XsltOutputStream::put(char ch)
{
    outbuf.push_back(ch);
	return 1;
}





} // namespace IO
} // namespace Inkscape


//#########################################################################
//# E N D    O F    F I L E
//#########################################################################