]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/gdome_xslt/C++/gdome_xslt/GdomeSmartDOMXSLTStylesheet.cc
ocaml 3.09 transition
[helm.git] / helm / DEVEL / gdome_xslt / C++ / gdome_xslt / GdomeSmartDOMXSLTStylesheet.cc
1 // This file is part of a XSLT engine working on Gdome documents. In fact,
2 // it just maps Gdome documents to libxml documents back and forth, and
3 // applies the transformation on libxml documents using libxlt.
4 // 
5 // Copyright (C) 2002: Luca Padovani <lpadovan@cs.unibo.it>
6 // 
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Lesser General Public License for more details.
16 // 
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 //
21 // For more information, please send an email to lpadovan@cs.unibo.it
22
23 #include "config.h"
24
25 #include <string>
26 #include <cassert>
27
28 #include <stdio.h>
29 #include <GdomeSmartDOM.hh>
30
31 #include "GdomeSmartDOMXSLTStylesheet.hh"
32
33 namespace GdomeSmartDOMExt {
34
35   XSLTStylesheet::XSLTStylesheet(const Document& doc)
36   {
37     assert(doc);
38     GdomeDocument* _doc = gdome_cast_doc(doc.gdome_object());
39     assert(_doc);
40     stylesheet = processStylesheet(_doc);
41     GdomeException _exc = 0;
42     gdome_doc_unref(_doc, &_exc);
43     assert(_exc == 0);
44   }
45
46   XSLTStylesheet::~XSLTStylesheet()
47   {
48     if (stylesheet)
49       {
50         xsltFreeStylesheet(stylesheet);
51         stylesheet = 0;
52       }
53   }
54
55   Document
56   XSLTStylesheet::apply(const Document& source) const
57   {
58     std::vector< std::pair<GdomeString,GdomeString> > noParams;
59     return apply(source, noParams);
60   }
61
62   Document
63   XSLTStylesheet::apply(const Document& source, const std::vector< std::pair<GdomeString,GdomeString> >& params) const
64   {
65     assert(source);
66
67     char** _params = (char**) malloc(sizeof(char*) * (2 * params.size() + 1));
68     for (unsigned i = 0; i < params.size(); i++)
69       {
70         std::string param = params[i].first;
71         _params[2 * i] = strdup(param.c_str());
72         std::string value = params[i].second;
73         _params[2 * i + 1] = strdup(value.c_str());
74       }
75     _params[2 * params.size()] = 0;
76
77     GdomeDocument* _source = gdome_cast_doc(source.gdome_object());
78     assert(_source);
79     GdomeDocument* _result = applyStylesheet(_source, stylesheet, const_cast<const char**>(_params));
80
81     GdomeException _exc = 0;
82     gdome_doc_unref(_source, &_exc);
83     assert(_exc == 0);
84
85     for (unsigned i = 0; i < 2 * params.size(); i++) free(_params[i]);
86     free(_params);
87
88     Document result(_result);
89     gdome_doc_unref(_result, &_exc);
90     assert(_exc == 0);
91
92     return result;
93   }
94
95   void
96   XSLTStylesheet::save(const Document& doc, const std::string& filename) const
97   {
98     assert(doc);
99     GdomeException _exc = 0;
100     GdomeDocument* _doc = gdome_cast_doc(doc.gdome_object());
101     assert(_doc);
102     if (saveResultToFilename(filename.c_str(), _doc, stylesheet, 0) < 0) throw SaveException();
103     gdome_doc_unref(_doc, &_exc);
104     assert(_exc == 0);
105   }
106
107   void
108   XSLTStylesheet::save(const Document& doc, FILE* f) const
109   {
110     assert(doc);
111     GdomeException _exc = 0;
112     GdomeDocument* _doc = gdome_cast_doc(doc.gdome_object());
113     assert(_doc);
114     if (saveResultToFile(f, _doc, stylesheet) < 0) throw SaveException();
115     gdome_doc_unref(_doc, &_exc);
116     assert(_exc == 0);
117   }
118
119   void
120   XSLTStylesheet::save(const Document& doc, int fd) const
121   {
122     assert(doc);
123     GdomeException _exc = 0;
124     GdomeDocument* _doc = gdome_cast_doc(doc.gdome_object());
125     assert(_doc);
126     if (saveResultToFd(fd, _doc, stylesheet) < 0) throw SaveException();
127     gdome_doc_unref(_doc, &_exc);
128     assert(_exc == 0);
129   }
130
131 }