Merge lp:~adrianboguszewski/inkscape/sax-parser into lp:~inkscape.dev/inkscape/trunk
- sax-parser
- Merge into trunk
Proposed by
Adrian Boguszewski
Status: | Needs review |
---|---|
Proposed branch: | lp:~adrianboguszewski/inkscape/sax-parser |
Merge into: | lp:~inkscape.dev/inkscape/trunk |
Diff against target: |
6517 lines (+4413/-1153) 67 files modified
CMakeLists.txt (+1/-0) CMakeScripts/ConfigCompileFlags.cmake (+1/-1) src/CMakeLists.txt (+2/-0) src/document.cpp (+3/-2) src/extension/implementation/xslt.cpp (+8/-5) src/extension/internal/filter/filter-file.cpp (+3/-2) src/extension/internal/filter/filter.cpp (+2/-1) src/extension/internal/svg.cpp (+4/-4) src/extension/loader.h (+2/-1) src/extension/prefdialog.cpp (+2/-1) src/extension/system.cpp (+3/-2) src/inkscape.cpp (+11/-15) src/libxml++/CMakeLists.txt (+34/-0) src/libxml++/README (+8/-0) src/libxml++/document.cc (+126/-0) src/libxml++/document.h (+139/-0) src/libxml++/dtd.cc (+147/-0) src/libxml++/dtd.h (+136/-0) src/libxml++/exceptions/exception.cc (+125/-0) src/libxml++/exceptions/exception.h (+99/-0) src/libxml++/exceptions/internal_error.cc (+23/-0) src/libxml++/exceptions/internal_error.h (+39/-0) src/libxml++/exceptions/parse_error.cc (+23/-0) src/libxml++/exceptions/parse_error.h (+42/-0) src/libxml++/exceptions/validity_error.cc (+23/-0) src/libxml++/exceptions/validity_error.h (+42/-0) src/libxml++/exceptions/wrapped_exception.cc (+46/-0) src/libxml++/exceptions/wrapped_exception.h (+56/-0) src/libxml++/io/istreamparserinputbuffer.cc (+44/-0) src/libxml++/io/istreamparserinputbuffer.h (+36/-0) src/libxml++/io/parserinputbuffer.cc (+84/-0) src/libxml++/io/parserinputbuffer.h (+75/-0) src/libxml++/keepblanks.cc (+29/-0) src/libxml++/keepblanks.h (+35/-0) src/libxml++/libxml++config.h (+26/-0) src/libxml++/noncopyable.cc (+21/-0) src/libxml++/noncopyable.h (+31/-0) src/libxml++/parsers/parser.cc (+365/-0) src/libxml++/parsers/parser.h (+220/-0) src/libxml++/parsers/saxparser.cc (+711/-0) src/libxml++/parsers/saxparser.h (+261/-0) src/preferences.cpp (+5/-3) src/shortcuts.cpp (+16/-16) src/ui/dialog/template-load-tab.cpp (+2/-1) src/ui/interface.cpp (+2/-1) src/xml/CMakeLists.txt (+3/-1) src/xml/comment-node.h (+9/-0) src/xml/node.h (+27/-4) src/xml/pi-node.h (+6/-0) src/xml/repr-io.cpp (+169/-1060) src/xml/repr-io.h (+45/-0) src/xml/repr-util.cpp (+7/-2) src/xml/repr.h (+0/-25) src/xml/simple-document.cpp (+12/-0) src/xml/simple-document.h (+2/-0) src/xml/simple-node.cpp (+89/-0) src/xml/simple-node.h (+25/-3) src/xml/svg-parser.cpp (+266/-0) src/xml/svg-parser.h (+85/-0) src/xml/text-node.h (+15/-0) testfiles/CMakeLists.txt (+5/-1) testfiles/doc-per-case-test.cpp (+4/-0) testfiles/src/object-set-test.cpp (+1/-1) testfiles/src/repr-io-test.cpp (+73/-0) testfiles/src/sp-object-test.cpp (+1/-1) testfiles/src/svg-parser-test.cpp (+237/-0) testfiles/src/svg-writer-test.cpp (+219/-0) |
To merge this branch: | bzr merge lp:~adrianboguszewski/inkscape/sax-parser |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Mc | Pending | ||
Review via email: mp+310142@code.launchpad.net |
Commit message
Description of the change
Added new file loading and saving, new sax parser and libxml++.
To post a comment you must log in.
Unmerged revisions
- 15065. By Adrian Boguszewski
-
Merged trunk
- 15064. By Adrian Boguszewski
-
Fixed Windows file loading
- 15063. By Adrian Boguszewski
-
Merged trunk
- 15062. By Adrian Boguszewski
-
Fixed libxml++ compilation on Windows
- 15061. By Adrian Boguszewski
-
Merged trunk
- 15060. By Adrian Boguszewski
-
Fixed memory management in sax parser
- 15059. By Adrian Boguszewski
-
Renamed some variables
- 15058. By Adrian Boguszewski
-
Added empty entity to sax parser
- 15057. By Adrian Boguszewski
-
Improved compressed file reading
- 15056. By Adrian Boguszewski
-
Fixed too many text nodes
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CMakeLists.txt' | |||
2 | --- CMakeLists.txt 2016-10-01 06:05:10 +0000 | |||
3 | +++ CMakeLists.txt 2016-11-06 17:18:04 +0000 | |||
4 | @@ -86,6 +86,7 @@ | |||
5 | 86 | message("No gmock/gtest found! Perhaps you wish to run 'bash download-gtest.sh' to download it.") | 86 | message("No gmock/gtest found! Perhaps you wish to run 'bash download-gtest.sh' to download it.") |
6 | 87 | endif() | 87 | endif() |
7 | 88 | 88 | ||
8 | 89 | include(CMakeScripts/ConfigCompileFlags.cmake) | ||
9 | 89 | include(CMakeScripts/DefineDependsandFlags.cmake) # Includes, Compiler Flags, and Link Libraries | 90 | include(CMakeScripts/DefineDependsandFlags.cmake) # Includes, Compiler Flags, and Link Libraries |
10 | 90 | include(CMakeScripts/HelperMacros.cmake) # Misc Utility Macros | 91 | include(CMakeScripts/HelperMacros.cmake) # Misc Utility Macros |
11 | 91 | 92 | ||
12 | 92 | 93 | ||
13 | === modified file 'CMakeScripts/ConfigCompileFlags.cmake' | |||
14 | --- CMakeScripts/ConfigCompileFlags.cmake 2016-08-09 09:33:34 +0000 | |||
15 | +++ CMakeScripts/ConfigCompileFlags.cmake 2016-11-06 17:18:04 +0000 | |||
16 | @@ -14,6 +14,6 @@ | |||
17 | 14 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 ${COMPILE_PROFILING_FLAGS} ") | 14 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 ${COMPILE_PROFILING_FLAGS} ") |
18 | 15 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 ${COMPILE_PROFILING_FLAGS} ") | 15 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 ${COMPILE_PROFILING_FLAGS} ") |
19 | 16 | 16 | ||
21 | 17 | set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} ") | 17 | set(CMAKE_MAKE_PROGRAM ${CMAKE_MAKE_PROGRAM}) |
22 | 18 | 18 | ||
23 | 19 | message(STATUS "${CMAKE_CXX_FLAGS}") | 19 | message(STATUS "${CMAKE_CXX_FLAGS}") |
24 | 20 | 20 | ||
25 | === modified file 'src/CMakeLists.txt' | |||
26 | --- src/CMakeLists.txt 2016-10-22 22:11:58 +0000 | |||
27 | +++ src/CMakeLists.txt 2016-11-06 17:18:04 +0000 | |||
28 | @@ -458,6 +458,7 @@ | |||
29 | 458 | add_subdirectory(inkgc) | 458 | add_subdirectory(inkgc) |
30 | 459 | add_subdirectory(libuemf) | 459 | add_subdirectory(libuemf) |
31 | 460 | add_subdirectory(libvpsc) | 460 | add_subdirectory(libvpsc) |
32 | 461 | add_subdirectory(libxml++) | ||
33 | 461 | add_subdirectory(livarot) | 462 | add_subdirectory(livarot) |
34 | 462 | add_subdirectory(libnrtype) | 463 | add_subdirectory(libnrtype) |
35 | 463 | add_subdirectory(libdepixelize) | 464 | add_subdirectory(libdepixelize) |
36 | @@ -515,6 +516,7 @@ | |||
37 | 515 | avoid_LIB | 516 | avoid_LIB |
38 | 516 | cola_LIB | 517 | cola_LIB |
39 | 517 | vpsc_LIB | 518 | vpsc_LIB |
40 | 519 | xmlpp_LIB | ||
41 | 518 | livarot_LIB | 520 | livarot_LIB |
42 | 519 | uemf_LIB | 521 | uemf_LIB |
43 | 520 | 2geom_LIB | 522 | 2geom_LIB |
44 | 521 | 523 | ||
45 | === modified file 'src/document.cpp' | |||
46 | --- src/document.cpp 2016-10-02 22:44:54 +0000 | |||
47 | +++ src/document.cpp 2016-11-06 17:18:04 +0000 | |||
48 | @@ -42,6 +42,7 @@ | |||
49 | 42 | #include <string> | 42 | #include <string> |
50 | 43 | #include <cstring> | 43 | #include <cstring> |
51 | 44 | #include <2geom/transforms.h> | 44 | #include <2geom/transforms.h> |
52 | 45 | #include <xml/repr-io.h> | ||
53 | 45 | 46 | ||
54 | 46 | #include "widgets/desktop-widget.h" | 47 | #include "widgets/desktop-widget.h" |
55 | 47 | #include "desktop.h" | 48 | #include "desktop.h" |
56 | @@ -496,7 +497,7 @@ | |||
57 | 496 | Inkscape::XML::Node *rroot; | 497 | Inkscape::XML::Node *rroot; |
58 | 497 | gchar *s, *p; | 498 | gchar *s, *p; |
59 | 498 | /* Try to fetch repr from file */ | 499 | /* Try to fetch repr from file */ |
61 | 499 | rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI); | 500 | rdoc = Inkscape::XML::read_svg_file(uri, false, SP_SVG_NS_URI); |
62 | 500 | /* If file cannot be loaded, return NULL without warning */ | 501 | /* If file cannot be loaded, return NULL without warning */ |
63 | 501 | if (rdoc == NULL) return NULL; | 502 | if (rdoc == NULL) return NULL; |
64 | 502 | rroot = rdoc->root(); | 503 | rroot = rdoc->root(); |
65 | @@ -542,7 +543,7 @@ | |||
66 | 542 | { | 543 | { |
67 | 543 | SPDocument *doc = NULL; | 544 | SPDocument *doc = NULL; |
68 | 544 | 545 | ||
70 | 545 | Inkscape::XML::Document *rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI); | 546 | Inkscape::XML::Document *rdoc = Inkscape::XML::read_svg_buffer(buffer, false, SP_SVG_NS_URI); |
71 | 546 | if ( rdoc ) { | 547 | if ( rdoc ) { |
72 | 547 | // Only continue to create a non-null doc if it could be loaded | 548 | // Only continue to create a non-null doc if it could be loaded |
73 | 548 | Inkscape::XML::Node *rroot = rdoc->root(); | 549 | Inkscape::XML::Node *rroot = rdoc->root(); |
74 | 549 | 550 | ||
75 | === modified file 'src/extension/implementation/xslt.cpp' | |||
76 | --- src/extension/implementation/xslt.cpp 2016-08-03 13:29:38 +0000 | |||
77 | +++ src/extension/implementation/xslt.cpp 2016-11-06 17:18:04 +0000 | |||
78 | @@ -29,8 +29,7 @@ | |||
79 | 29 | 29 | ||
80 | 30 | #include <libxslt/transform.h> | 30 | #include <libxslt/transform.h> |
81 | 31 | #include <libxslt/xsltutils.h> | 31 | #include <libxslt/xsltutils.h> |
84 | 32 | 32 | #include <xml/repr-io.h> | |
83 | 33 | Inkscape::XML::Document * sp_repr_do_read (xmlDocPtr doc, const gchar * default_ns); | ||
85 | 34 | 33 | ||
86 | 35 | /* Namespaces */ | 34 | /* Namespaces */ |
87 | 36 | namespace Inkscape { | 35 | namespace Inkscape { |
88 | @@ -146,9 +145,14 @@ | |||
89 | 146 | xmlDocPtr result = xsltApplyStylesheet(_stylesheet, filein, params); | 145 | xmlDocPtr result = xsltApplyStylesheet(_stylesheet, filein, params); |
90 | 147 | xmlFreeDoc(filein); | 146 | xmlFreeDoc(filein); |
91 | 148 | 147 | ||
93 | 149 | Inkscape::XML::Document * rdoc = sp_repr_do_read( result, SP_SVG_NS_URI); | 148 | xmlChar *str; |
94 | 149 | int size; | ||
95 | 150 | xmlDocDumpMemory(result, &str, &size); | ||
96 | 150 | xmlFreeDoc(result); | 151 | xmlFreeDoc(result); |
97 | 151 | 152 | ||
98 | 153 | Inkscape::XML::Document * rdoc = Inkscape::XML::read_svg_buffer((char*) str, false, SP_SVG_NS_URI); | ||
99 | 154 | xmlFree(str); | ||
100 | 155 | |||
101 | 152 | if (rdoc == NULL) { | 156 | if (rdoc == NULL) { |
102 | 153 | return NULL; | 157 | return NULL; |
103 | 154 | } | 158 | } |
104 | @@ -197,8 +201,7 @@ | |||
105 | 197 | return; | 201 | return; |
106 | 198 | } | 202 | } |
107 | 199 | 203 | ||
110 | 200 | if (!sp_repr_save_rebased_file(repr->document(), tempfilename_out.c_str(), SP_SVG_NS_URI, | 204 | if (!Inkscape::XML::save_svg_file(repr->document(), tempfilename_out.c_str(), SP_SVG_NS_URI, doc->getBase(), filename)) { |
109 | 201 | doc->getBase(), filename)) { | ||
111 | 202 | throw Inkscape::Extension::Output::save_failed(); | 205 | throw Inkscape::Extension::Output::save_failed(); |
112 | 203 | } | 206 | } |
113 | 204 | 207 | ||
114 | 205 | 208 | ||
115 | === modified file 'src/extension/internal/filter/filter-file.cpp' | |||
116 | --- src/extension/internal/filter/filter-file.cpp 2014-06-26 19:40:39 +0000 | |||
117 | +++ src/extension/internal/filter/filter-file.cpp 2016-11-06 17:18:04 +0000 | |||
118 | @@ -21,6 +21,7 @@ | |||
119 | 21 | /* System includes */ | 21 | /* System includes */ |
120 | 22 | #include <glibmm/i18n.h> | 22 | #include <glibmm/i18n.h> |
121 | 23 | #include <glibmm/fileutils.h> | 23 | #include <glibmm/fileutils.h> |
122 | 24 | #include <xml/repr-io.h> | ||
123 | 24 | 25 | ||
124 | 25 | namespace Inkscape { | 26 | namespace Inkscape { |
125 | 26 | namespace Extension { | 27 | namespace Extension { |
126 | @@ -84,7 +85,7 @@ | |||
127 | 84 | void | 85 | void |
128 | 85 | Filter::filters_load_file (gchar * filename, gchar * menuname) | 86 | Filter::filters_load_file (gchar * filename, gchar * menuname) |
129 | 86 | { | 87 | { |
131 | 87 | Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI); | 88 | Inkscape::XML::Document *doc = Inkscape::XML::read_svg_file(filename, true, INKSCAPE_EXTENSION_URI); |
132 | 88 | if (doc == NULL) { | 89 | if (doc == NULL) { |
133 | 89 | g_warning("File (%s) is not parseable as XML. Ignored.", filename); | 90 | g_warning("File (%s) is not parseable as XML. Ignored.", filename); |
134 | 90 | return; | 91 | return; |
135 | @@ -162,7 +163,7 @@ | |||
136 | 162 | node->setAttribute("xmlns:inkscape", SP_INKSCAPE_NS_URI); | 163 | node->setAttribute("xmlns:inkscape", SP_INKSCAPE_NS_URI); |
137 | 163 | 164 | ||
138 | 164 | mywriter writer; | 165 | mywriter writer; |
140 | 165 | sp_repr_write_stream(node, writer, 0, FALSE, g_quark_from_static_string("svg"), 0, 0); | 166 | node->serialize(writer, "svg", 0, 0, false, false); |
141 | 166 | 167 | ||
142 | 167 | Inkscape::Extension::build_from_mem(xml_str, new Filter(g_strdup(writer.c_str()))); | 168 | Inkscape::Extension::build_from_mem(xml_str, new Filter(g_strdup(writer.c_str()))); |
143 | 168 | g_free(xml_str); | 169 | g_free(xml_str); |
144 | 169 | 170 | ||
145 | === modified file 'src/extension/internal/filter/filter.cpp' | |||
146 | --- src/extension/internal/filter/filter.cpp 2016-07-03 18:53:39 +0000 | |||
147 | +++ src/extension/internal/filter/filter.cpp 2016-11-06 17:18:04 +0000 | |||
148 | @@ -7,6 +7,7 @@ | |||
149 | 7 | * Released under GNU GPL, read the file 'COPYING' for more information | 7 | * Released under GNU GPL, read the file 'COPYING' for more information |
150 | 8 | */ | 8 | */ |
151 | 9 | 9 | ||
152 | 10 | #include <xml/repr-io.h> | ||
153 | 10 | #include "desktop.h" | 11 | #include "desktop.h" |
154 | 11 | #include "selection.h" | 12 | #include "selection.h" |
155 | 12 | #include "document-private.h" | 13 | #include "document-private.h" |
156 | @@ -64,7 +65,7 @@ | |||
157 | 64 | Inkscape::XML::Document * | 65 | Inkscape::XML::Document * |
158 | 65 | Filter::get_filter (Inkscape::Extension::Extension * ext) { | 66 | Filter::get_filter (Inkscape::Extension::Extension * ext) { |
159 | 66 | gchar const * filter = get_filter_text(ext); | 67 | gchar const * filter = get_filter_text(ext); |
161 | 67 | return sp_repr_read_mem(filter, strlen(filter), NULL); | 68 | return Inkscape::XML::read_svg_buffer(filter, true); |
162 | 68 | } | 69 | } |
163 | 69 | 70 | ||
164 | 70 | void | 71 | void |
165 | 71 | 72 | ||
166 | === modified file 'src/extension/internal/svg.cpp' | |||
167 | --- src/extension/internal/svg.cpp 2016-10-01 21:30:34 +0000 | |||
168 | +++ src/extension/internal/svg.cpp 2016-11-06 17:18:04 +0000 | |||
169 | @@ -15,7 +15,7 @@ | |||
170 | 15 | */ | 15 | */ |
171 | 16 | 16 | ||
172 | 17 | #ifdef HAVE_CONFIG_H | 17 | #ifdef HAVE_CONFIG_H |
174 | 18 | # include <config.h> | 18 | #include <config.h> |
175 | 19 | #endif | 19 | #endif |
176 | 20 | #include "sp-object.h" | 20 | #include "sp-object.h" |
177 | 21 | #include "svg.h" | 21 | #include "svg.h" |
178 | @@ -25,11 +25,12 @@ | |||
179 | 25 | #include <vector> | 25 | #include <vector> |
180 | 26 | #include "xml/attribute-record.h" | 26 | #include "xml/attribute-record.h" |
181 | 27 | #include "xml/simple-document.h" | 27 | #include "xml/simple-document.h" |
182 | 28 | #include "xml/repr-io.h" | ||
183 | 28 | #include "sp-root.h" | 29 | #include "sp-root.h" |
184 | 29 | #include "document.h" | 30 | #include "document.h" |
185 | 30 | 31 | ||
186 | 31 | #ifdef WITH_GNOME_VFS | 32 | #ifdef WITH_GNOME_VFS |
188 | 32 | # include <libgnomevfs/gnome-vfs.h> | 33 | #include <libgnomevfs/gnome-vfs.h> |
189 | 33 | #endif | 34 | #endif |
190 | 34 | 35 | ||
191 | 35 | namespace Inkscape { | 36 | namespace Inkscape { |
192 | @@ -293,8 +294,7 @@ | |||
193 | 293 | rdoc = new_rdoc; | 294 | rdoc = new_rdoc; |
194 | 294 | } | 295 | } |
195 | 295 | 296 | ||
198 | 296 | if (!sp_repr_save_rebased_file(rdoc, filename, SP_SVG_NS_URI, | 297 | if (!Inkscape::XML::save_svg_file(rdoc, filename, SP_SVG_NS_URI, doc->getBase(), filename)) { |
197 | 297 | doc->getBase(), filename)) { | ||
199 | 298 | throw Inkscape::Extension::Output::save_failed(); | 298 | throw Inkscape::Extension::Output::save_failed(); |
200 | 299 | } | 299 | } |
201 | 300 | 300 | ||
202 | 301 | 301 | ||
203 | === modified file 'src/extension/loader.h' | |||
204 | --- src/extension/loader.h 2016-08-03 15:12:14 +0000 | |||
205 | +++ src/extension/loader.h 2016-11-06 17:18:04 +0000 | |||
206 | @@ -1,4 +1,5 @@ | |||
208 | 1 | /** @file | 1 | /** @file#include <config.h> |
209 | 2 | |||
210 | 2 | * Loader for external plug-ins. | 3 | * Loader for external plug-ins. |
211 | 3 | *//* | 4 | *//* |
212 | 4 | * | 5 | * |
213 | 5 | 6 | ||
214 | === modified file 'src/extension/prefdialog.cpp' | |||
215 | --- src/extension/prefdialog.cpp 2016-07-28 10:22:07 +0000 | |||
216 | +++ src/extension/prefdialog.cpp 2016-11-06 17:18:04 +0000 | |||
217 | @@ -12,6 +12,7 @@ | |||
218 | 12 | #include <gtkmm/checkbutton.h> | 12 | #include <gtkmm/checkbutton.h> |
219 | 13 | #include <gtkmm/separator.h> | 13 | #include <gtkmm/separator.h> |
220 | 14 | #include <glibmm/i18n.h> | 14 | #include <glibmm/i18n.h> |
221 | 15 | #include <xml/repr-io.h> | ||
222 | 15 | 16 | ||
223 | 16 | #include "ui/dialog-events.h" | 17 | #include "ui/dialog-events.h" |
224 | 17 | #include "xml/repr.h" | 18 | #include "xml/repr.h" |
225 | @@ -81,7 +82,7 @@ | |||
226 | 81 | 82 | ||
227 | 82 | if (_effect != NULL && !_effect->no_live_preview) { | 83 | if (_effect != NULL && !_effect->no_live_preview) { |
228 | 83 | if (_param_preview == NULL) { | 84 | if (_param_preview == NULL) { |
230 | 84 | XML::Document * doc = sp_repr_read_mem(live_param_xml, strlen(live_param_xml), NULL); | 85 | XML::Document * doc = Inkscape::XML::read_svg_buffer(live_param_xml, true); |
231 | 85 | if (doc == NULL) { | 86 | if (doc == NULL) { |
232 | 86 | std::cout << "Error encountered loading live parameter XML !!!" << std::endl; | 87 | std::cout << "Error encountered loading live parameter XML !!!" << std::endl; |
233 | 87 | return; | 88 | return; |
234 | 88 | 89 | ||
235 | === modified file 'src/extension/system.cpp' | |||
236 | --- src/extension/system.cpp 2016-08-03 13:29:38 +0000 | |||
237 | +++ src/extension/system.cpp 2016-11-06 17:18:04 +0000 | |||
238 | @@ -40,6 +40,7 @@ | |||
239 | 40 | #include "loader.h" | 40 | #include "loader.h" |
240 | 41 | 41 | ||
241 | 42 | #include <glibmm/miscutils.h> | 42 | #include <glibmm/miscutils.h> |
242 | 43 | #include <xml/repr-io.h> | ||
243 | 43 | 44 | ||
244 | 44 | namespace Inkscape { | 45 | namespace Inkscape { |
245 | 45 | namespace Extension { | 46 | namespace Extension { |
246 | @@ -546,7 +547,7 @@ | |||
247 | 546 | Extension * | 547 | Extension * |
248 | 547 | build_from_file(gchar const *filename) | 548 | build_from_file(gchar const *filename) |
249 | 548 | { | 549 | { |
251 | 549 | Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI); | 550 | Inkscape::XML::Document *doc = Inkscape::XML::read_svg_file(filename, true, INKSCAPE_EXTENSION_URI); |
252 | 550 | std::string dir = Glib::path_get_dirname(filename); | 551 | std::string dir = Glib::path_get_dirname(filename); |
253 | 551 | Extension *ext = build_from_reprdoc(doc, NULL, &dir); | 552 | Extension *ext = build_from_reprdoc(doc, NULL, &dir); |
254 | 552 | if (ext != NULL) | 553 | if (ext != NULL) |
255 | @@ -568,7 +569,7 @@ | |||
256 | 568 | Extension * | 569 | Extension * |
257 | 569 | build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp) | 570 | build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp) |
258 | 570 | { | 571 | { |
260 | 571 | Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), INKSCAPE_EXTENSION_URI); | 572 | Inkscape::XML::Document *doc = Inkscape::XML::read_svg_buffer(buffer, true, INKSCAPE_EXTENSION_URI); |
261 | 572 | g_return_val_if_fail(doc != NULL, NULL); | 573 | g_return_val_if_fail(doc != NULL, NULL); |
262 | 573 | Extension *ext = build_from_reprdoc(doc, in_imp, NULL); | 574 | Extension *ext = build_from_reprdoc(doc, in_imp, NULL); |
263 | 574 | Inkscape::GC::release(doc); | 575 | Inkscape::GC::release(doc); |
264 | 575 | 576 | ||
265 | === modified file 'src/inkscape.cpp' | |||
266 | --- src/inkscape.cpp 2016-09-02 22:45:03 +0000 | |||
267 | +++ src/inkscape.cpp 2016-11-06 17:18:04 +0000 | |||
268 | @@ -48,6 +48,7 @@ | |||
269 | 48 | #include <glibmm/i18n.h> | 48 | #include <glibmm/i18n.h> |
270 | 49 | #include <glibmm/miscutils.h> | 49 | #include <glibmm/miscutils.h> |
271 | 50 | #include <glibmm/convert.h> | 50 | #include <glibmm/convert.h> |
272 | 51 | #include <xml/repr-io.h> | ||
273 | 51 | 52 | ||
274 | 52 | #include "desktop.h" | 53 | #include "desktop.h" |
275 | 53 | 54 | ||
276 | @@ -322,21 +323,17 @@ | |||
277 | 322 | baseName = 0; | 323 | baseName = 0; |
278 | 323 | 324 | ||
279 | 324 | // Try to save the file | 325 | // Try to save the file |
280 | 325 | FILE *file = Inkscape::IO::fopen_utf8name(full_path, "w"); | ||
281 | 326 | gchar *errortext = 0; | 326 | gchar *errortext = 0; |
288 | 327 | if (file) { | 327 | try { |
289 | 328 | try{ | 328 | if (!Inkscape::XML::save_svg_file(repr->document(), full_path, SP_SVG_NS_URI)) { |
284 | 329 | sp_repr_save_stream(repr->document(), file, SP_SVG_NS_URI); | ||
285 | 330 | } catch (Inkscape::Extension::Output::no_extension_found &e) { | ||
286 | 331 | errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document.")); | ||
287 | 332 | } catch (Inkscape::Extension::Output::save_failed &e) { | ||
290 | 333 | gchar *safeUri = Inkscape::IO::sanitizeString(full_path); | 329 | gchar *safeUri = Inkscape::IO::sanitizeString(full_path); |
291 | 334 | errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri); | 330 | errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri); |
292 | 335 | g_free(safeUri); | 331 | g_free(safeUri); |
293 | 336 | } | 332 | } |
297 | 337 | fclose(file); | 333 | |
298 | 338 | } | 334 | } catch (Inkscape::Extension::Output::no_extension_found &e) { |
299 | 339 | else { | 335 | errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document.")); |
300 | 336 | } catch (Inkscape::Extension::Output::save_failed &e) { | ||
301 | 340 | gchar *safeUri = Inkscape::IO::sanitizeString(full_path); | 337 | gchar *safeUri = Inkscape::IO::sanitizeString(full_path); |
302 | 341 | errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri); | 338 | errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri); |
303 | 342 | g_free(safeUri); | 339 | g_free(safeUri); |
304 | @@ -701,15 +698,14 @@ | |||
305 | 701 | file = Inkscape::IO::fopen_utf8name(filename, "w"); | 698 | file = Inkscape::IO::fopen_utf8name(filename, "w"); |
306 | 702 | if (file) { | 699 | if (file) { |
307 | 703 | g_snprintf (c, 1024, "%s", filename); // we want the complete path to be stored in c (for reporting purposes) | 700 | g_snprintf (c, 1024, "%s", filename); // we want the complete path to be stored in c (for reporting purposes) |
308 | 701 | fclose (file); | ||
309 | 704 | break; | 702 | break; |
310 | 705 | } | 703 | } |
311 | 706 | } | 704 | } |
312 | 707 | 705 | ||
313 | 708 | // Save | 706 | // Save |
316 | 709 | if (file) { | 707 | if (Inkscape::XML::save_svg_file(repr->document(), c, SP_SVG_NS_URI)) { |
315 | 710 | sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI); | ||
317 | 711 | savednames = g_slist_prepend (savednames, g_strdup (c)); | 708 | savednames = g_slist_prepend (savednames, g_strdup (c)); |
318 | 712 | fclose (file); | ||
319 | 713 | } else { | 709 | } else { |
320 | 714 | failednames = g_slist_prepend (failednames, (doc->getName()) ? g_strdup(doc->getName()) : g_strdup (_("Untitled document"))); | 710 | failednames = g_slist_prepend (failednames, (doc->getName()) ? g_strdup(doc->getName()) : g_strdup (_("Untitled document"))); |
321 | 715 | } | 711 | } |
322 | @@ -819,7 +815,7 @@ | |||
323 | 819 | 815 | ||
324 | 820 | if ( g_file_get_contents(fn, &menus_xml, &len, NULL) ) { | 816 | if ( g_file_get_contents(fn, &menus_xml, &len, NULL) ) { |
325 | 821 | // load the menus_xml file | 817 | // load the menus_xml file |
327 | 822 | _menus = sp_repr_read_mem(menus_xml, len, NULL); | 818 | _menus = Inkscape::XML::read_svg_buffer(menus_xml, true); |
328 | 823 | 819 | ||
329 | 824 | g_free(menus_xml); | 820 | g_free(menus_xml); |
330 | 825 | menus_xml = 0; | 821 | menus_xml = 0; |
331 | @@ -828,7 +824,7 @@ | |||
332 | 828 | fn = 0; | 824 | fn = 0; |
333 | 829 | 825 | ||
334 | 830 | if ( !_menus ) { | 826 | if ( !_menus ) { |
336 | 831 | _menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL); | 827 | _menus = Inkscape::XML::read_svg_buffer(menus_skeleton, true); |
337 | 832 | } | 828 | } |
338 | 833 | 829 | ||
339 | 834 | return (_menus != 0); | 830 | return (_menus != 0); |
340 | 835 | 831 | ||
341 | === added directory 'src/libxml++' | |||
342 | === added file 'src/libxml++/CMakeLists.txt' | |||
343 | --- src/libxml++/CMakeLists.txt 1970-01-01 00:00:00 +0000 | |||
344 | +++ src/libxml++/CMakeLists.txt 2016-11-06 17:18:04 +0000 | |||
345 | @@ -0,0 +1,34 @@ | |||
346 | 1 | set(libxmlpp_SRC | ||
347 | 2 | document.cc | ||
348 | 3 | dtd.cc | ||
349 | 4 | keepblanks.cc | ||
350 | 5 | noncopyable.cc | ||
351 | 6 | exceptions/exception.cc | ||
352 | 7 | exceptions/internal_error.cc | ||
353 | 8 | exceptions/parse_error.cc | ||
354 | 9 | exceptions/validity_error.cc | ||
355 | 10 | exceptions/wrapped_exception.cc | ||
356 | 11 | io/istreamparserinputbuffer.cc | ||
357 | 12 | io/parserinputbuffer.cc | ||
358 | 13 | parsers/parser.cc | ||
359 | 14 | parsers/saxparser.cc | ||
360 | 15 | |||
361 | 16 | # ------- | ||
362 | 17 | # Headers | ||
363 | 18 | document.h | ||
364 | 19 | dtd.h | ||
365 | 20 | keepblanks.h | ||
366 | 21 | libxml++config.h | ||
367 | 22 | noncopyable.h | ||
368 | 23 | exceptions/exception.h | ||
369 | 24 | exceptions/internal_error.h | ||
370 | 25 | exceptions/parse_error.h | ||
371 | 26 | exceptions/validity_error.h | ||
372 | 27 | exceptions/wrapped_exception.h | ||
373 | 28 | io/istreamparserinputbuffer.h | ||
374 | 29 | io/parserinputbuffer.h | ||
375 | 30 | parsers/parser.h | ||
376 | 31 | parsers/saxparser.h | ||
377 | 32 | ) | ||
378 | 33 | |||
379 | 34 | add_inkscape_lib(xmlpp_LIB "${libxmlpp_SRC}") | ||
380 | 0 | 35 | ||
381 | === added file 'src/libxml++/README' | |||
382 | --- src/libxml++/README 1970-01-01 00:00:00 +0000 | |||
383 | +++ src/libxml++/README 2016-11-06 17:18:04 +0000 | |||
384 | @@ -0,0 +1,8 @@ | |||
385 | 1 | This code is a part of libxml++ library (ver 3.0) with local modifications | ||
386 | 2 | inter alia: | ||
387 | 3 | * DOM parser and node classes are removed | ||
388 | 4 | * Enabled SAX2 interface | ||
389 | 5 | * Added new SAX parser methods to handle Processing Instructions | ||
390 | 6 | and namespace information from nodes | ||
391 | 7 | |||
392 | 8 | Full library is available here: http://libxmlplusplus.sourceforge.net/ | ||
393 | 0 | \ No newline at end of file | 9 | \ No newline at end of file |
394 | 1 | 10 | ||
395 | === added file 'src/libxml++/document.cc' | |||
396 | --- src/libxml++/document.cc 1970-01-01 00:00:00 +0000 | |||
397 | +++ src/libxml++/document.cc 2016-11-06 17:18:04 +0000 | |||
398 | @@ -0,0 +1,126 @@ | |||
399 | 1 | /* document.cc | ||
400 | 2 | * this file is part of libxml++ | ||
401 | 3 | * | ||
402 | 4 | * copyright (C) 2003 by the libxml++ development team | ||
403 | 5 | * | ||
404 | 6 | * this file is covered by the GNU Lesser General Public License, | ||
405 | 7 | * which should be included with libxml++ as the file COPYING. | ||
406 | 8 | */ | ||
407 | 9 | |||
408 | 10 | #include <libxml++/document.h> | ||
409 | 11 | #include <libxml++/exceptions/internal_error.h> | ||
410 | 12 | |||
411 | 13 | #include <libxml/tree.h> | ||
412 | 14 | |||
413 | 15 | namespace xmlpp | ||
414 | 16 | { | ||
415 | 17 | |||
416 | 18 | static const char* get_encoding_or_utf8(const Glib::ustring& encoding) | ||
417 | 19 | { | ||
418 | 20 | if(encoding.empty()) | ||
419 | 21 | { | ||
420 | 22 | //If we don't specify this to the xmlDocDump* functions (using nullptr instead), | ||
421 | 23 | //then some other encoding is used, causing them to fail on non-ASCII characters. | ||
422 | 24 | return "UTF-8"; | ||
423 | 25 | } | ||
424 | 26 | else | ||
425 | 27 | return encoding.c_str(); | ||
426 | 28 | } | ||
427 | 29 | |||
428 | 30 | |||
429 | 31 | Document::Init::Init() | ||
430 | 32 | { | ||
431 | 33 | xmlInitParser(); //Not always necessary, but necessary for thread safety. | ||
432 | 34 | } | ||
433 | 35 | |||
434 | 36 | Document::Init::~Init() noexcept | ||
435 | 37 | { | ||
436 | 38 | //We don't call this because it breaks libxml generally and should only be | ||
437 | 39 | //called at the very end of a process, such as at the end of a main(). | ||
438 | 40 | //libxml might still be used by the application, so we don't want to break | ||
439 | 41 | //that. | ||
440 | 42 | //This is important even here, which usually happens only when the library | ||
441 | 43 | //is unloaded, because that might happen during normal application use, | ||
442 | 44 | //if the application does dynamic library loading, for instance to load | ||
443 | 45 | //plugins. | ||
444 | 46 | //See http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser | ||
445 | 47 | //xmlCleanupParser(); //As per xmlInitParser(), or memory leak will happen. | ||
446 | 48 | } | ||
447 | 49 | |||
448 | 50 | Document::Init Document::init_; | ||
449 | 51 | |||
450 | 52 | Document::Document(const Glib::ustring& version) | ||
451 | 53 | : impl_(xmlNewDoc((const xmlChar*)version.c_str())) | ||
452 | 54 | { | ||
453 | 55 | if (!impl_) | ||
454 | 56 | throw internal_error("Could not create Document."); | ||
455 | 57 | impl_->_private = this; | ||
456 | 58 | } | ||
457 | 59 | |||
458 | 60 | Document::Document(xmlDoc* doc) | ||
459 | 61 | : impl_(doc) | ||
460 | 62 | { | ||
461 | 63 | if (!impl_) | ||
462 | 64 | throw internal_error("xmlDoc pointer cannot be nullptr"); | ||
463 | 65 | |||
464 | 66 | impl_->_private = this; | ||
465 | 67 | } | ||
466 | 68 | |||
467 | 69 | Document::~Document() | ||
468 | 70 | { | ||
469 | 71 | xmlFreeDoc(impl_); | ||
470 | 72 | } | ||
471 | 73 | |||
472 | 74 | Glib::ustring Document::get_encoding() const | ||
473 | 75 | { | ||
474 | 76 | Glib::ustring encoding; | ||
475 | 77 | if(impl_->encoding) | ||
476 | 78 | encoding = (const char*)impl_->encoding; | ||
477 | 79 | |||
478 | 80 | return encoding; | ||
479 | 81 | } | ||
480 | 82 | |||
481 | 83 | Dtd* Document::get_internal_subset() const | ||
482 | 84 | { | ||
483 | 85 | auto dtd = xmlGetIntSubset(impl_); | ||
484 | 86 | if(!dtd) | ||
485 | 87 | return nullptr; | ||
486 | 88 | |||
487 | 89 | if(!dtd->_private) | ||
488 | 90 | dtd->_private = new Dtd(dtd); | ||
489 | 91 | |||
490 | 92 | return reinterpret_cast<Dtd*>(dtd->_private); | ||
491 | 93 | } | ||
492 | 94 | |||
493 | 95 | void Document::set_internal_subset(const Glib::ustring& name, | ||
494 | 96 | const Glib::ustring& external_id, | ||
495 | 97 | const Glib::ustring& system_id) | ||
496 | 98 | { | ||
497 | 99 | auto dtd = xmlCreateIntSubset(impl_, | ||
498 | 100 | (const xmlChar*)name.c_str(), | ||
499 | 101 | external_id.empty() ? nullptr : (const xmlChar*)external_id.c_str(), | ||
500 | 102 | system_id.empty() ? nullptr : (const xmlChar*)system_id.c_str()); | ||
501 | 103 | |||
502 | 104 | if (dtd && !dtd->_private) | ||
503 | 105 | dtd->_private = new Dtd(dtd); | ||
504 | 106 | } | ||
505 | 107 | |||
506 | 108 | void Document::set_entity_declaration(const Glib::ustring& name, XmlEntityType type, | ||
507 | 109 | const Glib::ustring& publicId, const Glib::ustring& systemId, | ||
508 | 110 | const Glib::ustring& content) | ||
509 | 111 | { | ||
510 | 112 | auto entity = xmlAddDocEntity(impl_, (const xmlChar*)name.c_str(), | ||
511 | 113 | static_cast<int>(type), | ||
512 | 114 | publicId.empty() ? nullptr : (const xmlChar*)publicId.c_str(), | ||
513 | 115 | systemId.empty() ? nullptr : (const xmlChar*)systemId.c_str(), | ||
514 | 116 | (const xmlChar*)content.c_str()); | ||
515 | 117 | if (!entity) | ||
516 | 118 | throw internal_error("Could not add entity declaration " + name); | ||
517 | 119 | } | ||
518 | 120 | |||
519 | 121 | _xmlEntity* Document::get_entity(const Glib::ustring& name) | ||
520 | 122 | { | ||
521 | 123 | return xmlGetDocEntity(impl_, (const xmlChar*) name.c_str()); | ||
522 | 124 | } | ||
523 | 125 | |||
524 | 126 | } //namespace xmlpp | ||
525 | 0 | 127 | ||
526 | === added file 'src/libxml++/document.h' | |||
527 | --- src/libxml++/document.h 1970-01-01 00:00:00 +0000 | |||
528 | +++ src/libxml++/document.h 2016-11-06 17:18:04 +0000 | |||
529 | @@ -0,0 +1,139 @@ | |||
530 | 1 | /* document.h | ||
531 | 2 | * this file is part of libxml++ | ||
532 | 3 | * | ||
533 | 4 | * parts of the code copyright (C) 2003 by Stefan Seefeld | ||
534 | 5 | * others copyright (C) 2003 by libxml++ developer's team | ||
535 | 6 | * | ||
536 | 7 | * this file is covered by the GNU Lesser General Public License, | ||
537 | 8 | * which should be included with libxml++ as the file COPYING. | ||
538 | 9 | */ | ||
539 | 10 | |||
540 | 11 | #ifndef __LIBXMLPP_DOCUMENT_H | ||
541 | 12 | #define __LIBXMLPP_DOCUMENT_H | ||
542 | 13 | |||
543 | 14 | #include <libxml++/dtd.h> | ||
544 | 15 | |||
545 | 16 | #include <string> | ||
546 | 17 | #include <ostream> | ||
547 | 18 | |||
548 | 19 | /* std::string or Glib::ustring in function prototypes in libxml++? | ||
549 | 20 | * | ||
550 | 21 | * If it's propagated to a libxml2 function that takes a xmlChar*, it's | ||
551 | 22 | * UTF-8 encoded, and Glib::ustring is the right choice. | ||
552 | 23 | * | ||
553 | 24 | * If it's propagated to a libxml2 function that takes a char*, it's not | ||
554 | 25 | * necessarily UTF-8 encoded, and std::string is usually the right choice. | ||
555 | 26 | * Most of these strings are filenames or URLs. | ||
556 | 27 | */ | ||
557 | 28 | |||
558 | 29 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | ||
559 | 30 | extern "C" { | ||
560 | 31 | struct _xmlDoc; | ||
561 | 32 | struct _xmlEntity; | ||
562 | 33 | } | ||
563 | 34 | #endif //DOXYGEN_SHOULD_SKIP_THIS | ||
564 | 35 | |||
565 | 36 | namespace xmlpp | ||
566 | 37 | { | ||
567 | 38 | |||
568 | 39 | // xmlpp::XmlEntityType is similar to xmlEntityType in libxml2. | ||
569 | 40 | /** The valid entity types. | ||
570 | 41 | */ | ||
571 | 42 | enum class XmlEntityType | ||
572 | 43 | { | ||
573 | 44 | INTERNAL_GENERAL = 1, | ||
574 | 45 | EXTERNAL_GENERAL_PARSED = 2, | ||
575 | 46 | EXTERNAL_GENERAL_UNPARSED = 3, | ||
576 | 47 | INTERNAL_PARAMETER = 4, | ||
577 | 48 | EXTERNAL_PARAMETER = 5, | ||
578 | 49 | INTERNAL_PREDEFINED = 6 | ||
579 | 50 | }; | ||
580 | 51 | |||
581 | 52 | //TODO: Make Document inherit from Node, when we can break ABI one day? | ||
582 | 53 | // | ||
583 | 54 | //libxml might intend xmlDoc to derive (theoretically) from xmlNode. | ||
584 | 55 | //This is suggested because the xmlNodeSet returned by xmlXPathEval (see the | ||
585 | 56 | //Node::find() implementation) can contain either xmlNode or xmlDocument elements. | ||
586 | 57 | // See https://bugzilla.gnome.org/show_bug.cgi?id=754673#c8 for an explanation | ||
587 | 58 | // why it has not been done in libxml++ 3.0. | ||
588 | 59 | /** | ||
589 | 60 | * Represents an XML document in the DOM model. | ||
590 | 61 | */ | ||
591 | 62 | class Document : public NonCopyable | ||
592 | 63 | { | ||
593 | 64 | //Ensure that libxml is properly initialised: | ||
594 | 65 | class Init | ||
595 | 66 | { | ||
596 | 67 | public: | ||
597 | 68 | Init(); | ||
598 | 69 | ~Init() noexcept; | ||
599 | 70 | }; | ||
600 | 71 | |||
601 | 72 | friend class SaxParser; | ||
602 | 73 | |||
603 | 74 | public: | ||
604 | 75 | /** Create a new document. | ||
605 | 76 | * @param version XML version. | ||
606 | 77 | * @throws xmlpp::internal_error If memory allocation fails. | ||
607 | 78 | */ | ||
608 | 79 | explicit Document(const Glib::ustring& version = "1.0"); | ||
609 | 80 | |||
610 | 81 | /** Create a new C++ wrapper for an xmlDoc struct. | ||
611 | 82 | * The created xmlpp::Document takes ownership of the xmlDoc. | ||
612 | 83 | * When the Document is deleted, so is the xmlDoc and all its nodes. | ||
613 | 84 | * @param doc A pointer to an xmlDoc struct. Must not be <tt>nullptr</tt>. | ||
614 | 85 | * @throws xmlpp::internal_error If @a doc is <tt>nullptr</tt>. | ||
615 | 86 | */ | ||
616 | 87 | explicit Document(_xmlDoc* doc); | ||
617 | 88 | |||
618 | 89 | ~Document() override; | ||
619 | 90 | |||
620 | 91 | /** @return The encoding used in the source from which the document has been loaded. | ||
621 | 92 | */ | ||
622 | 93 | Glib::ustring get_encoding() const; | ||
623 | 94 | |||
624 | 95 | /** Get the internal subset of this document. | ||
625 | 96 | * @returns A pointer to the DTD, or <tt>nullptr</tt> if not found. | ||
626 | 97 | */ | ||
627 | 98 | Dtd* get_internal_subset() const; | ||
628 | 99 | |||
629 | 100 | /** Create the internal subset of this document. | ||
630 | 101 | * If the document already has an internal subset, a new one is not created. | ||
631 | 102 | * @param name The DTD name. | ||
632 | 103 | * @param external_id The external (PUBLIC) ID, or an empty string. | ||
633 | 104 | * @param system_id The system ID, or an empty string. | ||
634 | 105 | */ | ||
635 | 106 | void set_internal_subset(const Glib::ustring& name, | ||
636 | 107 | const Glib::ustring& external_id, | ||
637 | 108 | const Glib::ustring& system_id); | ||
638 | 109 | |||
639 | 110 | /** Add an Entity declaration to the document. | ||
640 | 111 | * @param name The name of the entity that will be used in an entity reference. | ||
641 | 112 | * @param type The type of entity. | ||
642 | 113 | * @param publicId The public ID of the subset. | ||
643 | 114 | * @param systemId The system ID of the subset. | ||
644 | 115 | * @param content The value of the Entity. In entity reference substitutions, this | ||
645 | 116 | * is the replacement value. | ||
646 | 117 | * @throws xmlpp::internal_error | ||
647 | 118 | */ | ||
648 | 119 | virtual void set_entity_declaration(const Glib::ustring& name, XmlEntityType type, | ||
649 | 120 | const Glib::ustring& publicId, const Glib::ustring& systemId, | ||
650 | 121 | const Glib::ustring& content); | ||
651 | 122 | |||
652 | 123 | protected: | ||
653 | 124 | /** Retrieve an Entity. | ||
654 | 125 | * The entity can be from an external subset or internally declared. | ||
655 | 126 | * @param name The name of the entity to get. | ||
656 | 127 | * @returns A pointer to the libxml2 entity structure, or <tt>nullptr</tt> if not found. | ||
657 | 128 | */ | ||
658 | 129 | _xmlEntity* get_entity(const Glib::ustring& name); | ||
659 | 130 | |||
660 | 131 | private: | ||
661 | 132 | static Init init_; | ||
662 | 133 | |||
663 | 134 | _xmlDoc* impl_; | ||
664 | 135 | }; | ||
665 | 136 | |||
666 | 137 | } //namespace xmlpp | ||
667 | 138 | |||
668 | 139 | #endif //__LIBXMLPP_DOCUMENT_H | ||
669 | 0 | 140 | ||
670 | === added file 'src/libxml++/dtd.cc' | |||
671 | --- src/libxml++/dtd.cc 1970-01-01 00:00:00 +0000 | |||
672 | +++ src/libxml++/dtd.cc 2016-11-06 17:18:04 +0000 | |||
673 | @@ -0,0 +1,147 @@ | |||
674 | 1 | /* dtd.cc | ||
675 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
676 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
677 | 4 | * included with libxml++ as the file COPYING. | ||
678 | 5 | */ | ||
679 | 6 | |||
680 | 7 | #include <libxml++/dtd.h> | ||
681 | 8 | #include <libxml++/exceptions/parse_error.h> | ||
682 | 9 | #include <libxml++/io/istreamparserinputbuffer.h> | ||
683 | 10 | |||
684 | 11 | #include <libxml/tree.h> | ||
685 | 12 | |||
686 | 13 | namespace xmlpp | ||
687 | 14 | { | ||
688 | 15 | |||
689 | 16 | struct Dtd::Impl | ||
690 | 17 | { | ||
691 | 18 | Impl() noexcept : dtd(nullptr), is_dtd_owner(false) {} | ||
692 | 19 | |||
693 | 20 | _xmlDtd* dtd; | ||
694 | 21 | bool is_dtd_owner; | ||
695 | 22 | }; | ||
696 | 23 | |||
697 | 24 | Dtd::Dtd() | ||
698 | 25 | : pimpl_(new Impl) | ||
699 | 26 | { | ||
700 | 27 | } | ||
701 | 28 | |||
702 | 29 | Dtd::Dtd(_xmlDtd* dtd, bool take_ownership) | ||
703 | 30 | : pimpl_(new Impl) | ||
704 | 31 | { | ||
705 | 32 | pimpl_->dtd = dtd; | ||
706 | 33 | if (dtd) | ||
707 | 34 | { | ||
708 | 35 | pimpl_->dtd->_private = this; | ||
709 | 36 | pimpl_->is_dtd_owner = take_ownership; | ||
710 | 37 | } | ||
711 | 38 | } | ||
712 | 39 | |||
713 | 40 | Dtd::Dtd(const std::string& filename) | ||
714 | 41 | : pimpl_(new Impl) | ||
715 | 42 | { | ||
716 | 43 | parse_subset("", filename); | ||
717 | 44 | } | ||
718 | 45 | |||
719 | 46 | Dtd::Dtd(const Glib::ustring& external, const Glib::ustring& system) | ||
720 | 47 | : pimpl_(new Impl) | ||
721 | 48 | { | ||
722 | 49 | parse_subset(external, system); | ||
723 | 50 | } | ||
724 | 51 | |||
725 | 52 | Dtd::~Dtd() | ||
726 | 53 | { | ||
727 | 54 | release_underlying(); | ||
728 | 55 | } | ||
729 | 56 | |||
730 | 57 | void Dtd::parse_file(const std::string& filename) | ||
731 | 58 | { | ||
732 | 59 | parse_subset("", filename); | ||
733 | 60 | } | ||
734 | 61 | |||
735 | 62 | void Dtd::parse_subset(const Glib::ustring& external, const Glib::ustring& system) | ||
736 | 63 | { | ||
737 | 64 | release_underlying(); // Free any existing dtd. | ||
738 | 65 | xmlResetLastError(); | ||
739 | 66 | |||
740 | 67 | auto dtd = xmlParseDTD( | ||
741 | 68 | external.empty() ? nullptr : (const xmlChar*)external.c_str(), | ||
742 | 69 | system.empty() ? nullptr : (const xmlChar*)system.c_str()); | ||
743 | 70 | |||
744 | 71 | if (!dtd) | ||
745 | 72 | { | ||
746 | 73 | throw parse_error("Dtd could not be parsed.\n" + format_xml_error()); | ||
747 | 74 | } | ||
748 | 75 | |||
749 | 76 | pimpl_->dtd = dtd; | ||
750 | 77 | pimpl_->dtd->_private = this; | ||
751 | 78 | pimpl_->is_dtd_owner = true; | ||
752 | 79 | } | ||
753 | 80 | |||
754 | 81 | void Dtd::parse_memory(const Glib::ustring& contents) | ||
755 | 82 | { | ||
756 | 83 | // Prepare an istream with buffer | ||
757 | 84 | std::istringstream is(contents); | ||
758 | 85 | |||
759 | 86 | parse_stream(is); | ||
760 | 87 | } | ||
761 | 88 | |||
762 | 89 | void Dtd::parse_stream(std::istream& in) | ||
763 | 90 | { | ||
764 | 91 | release_underlying(); // Free any existing dtd. | ||
765 | 92 | xmlResetLastError(); | ||
766 | 93 | |||
767 | 94 | IStreamParserInputBuffer ibuff(in); | ||
768 | 95 | |||
769 | 96 | auto dtd = xmlIOParseDTD(nullptr, ibuff.cobj(), XML_CHAR_ENCODING_UTF8); | ||
770 | 97 | |||
771 | 98 | if (!dtd) | ||
772 | 99 | { | ||
773 | 100 | throw parse_error("Dtd could not be parsed.\n" + format_xml_error()); | ||
774 | 101 | } | ||
775 | 102 | |||
776 | 103 | pimpl_->dtd = dtd; | ||
777 | 104 | pimpl_->dtd->_private = this; | ||
778 | 105 | pimpl_->is_dtd_owner = true; | ||
779 | 106 | } | ||
780 | 107 | |||
781 | 108 | Glib::ustring Dtd::get_name() const | ||
782 | 109 | { | ||
783 | 110 | return (pimpl_->dtd && pimpl_->dtd->name) ? (const char*)pimpl_->dtd->name : ""; | ||
784 | 111 | } | ||
785 | 112 | |||
786 | 113 | Glib::ustring Dtd::get_external_id() const | ||
787 | 114 | { | ||
788 | 115 | return (pimpl_->dtd && pimpl_->dtd->ExternalID) ? (const char*)pimpl_->dtd->ExternalID : ""; | ||
789 | 116 | } | ||
790 | 117 | |||
791 | 118 | Glib::ustring Dtd::get_system_id() const | ||
792 | 119 | { | ||
793 | 120 | return (pimpl_->dtd && pimpl_->dtd->SystemID) ? (const char*)pimpl_->dtd->SystemID : ""; | ||
794 | 121 | } | ||
795 | 122 | |||
796 | 123 | _xmlDtd* Dtd::cobj() noexcept | ||
797 | 124 | { | ||
798 | 125 | return pimpl_->dtd; | ||
799 | 126 | } | ||
800 | 127 | |||
801 | 128 | const _xmlDtd* Dtd::cobj() const noexcept | ||
802 | 129 | { | ||
803 | 130 | return pimpl_->dtd; | ||
804 | 131 | } | ||
805 | 132 | |||
806 | 133 | void Dtd::release_underlying() | ||
807 | 134 | { | ||
808 | 135 | if (pimpl_->dtd) | ||
809 | 136 | { | ||
810 | 137 | pimpl_->dtd->_private = nullptr; | ||
811 | 138 | if (pimpl_->is_dtd_owner) | ||
812 | 139 | { | ||
813 | 140 | xmlFreeDtd(pimpl_->dtd); | ||
814 | 141 | pimpl_->is_dtd_owner = false; | ||
815 | 142 | } | ||
816 | 143 | pimpl_->dtd = nullptr; | ||
817 | 144 | } | ||
818 | 145 | } | ||
819 | 146 | |||
820 | 147 | } //namespace xmlpp | ||
821 | 0 | 148 | ||
822 | === added file 'src/libxml++/dtd.h' | |||
823 | --- src/libxml++/dtd.h 1970-01-01 00:00:00 +0000 | |||
824 | +++ src/libxml++/dtd.h 2016-11-06 17:18:04 +0000 | |||
825 | @@ -0,0 +1,136 @@ | |||
826 | 1 | /* dtd.h | ||
827 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
828 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
829 | 4 | * included with libxml++ as the file COPYING. | ||
830 | 5 | */ | ||
831 | 6 | |||
832 | 7 | #ifndef __LIBXMLPP_DTD_H | ||
833 | 8 | #define __LIBXMLPP_DTD_H | ||
834 | 9 | |||
835 | 10 | #include <libxml++/noncopyable.h> | ||
836 | 11 | #include <glibmm/ustring.h> | ||
837 | 12 | #include <string> | ||
838 | 13 | #include <memory> // std::unique_ptr | ||
839 | 14 | |||
840 | 15 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | ||
841 | 16 | extern "C" { | ||
842 | 17 | struct _xmlDtd; | ||
843 | 18 | } | ||
844 | 19 | #endif //DOXYGEN_SHOULD_SKIP_THIS | ||
845 | 20 | |||
846 | 21 | namespace xmlpp | ||
847 | 22 | { | ||
848 | 23 | |||
849 | 24 | //TODO: Derive from Node? | ||
850 | 25 | // See https://bugzilla.gnome.org/show_bug.cgi?id=754673#c8 for an explanation | ||
851 | 26 | // why it has not been done in libxml++ 3.0. | ||
852 | 27 | /** Represents an XML DTD for validating XML files. | ||
853 | 28 | * DTD = Document Type Definition | ||
854 | 29 | */ | ||
855 | 30 | class Dtd : public NonCopyable | ||
856 | 31 | { | ||
857 | 32 | public: | ||
858 | 33 | Dtd(); | ||
859 | 34 | |||
860 | 35 | /** Create a Dtd from the underlying libxml DTD element. | ||
861 | 36 | * @param dtd A pointer to the libxml DTD element. | ||
862 | 37 | * @param take_ownership If <tt>true</tt>, this Dtd instance takes ownership of | ||
863 | 38 | * the libxml DTD element. The caller must not delete it.<br> | ||
864 | 39 | * If <tt>false</tt>, this Dtd does not take ownership of the libxml | ||
865 | 40 | * DTD element. The caller must guarantee that the libxml DTD element | ||
866 | 41 | * exists as long as this Dtd keeps a pointer to it. The caller is | ||
867 | 42 | * responsible for deleting the libxml DTD element when it's no longer | ||
868 | 43 | * needed, unless it belongs to a Document, in which case it's deleted | ||
869 | 44 | * when the Document is deleted. | ||
870 | 45 | */ | ||
871 | 46 | explicit Dtd(_xmlDtd* dtd, bool take_ownership = false); | ||
872 | 47 | |||
873 | 48 | /** Create a Dtd and parse an external subset (DTD file) immediately. | ||
874 | 49 | * | ||
875 | 50 | * @newin{3,0} | ||
876 | 51 | * | ||
877 | 52 | * @param filename The URL of the DTD. | ||
878 | 53 | * @throws xmlpp::parse_error | ||
879 | 54 | */ | ||
880 | 55 | explicit Dtd(const std::string& filename); | ||
881 | 56 | |||
882 | 57 | /** Create a Dtd and parse an external subset (DTD file) immediately. | ||
883 | 58 | * | ||
884 | 59 | * @newin{3,0} | ||
885 | 60 | * | ||
886 | 61 | * @param external The external ID of the DTD. | ||
887 | 62 | * @param system The URL of the DTD. | ||
888 | 63 | * @throws xmlpp::parse_error | ||
889 | 64 | */ | ||
890 | 65 | Dtd(const Glib::ustring& external, const Glib::ustring& system); | ||
891 | 66 | |||
892 | 67 | ~Dtd() override; | ||
893 | 68 | |||
894 | 69 | /** Parse an external subset (DTD file). | ||
895 | 70 | * If another DTD has been parsed before, that DTD is replaced by the new one | ||
896 | 71 | * (deleted if this Dtd owns it). | ||
897 | 72 | * | ||
898 | 73 | * @newin{3,0} | ||
899 | 74 | * | ||
900 | 75 | * @param filename The URL of the DTD. | ||
901 | 76 | * @throws xmlpp::parse_error | ||
902 | 77 | */ | ||
903 | 78 | void parse_file(const std::string& filename); | ||
904 | 79 | |||
905 | 80 | /** Parse an external subset (DTD file). | ||
906 | 81 | * If another DTD has been parsed before, that DTD is replaced by the new one | ||
907 | 82 | * (deleted if this Dtd owns it). | ||
908 | 83 | * | ||
909 | 84 | * @newin{3,0} | ||
910 | 85 | * | ||
911 | 86 | * @param external The external ID of the DTD. | ||
912 | 87 | * @param system The URL of the DTD. | ||
913 | 88 | * @throws xmlpp::parse_error | ||
914 | 89 | */ | ||
915 | 90 | void parse_subset(const Glib::ustring& external, const Glib::ustring& system); | ||
916 | 91 | |||
917 | 92 | /** Parse a DTD from a string. | ||
918 | 93 | * If another DTD has been parsed before, that DTD is replaced by the new one | ||
919 | 94 | * (deleted if this Dtd owns it). | ||
920 | 95 | * | ||
921 | 96 | * @newin{3,0} | ||
922 | 97 | * | ||
923 | 98 | * @param contents The DTD as a string. | ||
924 | 99 | * @throws xmlpp::parse_error | ||
925 | 100 | */ | ||
926 | 101 | void parse_memory(const Glib::ustring& contents); | ||
927 | 102 | |||
928 | 103 | /** Parse a DTD from a stream. | ||
929 | 104 | * If another DTD has been parsed before, that DTD is replaced by the new one | ||
930 | 105 | * (deleted if this Dtd owns it). | ||
931 | 106 | * | ||
932 | 107 | * @newin{3,0} | ||
933 | 108 | * | ||
934 | 109 | * @param in The stream. | ||
935 | 110 | * @throws xmlpp::parse_error | ||
936 | 111 | */ | ||
937 | 112 | void parse_stream(std::istream& in); | ||
938 | 113 | |||
939 | 114 | Glib::ustring get_name() const; | ||
940 | 115 | Glib::ustring get_external_id() const; | ||
941 | 116 | Glib::ustring get_system_id() const; | ||
942 | 117 | |||
943 | 118 | /** Access the underlying libxml implementation. | ||
944 | 119 | */ | ||
945 | 120 | _xmlDtd* cobj() noexcept; | ||
946 | 121 | |||
947 | 122 | /** Access the underlying libxml implementation. | ||
948 | 123 | */ | ||
949 | 124 | const _xmlDtd* cobj() const noexcept; | ||
950 | 125 | |||
951 | 126 | protected: | ||
952 | 127 | void release_underlying(); | ||
953 | 128 | |||
954 | 129 | private: | ||
955 | 130 | struct Impl; | ||
956 | 131 | std::unique_ptr<Impl> pimpl_; | ||
957 | 132 | }; | ||
958 | 133 | |||
959 | 134 | } // namespace xmlpp | ||
960 | 135 | |||
961 | 136 | #endif //__LIBXMLPP_DTD_H | ||
962 | 0 | 137 | ||
963 | === added directory 'src/libxml++/exceptions' | |||
964 | === added file 'src/libxml++/exceptions/exception.cc' | |||
965 | --- src/libxml++/exceptions/exception.cc 1970-01-01 00:00:00 +0000 | |||
966 | +++ src/libxml++/exceptions/exception.cc 2016-11-06 17:18:04 +0000 | |||
967 | @@ -0,0 +1,125 @@ | |||
968 | 1 | #include "exception.h" | ||
969 | 2 | #include <libxml/xmlerror.h> | ||
970 | 3 | #include <libxml/parser.h> | ||
971 | 4 | #include <cstdio> | ||
972 | 5 | #include <vector> | ||
973 | 6 | |||
974 | 7 | namespace xmlpp { | ||
975 | 8 | |||
976 | 9 | exception::exception(const Glib::ustring& message) | ||
977 | 10 | : message_(message) | ||
978 | 11 | { | ||
979 | 12 | } | ||
980 | 13 | |||
981 | 14 | exception::~exception() noexcept | ||
982 | 15 | {} | ||
983 | 16 | |||
984 | 17 | const char* exception::what() const noexcept | ||
985 | 18 | { | ||
986 | 19 | return message_.c_str(); | ||
987 | 20 | } | ||
988 | 21 | |||
989 | 22 | void exception::raise() const | ||
990 | 23 | { | ||
991 | 24 | throw *this; | ||
992 | 25 | } | ||
993 | 26 | |||
994 | 27 | exception* exception::clone() const | ||
995 | 28 | { | ||
996 | 29 | return new exception(*this); | ||
997 | 30 | } | ||
998 | 31 | |||
999 | 32 | Glib::ustring format_xml_error(const _xmlError* error) | ||
1000 | 33 | { | ||
1001 | 34 | if (!error) | ||
1002 | 35 | error = xmlGetLastError(); | ||
1003 | 36 | |||
1004 | 37 | if (!error || error->code == XML_ERR_OK) | ||
1005 | 38 | return ""; // No error | ||
1006 | 39 | |||
1007 | 40 | Glib::ustring str; | ||
1008 | 41 | |||
1009 | 42 | if (error->file && *error->file != '\0') | ||
1010 | 43 | { | ||
1011 | 44 | str += "File "; | ||
1012 | 45 | str += error->file; | ||
1013 | 46 | } | ||
1014 | 47 | |||
1015 | 48 | if (error->line > 0) | ||
1016 | 49 | { | ||
1017 | 50 | str += (str.empty() ? "Line " : ", line ") + Glib::ustring::format(error->line); | ||
1018 | 51 | if (error->int2 > 0) | ||
1019 | 52 | str += ", column " + Glib::ustring::format(error->int2); | ||
1020 | 53 | } | ||
1021 | 54 | |||
1022 | 55 | const bool two_lines = !str.empty(); | ||
1023 | 56 | if (two_lines) | ||
1024 | 57 | str += ' '; | ||
1025 | 58 | |||
1026 | 59 | switch (error->level) | ||
1027 | 60 | { | ||
1028 | 61 | case XML_ERR_WARNING: | ||
1029 | 62 | str += "(warning):"; | ||
1030 | 63 | break; | ||
1031 | 64 | case XML_ERR_ERROR: | ||
1032 | 65 | str += "(error):"; | ||
1033 | 66 | break; | ||
1034 | 67 | case XML_ERR_FATAL: | ||
1035 | 68 | str += "(fatal):"; | ||
1036 | 69 | break; | ||
1037 | 70 | default: | ||
1038 | 71 | str += "():"; | ||
1039 | 72 | break; | ||
1040 | 73 | } | ||
1041 | 74 | |||
1042 | 75 | str += two_lines ? '\n' : ' '; | ||
1043 | 76 | |||
1044 | 77 | if (error->message && *error->message != '\0') | ||
1045 | 78 | str += error->message; | ||
1046 | 79 | else | ||
1047 | 80 | str += "Error code " + Glib::ustring::format(error->code); | ||
1048 | 81 | |||
1049 | 82 | // If the string does not end with end-of-line, append an end-of-line. | ||
1050 | 83 | if (*str.rbegin() != '\n') | ||
1051 | 84 | str += '\n'; | ||
1052 | 85 | |||
1053 | 86 | return str; | ||
1054 | 87 | } | ||
1055 | 88 | |||
1056 | 89 | Glib::ustring format_xml_parser_error(const _xmlParserCtxt* parser_context) | ||
1057 | 90 | { | ||
1058 | 91 | if (!parser_context) | ||
1059 | 92 | return "Error. xmlpp::format_xml_parser_error() called with parser_context == nullptr\n"; | ||
1060 | 93 | |||
1061 | 94 | const auto error = xmlCtxtGetLastError(const_cast<_xmlParserCtxt*>(parser_context)); | ||
1062 | 95 | |||
1063 | 96 | if (!error) | ||
1064 | 97 | return ""; // No error | ||
1065 | 98 | |||
1066 | 99 | Glib::ustring str; | ||
1067 | 100 | |||
1068 | 101 | if (!parser_context->wellFormed) | ||
1069 | 102 | str += "Document not well-formed.\n"; | ||
1070 | 103 | |||
1071 | 104 | return str + format_xml_error(error); | ||
1072 | 105 | } | ||
1073 | 106 | |||
1074 | 107 | Glib::ustring format_printf_message(const char* fmt, va_list args) | ||
1075 | 108 | { | ||
1076 | 109 | // This code was inspired by the example at | ||
1077 | 110 | // http://en.cppreference.com/w/cpp/io/c/vfprintf | ||
1078 | 111 | va_list args2; | ||
1079 | 112 | va_copy(args2, args); | ||
1080 | 113 | // Number of characters (bytes) in the resulting string; | ||
1081 | 114 | // error, if < 0. | ||
1082 | 115 | const int nchar = std::vsnprintf(nullptr, 0, fmt, args2); | ||
1083 | 116 | va_end(args2); | ||
1084 | 117 | if (nchar < 0) | ||
1085 | 118 | return Glib::ustring::format("Error code from std::vsnprintf = ", nchar); | ||
1086 | 119 | |||
1087 | 120 | std::vector<char> buf(nchar+1); | ||
1088 | 121 | std::vsnprintf(buf.data(), buf.size(), fmt, args); | ||
1089 | 122 | return Glib::ustring(buf.data()); | ||
1090 | 123 | } | ||
1091 | 124 | |||
1092 | 125 | } //namespace xmlpp | ||
1093 | 0 | 126 | ||
1094 | === added file 'src/libxml++/exceptions/exception.h' | |||
1095 | --- src/libxml++/exceptions/exception.h 1970-01-01 00:00:00 +0000 | |||
1096 | +++ src/libxml++/exceptions/exception.h 2016-11-06 17:18:04 +0000 | |||
1097 | @@ -0,0 +1,99 @@ | |||
1098 | 1 | /* exception.h | ||
1099 | 2 | * | ||
1100 | 3 | * Copyright (C) 2002 The libxml++ development team | ||
1101 | 4 | * | ||
1102 | 5 | * This library is free software; you can redistribute it and/or | ||
1103 | 6 | * modify it under the terms of the GNU Library General Public | ||
1104 | 7 | * License as published by the Free Software Foundation; either | ||
1105 | 8 | * version 2 of the License, or (at your option) any later version. | ||
1106 | 9 | * | ||
1107 | 10 | * This library is distributed in the hope that it will be useful, | ||
1108 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1109 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1110 | 13 | * Library General Public License for more details. | ||
1111 | 14 | * | ||
1112 | 15 | * You should have received a copy of the GNU Library General Public | ||
1113 | 16 | * License along with this library; if not, write to the Free | ||
1114 | 17 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
1115 | 18 | */ | ||
1116 | 19 | |||
1117 | 20 | #ifndef __LIBXMLPP_EXCEPTION_H | ||
1118 | 21 | #define __LIBXMLPP_EXCEPTION_H | ||
1119 | 22 | |||
1120 | 23 | #include <exception> | ||
1121 | 24 | #include <cstdarg> // va_list | ||
1122 | 25 | #include <glibmm/ustring.h> | ||
1123 | 26 | |||
1124 | 27 | #include <libxml++/libxml++config.h> | ||
1125 | 28 | |||
1126 | 29 | extern "C" { | ||
1127 | 30 | struct _xmlError; | ||
1128 | 31 | struct _xmlParserCtxt; | ||
1129 | 32 | } | ||
1130 | 33 | |||
1131 | 34 | namespace xmlpp | ||
1132 | 35 | { | ||
1133 | 36 | |||
1134 | 37 | /** Base class for all xmlpp exceptions. | ||
1135 | 38 | */ | ||
1136 | 39 | class LIBXMLPP_API exception : public std::exception | ||
1137 | 40 | { | ||
1138 | 41 | public: | ||
1139 | 42 | explicit exception(const Glib::ustring& message); | ||
1140 | 43 | ~exception() noexcept override; | ||
1141 | 44 | |||
1142 | 45 | const char* what() const noexcept override; | ||
1143 | 46 | |||
1144 | 47 | virtual void raise() const; | ||
1145 | 48 | virtual exception* clone() const; | ||
1146 | 49 | |||
1147 | 50 | private: | ||
1148 | 51 | Glib::ustring message_; | ||
1149 | 52 | }; | ||
1150 | 53 | |||
1151 | 54 | /** Format an _xmlError struct into a text string, suitable for printing. | ||
1152 | 55 | * | ||
1153 | 56 | * @newin{2,36} | ||
1154 | 57 | * | ||
1155 | 58 | * @param error Pointer to an _xmlError struct or <tt>nullptr</tt>. | ||
1156 | 59 | * If <tt>nullptr</tt>, the error returned by xmlGetLastError() is used. | ||
1157 | 60 | * @returns A formatted text string. If the error struct does not contain an | ||
1158 | 61 | * error (error->code == XML_ERR_OK), an empty string is returned. | ||
1159 | 62 | */ | ||
1160 | 63 | Glib::ustring format_xml_error(const _xmlError* error = nullptr); | ||
1161 | 64 | |||
1162 | 65 | /** Format a parser error into a text string, suitable for printing. | ||
1163 | 66 | * | ||
1164 | 67 | * @newin{2,36} | ||
1165 | 68 | * | ||
1166 | 69 | * @param parser_context Pointer to an _xmlParserCtxt struct. | ||
1167 | 70 | * @returns A formatted text string. If the parser context does not contain an | ||
1168 | 71 | * error (parser_context->lastError.code == XML_ERR_OK), an empty | ||
1169 | 72 | * string is returned. | ||
1170 | 73 | */ | ||
1171 | 74 | Glib::ustring format_xml_parser_error(const _xmlParserCtxt* parser_context); | ||
1172 | 75 | |||
1173 | 76 | /** Format a message from a function with C-style variadic parameters. | ||
1174 | 77 | * | ||
1175 | 78 | * Helper function that formats a message supplied in the form of a printf-style | ||
1176 | 79 | * format specification and zero or more ... parameters. | ||
1177 | 80 | * | ||
1178 | 81 | * @code | ||
1179 | 82 | * // Typical call: | ||
1180 | 83 | * void f(const char* fmt, ...) | ||
1181 | 84 | * { | ||
1182 | 85 | * va_list args; | ||
1183 | 86 | * va_start(args, fmt); | ||
1184 | 87 | * Glib::ustring msg = xmlpp::format_printf_message(fmt, args); | ||
1185 | 88 | * va_end(args); | ||
1186 | 89 | * // ... | ||
1187 | 90 | * } | ||
1188 | 91 | * @endcode | ||
1189 | 92 | * | ||
1190 | 93 | * @newin{3,0} | ||
1191 | 94 | */ | ||
1192 | 95 | Glib::ustring format_printf_message(const char* fmt, va_list args); | ||
1193 | 96 | |||
1194 | 97 | } // namespace xmlpp | ||
1195 | 98 | |||
1196 | 99 | #endif // __LIBXMLPP_EXCEPTION_H | ||
1197 | 0 | 100 | ||
1198 | === added file 'src/libxml++/exceptions/internal_error.cc' | |||
1199 | --- src/libxml++/exceptions/internal_error.cc 1970-01-01 00:00:00 +0000 | |||
1200 | +++ src/libxml++/exceptions/internal_error.cc 2016-11-06 17:18:04 +0000 | |||
1201 | @@ -0,0 +1,23 @@ | |||
1202 | 1 | #include "internal_error.h" | ||
1203 | 2 | |||
1204 | 3 | namespace xmlpp { | ||
1205 | 4 | |||
1206 | 5 | internal_error::internal_error(const Glib::ustring& message) | ||
1207 | 6 | : exception(message) | ||
1208 | 7 | { | ||
1209 | 8 | } | ||
1210 | 9 | |||
1211 | 10 | internal_error::~internal_error() noexcept | ||
1212 | 11 | {} | ||
1213 | 12 | |||
1214 | 13 | void internal_error::raise() const | ||
1215 | 14 | { | ||
1216 | 15 | throw *this; | ||
1217 | 16 | } | ||
1218 | 17 | |||
1219 | 18 | exception* internal_error::clone() const | ||
1220 | 19 | { | ||
1221 | 20 | return new internal_error(*this); | ||
1222 | 21 | } | ||
1223 | 22 | |||
1224 | 23 | } //namespace xmlpp | ||
1225 | 0 | 24 | ||
1226 | === added file 'src/libxml++/exceptions/internal_error.h' | |||
1227 | --- src/libxml++/exceptions/internal_error.h 1970-01-01 00:00:00 +0000 | |||
1228 | +++ src/libxml++/exceptions/internal_error.h 2016-11-06 17:18:04 +0000 | |||
1229 | @@ -0,0 +1,39 @@ | |||
1230 | 1 | /* internal_error.h | ||
1231 | 2 | * | ||
1232 | 3 | * Copyright (C) 2002 The libxml++ development team | ||
1233 | 4 | * | ||
1234 | 5 | * This library is free software; you can redistribute it and/or | ||
1235 | 6 | * modify it under the terms of the GNU Library General Public | ||
1236 | 7 | * License as published by the Free Software Foundation; either | ||
1237 | 8 | * version 2 of the License, or (at your option) any later version. | ||
1238 | 9 | * | ||
1239 | 10 | * This library is distributed in the hope that it will be useful, | ||
1240 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1241 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1242 | 13 | * Library General Public License for more details. | ||
1243 | 14 | * | ||
1244 | 15 | * You should have received a copy of the GNU Library General Public | ||
1245 | 16 | * License along with this library; if not, write to the Free | ||
1246 | 17 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
1247 | 18 | */ | ||
1248 | 19 | |||
1249 | 20 | #ifndef __LIBXMLPP_INTERNAL_ERROR_H | ||
1250 | 21 | #define __LIBXMLPP_INTERNAL_ERROR_H | ||
1251 | 22 | |||
1252 | 23 | #include <libxml++/exceptions/exception.h> | ||
1253 | 24 | |||
1254 | 25 | namespace xmlpp { | ||
1255 | 26 | |||
1256 | 27 | class internal_error : public exception | ||
1257 | 28 | { | ||
1258 | 29 | public: | ||
1259 | 30 | explicit internal_error(const Glib::ustring& message); | ||
1260 | 31 | ~internal_error() noexcept override; | ||
1261 | 32 | |||
1262 | 33 | void raise() const override; | ||
1263 | 34 | exception* clone() const override; | ||
1264 | 35 | }; | ||
1265 | 36 | |||
1266 | 37 | } // namespace xmlpp | ||
1267 | 38 | |||
1268 | 39 | #endif // __LIBXMLPP_INTERNAL_ERROR_H | ||
1269 | 0 | 40 | ||
1270 | === added file 'src/libxml++/exceptions/parse_error.cc' | |||
1271 | --- src/libxml++/exceptions/parse_error.cc 1970-01-01 00:00:00 +0000 | |||
1272 | +++ src/libxml++/exceptions/parse_error.cc 2016-11-06 17:18:04 +0000 | |||
1273 | @@ -0,0 +1,23 @@ | |||
1274 | 1 | #include "parse_error.h" | ||
1275 | 2 | |||
1276 | 3 | namespace xmlpp { | ||
1277 | 4 | |||
1278 | 5 | parse_error::parse_error(const Glib::ustring& message) | ||
1279 | 6 | : exception(message) | ||
1280 | 7 | { | ||
1281 | 8 | } | ||
1282 | 9 | |||
1283 | 10 | parse_error::~parse_error() noexcept | ||
1284 | 11 | {} | ||
1285 | 12 | |||
1286 | 13 | void parse_error::raise() const | ||
1287 | 14 | { | ||
1288 | 15 | throw *this; | ||
1289 | 16 | } | ||
1290 | 17 | |||
1291 | 18 | exception* parse_error::clone() const | ||
1292 | 19 | { | ||
1293 | 20 | return new parse_error(*this); | ||
1294 | 21 | } | ||
1295 | 22 | |||
1296 | 23 | } //namespace xmlpp | ||
1297 | 0 | 24 | ||
1298 | === added file 'src/libxml++/exceptions/parse_error.h' | |||
1299 | --- src/libxml++/exceptions/parse_error.h 1970-01-01 00:00:00 +0000 | |||
1300 | +++ src/libxml++/exceptions/parse_error.h 2016-11-06 17:18:04 +0000 | |||
1301 | @@ -0,0 +1,42 @@ | |||
1302 | 1 | /* parse_error.h | ||
1303 | 2 | * | ||
1304 | 3 | * Copyright (C) 2002 The libxml++ development team | ||
1305 | 4 | * | ||
1306 | 5 | * This library is free software; you can redistribute it and/or | ||
1307 | 6 | * modify it under the terms of the GNU Library General Public | ||
1308 | 7 | * License as published by the Free Software Foundation; either | ||
1309 | 8 | * version 2 of the License, or (at your option) any later version. | ||
1310 | 9 | * | ||
1311 | 10 | * This library is distributed in the hope that it will be useful, | ||
1312 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1313 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1314 | 13 | * Library General Public License for more details. | ||
1315 | 14 | * | ||
1316 | 15 | * You should have received a copy of the GNU Library General Public | ||
1317 | 16 | * License along with this library; if not, write to the Free | ||
1318 | 17 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
1319 | 18 | */ | ||
1320 | 19 | |||
1321 | 20 | #ifndef __LIBXMLPP_PARSE_ERROR_H | ||
1322 | 21 | #define __LIBXMLPP_PARSE_ERROR_H | ||
1323 | 22 | |||
1324 | 23 | #include <libxml++/exceptions/exception.h> | ||
1325 | 24 | |||
1326 | 25 | namespace xmlpp | ||
1327 | 26 | { | ||
1328 | 27 | |||
1329 | 28 | /** This exception will be thrown when the parser encounters an error in the XML document. | ||
1330 | 29 | */ | ||
1331 | 30 | class parse_error : public exception | ||
1332 | 31 | { | ||
1333 | 32 | public: | ||
1334 | 33 | explicit parse_error(const Glib::ustring& message); | ||
1335 | 34 | ~parse_error() noexcept override; | ||
1336 | 35 | |||
1337 | 36 | void raise() const override; | ||
1338 | 37 | exception* clone() const override; | ||
1339 | 38 | }; | ||
1340 | 39 | |||
1341 | 40 | } // namespace xmlpp | ||
1342 | 41 | |||
1343 | 42 | #endif // __LIBXMLPP_PARSE_ERROR_H | ||
1344 | 0 | 43 | ||
1345 | === added file 'src/libxml++/exceptions/validity_error.cc' | |||
1346 | --- src/libxml++/exceptions/validity_error.cc 1970-01-01 00:00:00 +0000 | |||
1347 | +++ src/libxml++/exceptions/validity_error.cc 2016-11-06 17:18:04 +0000 | |||
1348 | @@ -0,0 +1,23 @@ | |||
1349 | 1 | #include "validity_error.h" | ||
1350 | 2 | |||
1351 | 3 | namespace xmlpp { | ||
1352 | 4 | |||
1353 | 5 | validity_error::validity_error(const Glib::ustring& message) | ||
1354 | 6 | : parse_error(message) | ||
1355 | 7 | { | ||
1356 | 8 | } | ||
1357 | 9 | |||
1358 | 10 | validity_error::~validity_error() noexcept | ||
1359 | 11 | {} | ||
1360 | 12 | |||
1361 | 13 | void validity_error::raise() const | ||
1362 | 14 | { | ||
1363 | 15 | throw *this; | ||
1364 | 16 | } | ||
1365 | 17 | |||
1366 | 18 | exception* validity_error::clone() const | ||
1367 | 19 | { | ||
1368 | 20 | return new validity_error(*this); | ||
1369 | 21 | } | ||
1370 | 22 | |||
1371 | 23 | } //namespace xmlpp | ||
1372 | 0 | 24 | ||
1373 | === added file 'src/libxml++/exceptions/validity_error.h' | |||
1374 | --- src/libxml++/exceptions/validity_error.h 1970-01-01 00:00:00 +0000 | |||
1375 | +++ src/libxml++/exceptions/validity_error.h 2016-11-06 17:18:04 +0000 | |||
1376 | @@ -0,0 +1,42 @@ | |||
1377 | 1 | /* validity_error.h | ||
1378 | 2 | * | ||
1379 | 3 | * Copyright (C) 2002 The libxml++ development team | ||
1380 | 4 | * | ||
1381 | 5 | * This library is free software; you can redistribute it and/or | ||
1382 | 6 | * modify it under the terms of the GNU Library General Public | ||
1383 | 7 | * License as published by the Free Software Foundation; either | ||
1384 | 8 | * version 2 of the License, or (at your option) any later version. | ||
1385 | 9 | * | ||
1386 | 10 | * This library is distributed in the hope that it will be useful, | ||
1387 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1388 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1389 | 13 | * Library General Public License for more details. | ||
1390 | 14 | * | ||
1391 | 15 | * You should have received a copy of the GNU Library General Public | ||
1392 | 16 | * License along with this library; if not, write to the Free | ||
1393 | 17 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
1394 | 18 | */ | ||
1395 | 19 | |||
1396 | 20 | #ifndef __LIBXMLPP_VALIDITY_ERROR_H | ||
1397 | 21 | #define __LIBXMLPP_VALIDITY_ERROR_H | ||
1398 | 22 | |||
1399 | 23 | #include <libxml++/exceptions/parse_error.h> | ||
1400 | 24 | |||
1401 | 25 | namespace xmlpp | ||
1402 | 26 | { | ||
1403 | 27 | |||
1404 | 28 | /** This exception will be thrown when the parser encounters a validity error in the XML document. | ||
1405 | 29 | */ | ||
1406 | 30 | class validity_error : public parse_error | ||
1407 | 31 | { | ||
1408 | 32 | public: | ||
1409 | 33 | explicit validity_error(const Glib::ustring& message); | ||
1410 | 34 | ~validity_error() noexcept override; | ||
1411 | 35 | |||
1412 | 36 | void raise() const override; | ||
1413 | 37 | exception* clone() const override; | ||
1414 | 38 | }; | ||
1415 | 39 | |||
1416 | 40 | } // namespace xmlpp | ||
1417 | 41 | |||
1418 | 42 | #endif // __LIBXMLPP_VALIDITY_ERROR_H | ||
1419 | 0 | 43 | ||
1420 | === added file 'src/libxml++/exceptions/wrapped_exception.cc' | |||
1421 | --- src/libxml++/exceptions/wrapped_exception.cc 1970-01-01 00:00:00 +0000 | |||
1422 | +++ src/libxml++/exceptions/wrapped_exception.cc 2016-11-06 17:18:04 +0000 | |||
1423 | @@ -0,0 +1,46 @@ | |||
1424 | 1 | /* Copyright (C) 2015 The libxml++ development team | ||
1425 | 2 | * | ||
1426 | 3 | * This library is free software; you can redistribute it and/or | ||
1427 | 4 | * modify it under the terms of the GNU Lesser General Public | ||
1428 | 5 | * License as published by the Free Software Foundation; either | ||
1429 | 6 | * version 2.1 of the License, or (at your option) any later version. | ||
1430 | 7 | * | ||
1431 | 8 | * This library is distributed in the hope that it will be useful, | ||
1432 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1433 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1434 | 11 | * Lesser General Public License for more details. | ||
1435 | 12 | * | ||
1436 | 13 | * You should have received a copy of the GNU Lesser General Public | ||
1437 | 14 | * License along with this library; if not, write to the Free Software | ||
1438 | 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
1439 | 16 | */ | ||
1440 | 17 | |||
1441 | 18 | #include "wrapped_exception.h" | ||
1442 | 19 | |||
1443 | 20 | namespace xmlpp | ||
1444 | 21 | { | ||
1445 | 22 | |||
1446 | 23 | #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR | ||
1447 | 24 | |||
1448 | 25 | wrapped_exception::wrapped_exception(std::exception_ptr exception_ptr) | ||
1449 | 26 | : exception("Wrapped exception"), exception_ptr_(exception_ptr) | ||
1450 | 27 | { | ||
1451 | 28 | } | ||
1452 | 29 | |||
1453 | 30 | wrapped_exception::~wrapped_exception() noexcept | ||
1454 | 31 | { | ||
1455 | 32 | } | ||
1456 | 33 | |||
1457 | 34 | void wrapped_exception::raise() const | ||
1458 | 35 | { | ||
1459 | 36 | std::rethrow_exception(exception_ptr_); | ||
1460 | 37 | } | ||
1461 | 38 | |||
1462 | 39 | exception* wrapped_exception::clone() const | ||
1463 | 40 | { | ||
1464 | 41 | return new wrapped_exception(exception_ptr_); | ||
1465 | 42 | } | ||
1466 | 43 | |||
1467 | 44 | #endif // LIBXMLXX_HAVE_EXCEPTION_PTR | ||
1468 | 45 | |||
1469 | 46 | } // namespace xmlpp | ||
1470 | 0 | 47 | ||
1471 | === added file 'src/libxml++/exceptions/wrapped_exception.h' | |||
1472 | --- src/libxml++/exceptions/wrapped_exception.h 1970-01-01 00:00:00 +0000 | |||
1473 | +++ src/libxml++/exceptions/wrapped_exception.h 2016-11-06 17:18:04 +0000 | |||
1474 | @@ -0,0 +1,56 @@ | |||
1475 | 1 | /* Copyright (C) 2015 The libxml++ development team | ||
1476 | 2 | * | ||
1477 | 3 | * This library is free software; you can redistribute it and/or | ||
1478 | 4 | * modify it under the terms of the GNU Lesser General Public | ||
1479 | 5 | * License as published by the Free Software Foundation; either | ||
1480 | 6 | * version 2.1 of the License, or (at your option) any later version. | ||
1481 | 7 | * | ||
1482 | 8 | * This library is distributed in the hope that it will be useful, | ||
1483 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1484 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1485 | 11 | * Lesser General Public License for more details. | ||
1486 | 12 | * | ||
1487 | 13 | * You should have received a copy of the GNU Lesser General Public | ||
1488 | 14 | * License along with this library; if not, write to the Free Software | ||
1489 | 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
1490 | 16 | */ | ||
1491 | 17 | |||
1492 | 18 | #ifndef __LIBXMLPP_WRAPPED_EXCEPTION_H | ||
1493 | 19 | #define __LIBXMLPP_WRAPPED_EXCEPTION_H | ||
1494 | 20 | |||
1495 | 21 | #include <exception> | ||
1496 | 22 | |||
1497 | 23 | #include <libxml++/exceptions/exception.h> | ||
1498 | 24 | #include <libxml++config.h> | ||
1499 | 25 | |||
1500 | 26 | namespace xmlpp | ||
1501 | 27 | { | ||
1502 | 28 | |||
1503 | 29 | #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR | ||
1504 | 30 | |||
1505 | 31 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | ||
1506 | 32 | /** Helper class for propagating an exception through C code. | ||
1507 | 33 | * Should not be used by applications. | ||
1508 | 34 | * Does not exist in systems that don't support std::exception_ptr. | ||
1509 | 35 | * | ||
1510 | 36 | * @newin{2,40} | ||
1511 | 37 | */ | ||
1512 | 38 | class wrapped_exception : public exception | ||
1513 | 39 | { | ||
1514 | 40 | public: | ||
1515 | 41 | explicit wrapped_exception(std::exception_ptr exception_ptr); | ||
1516 | 42 | ~wrapped_exception() noexcept override; | ||
1517 | 43 | |||
1518 | 44 | void raise() const override; | ||
1519 | 45 | exception* clone() const override; | ||
1520 | 46 | |||
1521 | 47 | private: | ||
1522 | 48 | std::exception_ptr exception_ptr_; | ||
1523 | 49 | }; | ||
1524 | 50 | #endif //DOXYGEN_SHOULD_SKIP_THIS | ||
1525 | 51 | |||
1526 | 52 | #endif // LIBXMLXX_HAVE_EXCEPTION_PTR | ||
1527 | 53 | |||
1528 | 54 | } // namespace xmlpp | ||
1529 | 55 | |||
1530 | 56 | #endif // __LIBXMLPP_WRAPPED_EXCEPTION_H | ||
1531 | 0 | 57 | ||
1532 | === added directory 'src/libxml++/io' | |||
1533 | === added file 'src/libxml++/io/istreamparserinputbuffer.cc' | |||
1534 | --- src/libxml++/io/istreamparserinputbuffer.cc 1970-01-01 00:00:00 +0000 | |||
1535 | +++ src/libxml++/io/istreamparserinputbuffer.cc 2016-11-06 17:18:04 +0000 | |||
1536 | @@ -0,0 +1,44 @@ | |||
1537 | 1 | /* istreamparserinputbuffer | ||
1538 | 2 | * this file is part of libxml++ | ||
1539 | 3 | * | ||
1540 | 4 | * copyright (C) 2003 by libxml++ developer's team | ||
1541 | 5 | * | ||
1542 | 6 | * this file is covered by the GNU Lesser General Public License, | ||
1543 | 7 | * which should be included with libxml++ as the file COPYING. | ||
1544 | 8 | */ | ||
1545 | 9 | |||
1546 | 10 | #include <libxml++/io/istreamparserinputbuffer.h> | ||
1547 | 11 | |||
1548 | 12 | namespace xmlpp | ||
1549 | 13 | { | ||
1550 | 14 | IStreamParserInputBuffer::IStreamParserInputBuffer( | ||
1551 | 15 | std::istream & input) | ||
1552 | 16 | : ParserInputBuffer(), input_(input) | ||
1553 | 17 | { | ||
1554 | 18 | } | ||
1555 | 19 | |||
1556 | 20 | IStreamParserInputBuffer::~IStreamParserInputBuffer() | ||
1557 | 21 | { | ||
1558 | 22 | } | ||
1559 | 23 | |||
1560 | 24 | int IStreamParserInputBuffer::do_read( | ||
1561 | 25 | char * buffer, | ||
1562 | 26 | int len) | ||
1563 | 27 | { | ||
1564 | 28 | int l=0; | ||
1565 | 29 | if(input_) | ||
1566 | 30 | { | ||
1567 | 31 | // This is the correct statement - but gcc 2.95.3 lacks this method | ||
1568 | 32 | //l = input_.readsome(buffer, len); | ||
1569 | 33 | input_.read(buffer, len); | ||
1570 | 34 | l = input_.gcount(); | ||
1571 | 35 | } | ||
1572 | 36 | |||
1573 | 37 | return l; | ||
1574 | 38 | } | ||
1575 | 39 | |||
1576 | 40 | bool IStreamParserInputBuffer::do_close() | ||
1577 | 41 | { | ||
1578 | 42 | return input_.good(); | ||
1579 | 43 | } | ||
1580 | 44 | } | ||
1581 | 0 | 45 | ||
1582 | === added file 'src/libxml++/io/istreamparserinputbuffer.h' | |||
1583 | --- src/libxml++/io/istreamparserinputbuffer.h 1970-01-01 00:00:00 +0000 | |||
1584 | +++ src/libxml++/io/istreamparserinputbuffer.h 2016-11-06 17:18:04 +0000 | |||
1585 | @@ -0,0 +1,36 @@ | |||
1586 | 1 | /* inputstreamparserinputbuffer.h | ||
1587 | 2 | * this file is part of libxml++ | ||
1588 | 3 | * | ||
1589 | 4 | * copyright (C) 2003 by libxml++ developer's team | ||
1590 | 5 | * | ||
1591 | 6 | * this file is covered by the GNU Lesser General Public License, | ||
1592 | 7 | * which should be included with libxml++ as the file COPYING. | ||
1593 | 8 | */ | ||
1594 | 9 | |||
1595 | 10 | #ifndef __LIBXMLPP_ISTREAMPARSEROUTPUTBUFFER_H | ||
1596 | 11 | #define __LIBXMLPP_ISTREAMPARSEROUTPUTBUFFER_H | ||
1597 | 12 | |||
1598 | 13 | #include <libxml++/io/parserinputbuffer.h> | ||
1599 | 14 | |||
1600 | 15 | #include <istream> | ||
1601 | 16 | |||
1602 | 17 | namespace xmlpp | ||
1603 | 18 | { | ||
1604 | 19 | class IStreamParserInputBuffer: public ParserInputBuffer | ||
1605 | 20 | { | ||
1606 | 21 | public: | ||
1607 | 22 | /** | ||
1608 | 23 | * @param input The istream datas will be read from | ||
1609 | 24 | */ | ||
1610 | 25 | IStreamParserInputBuffer(std::istream& input); | ||
1611 | 26 | ~IStreamParserInputBuffer() override; | ||
1612 | 27 | |||
1613 | 28 | private: | ||
1614 | 29 | int do_read(char * buffer, int len) override; | ||
1615 | 30 | bool do_close() override; | ||
1616 | 31 | |||
1617 | 32 | std::istream& input_; | ||
1618 | 33 | }; | ||
1619 | 34 | } | ||
1620 | 35 | |||
1621 | 36 | #endif | ||
1622 | 0 | 37 | ||
1623 | === added file 'src/libxml++/io/parserinputbuffer.cc' | |||
1624 | --- src/libxml++/io/parserinputbuffer.cc 1970-01-01 00:00:00 +0000 | |||
1625 | +++ src/libxml++/io/parserinputbuffer.cc 2016-11-06 17:18:04 +0000 | |||
1626 | @@ -0,0 +1,84 @@ | |||
1627 | 1 | /* parserinputbuffer.cc | ||
1628 | 2 | * this file is part of libxml++ | ||
1629 | 3 | * | ||
1630 | 4 | * copyright (C) 2003 by libxml++ developer's team | ||
1631 | 5 | * | ||
1632 | 6 | * this file is covered by the GNU Lesser General Public License, | ||
1633 | 7 | * which should be included with libxml++ as the file COPYING. | ||
1634 | 8 | */ | ||
1635 | 9 | |||
1636 | 10 | #include <libxml++/io/parserinputbuffer.h> | ||
1637 | 11 | #include <libxml++/exceptions/internal_error.h> | ||
1638 | 12 | |||
1639 | 13 | #include <libxml/globals.h> //Needed by libxml/xmlIO.h | ||
1640 | 14 | #include <libxml/xmlIO.h> | ||
1641 | 15 | |||
1642 | 16 | namespace xmlpp | ||
1643 | 17 | { | ||
1644 | 18 | |||
1645 | 19 | struct ParserInputBufferCallback | ||
1646 | 20 | { | ||
1647 | 21 | static int on_read(void * context, char * buffer, int len) | ||
1648 | 22 | { | ||
1649 | 23 | auto tmp = static_cast<ParserInputBuffer*>(context); | ||
1650 | 24 | return tmp->do_read(buffer, len); | ||
1651 | 25 | } | ||
1652 | 26 | |||
1653 | 27 | static int on_close(void * context) | ||
1654 | 28 | { | ||
1655 | 29 | auto tmp = static_cast<ParserInputBuffer*>(context); | ||
1656 | 30 | return tmp->do_close(); | ||
1657 | 31 | } | ||
1658 | 32 | }; | ||
1659 | 33 | |||
1660 | 34 | |||
1661 | 35 | ParserInputBuffer::ParserInputBuffer() | ||
1662 | 36 | { | ||
1663 | 37 | impl_ = xmlParserInputBufferCreateIO( | ||
1664 | 38 | &ParserInputBufferCallback::on_read, | ||
1665 | 39 | &ParserInputBufferCallback::on_close, | ||
1666 | 40 | static_cast<void*>(this), | ||
1667 | 41 | XML_CHAR_ENCODING_NONE); | ||
1668 | 42 | if (!impl_) | ||
1669 | 43 | { | ||
1670 | 44 | throw internal_error("Cannot initialise underlying xmlParserInputBuffer"); | ||
1671 | 45 | } | ||
1672 | 46 | } | ||
1673 | 47 | |||
1674 | 48 | ParserInputBuffer::~ParserInputBuffer() | ||
1675 | 49 | { | ||
1676 | 50 | } | ||
1677 | 51 | |||
1678 | 52 | bool ParserInputBuffer::on_close() | ||
1679 | 53 | { | ||
1680 | 54 | bool result = do_close(); | ||
1681 | 55 | // the underlying structure is being freed by libxml, the pointer will soon be | ||
1682 | 56 | // invalid. | ||
1683 | 57 | impl_ = nullptr; | ||
1684 | 58 | |||
1685 | 59 | return result; | ||
1686 | 60 | } | ||
1687 | 61 | |||
1688 | 62 | int ParserInputBuffer::on_read( | ||
1689 | 63 | char * buffer, | ||
1690 | 64 | int len) | ||
1691 | 65 | { | ||
1692 | 66 | return do_read(buffer, len); | ||
1693 | 67 | } | ||
1694 | 68 | |||
1695 | 69 | bool ParserInputBuffer::do_close() | ||
1696 | 70 | { | ||
1697 | 71 | return true; | ||
1698 | 72 | } | ||
1699 | 73 | |||
1700 | 74 | _xmlParserInputBuffer* ParserInputBuffer::cobj() noexcept | ||
1701 | 75 | { | ||
1702 | 76 | return impl_; | ||
1703 | 77 | } | ||
1704 | 78 | |||
1705 | 79 | const _xmlParserInputBuffer* ParserInputBuffer::cobj() const noexcept | ||
1706 | 80 | { | ||
1707 | 81 | return impl_; | ||
1708 | 82 | } | ||
1709 | 83 | |||
1710 | 84 | } | ||
1711 | 0 | 85 | ||
1712 | === added file 'src/libxml++/io/parserinputbuffer.h' | |||
1713 | --- src/libxml++/io/parserinputbuffer.h 1970-01-01 00:00:00 +0000 | |||
1714 | +++ src/libxml++/io/parserinputbuffer.h 2016-11-06 17:18:04 +0000 | |||
1715 | @@ -0,0 +1,75 @@ | |||
1716 | 1 | /* parserinputbuffer.h | ||
1717 | 2 | * this file is part of libxml++ | ||
1718 | 3 | * | ||
1719 | 4 | * copyright (C) 2003 by libxml++ developer's team | ||
1720 | 5 | * | ||
1721 | 6 | * this file is covered by the GNU Lesser General Public License, | ||
1722 | 7 | * which should be included with libxml++ as the file COPYING. | ||
1723 | 8 | */ | ||
1724 | 9 | |||
1725 | 10 | #ifndef __LIBXMLPP_PARSERINPUTBUFFER_H | ||
1726 | 11 | #define __LIBXMLPP_PARSERINPUTBUFFER_H | ||
1727 | 12 | |||
1728 | 13 | #include <string> | ||
1729 | 14 | #include <libxml++/noncopyable.h> | ||
1730 | 15 | |||
1731 | 16 | extern "C" | ||
1732 | 17 | { | ||
1733 | 18 | struct _xmlParserInputBuffer; | ||
1734 | 19 | } | ||
1735 | 20 | |||
1736 | 21 | namespace xmlpp | ||
1737 | 22 | { | ||
1738 | 23 | struct ParserInputBufferCallback; | ||
1739 | 24 | |||
1740 | 25 | /** Base class for xmlParserInputBuffer wrapper | ||
1741 | 26 | * | ||
1742 | 27 | * It can be derived from to create a new output buffer. | ||
1743 | 28 | * A child class has to override do_write(), and possibly | ||
1744 | 29 | * do_close() if some actions are required before buffer closing. | ||
1745 | 30 | */ | ||
1746 | 31 | class ParserInputBuffer: public NonCopyable | ||
1747 | 32 | { | ||
1748 | 33 | public: | ||
1749 | 34 | ParserInputBuffer(); | ||
1750 | 35 | ~ParserInputBuffer() override; | ||
1751 | 36 | |||
1752 | 37 | public: | ||
1753 | 38 | /** gives an access to the underlying libxml structure to the children | ||
1754 | 39 | */ | ||
1755 | 40 | _xmlParserInputBuffer* cobj() noexcept; | ||
1756 | 41 | |||
1757 | 42 | /** gives an access to the underlying libxml structure to the children | ||
1758 | 43 | */ | ||
1759 | 44 | const _xmlParserInputBuffer* cobj() const noexcept; | ||
1760 | 45 | |||
1761 | 46 | private: | ||
1762 | 47 | int on_read(char * buffer, int len); | ||
1763 | 48 | bool on_close(); | ||
1764 | 49 | |||
1765 | 50 | /** Function called when some data are read from the buffer. | ||
1766 | 51 | * @param buffer The datas encoded in the charset given to the constructor | ||
1767 | 52 | * @param len bytes to read | ||
1768 | 53 | * @return Number of bytes read | ||
1769 | 54 | * | ||
1770 | 55 | * This function MUST be overriden in derived classes. | ||
1771 | 56 | */ | ||
1772 | 57 | virtual int do_read(char * buffer, int len) = 0; | ||
1773 | 58 | |||
1774 | 59 | /** Function called before closing the buffer. | ||
1775 | 60 | * Derived classes should override it if some actions are required before | ||
1776 | 61 | * closing the buffer, instead of doing them in the destructor. | ||
1777 | 62 | */ | ||
1778 | 63 | virtual bool do_close(); | ||
1779 | 64 | |||
1780 | 65 | /** | ||
1781 | 66 | * Underlying libxml2 structure. | ||
1782 | 67 | */ | ||
1783 | 68 | _xmlParserInputBuffer* impl_; | ||
1784 | 69 | |||
1785 | 70 | friend struct ParserInputBufferCallback; | ||
1786 | 71 | }; | ||
1787 | 72 | |||
1788 | 73 | } | ||
1789 | 74 | |||
1790 | 75 | #endif | ||
1791 | 0 | 76 | ||
1792 | === added file 'src/libxml++/keepblanks.cc' | |||
1793 | --- src/libxml++/keepblanks.cc 1970-01-01 00:00:00 +0000 | |||
1794 | +++ src/libxml++/keepblanks.cc 2016-11-06 17:18:04 +0000 | |||
1795 | @@ -0,0 +1,29 @@ | |||
1796 | 1 | /* keepblanks.cc | ||
1797 | 2 | * libxml++ and this file are | ||
1798 | 3 | * copyright (C) 2003 by The libxml++ Development Team, and | ||
1799 | 4 | * are covered by the GNU Lesser General Public License, which should be | ||
1800 | 5 | * included with libxml++ as the file COPYING. | ||
1801 | 6 | */ | ||
1802 | 7 | |||
1803 | 8 | #include <libxml++/keepblanks.h> | ||
1804 | 9 | |||
1805 | 10 | #include <libxml/globals.h> | ||
1806 | 11 | |||
1807 | 12 | namespace xmlpp | ||
1808 | 13 | { | ||
1809 | 14 | #if _MSC_VER == 1200 // detect MSVC 6.0 | ||
1810 | 15 | const bool KeepBlanks::Default = true; | ||
1811 | 16 | #endif | ||
1812 | 17 | |||
1813 | 18 | KeepBlanks::KeepBlanks(bool value) noexcept | ||
1814 | 19 | { | ||
1815 | 20 | oldIndentTreeOutput_ = xmlIndentTreeOutput; | ||
1816 | 21 | oldKeepBlanksDefault_ = xmlKeepBlanksDefault( value?1:0 ); | ||
1817 | 22 | } | ||
1818 | 23 | |||
1819 | 24 | KeepBlanks::~KeepBlanks() noexcept | ||
1820 | 25 | { | ||
1821 | 26 | xmlKeepBlanksDefault(oldKeepBlanksDefault_); | ||
1822 | 27 | xmlIndentTreeOutput = oldIndentTreeOutput_; | ||
1823 | 28 | } | ||
1824 | 29 | } | ||
1825 | 0 | 30 | ||
1826 | === added file 'src/libxml++/keepblanks.h' | |||
1827 | --- src/libxml++/keepblanks.h 1970-01-01 00:00:00 +0000 | |||
1828 | +++ src/libxml++/keepblanks.h 2016-11-06 17:18:04 +0000 | |||
1829 | @@ -0,0 +1,35 @@ | |||
1830 | 1 | /* keepblanks.h | ||
1831 | 2 | * libxml++ and this file are | ||
1832 | 3 | * copyright (C) 2003 by The libxml++ Development Team, and | ||
1833 | 4 | * are covered by the GNU Lesser General Public License, which should be | ||
1834 | 5 | * included with libxml++ as the file COPYING. | ||
1835 | 6 | */ | ||
1836 | 7 | |||
1837 | 8 | #ifndef __LIBXMLPP_KEEPBLANKS_H | ||
1838 | 9 | #define __LIBXMLPP_KEEPBLANKS_H | ||
1839 | 10 | |||
1840 | 11 | namespace xmlpp | ||
1841 | 12 | { | ||
1842 | 13 | /** This class sets KeepBlanksDefault and IndentTreeOutput of libxmlpp | ||
1843 | 14 | * and restores their initial values in its destructor. As a consequence | ||
1844 | 15 | * the wanted setting is kept during instance lifetime. | ||
1845 | 16 | */ | ||
1846 | 17 | class KeepBlanks { | ||
1847 | 18 | public: | ||
1848 | 19 | #if _MSC_VER == 1200 // detect MSVC 6.0 | ||
1849 | 20 | static const bool Default; | ||
1850 | 21 | #else | ||
1851 | 22 | static const bool Default = true; | ||
1852 | 23 | #endif | ||
1853 | 24 | |||
1854 | 25 | public: | ||
1855 | 26 | KeepBlanks(bool value) noexcept; | ||
1856 | 27 | ~KeepBlanks() noexcept; | ||
1857 | 28 | |||
1858 | 29 | private: | ||
1859 | 30 | int oldKeepBlanksDefault_; | ||
1860 | 31 | int oldIndentTreeOutput_; | ||
1861 | 32 | }; | ||
1862 | 33 | } | ||
1863 | 34 | |||
1864 | 35 | #endif // __LIBXMLPP_KEEPBLANKS_H | ||
1865 | 0 | 36 | ||
1866 | === added file 'src/libxml++/libxml++config.h' | |||
1867 | --- src/libxml++/libxml++config.h 1970-01-01 00:00:00 +0000 | |||
1868 | +++ src/libxml++/libxml++config.h 2016-11-06 17:18:04 +0000 | |||
1869 | @@ -0,0 +1,26 @@ | |||
1870 | 1 | /* libxml++config.h. Generated from libxml++config.h.in by configure. */ | ||
1871 | 2 | #ifndef _LIBXMLPP_CONFIG_H | ||
1872 | 3 | #define _LIBXMLPP_CONFIG_H | ||
1873 | 4 | |||
1874 | 5 | #include <glibmmconfig.h> | ||
1875 | 6 | |||
1876 | 7 | /* Define to omit deprecated API from the library. */ | ||
1877 | 8 | /* #undef LIBXMLXX_DISABLE_DEPRECATED */ | ||
1878 | 9 | |||
1879 | 10 | /* Defined if the C++ library supports std::exception_ptr. */ | ||
1880 | 11 | #define LIBXMLXX_HAVE_EXCEPTION_PTR 1 | ||
1881 | 12 | |||
1882 | 13 | /* Major version number of libxml++. */ | ||
1883 | 14 | #define LIBXMLXX_MAJOR_VERSION 3 | ||
1884 | 15 | |||
1885 | 16 | /* Minor version number of libxml++. */ | ||
1886 | 17 | #define LIBXMLXX_MINOR_VERSION 0 | ||
1887 | 18 | |||
1888 | 19 | /* Micro version number of libxml++. */ | ||
1889 | 20 | #define LIBXMLXX_MICRO_VERSION 0 | ||
1890 | 21 | |||
1891 | 22 | /* Create static library */ | ||
1892 | 23 | #define LIBXMLPP_API | ||
1893 | 24 | |||
1894 | 25 | #endif /* _LIBXMLPP_CONFIG_H */ | ||
1895 | 26 | |||
1896 | 0 | 27 | ||
1897 | === added file 'src/libxml++/noncopyable.cc' | |||
1898 | --- src/libxml++/noncopyable.cc 1970-01-01 00:00:00 +0000 | |||
1899 | +++ src/libxml++/noncopyable.cc 2016-11-06 17:18:04 +0000 | |||
1900 | @@ -0,0 +1,21 @@ | |||
1901 | 1 | /* noncopyable.cc | ||
1902 | 2 | * libxml++ and this file are | ||
1903 | 3 | * copyright (C) 2000 by The libxml++ Development Team, and | ||
1904 | 4 | * are covered by the GNU Lesser General Public License, which should be | ||
1905 | 5 | * included with libxml++ as the file COPYING. | ||
1906 | 6 | */ | ||
1907 | 7 | |||
1908 | 8 | #include <libxml++/noncopyable.h> | ||
1909 | 9 | |||
1910 | 10 | namespace xmlpp | ||
1911 | 11 | { | ||
1912 | 12 | |||
1913 | 13 | NonCopyable::NonCopyable() noexcept | ||
1914 | 14 | { | ||
1915 | 15 | } | ||
1916 | 16 | |||
1917 | 17 | NonCopyable::~NonCopyable() | ||
1918 | 18 | { | ||
1919 | 19 | } | ||
1920 | 20 | |||
1921 | 21 | } //namespace xmlpp | ||
1922 | 0 | 22 | ||
1923 | === added file 'src/libxml++/noncopyable.h' | |||
1924 | --- src/libxml++/noncopyable.h 1970-01-01 00:00:00 +0000 | |||
1925 | +++ src/libxml++/noncopyable.h 2016-11-06 17:18:04 +0000 | |||
1926 | @@ -0,0 +1,31 @@ | |||
1927 | 1 | /* noncopyable.h | ||
1928 | 2 | * libxml++ and this file are | ||
1929 | 3 | * copyright (C) 2000 by The libxml++ Development Team, and | ||
1930 | 4 | * are covered by the GNU Lesser General Public License, which should be | ||
1931 | 5 | * included with libxml++ as the file COPYING. | ||
1932 | 6 | */ | ||
1933 | 7 | |||
1934 | 8 | #ifndef __LIBXMLPP_NONCOPYABLE_H | ||
1935 | 9 | #define __LIBXMLPP_NONCOPYABLE_H | ||
1936 | 10 | |||
1937 | 11 | namespace xmlpp | ||
1938 | 12 | { | ||
1939 | 13 | |||
1940 | 14 | /** A base for classes which cannot be copied or moved. | ||
1941 | 15 | */ | ||
1942 | 16 | class NonCopyable | ||
1943 | 17 | { | ||
1944 | 18 | protected: | ||
1945 | 19 | NonCopyable() noexcept; | ||
1946 | 20 | virtual ~NonCopyable(); | ||
1947 | 21 | |||
1948 | 22 | NonCopyable(const NonCopyable&) = delete; | ||
1949 | 23 | NonCopyable& operator=(const NonCopyable&) = delete; | ||
1950 | 24 | NonCopyable(NonCopyable&&) = delete; | ||
1951 | 25 | NonCopyable& operator=(NonCopyable&&) = delete; | ||
1952 | 26 | }; | ||
1953 | 27 | |||
1954 | 28 | } // namespace xmlpp | ||
1955 | 29 | |||
1956 | 30 | #endif //__LIBXMLPP_NONCOPYABLE_H | ||
1957 | 31 | |||
1958 | 0 | 32 | ||
1959 | === added directory 'src/libxml++/parsers' | |||
1960 | === added file 'src/libxml++/parsers/parser.cc' | |||
1961 | --- src/libxml++/parsers/parser.cc 1970-01-01 00:00:00 +0000 | |||
1962 | +++ src/libxml++/parsers/parser.cc 2016-11-06 17:18:04 +0000 | |||
1963 | @@ -0,0 +1,365 @@ | |||
1964 | 1 | /* parser.cc | ||
1965 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
1966 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
1967 | 4 | * included with libxml++ as the file COPYING. | ||
1968 | 5 | */ | ||
1969 | 6 | |||
1970 | 7 | #include "libxml++/exceptions/wrapped_exception.h" | ||
1971 | 8 | #include "libxml++/parsers/parser.h" | ||
1972 | 9 | |||
1973 | 10 | #include <libxml/parser.h> | ||
1974 | 11 | |||
1975 | 12 | namespace xmlpp | ||
1976 | 13 | { | ||
1977 | 14 | |||
1978 | 15 | struct Parser::Impl | ||
1979 | 16 | { | ||
1980 | 17 | Impl() | ||
1981 | 18 | : | ||
1982 | 19 | throw_messages_(true), validate_(false), substitute_entities_(false), | ||
1983 | 20 | include_default_attributes_(false), set_options_(0), clear_options_(0) | ||
1984 | 21 | {} | ||
1985 | 22 | |||
1986 | 23 | // Built gradually - used in an exception at the end of parsing. | ||
1987 | 24 | Glib::ustring parser_error_; | ||
1988 | 25 | Glib::ustring parser_warning_; | ||
1989 | 26 | Glib::ustring validate_error_; | ||
1990 | 27 | Glib::ustring validate_warning_; | ||
1991 | 28 | |||
1992 | 29 | bool throw_messages_; | ||
1993 | 30 | bool validate_; | ||
1994 | 31 | bool substitute_entities_; | ||
1995 | 32 | bool include_default_attributes_; | ||
1996 | 33 | int set_options_; | ||
1997 | 34 | int clear_options_; | ||
1998 | 35 | }; | ||
1999 | 36 | |||
2000 | 37 | Parser::Parser() | ||
2001 | 38 | : context_(nullptr), exception_(nullptr), pimpl_(new Impl) | ||
2002 | 39 | { | ||
2003 | 40 | } | ||
2004 | 41 | |||
2005 | 42 | Parser::~Parser() | ||
2006 | 43 | { | ||
2007 | 44 | release_underlying(); | ||
2008 | 45 | } | ||
2009 | 46 | |||
2010 | 47 | void Parser::set_validate(bool val) noexcept | ||
2011 | 48 | { | ||
2012 | 49 | pimpl_->validate_ = val; | ||
2013 | 50 | } | ||
2014 | 51 | |||
2015 | 52 | bool Parser::get_validate() const noexcept | ||
2016 | 53 | { | ||
2017 | 54 | return pimpl_->validate_; | ||
2018 | 55 | } | ||
2019 | 56 | |||
2020 | 57 | void Parser::set_substitute_entities(bool val) noexcept | ||
2021 | 58 | { | ||
2022 | 59 | pimpl_->substitute_entities_ = val; | ||
2023 | 60 | } | ||
2024 | 61 | |||
2025 | 62 | bool Parser::get_substitute_entities() const noexcept | ||
2026 | 63 | { | ||
2027 | 64 | return pimpl_->substitute_entities_; | ||
2028 | 65 | } | ||
2029 | 66 | |||
2030 | 67 | void Parser::set_throw_messages(bool val) noexcept | ||
2031 | 68 | { | ||
2032 | 69 | pimpl_->throw_messages_ = val; | ||
2033 | 70 | } | ||
2034 | 71 | |||
2035 | 72 | bool Parser::get_throw_messages() const noexcept | ||
2036 | 73 | { | ||
2037 | 74 | return pimpl_->throw_messages_; | ||
2038 | 75 | } | ||
2039 | 76 | |||
2040 | 77 | void Parser::set_include_default_attributes(bool val) noexcept | ||
2041 | 78 | { | ||
2042 | 79 | pimpl_->include_default_attributes_ = val; | ||
2043 | 80 | } | ||
2044 | 81 | |||
2045 | 82 | bool Parser::get_include_default_attributes() noexcept | ||
2046 | 83 | { | ||
2047 | 84 | return pimpl_->include_default_attributes_; | ||
2048 | 85 | } | ||
2049 | 86 | |||
2050 | 87 | void Parser::set_parser_options(int set_options, int clear_options) noexcept | ||
2051 | 88 | { | ||
2052 | 89 | pimpl_->set_options_ = set_options; | ||
2053 | 90 | pimpl_->clear_options_ = clear_options; | ||
2054 | 91 | } | ||
2055 | 92 | |||
2056 | 93 | void Parser::get_parser_options(int& set_options, int& clear_options) noexcept | ||
2057 | 94 | { | ||
2058 | 95 | set_options = pimpl_->set_options_; | ||
2059 | 96 | clear_options = pimpl_->clear_options_; | ||
2060 | 97 | } | ||
2061 | 98 | |||
2062 | 99 | void Parser::initialize_context() | ||
2063 | 100 | { | ||
2064 | 101 | //Clear these temporary buffers: | ||
2065 | 102 | pimpl_->parser_error_.erase(); | ||
2066 | 103 | pimpl_->parser_warning_.erase(); | ||
2067 | 104 | pimpl_->validate_error_.erase(); | ||
2068 | 105 | pimpl_->validate_warning_.erase(); | ||
2069 | 106 | |||
2070 | 107 | //Disactivate any non-standards-compliant libxml1 features. | ||
2071 | 108 | //These are disactivated by default, but if we don't deactivate them for each context | ||
2072 | 109 | //then some other code which uses a global function, such as xmlKeepBlanksDefault(), | ||
2073 | 110 | // could cause this to use the wrong settings: | ||
2074 | 111 | context_->linenumbers = 1; // TRUE - This is the default anyway. | ||
2075 | 112 | |||
2076 | 113 | //Turn on/off validation, entity substitution and default attribute inclusion. | ||
2077 | 114 | int options = context_->options; | ||
2078 | 115 | if (pimpl_->validate_) | ||
2079 | 116 | options |= XML_PARSE_DTDVALID; | ||
2080 | 117 | else | ||
2081 | 118 | options &= ~XML_PARSE_DTDVALID; | ||
2082 | 119 | |||
2083 | 120 | if (pimpl_->substitute_entities_) | ||
2084 | 121 | options |= XML_PARSE_NOENT; | ||
2085 | 122 | else | ||
2086 | 123 | options &= ~XML_PARSE_NOENT; | ||
2087 | 124 | |||
2088 | 125 | if (pimpl_->include_default_attributes_) | ||
2089 | 126 | options |= XML_PARSE_DTDATTR; | ||
2090 | 127 | else | ||
2091 | 128 | options &= ~XML_PARSE_DTDATTR; | ||
2092 | 129 | |||
2093 | 130 | //Turn on/off any parser options. | ||
2094 | 131 | options |= pimpl_->set_options_; | ||
2095 | 132 | options &= ~pimpl_->clear_options_; | ||
2096 | 133 | |||
2097 | 134 | xmlCtxtUseOptions(context_, options); | ||
2098 | 135 | |||
2099 | 136 | if (context_->sax && pimpl_->throw_messages_) | ||
2100 | 137 | { | ||
2101 | 138 | //Tell the parser context about the callbacks. | ||
2102 | 139 | context_->sax->fatalError = &callback_parser_error; | ||
2103 | 140 | context_->sax->error = &callback_parser_error; | ||
2104 | 141 | context_->sax->warning = &callback_parser_warning; | ||
2105 | 142 | } | ||
2106 | 143 | |||
2107 | 144 | if (pimpl_->throw_messages_) | ||
2108 | 145 | { | ||
2109 | 146 | //Tell the validity context about the callbacks: | ||
2110 | 147 | //(These are only called if validation is on - see above) | ||
2111 | 148 | context_->vctxt.error = &callback_validity_error; | ||
2112 | 149 | context_->vctxt.warning = &callback_validity_warning; | ||
2113 | 150 | } | ||
2114 | 151 | |||
2115 | 152 | //Allow callback_error_or_warning() to retrieve the C++ instance: | ||
2116 | 153 | context_->_private = this; | ||
2117 | 154 | } | ||
2118 | 155 | |||
2119 | 156 | void Parser::release_underlying() | ||
2120 | 157 | { | ||
2121 | 158 | if(context_) | ||
2122 | 159 | { | ||
2123 | 160 | context_->_private = nullptr; //Not really necessary. | ||
2124 | 161 | |||
2125 | 162 | if( context_->myDoc != nullptr ) | ||
2126 | 163 | { | ||
2127 | 164 | xmlFreeDoc(context_->myDoc); | ||
2128 | 165 | } | ||
2129 | 166 | |||
2130 | 167 | xmlFreeParserCtxt(context_); | ||
2131 | 168 | context_ = nullptr; | ||
2132 | 169 | } | ||
2133 | 170 | } | ||
2134 | 171 | |||
2135 | 172 | void Parser::on_parser_error(const Glib::ustring& message) | ||
2136 | 173 | { | ||
2137 | 174 | //Throw an exception later when the whole message has been received: | ||
2138 | 175 | pimpl_->parser_error_ += message; | ||
2139 | 176 | } | ||
2140 | 177 | |||
2141 | 178 | void Parser::on_parser_warning(const Glib::ustring& message) | ||
2142 | 179 | { | ||
2143 | 180 | //Throw an exception later when the whole message has been received: | ||
2144 | 181 | pimpl_->parser_warning_ += message; | ||
2145 | 182 | } | ||
2146 | 183 | void Parser::on_validity_error(const Glib::ustring& message) | ||
2147 | 184 | { | ||
2148 | 185 | //Throw an exception later when the whole message has been received: | ||
2149 | 186 | pimpl_->validate_error_ += message; | ||
2150 | 187 | } | ||
2151 | 188 | |||
2152 | 189 | void Parser::on_validity_warning(const Glib::ustring& message) | ||
2153 | 190 | { | ||
2154 | 191 | //Throw an exception later when the whole message has been received: | ||
2155 | 192 | pimpl_->validate_warning_ += message; | ||
2156 | 193 | } | ||
2157 | 194 | |||
2158 | 195 | void Parser::check_for_error_and_warning_messages() | ||
2159 | 196 | { | ||
2160 | 197 | Glib::ustring msg(exception_ ? exception_->what() : ""); | ||
2161 | 198 | bool parser_msg = false; | ||
2162 | 199 | bool validity_msg = false; | ||
2163 | 200 | |||
2164 | 201 | if (!pimpl_->parser_error_.empty()) | ||
2165 | 202 | { | ||
2166 | 203 | parser_msg = true; | ||
2167 | 204 | msg += "\nParser error:\n" + pimpl_->parser_error_; | ||
2168 | 205 | pimpl_->parser_error_.erase(); | ||
2169 | 206 | } | ||
2170 | 207 | |||
2171 | 208 | if (!pimpl_->parser_warning_.empty()) | ||
2172 | 209 | { | ||
2173 | 210 | parser_msg = true; | ||
2174 | 211 | msg += "\nParser warning:\n" + pimpl_->parser_warning_; | ||
2175 | 212 | pimpl_->parser_warning_.erase(); | ||
2176 | 213 | } | ||
2177 | 214 | |||
2178 | 215 | if (!pimpl_->validate_error_.empty()) | ||
2179 | 216 | { | ||
2180 | 217 | validity_msg = true; | ||
2181 | 218 | msg += "\nValidity error:\n" + pimpl_->validate_error_; | ||
2182 | 219 | pimpl_->validate_error_.erase(); | ||
2183 | 220 | } | ||
2184 | 221 | |||
2185 | 222 | if (!pimpl_->validate_warning_.empty()) | ||
2186 | 223 | { | ||
2187 | 224 | validity_msg = true; | ||
2188 | 225 | msg += "\nValidity warning:\n" + pimpl_->validate_warning_; | ||
2189 | 226 | pimpl_->validate_warning_.erase(); | ||
2190 | 227 | } | ||
2191 | 228 | |||
2192 | 229 | if (validity_msg) | ||
2193 | 230 | exception_.reset(new validity_error(msg)); | ||
2194 | 231 | else if (parser_msg) | ||
2195 | 232 | exception_.reset(new parse_error(msg)); | ||
2196 | 233 | } | ||
2197 | 234 | |||
2198 | 235 | //static | ||
2199 | 236 | void Parser::callback_parser_error(void* ctx, const char* msg, ...) | ||
2200 | 237 | { | ||
2201 | 238 | va_list var_args; | ||
2202 | 239 | va_start(var_args, msg); | ||
2203 | 240 | callback_error_or_warning(MsgType::ParserError, ctx, msg, var_args); | ||
2204 | 241 | va_end(var_args); | ||
2205 | 242 | } | ||
2206 | 243 | |||
2207 | 244 | //static | ||
2208 | 245 | void Parser::callback_parser_warning(void* ctx, const char* msg, ...) | ||
2209 | 246 | { | ||
2210 | 247 | va_list var_args; | ||
2211 | 248 | va_start(var_args, msg); | ||
2212 | 249 | callback_error_or_warning(MsgType::ParserWarning, ctx, msg, var_args); | ||
2213 | 250 | va_end(var_args); | ||
2214 | 251 | } | ||
2215 | 252 | |||
2216 | 253 | //static | ||
2217 | 254 | void Parser::callback_validity_error(void* ctx, const char* msg, ...) | ||
2218 | 255 | { | ||
2219 | 256 | va_list var_args; | ||
2220 | 257 | va_start(var_args, msg); | ||
2221 | 258 | callback_error_or_warning(MsgType::ValidityError, ctx, msg, var_args); | ||
2222 | 259 | va_end(var_args); | ||
2223 | 260 | } | ||
2224 | 261 | |||
2225 | 262 | //static | ||
2226 | 263 | void Parser::callback_validity_warning(void* ctx, const char* msg, ...) | ||
2227 | 264 | { | ||
2228 | 265 | va_list var_args; | ||
2229 | 266 | va_start(var_args, msg); | ||
2230 | 267 | callback_error_or_warning(MsgType::ValidityWarning, ctx, msg, var_args); | ||
2231 | 268 | va_end(var_args); | ||
2232 | 269 | } | ||
2233 | 270 | |||
2234 | 271 | //static | ||
2235 | 272 | void Parser::callback_error_or_warning(MsgType msg_type, void* ctx, | ||
2236 | 273 | const char* msg, va_list var_args) | ||
2237 | 274 | { | ||
2238 | 275 | //See xmlHTMLValidityError() in xmllint.c in libxml for more about this: | ||
2239 | 276 | |||
2240 | 277 | auto context = (xmlParserCtxtPtr)ctx; | ||
2241 | 278 | if(context) | ||
2242 | 279 | { | ||
2243 | 280 | auto parser = static_cast<Parser*>(context->_private); | ||
2244 | 281 | if(parser) | ||
2245 | 282 | { | ||
2246 | 283 | auto ubuff = format_xml_error(&context->lastError); | ||
2247 | 284 | if (ubuff.empty()) | ||
2248 | 285 | { | ||
2249 | 286 | // Usually the result of formatting var_args with the format string msg | ||
2250 | 287 | // is the same string as is stored in context->lastError.message. | ||
2251 | 288 | // It's unnecessary to use msg and var_args, if format_xml_error() | ||
2252 | 289 | // returns an error message (as it usually does). | ||
2253 | 290 | |||
2254 | 291 | //Convert the ... to a string: | ||
2255 | 292 | ubuff = format_printf_message(msg, var_args); | ||
2256 | 293 | } | ||
2257 | 294 | |||
2258 | 295 | try | ||
2259 | 296 | { | ||
2260 | 297 | switch (msg_type) | ||
2261 | 298 | { | ||
2262 | 299 | case MsgType::ParserError: | ||
2263 | 300 | parser->on_parser_error(ubuff); | ||
2264 | 301 | break; | ||
2265 | 302 | case MsgType::ParserWarning: | ||
2266 | 303 | parser->on_parser_warning(ubuff); | ||
2267 | 304 | break; | ||
2268 | 305 | case MsgType::ValidityError: | ||
2269 | 306 | parser->on_validity_error(ubuff); | ||
2270 | 307 | break; | ||
2271 | 308 | case MsgType::ValidityWarning: | ||
2272 | 309 | parser->on_validity_warning(ubuff); | ||
2273 | 310 | break; | ||
2274 | 311 | } | ||
2275 | 312 | } | ||
2276 | 313 | catch (...) | ||
2277 | 314 | { | ||
2278 | 315 | parser->handle_exception(); | ||
2279 | 316 | } | ||
2280 | 317 | } | ||
2281 | 318 | } | ||
2282 | 319 | } | ||
2283 | 320 | |||
2284 | 321 | void Parser::handle_exception() | ||
2285 | 322 | { | ||
2286 | 323 | try | ||
2287 | 324 | { | ||
2288 | 325 | throw; // Re-throw current exception | ||
2289 | 326 | } | ||
2290 | 327 | catch (const exception& e) | ||
2291 | 328 | { | ||
2292 | 329 | exception_.reset(e.clone()); | ||
2293 | 330 | } | ||
2294 | 331 | #ifdef LIBXMLXX_HAVE_EXCEPTION_PTR | ||
2295 | 332 | catch (...) | ||
2296 | 333 | { | ||
2297 | 334 | exception_.reset(new wrapped_exception(std::current_exception())); | ||
2298 | 335 | } | ||
2299 | 336 | #else | ||
2300 | 337 | catch (const std::exception& e) | ||
2301 | 338 | { | ||
2302 | 339 | exception_.reset(new exception(e.what())); | ||
2303 | 340 | } | ||
2304 | 341 | catch (...) | ||
2305 | 342 | { | ||
2306 | 343 | exception_.reset(new exception("An exception was thrown that is not derived from std::exception or xmlpp::exception.\n" | ||
2307 | 344 | "It could not be caught and rethrown because this platform does not support std::exception_ptr.")); | ||
2308 | 345 | } | ||
2309 | 346 | #endif | ||
2310 | 347 | |||
2311 | 348 | if (context_) | ||
2312 | 349 | xmlStopParser(context_); | ||
2313 | 350 | |||
2314 | 351 | //release_underlying(); | ||
2315 | 352 | } | ||
2316 | 353 | |||
2317 | 354 | void Parser::check_for_exception() | ||
2318 | 355 | { | ||
2319 | 356 | check_for_error_and_warning_messages(); | ||
2320 | 357 | |||
2321 | 358 | if (exception_) | ||
2322 | 359 | { | ||
2323 | 360 | std::unique_ptr<exception> tmp(std::move(exception_)); | ||
2324 | 361 | tmp->raise(); | ||
2325 | 362 | } | ||
2326 | 363 | } | ||
2327 | 364 | |||
2328 | 365 | } // namespace xmlpp | ||
2329 | 0 | 366 | ||
2330 | === added file 'src/libxml++/parsers/parser.h' | |||
2331 | --- src/libxml++/parsers/parser.h 1970-01-01 00:00:00 +0000 | |||
2332 | +++ src/libxml++/parsers/parser.h 2016-11-06 17:18:04 +0000 | |||
2333 | @@ -0,0 +1,220 @@ | |||
2334 | 1 | /* parser.h | ||
2335 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
2336 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
2337 | 4 | * included with libxml++ as the file COPYING. | ||
2338 | 5 | */ | ||
2339 | 6 | |||
2340 | 7 | #ifndef __LIBXMLPP_PARSER_H | ||
2341 | 8 | #define __LIBXMLPP_PARSER_H | ||
2342 | 9 | |||
2343 | 10 | #ifdef _MSC_VER //Ignore warnings about the Visual C++ Bug, where we can not do anything | ||
2344 | 11 | #pragma warning (disable : 4786) | ||
2345 | 12 | #endif | ||
2346 | 13 | |||
2347 | 14 | #include <libxml++/noncopyable.h> | ||
2348 | 15 | #include <libxml++/exceptions/validity_error.h> | ||
2349 | 16 | #include <libxml++/exceptions/internal_error.h> | ||
2350 | 17 | |||
2351 | 18 | #include <string> | ||
2352 | 19 | #include <istream> | ||
2353 | 20 | #include <cstdarg> // va_list | ||
2354 | 21 | #include <memory> // std::unique_ptr | ||
2355 | 22 | |||
2356 | 23 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | ||
2357 | 24 | extern "C" { | ||
2358 | 25 | struct _xmlParserCtxt; | ||
2359 | 26 | } | ||
2360 | 27 | #endif //DOXYGEN_SHOULD_SKIP_THIS | ||
2361 | 28 | |||
2362 | 29 | namespace xmlpp { | ||
2363 | 30 | |||
2364 | 31 | /** XML parser. | ||
2365 | 32 | * | ||
2366 | 33 | * Abstract base class for DOM parser and SAX parser. | ||
2367 | 34 | */ | ||
2368 | 35 | class Parser : public NonCopyable | ||
2369 | 36 | { | ||
2370 | 37 | public: | ||
2371 | 38 | Parser(); | ||
2372 | 39 | ~Parser() override; | ||
2373 | 40 | |||
2374 | 41 | using size_type = unsigned int; | ||
2375 | 42 | |||
2376 | 43 | /** By default, the parser will not validate the XML file. | ||
2377 | 44 | * @param val Whether the document should be validated. | ||
2378 | 45 | */ | ||
2379 | 46 | void set_validate(bool val = true) noexcept; | ||
2380 | 47 | |||
2381 | 48 | /** See set_validate(). | ||
2382 | 49 | * @returns Whether the parser will validate the XML file. | ||
2383 | 50 | */ | ||
2384 | 51 | bool get_validate() const noexcept; | ||
2385 | 52 | |||
2386 | 53 | /** Set whether the parser will automatically substitute entity references with the text of the entities' definitions. | ||
2387 | 54 | * For instance, this affects the text returned by ContentNode::get_content(). | ||
2388 | 55 | * By default, the parser will not substitute entities, so that you do not lose the entity reference information. | ||
2389 | 56 | * @param val Whether entities will be substitued. | ||
2390 | 57 | */ | ||
2391 | 58 | void set_substitute_entities(bool val = true) noexcept; | ||
2392 | 59 | |||
2393 | 60 | /** See set_substitute_entities(). | ||
2394 | 61 | * @returns Whether entities will be substituted during parsing. | ||
2395 | 62 | */ | ||
2396 | 63 | bool get_substitute_entities() const noexcept; | ||
2397 | 64 | |||
2398 | 65 | /** Set whether the parser will collect and throw error and warning messages. | ||
2399 | 66 | * | ||
2400 | 67 | * If messages are collected, they are included in an exception thrown at the | ||
2401 | 68 | * end of parsing. | ||
2402 | 69 | * | ||
2403 | 70 | * - DOM parser | ||
2404 | 71 | * | ||
2405 | 72 | * If the messages are not collected, they are written on stderr. | ||
2406 | 73 | * The messages written on stderr are slightly different, and may be | ||
2407 | 74 | * preferred in a program started from the command-line. The default, if | ||
2408 | 75 | * set_throw_messages() is not called, is to collect and throw messages. | ||
2409 | 76 | * | ||
2410 | 77 | * - SAX parser | ||
2411 | 78 | * | ||
2412 | 79 | * If the messages are not collected, the error handling on_*() methods in | ||
2413 | 80 | * the user's SAX parser subclass are called. This is the default, if | ||
2414 | 81 | * set_throw_messages() is not called. | ||
2415 | 82 | * | ||
2416 | 83 | * @newin{2,36} | ||
2417 | 84 | * | ||
2418 | 85 | * @param val Whether messages will be collected and thrown in an exception. | ||
2419 | 86 | */ | ||
2420 | 87 | void set_throw_messages(bool val = true) noexcept; | ||
2421 | 88 | |||
2422 | 89 | /** See set_throw_messages(). | ||
2423 | 90 | * | ||
2424 | 91 | * @newin{2,36} | ||
2425 | 92 | * | ||
2426 | 93 | * @returns Whether messages will be collected and thrown in an exception. | ||
2427 | 94 | */ | ||
2428 | 95 | bool get_throw_messages() const noexcept; | ||
2429 | 96 | |||
2430 | 97 | /** Set whether default attribute values from the DTD shall be included in the node tree. | ||
2431 | 98 | * If set, attributes not assigned a value in the XML file, but with a default value | ||
2432 | 99 | * in the DTD file, will be included in the node tree that the parser creates. | ||
2433 | 100 | * These attributes will be represented by AttributeNode instances (not AttributeDeclaration | ||
2434 | 101 | * instances), just like attributes which are assigned a value in the XML file. | ||
2435 | 102 | * | ||
2436 | 103 | * @newin{2,38} | ||
2437 | 104 | * | ||
2438 | 105 | * @param val Whether attributes with default values will be included in the node tree. | ||
2439 | 106 | */ | ||
2440 | 107 | void set_include_default_attributes(bool val = true) noexcept; | ||
2441 | 108 | |||
2442 | 109 | /** See set_include_default_attributes(). | ||
2443 | 110 | * | ||
2444 | 111 | * @newin{2,38} | ||
2445 | 112 | * | ||
2446 | 113 | * @returns Whether attributes with default values will be included in the node tree. | ||
2447 | 114 | */ | ||
2448 | 115 | bool get_include_default_attributes() noexcept; | ||
2449 | 116 | |||
2450 | 117 | /** Set and/or clear parser option flags. | ||
2451 | 118 | * See the libxml2 documentation, enum xmlParserOption, for a list of parser options. | ||
2452 | 119 | * This method overrides other methods that set parser options, such as set_validate(), | ||
2453 | 120 | * set_substitute_entities() and set_include_default_attributes(). Use set_parser_options() | ||
2454 | 121 | * only if no other method can set the parser options you want. | ||
2455 | 122 | * | ||
2456 | 123 | * @newin{2,38} | ||
2457 | 124 | * | ||
2458 | 125 | * @param set_options Set bits correspond to flags that shall be set during parsing. | ||
2459 | 126 | * @param clear_options Set bits correspond to flags that shall be cleared during parsing. | ||
2460 | 127 | * Bits that are set in neither @a set_options nor @a clear_options are not affected. | ||
2461 | 128 | */ | ||
2462 | 129 | void set_parser_options(int set_options = 0, int clear_options = 0) noexcept; | ||
2463 | 130 | |||
2464 | 131 | /** See set_parser_options(). | ||
2465 | 132 | * | ||
2466 | 133 | * @newin{2,38} | ||
2467 | 134 | * | ||
2468 | 135 | * @param [out] set_options Set bits correspond to flags that shall be set during parsing. | ||
2469 | 136 | * @param [out] clear_options Set bits correspond to flags that shall be cleared during parsing. | ||
2470 | 137 | * Bits that are set in neither @a set_options nor @a clear_options are not affected. | ||
2471 | 138 | */ | ||
2472 | 139 | void get_parser_options(int& set_options, int& clear_options) noexcept; | ||
2473 | 140 | |||
2474 | 141 | /** Parse an XML document from a file. | ||
2475 | 142 | * @throw exception | ||
2476 | 143 | * @param filename The path to the file. | ||
2477 | 144 | */ | ||
2478 | 145 | virtual void parse_file(const std::string& filename) = 0; | ||
2479 | 146 | |||
2480 | 147 | /** Parse an XML document from raw memory. | ||
2481 | 148 | * @throw exception | ||
2482 | 149 | * @param contents The XML document as an array of bytes. | ||
2483 | 150 | * @param bytes_count The number of bytes in the @a contents array. | ||
2484 | 151 | */ | ||
2485 | 152 | virtual void parse_memory_raw(const unsigned char* contents, size_type bytes_count) = 0; | ||
2486 | 153 | |||
2487 | 154 | /** Parse an XML document from a string. | ||
2488 | 155 | * @throw exception | ||
2489 | 156 | * @param contents The XML document as a string. | ||
2490 | 157 | */ | ||
2491 | 158 | virtual void parse_memory(const Glib::ustring& contents) = 0; | ||
2492 | 159 | |||
2493 | 160 | /** Parse an XML document from a stream. | ||
2494 | 161 | * @throw exception | ||
2495 | 162 | * @param in The stream. | ||
2496 | 163 | */ | ||
2497 | 164 | virtual void parse_stream(std::istream& in) = 0; | ||
2498 | 165 | |||
2499 | 166 | //TODO: Add stop_parser()/stop_parsing(), wrapping xmlStopParser()? | ||
2500 | 167 | |||
2501 | 168 | protected: | ||
2502 | 169 | virtual void initialize_context(); | ||
2503 | 170 | virtual void release_underlying(); | ||
2504 | 171 | |||
2505 | 172 | virtual void on_parser_error(const Glib::ustring& message); | ||
2506 | 173 | virtual void on_parser_warning(const Glib::ustring& message); | ||
2507 | 174 | virtual void on_validity_error(const Glib::ustring& message); | ||
2508 | 175 | virtual void on_validity_warning(const Glib::ustring& message); | ||
2509 | 176 | |||
2510 | 177 | /// To be called in an exception handler. | ||
2511 | 178 | virtual void handle_exception(); | ||
2512 | 179 | virtual void check_for_exception(); | ||
2513 | 180 | |||
2514 | 181 | virtual void check_for_error_and_warning_messages(); | ||
2515 | 182 | |||
2516 | 183 | static void callback_parser_error(void* ctx, const char* msg, ...); | ||
2517 | 184 | static void callback_parser_warning(void* ctx, const char* msg, ...); | ||
2518 | 185 | static void callback_validity_error(void* ctx, const char* msg, ...); | ||
2519 | 186 | static void callback_validity_warning(void* ctx, const char* msg, ...); | ||
2520 | 187 | |||
2521 | 188 | enum class MsgType | ||
2522 | 189 | { | ||
2523 | 190 | ParserError, | ||
2524 | 191 | ParserWarning, | ||
2525 | 192 | ValidityError, | ||
2526 | 193 | ValidityWarning | ||
2527 | 194 | }; | ||
2528 | 195 | |||
2529 | 196 | static void callback_error_or_warning(MsgType msg_type, void* ctx, | ||
2530 | 197 | const char* msg, va_list var_args); | ||
2531 | 198 | |||
2532 | 199 | _xmlParserCtxt* context_; | ||
2533 | 200 | std::unique_ptr<exception> exception_; | ||
2534 | 201 | |||
2535 | 202 | private: | ||
2536 | 203 | struct Impl; | ||
2537 | 204 | std::unique_ptr<Impl> pimpl_; | ||
2538 | 205 | }; | ||
2539 | 206 | |||
2540 | 207 | /** Equivalent to Parser::parse_stream(). | ||
2541 | 208 | * | ||
2542 | 209 | * @newin{2,38} | ||
2543 | 210 | */ | ||
2544 | 211 | inline std::istream& operator>>(std::istream& in, Parser& parser) | ||
2545 | 212 | { | ||
2546 | 213 | parser.parse_stream(in); | ||
2547 | 214 | return in; | ||
2548 | 215 | } | ||
2549 | 216 | |||
2550 | 217 | } // namespace xmlpp | ||
2551 | 218 | |||
2552 | 219 | #endif //__LIBXMLPP_PARSER_H | ||
2553 | 220 | |||
2554 | 0 | 221 | ||
2555 | === added file 'src/libxml++/parsers/saxparser.cc' | |||
2556 | --- src/libxml++/parsers/saxparser.cc 1970-01-01 00:00:00 +0000 | |||
2557 | +++ src/libxml++/parsers/saxparser.cc 2016-11-06 17:18:04 +0000 | |||
2558 | @@ -0,0 +1,711 @@ | |||
2559 | 1 | /* saxparser.cc | ||
2560 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
2561 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
2562 | 4 | * included with libxml++ as the file COPYING. | ||
2563 | 5 | * | ||
2564 | 6 | * 2002/01/05 Valentin Rusu - fixed some potential buffer overruns | ||
2565 | 7 | * 2002/01/21 Valentin Rusu - added CDATA handlers | ||
2566 | 8 | */ | ||
2567 | 9 | |||
2568 | 10 | #include "libxml++/parsers/saxparser.h" | ||
2569 | 11 | #include "libxml++/keepblanks.h" | ||
2570 | 12 | |||
2571 | 13 | #include <libxml/parser.h> | ||
2572 | 14 | #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt | ||
2573 | 15 | |||
2574 | 16 | #include <cstdarg> //For va_list. | ||
2575 | 17 | #include <iostream> | ||
2576 | 18 | |||
2577 | 19 | namespace xmlpp { | ||
2578 | 20 | |||
2579 | 21 | struct SaxParserCallback | ||
2580 | 22 | { | ||
2581 | 23 | static xmlEntityPtr get_entity(void* context, const xmlChar* name); | ||
2582 | 24 | static void entity_decl(void* context, const xmlChar* name, int type, const xmlChar* publicId, const xmlChar* systemId, xmlChar* content); | ||
2583 | 25 | static void start_document(void* context); | ||
2584 | 26 | static void end_document(void* context); | ||
2585 | 27 | static void start_element(void* context, const xmlChar* name, const xmlChar** p); | ||
2586 | 28 | static void end_element(void* context, const xmlChar* name); | ||
2587 | 29 | static void start_element_ns(void *context, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, | ||
2588 | 30 | const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes); | ||
2589 | 31 | static void end_element_ns(void *context, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI); | ||
2590 | 32 | static void characters(void* context, const xmlChar* ch, int len); | ||
2591 | 33 | static void comment(void* context, const xmlChar* value); | ||
2592 | 34 | static void processing_instruction(void* context, const xmlChar* target, const xmlChar* data); | ||
2593 | 35 | static void warning(void* context, const char* fmt, ...); | ||
2594 | 36 | static void error(void* context, const char* fmt, ...); | ||
2595 | 37 | static void fatal_error(void* context, const char* fmt, ...); | ||
2596 | 38 | static void cdata_block(void* context, const xmlChar* value, int len); | ||
2597 | 39 | static void internal_subset(void* context, const xmlChar* name, const xmlChar*publicId, const xmlChar*systemId); | ||
2598 | 40 | }; | ||
2599 | 41 | |||
2600 | 42 | |||
2601 | 43 | |||
2602 | 44 | SaxParser::SaxParser(bool use_get_entity) | ||
2603 | 45 | : sax_handler_(new _xmlSAXHandler), entity_resolver_doc_(new Document) | ||
2604 | 46 | { | ||
2605 | 47 | xmlSAXHandler temp = { | ||
2606 | 48 | SaxParserCallback::internal_subset, | ||
2607 | 49 | nullptr, // isStandalone | ||
2608 | 50 | nullptr, // hasInternalSubset | ||
2609 | 51 | nullptr, // hasExternalSubset | ||
2610 | 52 | nullptr, // resolveEntity | ||
2611 | 53 | use_get_entity ? SaxParserCallback::get_entity : nullptr, // getEntity | ||
2612 | 54 | SaxParserCallback::entity_decl, // entityDecl | ||
2613 | 55 | nullptr, // notationDecl | ||
2614 | 56 | nullptr, // attributeDecl | ||
2615 | 57 | nullptr, // elementDecl | ||
2616 | 58 | nullptr, // unparsedEntityDecl | ||
2617 | 59 | nullptr, // setDocumentLocator | ||
2618 | 60 | SaxParserCallback::start_document, // startDocument | ||
2619 | 61 | SaxParserCallback::end_document, // endDocument | ||
2620 | 62 | SaxParserCallback::start_element, // startElement | ||
2621 | 63 | SaxParserCallback::end_element, // endElement | ||
2622 | 64 | nullptr, // reference | ||
2623 | 65 | SaxParserCallback::characters, // characters | ||
2624 | 66 | nullptr, // ignorableWhitespace | ||
2625 | 67 | SaxParserCallback::processing_instruction, // processingInstruction | ||
2626 | 68 | SaxParserCallback::comment, // comment | ||
2627 | 69 | SaxParserCallback::warning, // warning | ||
2628 | 70 | SaxParserCallback::error, // error | ||
2629 | 71 | SaxParserCallback::fatal_error, // fatalError | ||
2630 | 72 | nullptr, // getParameterEntity | ||
2631 | 73 | SaxParserCallback::cdata_block, // cdataBlock | ||
2632 | 74 | nullptr, // externalSubset | ||
2633 | 75 | XML_SAX2_MAGIC, // initialized | ||
2634 | 76 | nullptr, // private | ||
2635 | 77 | SaxParserCallback::start_element_ns, // startElementNs | ||
2636 | 78 | SaxParserCallback::end_element_ns, // endElementNs | ||
2637 | 79 | nullptr, // serror | ||
2638 | 80 | }; | ||
2639 | 81 | *sax_handler_ = temp; | ||
2640 | 82 | |||
2641 | 83 | // The default action is to call on_warning(), on_error(), on_fatal_error(). | ||
2642 | 84 | set_throw_messages(false); | ||
2643 | 85 | } | ||
2644 | 86 | |||
2645 | 87 | SaxParser::~SaxParser() | ||
2646 | 88 | { | ||
2647 | 89 | release_underlying(); | ||
2648 | 90 | } | ||
2649 | 91 | |||
2650 | 92 | xmlEntityPtr SaxParser::on_get_entity(const Glib::ustring& name) | ||
2651 | 93 | { | ||
2652 | 94 | return entity_resolver_doc_->get_entity(name); | ||
2653 | 95 | } | ||
2654 | 96 | |||
2655 | 97 | void SaxParser::on_entity_declaration(const Glib::ustring& name, XmlEntityType type, const Glib::ustring& publicId, const Glib::ustring& systemId, const Glib::ustring& content) | ||
2656 | 98 | { | ||
2657 | 99 | entity_resolver_doc_->set_entity_declaration(name, type, publicId, systemId, content); | ||
2658 | 100 | } | ||
2659 | 101 | |||
2660 | 102 | void SaxParser::on_start_document() | ||
2661 | 103 | { | ||
2662 | 104 | } | ||
2663 | 105 | |||
2664 | 106 | void SaxParser::on_end_document() | ||
2665 | 107 | { | ||
2666 | 108 | } | ||
2667 | 109 | |||
2668 | 110 | void SaxParser::on_start_element(const Glib::ustring& /* name */, const AttributeList& /* attributes */) | ||
2669 | 111 | { | ||
2670 | 112 | } | ||
2671 | 113 | |||
2672 | 114 | void SaxParser::on_end_element(const Glib::ustring& /* name */) | ||
2673 | 115 | { | ||
2674 | 116 | } | ||
2675 | 117 | |||
2676 | 118 | void SaxParser::on_start_element_ns(const Glib::ustring& /* name */, const Glib::ustring& /* prefix */, const Glib::ustring& /* uri */, const NamespaceList& /* namespaces */, const AttributeList& /* attributes */) | ||
2677 | 119 | { | ||
2678 | 120 | } | ||
2679 | 121 | |||
2680 | 122 | void SaxParser::on_end_element_ns(const Glib::ustring& /* name */, const Glib::ustring& /* prefix */, const Glib::ustring& /* uri */) | ||
2681 | 123 | { | ||
2682 | 124 | } | ||
2683 | 125 | |||
2684 | 126 | void SaxParser::on_characters(const Glib::ustring& /* text */) | ||
2685 | 127 | { | ||
2686 | 128 | } | ||
2687 | 129 | |||
2688 | 130 | void SaxParser::on_comment(const Glib::ustring& /* text */) | ||
2689 | 131 | { | ||
2690 | 132 | } | ||
2691 | 133 | |||
2692 | 134 | void SaxParser::on_processing_instruction(const Glib::ustring &target, const Glib::ustring &data) | ||
2693 | 135 | { | ||
2694 | 136 | } | ||
2695 | 137 | |||
2696 | 138 | void SaxParser::on_warning(const Glib::ustring& /* text */) | ||
2697 | 139 | { | ||
2698 | 140 | } | ||
2699 | 141 | |||
2700 | 142 | void SaxParser::on_error(const Glib::ustring& /* text */) | ||
2701 | 143 | { | ||
2702 | 144 | } | ||
2703 | 145 | |||
2704 | 146 | void SaxParser::on_fatal_error(const Glib::ustring& text) | ||
2705 | 147 | { | ||
2706 | 148 | throw parse_error("Fatal error: " + text); | ||
2707 | 149 | } | ||
2708 | 150 | |||
2709 | 151 | void SaxParser::on_cdata_block(const Glib::ustring& /* text */) | ||
2710 | 152 | { | ||
2711 | 153 | } | ||
2712 | 154 | |||
2713 | 155 | void SaxParser::on_internal_subset(const Glib::ustring& name, | ||
2714 | 156 | const Glib::ustring& publicId, | ||
2715 | 157 | const Glib::ustring& systemId) | ||
2716 | 158 | { | ||
2717 | 159 | entity_resolver_doc_->set_internal_subset(name, publicId, systemId); | ||
2718 | 160 | } | ||
2719 | 161 | |||
2720 | 162 | // implementation of this function is inspired by the SAX documentation by James Henstridge. | ||
2721 | 163 | // (http://www.daa.com.au/~james/gnome/xml-sax/implementing.html) | ||
2722 | 164 | void SaxParser::parse() | ||
2723 | 165 | { | ||
2724 | 166 | if(!context_) | ||
2725 | 167 | { | ||
2726 | 168 | throw internal_error("Parser context not created."); | ||
2727 | 169 | } | ||
2728 | 170 | |||
2729 | 171 | auto old_sax = context_->sax; | ||
2730 | 172 | context_->sax = sax_handler_.get(); | ||
2731 | 173 | |||
2732 | 174 | xmlResetLastError(); | ||
2733 | 175 | initialize_context(); | ||
2734 | 176 | |||
2735 | 177 | const int parseError = xmlParseDocument(context_); | ||
2736 | 178 | |||
2737 | 179 | context_->sax = old_sax; | ||
2738 | 180 | |||
2739 | 181 | auto error_str = format_xml_parser_error(context_); | ||
2740 | 182 | if (error_str.empty() && parseError == -1) | ||
2741 | 183 | error_str = "xmlParseDocument() failed."; | ||
2742 | 184 | |||
2743 | 185 | release_underlying(); // Free context_ | ||
2744 | 186 | |||
2745 | 187 | check_for_exception(); | ||
2746 | 188 | |||
2747 | 189 | if(!error_str.empty()) | ||
2748 | 190 | { | ||
2749 | 191 | throw parse_error(error_str); | ||
2750 | 192 | } | ||
2751 | 193 | } | ||
2752 | 194 | |||
2753 | 195 | void SaxParser::parse_file(const std::string& filename) | ||
2754 | 196 | { | ||
2755 | 197 | if(context_) | ||
2756 | 198 | { | ||
2757 | 199 | throw parse_error("Attempt to start a second parse while a parse is in progress."); | ||
2758 | 200 | } | ||
2759 | 201 | |||
2760 | 202 | KeepBlanks k(KeepBlanks::Default); | ||
2761 | 203 | |||
2762 | 204 | context_ = xmlCreateFileParserCtxt(filename.c_str()); | ||
2763 | 205 | parse(); | ||
2764 | 206 | } | ||
2765 | 207 | |||
2766 | 208 | void SaxParser::parse_memory_raw(const unsigned char* contents, size_type bytes_count) | ||
2767 | 209 | { | ||
2768 | 210 | if(context_) | ||
2769 | 211 | { | ||
2770 | 212 | throw parse_error("Attempt to start a second parse while a parse is in progress."); | ||
2771 | 213 | } | ||
2772 | 214 | |||
2773 | 215 | KeepBlanks k(KeepBlanks::Default); | ||
2774 | 216 | |||
2775 | 217 | context_ = xmlCreateMemoryParserCtxt((const char*)contents, bytes_count); | ||
2776 | 218 | parse(); | ||
2777 | 219 | } | ||
2778 | 220 | |||
2779 | 221 | void SaxParser::parse_memory(const Glib::ustring& contents) | ||
2780 | 222 | { | ||
2781 | 223 | parse_memory_raw((const unsigned char*)contents.c_str(), contents.bytes()); | ||
2782 | 224 | } | ||
2783 | 225 | |||
2784 | 226 | void SaxParser::parse_stream(std::istream& in) | ||
2785 | 227 | { | ||
2786 | 228 | if(context_) | ||
2787 | 229 | { | ||
2788 | 230 | throw parse_error("Attempt to start a second parse while a parse is in progress."); | ||
2789 | 231 | } | ||
2790 | 232 | |||
2791 | 233 | KeepBlanks k(KeepBlanks::Default); | ||
2792 | 234 | xmlResetLastError(); | ||
2793 | 235 | |||
2794 | 236 | context_ = xmlCreatePushParserCtxt( | ||
2795 | 237 | sax_handler_.get(), | ||
2796 | 238 | nullptr, // user_data | ||
2797 | 239 | nullptr, // chunk | ||
2798 | 240 | 0, // size | ||
2799 | 241 | nullptr); // no filename for fetching external entities | ||
2800 | 242 | |||
2801 | 243 | if(!context_) | ||
2802 | 244 | { | ||
2803 | 245 | throw internal_error("Could not create parser context\n" + format_xml_error()); | ||
2804 | 246 | } | ||
2805 | 247 | |||
2806 | 248 | initialize_context(); | ||
2807 | 249 | |||
2808 | 250 | // std::string or Glib::ustring? | ||
2809 | 251 | // Output from the XML parser is UTF-8 encoded. | ||
2810 | 252 | // But the istream "in" is input, i.e. an XML file. It can use any encoding. | ||
2811 | 253 | // If it's not UTF-8, the file itself must contain information about which | ||
2812 | 254 | // encoding it uses. See the XML specification. Thus use std::string. | ||
2813 | 255 | int firstParseError = XML_ERR_OK; | ||
2814 | 256 | std::string line; | ||
2815 | 257 | while (!exception_ && std::getline(in, line)) | ||
2816 | 258 | { | ||
2817 | 259 | // since getline does not get the line separator, we have to add it since the parser care | ||
2818 | 260 | // about layout in certain cases. | ||
2819 | 261 | line += '\n'; | ||
2820 | 262 | |||
2821 | 263 | const int parseError = xmlParseChunk(context_, line.c_str(), | ||
2822 | 264 | line.size() /* This is a std::string, not a ustring, so this is the number of bytes. */, | ||
2823 | 265 | 0 /* don't terminate */); | ||
2824 | 266 | |||
2825 | 267 | // Save the first error code if any, but read on. | ||
2826 | 268 | // More errors might be reported and then thrown by check_for_exception(). | ||
2827 | 269 | if (parseError != XML_ERR_OK && firstParseError == XML_ERR_OK) | ||
2828 | 270 | firstParseError = parseError; | ||
2829 | 271 | } | ||
2830 | 272 | |||
2831 | 273 | if (!exception_) | ||
2832 | 274 | { | ||
2833 | 275 | //This is called just to terminate parsing. | ||
2834 | 276 | const int parseError = xmlParseChunk(context_, nullptr /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */); | ||
2835 | 277 | |||
2836 | 278 | if (parseError != XML_ERR_OK && firstParseError == XML_ERR_OK) | ||
2837 | 279 | firstParseError = parseError; | ||
2838 | 280 | } | ||
2839 | 281 | |||
2840 | 282 | auto error_str = format_xml_parser_error(context_); | ||
2841 | 283 | if (error_str.empty() && firstParseError != XML_ERR_OK) | ||
2842 | 284 | error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(firstParseError); | ||
2843 | 285 | |||
2844 | 286 | release_underlying(); // Free context_ | ||
2845 | 287 | |||
2846 | 288 | check_for_exception(); | ||
2847 | 289 | |||
2848 | 290 | if(!error_str.empty()) | ||
2849 | 291 | { | ||
2850 | 292 | throw parse_error(error_str); | ||
2851 | 293 | } | ||
2852 | 294 | } | ||
2853 | 295 | |||
2854 | 296 | void SaxParser::parse_chunk(const Glib::ustring& chunk) | ||
2855 | 297 | { | ||
2856 | 298 | parse_chunk_raw((const unsigned char*)chunk.c_str(), chunk.bytes()); | ||
2857 | 299 | } | ||
2858 | 300 | |||
2859 | 301 | void SaxParser::parse_chunk_raw(const unsigned char* contents, size_type bytes_count) | ||
2860 | 302 | { | ||
2861 | 303 | KeepBlanks k(KeepBlanks::Default); | ||
2862 | 304 | xmlResetLastError(); | ||
2863 | 305 | |||
2864 | 306 | if(!context_) | ||
2865 | 307 | { | ||
2866 | 308 | context_ = xmlCreatePushParserCtxt( | ||
2867 | 309 | sax_handler_.get(), | ||
2868 | 310 | nullptr, // user_data | ||
2869 | 311 | nullptr, // chunk | ||
2870 | 312 | 0, // size | ||
2871 | 313 | nullptr); // no filename for fetching external entities | ||
2872 | 314 | |||
2873 | 315 | if(!context_) | ||
2874 | 316 | { | ||
2875 | 317 | throw internal_error("Could not create parser context\n" + format_xml_error()); | ||
2876 | 318 | } | ||
2877 | 319 | initialize_context(); | ||
2878 | 320 | } | ||
2879 | 321 | else | ||
2880 | 322 | xmlCtxtResetLastError(context_); | ||
2881 | 323 | |||
2882 | 324 | int parseError = XML_ERR_OK; | ||
2883 | 325 | if (!exception_) | ||
2884 | 326 | parseError = xmlParseChunk(context_, (const char*)contents, bytes_count, 0 /* don't terminate */); | ||
2885 | 327 | |||
2886 | 328 | check_for_exception(); | ||
2887 | 329 | |||
2888 | 330 | auto error_str = format_xml_parser_error(context_); | ||
2889 | 331 | if (error_str.empty() && parseError != XML_ERR_OK) | ||
2890 | 332 | error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(parseError); | ||
2891 | 333 | if(!error_str.empty()) | ||
2892 | 334 | { | ||
2893 | 335 | throw parse_error(error_str); | ||
2894 | 336 | } | ||
2895 | 337 | } | ||
2896 | 338 | |||
2897 | 339 | void SaxParser::finish_chunk_parsing() | ||
2898 | 340 | { | ||
2899 | 341 | xmlResetLastError(); | ||
2900 | 342 | if(!context_) | ||
2901 | 343 | { | ||
2902 | 344 | context_ = xmlCreatePushParserCtxt( | ||
2903 | 345 | sax_handler_.get(), | ||
2904 | 346 | nullptr, // user_data | ||
2905 | 347 | nullptr, // chunk | ||
2906 | 348 | 0, // size | ||
2907 | 349 | nullptr); // no filename for fetching external entities | ||
2908 | 350 | |||
2909 | 351 | if(!context_) | ||
2910 | 352 | { | ||
2911 | 353 | throw internal_error("Could not create parser context\n" + format_xml_error()); | ||
2912 | 354 | } | ||
2913 | 355 | initialize_context(); | ||
2914 | 356 | } | ||
2915 | 357 | else | ||
2916 | 358 | xmlCtxtResetLastError(context_); | ||
2917 | 359 | |||
2918 | 360 | int parseError = XML_ERR_OK; | ||
2919 | 361 | if (!exception_) | ||
2920 | 362 | //This is called just to terminate parsing. | ||
2921 | 363 | parseError = xmlParseChunk(context_, nullptr /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */); | ||
2922 | 364 | |||
2923 | 365 | auto error_str = format_xml_parser_error(context_); | ||
2924 | 366 | if (error_str.empty() && parseError != XML_ERR_OK) | ||
2925 | 367 | error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(parseError); | ||
2926 | 368 | |||
2927 | 369 | release_underlying(); // Free context_ | ||
2928 | 370 | |||
2929 | 371 | check_for_exception(); | ||
2930 | 372 | |||
2931 | 373 | if(!error_str.empty()) | ||
2932 | 374 | { | ||
2933 | 375 | throw parse_error(error_str); | ||
2934 | 376 | } | ||
2935 | 377 | } | ||
2936 | 378 | |||
2937 | 379 | void SaxParser::release_underlying() | ||
2938 | 380 | { | ||
2939 | 381 | Parser::release_underlying(); | ||
2940 | 382 | } | ||
2941 | 383 | |||
2942 | 384 | void SaxParser::initialize_context() | ||
2943 | 385 | { | ||
2944 | 386 | Parser::initialize_context(); | ||
2945 | 387 | // Start with an empty Document for entity resolution. | ||
2946 | 388 | entity_resolver_doc_.reset(new Document); | ||
2947 | 389 | } | ||
2948 | 390 | |||
2949 | 391 | xmlEntityPtr SaxParserCallback::get_entity(void* context, const xmlChar* name) | ||
2950 | 392 | { | ||
2951 | 393 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
2952 | 394 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
2953 | 395 | xmlEntityPtr result = nullptr; | ||
2954 | 396 | |||
2955 | 397 | try | ||
2956 | 398 | { | ||
2957 | 399 | result = parser->on_get_entity((const char*)name); | ||
2958 | 400 | } | ||
2959 | 401 | catch (...) | ||
2960 | 402 | { | ||
2961 | 403 | parser->handle_exception(); | ||
2962 | 404 | } | ||
2963 | 405 | |||
2964 | 406 | return result; | ||
2965 | 407 | } | ||
2966 | 408 | |||
2967 | 409 | void SaxParserCallback::entity_decl(void* context, const xmlChar* name, int type, const xmlChar* publicId, const xmlChar* systemId, xmlChar* content) | ||
2968 | 410 | { | ||
2969 | 411 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
2970 | 412 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
2971 | 413 | |||
2972 | 414 | try | ||
2973 | 415 | { | ||
2974 | 416 | parser->on_entity_declaration( | ||
2975 | 417 | ( name ? Glib::ustring((const char*)name) : ""), | ||
2976 | 418 | static_cast<XmlEntityType>(type), | ||
2977 | 419 | ( publicId ? Glib::ustring((const char*)publicId) : ""), | ||
2978 | 420 | ( systemId ? Glib::ustring((const char*)systemId) : ""), | ||
2979 | 421 | ( content ? Glib::ustring((const char*)content) : "") ); | ||
2980 | 422 | } | ||
2981 | 423 | catch (...) | ||
2982 | 424 | { | ||
2983 | 425 | parser->handle_exception(); | ||
2984 | 426 | } | ||
2985 | 427 | } | ||
2986 | 428 | |||
2987 | 429 | void SaxParserCallback::start_document(void* context) | ||
2988 | 430 | { | ||
2989 | 431 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
2990 | 432 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
2991 | 433 | |||
2992 | 434 | try | ||
2993 | 435 | { | ||
2994 | 436 | parser->on_start_document(); | ||
2995 | 437 | } | ||
2996 | 438 | catch (...) | ||
2997 | 439 | { | ||
2998 | 440 | parser->handle_exception(); | ||
2999 | 441 | } | ||
3000 | 442 | } | ||
3001 | 443 | |||
3002 | 444 | void SaxParserCallback::end_document(void* context) | ||
3003 | 445 | { | ||
3004 | 446 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3005 | 447 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3006 | 448 | |||
3007 | 449 | if (parser->exception_) | ||
3008 | 450 | return; | ||
3009 | 451 | |||
3010 | 452 | try | ||
3011 | 453 | { | ||
3012 | 454 | parser->on_end_document(); | ||
3013 | 455 | } | ||
3014 | 456 | catch (...) | ||
3015 | 457 | { | ||
3016 | 458 | parser->handle_exception(); | ||
3017 | 459 | } | ||
3018 | 460 | } | ||
3019 | 461 | |||
3020 | 462 | void SaxParserCallback::start_element(void* context, | ||
3021 | 463 | const xmlChar* name, | ||
3022 | 464 | const xmlChar** p) | ||
3023 | 465 | { | ||
3024 | 466 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3025 | 467 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3026 | 468 | |||
3027 | 469 | SaxParser::AttributeList attributes; | ||
3028 | 470 | |||
3029 | 471 | if(p) | ||
3030 | 472 | for(const xmlChar** cur = p; cur && *cur; cur += 2) | ||
3031 | 473 | attributes.push_back( | ||
3032 | 474 | SaxParser::Attribute( (char*)*cur, (char*)*(cur + 1) )); | ||
3033 | 475 | |||
3034 | 476 | try | ||
3035 | 477 | { | ||
3036 | 478 | parser->on_start_element(Glib::ustring((const char*) name), attributes); | ||
3037 | 479 | } | ||
3038 | 480 | catch (...) | ||
3039 | 481 | { | ||
3040 | 482 | parser->handle_exception(); | ||
3041 | 483 | } | ||
3042 | 484 | } | ||
3043 | 485 | |||
3044 | 486 | void SaxParserCallback::end_element(void* context, const xmlChar* name) | ||
3045 | 487 | { | ||
3046 | 488 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3047 | 489 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3048 | 490 | |||
3049 | 491 | try | ||
3050 | 492 | { | ||
3051 | 493 | parser->on_end_element(Glib::ustring((const char*) name)); | ||
3052 | 494 | } | ||
3053 | 495 | catch (...) | ||
3054 | 496 | { | ||
3055 | 497 | parser->handle_exception(); | ||
3056 | 498 | } | ||
3057 | 499 | } | ||
3058 | 500 | |||
3059 | 501 | void SaxParserCallback::start_element_ns(void *context, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI, | ||
3060 | 502 | int nb_namespaces, const xmlChar **namespaces, int nb_attributes, | ||
3061 | 503 | int nb_defaulted, const xmlChar **attributes) | ||
3062 | 504 | { | ||
3063 | 505 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3064 | 506 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3065 | 507 | |||
3066 | 508 | SaxParser::AttributeList attrs; | ||
3067 | 509 | SaxParser::NamespaceList nss; | ||
3068 | 510 | try | ||
3069 | 511 | { | ||
3070 | 512 | const auto pref = prefix ? Glib::ustring((const char*) prefix) : ""; | ||
3071 | 513 | const auto uri = URI ? Glib::ustring((const char*) URI) : ""; | ||
3072 | 514 | for (int i = 0; i < nb_namespaces; ++i) | ||
3073 | 515 | { | ||
3074 | 516 | const auto p = namespaces[i * 2] ? Glib::ustring((const char*) namespaces[i * 2]) : ""; | ||
3075 | 517 | const auto u = namespaces[i * 2 + 1] ? Glib::ustring((const char*) namespaces[i * 2 + 1]) : ""; | ||
3076 | 518 | nss.push_back(SaxParser::Namespace(p, u)); | ||
3077 | 519 | } | ||
3078 | 520 | |||
3079 | 521 | unsigned int index = 0; | ||
3080 | 522 | for (int ia = 0; ia < nb_attributes; ++ia, index += 5 ) | ||
3081 | 523 | { | ||
3082 | 524 | const auto attrName = attributes[index] ? Glib::ustring((const char*) attributes[index]) : ""; | ||
3083 | 525 | const auto attrPrefix = attributes[index + 1] ? Glib::ustring((const char*) attributes[index + 1]) : ""; | ||
3084 | 526 | const auto attrUri = attributes[index + 2] ? Glib::ustring((const char*) attributes[index + 2]) : ""; | ||
3085 | 527 | const auto attrValueBegin = attributes[index + 3]; | ||
3086 | 528 | const auto attrValueEnd = attributes[index + 4]; | ||
3087 | 529 | Glib::ustring value((const char *)attrValueBegin, (const char *)attrValueEnd); | ||
3088 | 530 | attrs.push_back(SaxParser::Attribute(attrName, value, attrPrefix, attrUri)); | ||
3089 | 531 | } | ||
3090 | 532 | parser->on_start_element_ns(Glib::ustring((const char *) name), pref, uri, nss, attrs); | ||
3091 | 533 | } | ||
3092 | 534 | catch (...) | ||
3093 | 535 | { | ||
3094 | 536 | parser->handle_exception(); | ||
3095 | 537 | } | ||
3096 | 538 | } | ||
3097 | 539 | |||
3098 | 540 | void SaxParserCallback::end_element_ns(void *context, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI) | ||
3099 | 541 | { | ||
3100 | 542 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3101 | 543 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3102 | 544 | |||
3103 | 545 | try | ||
3104 | 546 | { | ||
3105 | 547 | const auto pref = prefix ? Glib::ustring((const char*) prefix) : ""; | ||
3106 | 548 | const auto uri = URI ? Glib::ustring((const char*) URI) : ""; | ||
3107 | 549 | |||
3108 | 550 | parser->on_end_element_ns(Glib::ustring((const char*) name), pref, uri); | ||
3109 | 551 | } | ||
3110 | 552 | catch (...) | ||
3111 | 553 | { | ||
3112 | 554 | parser->handle_exception(); | ||
3113 | 555 | } | ||
3114 | 556 | } | ||
3115 | 557 | |||
3116 | 558 | void SaxParserCallback::characters(void * context, const xmlChar* ch, int len) | ||
3117 | 559 | { | ||
3118 | 560 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3119 | 561 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3120 | 562 | |||
3121 | 563 | try | ||
3122 | 564 | { | ||
3123 | 565 | // Here we force the use of Glib::ustring::ustring( InputIterator begin, InputIterator end ) | ||
3124 | 566 | // instead of Glib::ustring::ustring( const char*, size_type ) because it | ||
3125 | 567 | // expects the length of the string in characters, not in bytes. | ||
3126 | 568 | parser->on_characters( | ||
3127 | 569 | Glib::ustring( | ||
3128 | 570 | reinterpret_cast<const char *>(ch), | ||
3129 | 571 | reinterpret_cast<const char *>(ch + len) ) ); | ||
3130 | 572 | } | ||
3131 | 573 | catch (...) | ||
3132 | 574 | { | ||
3133 | 575 | parser->handle_exception(); | ||
3134 | 576 | } | ||
3135 | 577 | } | ||
3136 | 578 | |||
3137 | 579 | void SaxParserCallback::comment(void* context, const xmlChar* value) | ||
3138 | 580 | { | ||
3139 | 581 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3140 | 582 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3141 | 583 | |||
3142 | 584 | try | ||
3143 | 585 | { | ||
3144 | 586 | parser->on_comment(Glib::ustring((const char*) value)); | ||
3145 | 587 | } | ||
3146 | 588 | catch (...) | ||
3147 | 589 | { | ||
3148 | 590 | parser->handle_exception(); | ||
3149 | 591 | } | ||
3150 | 592 | } | ||
3151 | 593 | |||
3152 | 594 | void SaxParserCallback::warning(void* context, const char* fmt, ...) | ||
3153 | 595 | { | ||
3154 | 596 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3155 | 597 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3156 | 598 | |||
3157 | 599 | va_list arg; | ||
3158 | 600 | va_start(arg, fmt); | ||
3159 | 601 | const Glib::ustring buff = format_printf_message(fmt, arg); | ||
3160 | 602 | va_end(arg); | ||
3161 | 603 | |||
3162 | 604 | try | ||
3163 | 605 | { | ||
3164 | 606 | parser->on_warning(buff); | ||
3165 | 607 | } | ||
3166 | 608 | catch (...) | ||
3167 | 609 | { | ||
3168 | 610 | parser->handle_exception(); | ||
3169 | 611 | } | ||
3170 | 612 | } | ||
3171 | 613 | |||
3172 | 614 | void SaxParserCallback::error(void* context, const char* fmt, ...) | ||
3173 | 615 | { | ||
3174 | 616 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3175 | 617 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3176 | 618 | |||
3177 | 619 | if (parser->exception_) | ||
3178 | 620 | return; | ||
3179 | 621 | |||
3180 | 622 | va_list arg; | ||
3181 | 623 | va_start(arg, fmt); | ||
3182 | 624 | const Glib::ustring buff = format_printf_message(fmt, arg); | ||
3183 | 625 | va_end(arg); | ||
3184 | 626 | |||
3185 | 627 | try | ||
3186 | 628 | { | ||
3187 | 629 | parser->on_error(buff); | ||
3188 | 630 | } | ||
3189 | 631 | catch (...) | ||
3190 | 632 | { | ||
3191 | 633 | parser->handle_exception(); | ||
3192 | 634 | } | ||
3193 | 635 | } | ||
3194 | 636 | |||
3195 | 637 | void SaxParserCallback::fatal_error(void* context, const char* fmt, ...) | ||
3196 | 638 | { | ||
3197 | 639 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3198 | 640 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3199 | 641 | |||
3200 | 642 | va_list arg; | ||
3201 | 643 | va_start(arg, fmt); | ||
3202 | 644 | const Glib::ustring buff = format_printf_message(fmt, arg); | ||
3203 | 645 | va_end(arg); | ||
3204 | 646 | |||
3205 | 647 | try | ||
3206 | 648 | { | ||
3207 | 649 | parser->on_fatal_error(buff); | ||
3208 | 650 | } | ||
3209 | 651 | catch (...) | ||
3210 | 652 | { | ||
3211 | 653 | parser->handle_exception(); | ||
3212 | 654 | } | ||
3213 | 655 | } | ||
3214 | 656 | |||
3215 | 657 | void SaxParserCallback::cdata_block(void* context, const xmlChar* value, int len) | ||
3216 | 658 | { | ||
3217 | 659 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3218 | 660 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3219 | 661 | |||
3220 | 662 | try | ||
3221 | 663 | { | ||
3222 | 664 | // Here we force the use of Glib::ustring::ustring( InputIterator begin, InputIterator end ) | ||
3223 | 665 | // see comments in SaxParserCallback::characters | ||
3224 | 666 | parser->on_cdata_block( | ||
3225 | 667 | Glib::ustring( | ||
3226 | 668 | reinterpret_cast<const char *>(value), | ||
3227 | 669 | reinterpret_cast<const char *>(value + len) ) ); | ||
3228 | 670 | } | ||
3229 | 671 | catch (...) | ||
3230 | 672 | { | ||
3231 | 673 | parser->handle_exception(); | ||
3232 | 674 | } | ||
3233 | 675 | } | ||
3234 | 676 | |||
3235 | 677 | void SaxParserCallback::internal_subset(void* context, const xmlChar* name, | ||
3236 | 678 | const xmlChar* publicId, const xmlChar* systemId) | ||
3237 | 679 | { | ||
3238 | 680 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3239 | 681 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3240 | 682 | |||
3241 | 683 | try | ||
3242 | 684 | { | ||
3243 | 685 | const auto pid = publicId ? Glib::ustring((const char*) publicId) : ""; | ||
3244 | 686 | const auto sid = systemId ? Glib::ustring((const char*) systemId) : ""; | ||
3245 | 687 | |||
3246 | 688 | parser->on_internal_subset( Glib::ustring((const char*) name), pid, sid); | ||
3247 | 689 | } | ||
3248 | 690 | catch (...) | ||
3249 | 691 | { | ||
3250 | 692 | parser->handle_exception(); | ||
3251 | 693 | } | ||
3252 | 694 | } | ||
3253 | 695 | |||
3254 | 696 | void SaxParserCallback::processing_instruction(void *context, const xmlChar *target, const xmlChar *data) | ||
3255 | 697 | { | ||
3256 | 698 | auto the_context = static_cast<_xmlParserCtxt*>(context); | ||
3257 | 699 | auto parser = static_cast<SaxParser*>(the_context->_private); | ||
3258 | 700 | |||
3259 | 701 | try | ||
3260 | 702 | { | ||
3261 | 703 | parser->on_processing_instruction(Glib::ustring((const char*) target), Glib::ustring((const char*) data)); | ||
3262 | 704 | } | ||
3263 | 705 | catch (...) | ||
3264 | 706 | { | ||
3265 | 707 | parser->handle_exception(); | ||
3266 | 708 | } | ||
3267 | 709 | } | ||
3268 | 710 | |||
3269 | 711 | } // namespace xmlpp | ||
3270 | 0 | 712 | ||
3271 | === added file 'src/libxml++/parsers/saxparser.h' | |||
3272 | --- src/libxml++/parsers/saxparser.h 1970-01-01 00:00:00 +0000 | |||
3273 | +++ src/libxml++/parsers/saxparser.h 2016-11-06 17:18:04 +0000 | |||
3274 | @@ -0,0 +1,261 @@ | |||
3275 | 1 | /* saxparser.h | ||
3276 | 2 | * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and | ||
3277 | 3 | * are covered by the GNU Lesser General Public License, which should be | ||
3278 | 4 | * included with libxml++ as the file COPYING. | ||
3279 | 5 | */ | ||
3280 | 6 | |||
3281 | 7 | #ifndef __LIBXMLPP_PARSERS_SAXPARSER_H | ||
3282 | 8 | #define __LIBXMLPP_PARSERS_SAXPARSER_H | ||
3283 | 9 | |||
3284 | 10 | #include <libxml++/parsers/parser.h> | ||
3285 | 11 | |||
3286 | 12 | #include <deque> | ||
3287 | 13 | #include <memory> | ||
3288 | 14 | #include "libxml++/document.h" | ||
3289 | 15 | |||
3290 | 16 | #ifndef DOXYGEN_SHOULD_SKIP_THIS | ||
3291 | 17 | extern "C" { | ||
3292 | 18 | struct _xmlSAXHandler; | ||
3293 | 19 | struct _xmlEntity; | ||
3294 | 20 | } | ||
3295 | 21 | #endif //DOXYGEN_SHOULD_SKIP_THIS | ||
3296 | 22 | |||
3297 | 23 | namespace xmlpp { | ||
3298 | 24 | |||
3299 | 25 | /** SAX XML parser. | ||
3300 | 26 | * Derive your own class and override the on_*() methods. | ||
3301 | 27 | * SAX = Simple API for XML | ||
3302 | 28 | * | ||
3303 | 29 | * In a system that does not support std::exception_ptr: If an overridden on_*() | ||
3304 | 30 | * method throws an exception which is not derived from xmlpp::exception, | ||
3305 | 31 | * that exception is replaced by a xmlpp::exception before it is propagated | ||
3306 | 32 | * out of the parse method, such as parse_file(). | ||
3307 | 33 | */ | ||
3308 | 34 | class SaxParser : public Parser | ||
3309 | 35 | { | ||
3310 | 36 | public: | ||
3311 | 37 | /** | ||
3312 | 38 | * Simple structure used in the start_element callbacks, in which | ||
3313 | 39 | * the namespaces are a list of prefix/uri pairs. | ||
3314 | 40 | */ | ||
3315 | 41 | struct Namespace | ||
3316 | 42 | { | ||
3317 | 43 | Glib::ustring prefix; | ||
3318 | 44 | Glib::ustring uri; | ||
3319 | 45 | |||
3320 | 46 | Namespace() | ||
3321 | 47 | { | ||
3322 | 48 | } | ||
3323 | 49 | |||
3324 | 50 | Namespace(Glib::ustring const & p, Glib::ustring const & u) | ||
3325 | 51 | : prefix(p), uri(u) | ||
3326 | 52 | { | ||
3327 | 53 | } | ||
3328 | 54 | }; | ||
3329 | 55 | /** | ||
3330 | 56 | * Simple structure used in the start_element callbacks, in which | ||
3331 | 57 | * the attributes are a list of name/value/namespace tuples. | ||
3332 | 58 | */ | ||
3333 | 59 | struct Attribute | ||
3334 | 60 | { | ||
3335 | 61 | Glib::ustring name; | ||
3336 | 62 | Glib::ustring value; | ||
3337 | 63 | Namespace ns; | ||
3338 | 64 | |||
3339 | 65 | Attribute(Glib::ustring const & n, Glib::ustring const & v) | ||
3340 | 66 | : name(n), value(v) | ||
3341 | 67 | { | ||
3342 | 68 | } | ||
3343 | 69 | |||
3344 | 70 | Attribute(Glib::ustring const & n, Glib::ustring const & v, Glib::ustring const & p, Glib::ustring const & u) | ||
3345 | 71 | : name(n), value(v), ns(Namespace(p, u)) | ||
3346 | 72 | { | ||
3347 | 73 | } | ||
3348 | 74 | }; | ||
3349 | 75 | |||
3350 | 76 | using AttributeList = std::deque<Attribute>; | ||
3351 | 77 | using NamespaceList = std::deque<Namespace>; | ||
3352 | 78 | |||
3353 | 79 | /** This functor is a helper to find an attribute by name in an | ||
3354 | 80 | * AttributeList using the standard algorithm std::find_if | ||
3355 | 81 | * | ||
3356 | 82 | * Example:@n | ||
3357 | 83 | * <code> | ||
3358 | 84 | * Glib::ustring name = "foo";@n | ||
3359 | 85 | * AttributeList::const_iterator attribute = std::find_if(attributes.begin(), attributes.end(), AttributeHasName(name)); | ||
3360 | 86 | * </code> | ||
3361 | 87 | */ | ||
3362 | 88 | struct AttributeHasName | ||
3363 | 89 | { | ||
3364 | 90 | Glib::ustring const & name; | ||
3365 | 91 | |||
3366 | 92 | AttributeHasName(Glib::ustring const & n) | ||
3367 | 93 | : name(n) | ||
3368 | 94 | { | ||
3369 | 95 | } | ||
3370 | 96 | |||
3371 | 97 | bool operator()(Attribute const & attribute) | ||
3372 | 98 | { | ||
3373 | 99 | return attribute.name == name; | ||
3374 | 100 | } | ||
3375 | 101 | }; | ||
3376 | 102 | |||
3377 | 103 | /** | ||
3378 | 104 | * @param use_get_entity Set this to true if you will override on_get_entity(). | ||
3379 | 105 | * In theory, if you do not override on_get_entity() the parser should behave exactly the same | ||
3380 | 106 | * whether you use true or false here. But the default implementation of on_get_entity(), needed | ||
3381 | 107 | * if you override on_get_entity() might not have the same behaviour as the underlying default | ||
3382 | 108 | * behaviour of libxml, so the libxml implementation is the default here. | ||
3383 | 109 | */ | ||
3384 | 110 | SaxParser(bool use_get_entity = false); | ||
3385 | 111 | ~SaxParser() override; | ||
3386 | 112 | |||
3387 | 113 | /** Parse an XML document from a file. | ||
3388 | 114 | * @param filename The path to the file. | ||
3389 | 115 | * @throws xmlpp::internal_error | ||
3390 | 116 | * @throws xmlpp::parse_error | ||
3391 | 117 | * @throws xmlpp::validity_error | ||
3392 | 118 | */ | ||
3393 | 119 | void parse_file(const std::string& filename) override; | ||
3394 | 120 | |||
3395 | 121 | /** Parse an XML document from a string. | ||
3396 | 122 | * @param contents The XML document as a string. | ||
3397 | 123 | * @throws xmlpp::internal_error | ||
3398 | 124 | * @throws xmlpp::parse_error | ||
3399 | 125 | * @throws xmlpp::validity_error | ||
3400 | 126 | */ | ||
3401 | 127 | void parse_memory(const Glib::ustring& contents) override; | ||
3402 | 128 | |||
3403 | 129 | /** Parse an XML document from raw memory. | ||
3404 | 130 | * @param contents The XML document as an array of bytes. | ||
3405 | 131 | * @param bytes_count The number of bytes in the @a contents array. | ||
3406 | 132 | * @throws xmlpp::internal_error | ||
3407 | 133 | * @throws xmlpp::parse_error | ||
3408 | 134 | * @throws xmlpp::validity_error | ||
3409 | 135 | */ | ||
3410 | 136 | void parse_memory_raw(const unsigned char* contents, size_type bytes_count) override; | ||
3411 | 137 | |||
3412 | 138 | /** Parse an XML document from a stream. | ||
3413 | 139 | * @param in The stream. | ||
3414 | 140 | * @throws xmlpp::internal_error | ||
3415 | 141 | * @throws xmlpp::parse_error | ||
3416 | 142 | * @throws xmlpp::validity_error | ||
3417 | 143 | */ | ||
3418 | 144 | void parse_stream(std::istream& in) override; | ||
3419 | 145 | |||
3420 | 146 | /** Parse a chunk of data. | ||
3421 | 147 | * | ||
3422 | 148 | * This lets you pass a document in small chunks, e.g. from a network | ||
3423 | 149 | * connection. The on_* virtual functions are called each time the chunks | ||
3424 | 150 | * provide enough information to advance the parser. | ||
3425 | 151 | * | ||
3426 | 152 | * The first call to parse_chunk() will setup the parser. When the last chunk | ||
3427 | 153 | * has been parsed, call finish_chunk_parsing() to finish the parse. | ||
3428 | 154 | * | ||
3429 | 155 | * @param chunk The next piece of the XML document. | ||
3430 | 156 | * @throws xmlpp::internal_error | ||
3431 | 157 | * @throws xmlpp::parse_error | ||
3432 | 158 | * @throws xmlpp::validity_error | ||
3433 | 159 | */ | ||
3434 | 160 | void parse_chunk(const Glib::ustring& chunk); | ||
3435 | 161 | |||
3436 | 162 | /** Parse a chunk of data. | ||
3437 | 163 | * | ||
3438 | 164 | * @newin{2,24} | ||
3439 | 165 | * | ||
3440 | 166 | * This lets you pass a document in small chunks, e.g. from a network | ||
3441 | 167 | * connection. The on_* virtual functions are called each time the chunks | ||
3442 | 168 | * provide enough information to advance the parser. | ||
3443 | 169 | * | ||
3444 | 170 | * The first call to parse_chunk_raw() will setup the parser. When the last chunk | ||
3445 | 171 | * has been parsed, call finish_chunk_parsing() to finish the parse. | ||
3446 | 172 | * | ||
3447 | 173 | * @param contents The next piece of the XML document as an array of bytes. | ||
3448 | 174 | * @param bytes_count The number of bytes in the @a contents array. | ||
3449 | 175 | * @throws xmlpp::internal_error | ||
3450 | 176 | * @throws xmlpp::parse_error | ||
3451 | 177 | * @throws xmlpp::validity_error | ||
3452 | 178 | */ | ||
3453 | 179 | void parse_chunk_raw(const unsigned char* contents, size_type bytes_count); | ||
3454 | 180 | |||
3455 | 181 | /** Finish a chunk-wise parse. | ||
3456 | 182 | * | ||
3457 | 183 | * Call this after the last call to parse_chunk() or parse_chunk_raw(). | ||
3458 | 184 | * Don't use this function with the other parsing methods. | ||
3459 | 185 | * @throws xmlpp::internal_error | ||
3460 | 186 | * @throws xmlpp::parse_error | ||
3461 | 187 | * @throws xmlpp::validity_error | ||
3462 | 188 | */ | ||
3463 | 189 | void finish_chunk_parsing(); | ||
3464 | 190 | |||
3465 | 191 | protected: | ||
3466 | 192 | |||
3467 | 193 | virtual void on_start_document(); | ||
3468 | 194 | virtual void on_end_document(); | ||
3469 | 195 | virtual void on_start_element(const Glib::ustring& name, const AttributeList& attributes); | ||
3470 | 196 | virtual void on_end_element(const Glib::ustring& name); | ||
3471 | 197 | virtual void on_start_element_ns(const Glib::ustring& name, const Glib::ustring& prefix, const Glib::ustring& uri, const NamespaceList& namespaces, const AttributeList& attributes); | ||
3472 | 198 | virtual void on_end_element_ns(const Glib::ustring& name, const Glib::ustring& prefix, const Glib::ustring& uri); | ||
3473 | 199 | virtual void on_characters(const Glib::ustring& characters); | ||
3474 | 200 | virtual void on_comment(const Glib::ustring& text); | ||
3475 | 201 | virtual void on_processing_instruction(const Glib::ustring& target, const Glib::ustring& data); | ||
3476 | 202 | virtual void on_warning(const Glib::ustring& text); | ||
3477 | 203 | virtual void on_error(const Glib::ustring& text); | ||
3478 | 204 | |||
3479 | 205 | /** @throws xmlpp::parse_error | ||
3480 | 206 | */ | ||
3481 | 207 | virtual void on_fatal_error(const Glib::ustring& text); | ||
3482 | 208 | virtual void on_cdata_block(const Glib::ustring& text); | ||
3483 | 209 | |||
3484 | 210 | /** Override this to receive information about the document's DTD and any entity declarations. | ||
3485 | 211 | */ | ||
3486 | 212 | virtual void on_internal_subset(const Glib::ustring& name, const Glib::ustring& publicId, const Glib::ustring& systemId); | ||
3487 | 213 | |||
3488 | 214 | /** Override this method to resolve entities references in your derived parser, instead of using the default entity resolution, | ||
3489 | 215 | * or to be informed when entity references are encountered. | ||
3490 | 216 | * | ||
3491 | 217 | * If you override this function then you must also specify true for use_get_entity constructor parameter. | ||
3492 | 218 | * You will probably need to override on_entity_declaration() as well so that you can use that information when | ||
3493 | 219 | * resolving the entity reference. | ||
3494 | 220 | * | ||
3495 | 221 | * This is known to be difficult, because it requires both an understanding of the W3C specifications and knowledge of the | ||
3496 | 222 | * libxml internals. Entity resolution is easier with the DomParser. | ||
3497 | 223 | * | ||
3498 | 224 | * Call this method in this base class for default processing. For instance, if you just want to know about the existence of | ||
3499 | 225 | * an entity reference, without affecting the normal substitution, just override and call the base class. | ||
3500 | 226 | * | ||
3501 | 227 | * Unlike the DomParser, the SaxParser will also tell you about entity references for the 5 predefined entities. | ||
3502 | 228 | * | ||
3503 | 229 | * @param name The entity reference name. | ||
3504 | 230 | * @returns The resolved xmlEntity for the entity reference, or <tt>nullptr</tt> if not found. | ||
3505 | 231 | * You must include libxml/parser.h in order to use this C struct. | ||
3506 | 232 | * This instance will not be freed by the caller. | ||
3507 | 233 | */ | ||
3508 | 234 | virtual _xmlEntity* on_get_entity(const Glib::ustring& name); | ||
3509 | 235 | |||
3510 | 236 | /** Override this to receive information about every entity declaration. | ||
3511 | 237 | * If you override this function, and you want normal entity substitution to work, then you must call the base class in your override. | ||
3512 | 238 | * | ||
3513 | 239 | * This would be useful when overriding on_get_entity(). | ||
3514 | 240 | * @throws xmlpp::internal_error | ||
3515 | 241 | */ | ||
3516 | 242 | virtual void on_entity_declaration(const Glib::ustring& name, XmlEntityType type, const Glib::ustring& publicId, const Glib::ustring& systemId, const Glib::ustring& content); | ||
3517 | 243 | |||
3518 | 244 | void release_underlying() override; | ||
3519 | 245 | void initialize_context() override; | ||
3520 | 246 | |||
3521 | 247 | private: | ||
3522 | 248 | void parse(); | ||
3523 | 249 | |||
3524 | 250 | std::unique_ptr<_xmlSAXHandler> sax_handler_; | ||
3525 | 251 | |||
3526 | 252 | // A separate xmlpp::Document that is just used for entity resolution, | ||
3527 | 253 | // and never seen in the API: | ||
3528 | 254 | std::unique_ptr<Document> entity_resolver_doc_; | ||
3529 | 255 | |||
3530 | 256 | friend struct SaxParserCallback; | ||
3531 | 257 | }; | ||
3532 | 258 | |||
3533 | 259 | } // namespace xmlpp | ||
3534 | 260 | |||
3535 | 261 | #endif //__LIBXMLPP_PARSERS_SAXPARSER_H | ||
3536 | 0 | 262 | ||
3537 | === modified file 'src/preferences.cpp' | |||
3538 | --- src/preferences.cpp 2016-09-20 11:19:44 +0000 | |||
3539 | +++ src/preferences.cpp 2016-11-06 17:18:04 +0000 | |||
3540 | @@ -17,6 +17,7 @@ | |||
3541 | 17 | #include <glibmm/i18n.h> | 17 | #include <glibmm/i18n.h> |
3542 | 18 | #include <glib/gstdio.h> | 18 | #include <glib/gstdio.h> |
3543 | 19 | #include <gtk/gtk.h> | 19 | #include <gtk/gtk.h> |
3544 | 20 | #include <xml/repr-io.h> | ||
3545 | 20 | #include "preferences.h" | 21 | #include "preferences.h" |
3546 | 21 | #include "preferences-skeleton.h" | 22 | #include "preferences-skeleton.h" |
3547 | 22 | #include "inkscape.h" | 23 | #include "inkscape.h" |
3548 | @@ -123,7 +124,7 @@ | |||
3549 | 123 | */ | 124 | */ |
3550 | 124 | void Preferences::_loadDefaults() | 125 | void Preferences::_loadDefaults() |
3551 | 125 | { | 126 | { |
3553 | 126 | _prefs_doc = sp_repr_read_mem(preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL); | 127 | _prefs_doc = Inkscape::XML::read_svg_buffer(preferences_skeleton, true); |
3554 | 127 | } | 128 | } |
3555 | 128 | 129 | ||
3556 | 129 | /** | 130 | /** |
3557 | @@ -231,7 +232,8 @@ | |||
3558 | 231 | } | 232 | } |
3559 | 232 | 233 | ||
3560 | 233 | // 4. Is it valid XML? | 234 | // 4. Is it valid XML? |
3562 | 234 | Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL); | 235 | Inkscape::XML::Document *prefs_read = Inkscape::XML::read_svg_buffer(prefs_xml, true); |
3563 | 236 | |||
3564 | 235 | g_free(prefs_xml); | 237 | g_free(prefs_xml); |
3565 | 236 | if (!prefs_read) { | 238 | if (!prefs_read) { |
3566 | 237 | gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."), | 239 | gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."), |
3567 | @@ -281,7 +283,7 @@ | |||
3568 | 281 | // There are many other factors, so ask if you would like to learn them. - JAC | 283 | // There are many other factors, so ask if you would like to learn them. - JAC |
3569 | 282 | Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename); | 284 | Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename); |
3570 | 283 | if (!utf8name.empty()) { | 285 | if (!utf8name.empty()) { |
3572 | 284 | sp_repr_save_file(_prefs_doc, utf8name.c_str()); | 286 | Inkscape::XML::save_svg_file(_prefs_doc, utf8name); |
3573 | 285 | } | 287 | } |
3574 | 286 | } | 288 | } |
3575 | 287 | } | 289 | } |
3576 | 288 | 290 | ||
3577 | === modified file 'src/shortcuts.cpp' | |||
3578 | --- src/shortcuts.cpp 2016-08-03 13:29:38 +0000 | |||
3579 | +++ src/shortcuts.cpp 2016-11-06 17:18:04 +0000 | |||
3580 | @@ -32,6 +32,7 @@ | |||
3581 | 32 | #include <glibmm/i18n.h> | 32 | #include <glibmm/i18n.h> |
3582 | 33 | #include <glibmm/convert.h> | 33 | #include <glibmm/convert.h> |
3583 | 34 | #include <glibmm/miscutils.h> | 34 | #include <glibmm/miscutils.h> |
3584 | 35 | #include <xml/repr-io.h> | ||
3585 | 35 | 36 | ||
3586 | 36 | #include "helper/action.h" | 37 | #include "helper/action.h" |
3587 | 37 | #include "io/sys.h" | 38 | #include "io/sys.h" |
3588 | @@ -160,7 +161,7 @@ | |||
3589 | 160 | 161 | ||
3590 | 161 | char const *filename = get_path(USER, KEYS, "default.xml"); | 162 | char const *filename = get_path(USER, KEYS, "default.xml"); |
3591 | 162 | 163 | ||
3593 | 163 | XML::Document *doc=sp_repr_read_file(filename, NULL); | 164 | XML::Document *doc = Inkscape::XML::read_svg_file(filename, true); |
3594 | 164 | if (!doc) { | 165 | if (!doc) { |
3595 | 165 | g_warning("Unable to read keys file %s", filename); | 166 | g_warning("Unable to read keys file %s", filename); |
3596 | 166 | return; | 167 | return; |
3597 | @@ -183,8 +184,7 @@ | |||
3598 | 183 | iter=root->firstChild(); | 184 | iter=root->firstChild(); |
3599 | 184 | } | 185 | } |
3600 | 185 | 186 | ||
3603 | 186 | 187 | Inkscape::XML::save_svg_file(doc, filename); | |
3602 | 187 | sp_repr_save_file(doc, filename, NULL); | ||
3604 | 188 | 188 | ||
3605 | 189 | GC::release(doc); | 189 | GC::release(doc); |
3606 | 190 | } | 190 | } |
3607 | @@ -196,10 +196,10 @@ | |||
3608 | 196 | "<keys name=\"My custom shortcuts\">" | 196 | "<keys name=\"My custom shortcuts\">" |
3609 | 197 | "</keys>"; | 197 | "</keys>"; |
3610 | 198 | 198 | ||
3613 | 199 | Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL); | 199 | Inkscape::XML::Document *doc = Inkscape::XML::read_svg_buffer(buffer, true); |
3614 | 200 | sp_repr_save_file(doc, filename, NULL); | 200 | Inkscape::XML::save_svg_file(doc, filename); |
3615 | 201 | 201 | ||
3617 | 202 | return sp_repr_read_file(filename, NULL); | 202 | return Inkscape::XML::read_svg_file(filename, true); |
3618 | 203 | } | 203 | } |
3619 | 204 | 204 | ||
3620 | 205 | /* | 205 | /* |
3621 | @@ -242,7 +242,7 @@ | |||
3622 | 242 | if (!Inkscape::IO::file_test(full, G_FILE_TEST_IS_DIR)) { | 242 | if (!Inkscape::IO::file_test(full, G_FILE_TEST_IS_DIR)) { |
3623 | 243 | 243 | ||
3624 | 244 | // Get the "key name" from the root element of each file | 244 | // Get the "key name" from the root element of each file |
3626 | 245 | XML::Document *doc=sp_repr_read_file(full, NULL); | 245 | XML::Document *doc = Inkscape::XML::read_svg_file(full, true); |
3627 | 246 | if (!doc) { | 246 | if (!doc) { |
3628 | 247 | g_warning("Unable to read keyboard shortcut file %s", full); | 247 | g_warning("Unable to read keyboard shortcut file %s", full); |
3629 | 248 | continue; | 248 | continue; |
3630 | @@ -385,15 +385,15 @@ | |||
3631 | 385 | } | 385 | } |
3632 | 386 | 386 | ||
3633 | 387 | void sp_shortcut_file_import_do(char const *importname) { | 387 | void sp_shortcut_file_import_do(char const *importname) { |
3634 | 388 | XML::Document *doc = Inkscape::XML::read_svg_file(importname, true); | ||
3635 | 388 | 389 | ||
3636 | 389 | XML::Document *doc=sp_repr_read_file(importname, NULL); | ||
3637 | 390 | if (!doc) { | 390 | if (!doc) { |
3638 | 391 | g_warning("Unable to read keyboard shortcut file %s", importname); | 391 | g_warning("Unable to read keyboard shortcut file %s", importname); |
3639 | 392 | return; | 392 | return; |
3640 | 393 | } | 393 | } |
3641 | 394 | 394 | ||
3642 | 395 | char const *filename = get_path(USER, KEYS, "default.xml"); | 395 | char const *filename = get_path(USER, KEYS, "default.xml"); |
3644 | 396 | sp_repr_save_file(doc, filename, NULL); | 396 | Inkscape::XML::save_svg_file(doc, filename); |
3645 | 397 | 397 | ||
3646 | 398 | GC::release(doc); | 398 | GC::release(doc); |
3647 | 399 | 399 | ||
3648 | @@ -404,13 +404,13 @@ | |||
3649 | 404 | 404 | ||
3650 | 405 | char const *filename = get_path(USER, KEYS, "default.xml"); | 405 | char const *filename = get_path(USER, KEYS, "default.xml"); |
3651 | 406 | 406 | ||
3653 | 407 | XML::Document *doc=sp_repr_read_file(filename, NULL); | 407 | XML::Document *doc = Inkscape::XML::read_svg_file(filename, true); |
3654 | 408 | if (!doc) { | 408 | if (!doc) { |
3655 | 409 | g_warning("Unable to read keyboard shortcut file %s", filename); | 409 | g_warning("Unable to read keyboard shortcut file %s", filename); |
3656 | 410 | return; | 410 | return; |
3657 | 411 | } | 411 | } |
3658 | 412 | 412 | ||
3660 | 413 | sp_repr_save_file(doc, exportname, NULL); | 413 | Inkscape::XML::save_svg_file(doc, exportname); |
3661 | 414 | 414 | ||
3662 | 415 | GC::release(doc); | 415 | GC::release(doc); |
3663 | 416 | } | 416 | } |
3664 | @@ -428,7 +428,7 @@ | |||
3665 | 428 | 428 | ||
3666 | 429 | char const *filename = get_path(USER, KEYS, "default.xml"); | 429 | char const *filename = get_path(USER, KEYS, "default.xml"); |
3667 | 430 | 430 | ||
3669 | 431 | XML::Document *doc=sp_repr_read_file(filename, NULL); | 431 | XML::Document *doc = Inkscape::XML::read_svg_file(filename, true); |
3670 | 432 | if (!doc) { | 432 | if (!doc) { |
3671 | 433 | g_warning("Unable to read keyboard shortcut file %s", filename); | 433 | g_warning("Unable to read keyboard shortcut file %s", filename); |
3672 | 434 | return; | 434 | return; |
3673 | @@ -486,7 +486,7 @@ | |||
3674 | 486 | iter = iter->next(); | 486 | iter = iter->next(); |
3675 | 487 | } | 487 | } |
3676 | 488 | 488 | ||
3678 | 489 | sp_repr_save_file(doc, filename, NULL); | 489 | Inkscape::XML::save_svg_file(doc, filename); |
3679 | 490 | 490 | ||
3680 | 491 | GC::release(doc); | 491 | GC::release(doc); |
3681 | 492 | 492 | ||
3682 | @@ -496,7 +496,7 @@ | |||
3683 | 496 | 496 | ||
3684 | 497 | char const *filename = get_path(USER, KEYS, "default.xml"); | 497 | char const *filename = get_path(USER, KEYS, "default.xml"); |
3685 | 498 | 498 | ||
3687 | 499 | XML::Document *doc=sp_repr_read_file(filename, NULL); | 499 | XML::Document *doc = Inkscape::XML::read_svg_file(filename, true); |
3688 | 500 | if (!doc) { | 500 | if (!doc) { |
3689 | 501 | g_warning("Unable to read keyboard shortcut file %s, creating ....", filename); | 501 | g_warning("Unable to read keyboard shortcut file %s, creating ....", filename); |
3690 | 502 | doc = sp_shortcut_create_template_file(filename); | 502 | doc = sp_shortcut_create_template_file(filename); |
3691 | @@ -541,13 +541,13 @@ | |||
3692 | 541 | doc->root()->appendChild(newnode); | 541 | doc->root()->appendChild(newnode); |
3693 | 542 | } | 542 | } |
3694 | 543 | 543 | ||
3696 | 544 | sp_repr_save_file(doc, filename, NULL); | 544 | Inkscape::XML::save_svg_file(doc, filename); |
3697 | 545 | 545 | ||
3698 | 546 | GC::release(doc); | 546 | GC::release(doc); |
3699 | 547 | 547 | ||
3700 | 548 | } | 548 | } |
3701 | 549 | static void read_shortcuts_file(char const *filename, bool const is_user_set) { | 549 | static void read_shortcuts_file(char const *filename, bool const is_user_set) { |
3703 | 550 | XML::Document *doc=sp_repr_read_file(filename, NULL); | 550 | XML::Document *doc = Inkscape::XML::read_svg_file(filename, true); |
3704 | 551 | if (!doc) { | 551 | if (!doc) { |
3705 | 552 | g_warning("Unable to read keys file %s", filename); | 552 | g_warning("Unable to read keys file %s", filename); |
3706 | 553 | return; | 553 | return; |
3707 | 554 | 554 | ||
3708 | === modified file 'src/ui/dialog/template-load-tab.cpp' | |||
3709 | --- src/ui/dialog/template-load-tab.cpp 2016-08-03 13:29:38 +0000 | |||
3710 | +++ src/ui/dialog/template-load-tab.cpp 2016-11-06 17:18:04 +0000 | |||
3711 | @@ -17,6 +17,7 @@ | |||
3712 | 17 | #include <gtkmm/messagedialog.h> | 17 | #include <gtkmm/messagedialog.h> |
3713 | 18 | #include <gtkmm/scrolledwindow.h> | 18 | #include <gtkmm/scrolledwindow.h> |
3714 | 19 | #include <iostream> | 19 | #include <iostream> |
3715 | 20 | #include <xml/repr-io.h> | ||
3716 | 20 | 21 | ||
3717 | 21 | #include "extension/db.h" | 22 | #include "extension/db.h" |
3718 | 22 | #include "inkscape.h" | 23 | #include "inkscape.h" |
3719 | @@ -234,7 +235,7 @@ | |||
3720 | 234 | n = result.display_name.rfind(".svg"); | 235 | n = result.display_name.rfind(".svg"); |
3721 | 235 | result.display_name.replace(n, 4, 1, ' '); | 236 | result.display_name.replace(n, 4, 1, ' '); |
3722 | 236 | 237 | ||
3724 | 237 | Inkscape::XML::Document *rdoc = sp_repr_read_file(path.data(), SP_SVG_NS_URI); | 238 | Inkscape::XML::Document *rdoc = Inkscape::XML::read_svg_file(path.data(), false, SP_SVG_NS_URI); |
3725 | 238 | if (rdoc){ | 239 | if (rdoc){ |
3726 | 239 | Inkscape::XML::Node *myRoot = rdoc->root(); | 240 | Inkscape::XML::Node *myRoot = rdoc->root(); |
3727 | 240 | if (strcmp(myRoot->name(), "svg:svg") != 0){ // Wrong file format | 241 | if (strcmp(myRoot->name(), "svg:svg") != 0){ // Wrong file format |
3728 | 241 | 242 | ||
3729 | === modified file 'src/ui/interface.cpp' | |||
3730 | --- src/ui/interface.cpp 2016-10-24 22:58:43 +0000 | |||
3731 | +++ src/ui/interface.cpp 2016-11-06 17:18:04 +0000 | |||
3732 | @@ -29,6 +29,7 @@ | |||
3733 | 29 | #include <glibmm/miscutils.h> | 29 | #include <glibmm/miscutils.h> |
3734 | 30 | #include <gtkmm/imagemenuitem.h> | 30 | #include <gtkmm/imagemenuitem.h> |
3735 | 31 | #include <gtkmm/separatormenuitem.h> | 31 | #include <gtkmm/separatormenuitem.h> |
3736 | 32 | #include <xml/repr-io.h> | ||
3737 | 32 | 33 | ||
3738 | 33 | #include "inkscape.h" | 34 | #include "inkscape.h" |
3739 | 34 | #include "extension/db.h" | 35 | #include "extension/db.h" |
3740 | @@ -1174,7 +1175,7 @@ | |||
3741 | 1174 | case SVG_XML_DATA: { | 1175 | case SVG_XML_DATA: { |
3742 | 1175 | gchar *svgdata = (gchar *)gtk_selection_data_get_data (data); | 1176 | gchar *svgdata = (gchar *)gtk_selection_data_get_data (data); |
3743 | 1176 | 1177 | ||
3745 | 1177 | Inkscape::XML::Document *rnewdoc = sp_repr_read_mem(svgdata, gtk_selection_data_get_length (data), SP_SVG_NS_URI); | 1178 | Inkscape::XML::Document *rnewdoc = Inkscape::XML::read_svg_buffer(svgdata, false, SP_SVG_NS_URI); |
3746 | 1178 | 1179 | ||
3747 | 1179 | if (rnewdoc == NULL) { | 1180 | if (rnewdoc == NULL) { |
3748 | 1180 | sp_ui_error_dialog(_("Could not parse SVG data")); | 1181 | sp_ui_error_dialog(_("Could not parse SVG data")); |
3749 | 1181 | 1182 | ||
3750 | === modified file 'src/xml/CMakeLists.txt' | |||
3751 | --- src/xml/CMakeLists.txt 2011-10-08 06:34:23 +0000 | |||
3752 | +++ src/xml/CMakeLists.txt 2016-11-06 17:18:04 +0000 | |||
3753 | @@ -16,7 +16,7 @@ | |||
3754 | 16 | subtree.cpp | 16 | subtree.cpp |
3755 | 17 | helper-observer.cpp | 17 | helper-observer.cpp |
3756 | 18 | rebase-hrefs.cpp | 18 | rebase-hrefs.cpp |
3758 | 19 | 19 | svg-parser.cpp | |
3759 | 20 | 20 | ||
3760 | 21 | # ------- | 21 | # ------- |
3761 | 22 | # Headers | 22 | # Headers |
3762 | @@ -42,12 +42,14 @@ | |||
3763 | 42 | rebase-hrefs-test.h | 42 | rebase-hrefs-test.h |
3764 | 43 | rebase-hrefs.h | 43 | rebase-hrefs.h |
3765 | 44 | repr-action-test.h | 44 | repr-action-test.h |
3766 | 45 | repr-io.h | ||
3767 | 45 | repr-sorting.h | 46 | repr-sorting.h |
3768 | 46 | repr.h | 47 | repr.h |
3769 | 47 | simple-document.h | 48 | simple-document.h |
3770 | 48 | simple-node.h | 49 | simple-node.h |
3771 | 49 | sp-css-attr.h | 50 | sp-css-attr.h |
3772 | 50 | subtree.h | 51 | subtree.h |
3773 | 52 | svg-parser.h | ||
3774 | 51 | text-node.h | 53 | text-node.h |
3775 | 52 | ) | 54 | ) |
3776 | 53 | 55 | ||
3777 | 54 | 56 | ||
3778 | === modified file 'src/xml/comment-node.h' | |||
3779 | --- src/xml/comment-node.h 2011-12-08 11:53:54 +0000 | |||
3780 | +++ src/xml/comment-node.h 2016-11-06 17:18:04 +0000 | |||
3781 | @@ -17,6 +17,7 @@ | |||
3782 | 17 | 17 | ||
3783 | 18 | #include <glib.h> | 18 | #include <glib.h> |
3784 | 19 | #include "xml/simple-node.h" | 19 | #include "xml/simple-node.h" |
3785 | 20 | #include "io/inkscapestream.h" | ||
3786 | 20 | 21 | ||
3787 | 21 | namespace Inkscape { | 22 | namespace Inkscape { |
3788 | 22 | 23 | ||
3789 | @@ -37,6 +38,14 @@ | |||
3790 | 37 | 38 | ||
3791 | 38 | Inkscape::XML::NodeType type() const { return Inkscape::XML::COMMENT_NODE; } | 39 | Inkscape::XML::NodeType type() const { return Inkscape::XML::COMMENT_NODE; } |
3792 | 39 | 40 | ||
3793 | 41 | void serialize(Inkscape::IO::Writer& out, const Glib::ustring& defaultPrefix, int indent, int indent_level, bool inline_attributes, bool preserve_spaces) override { | ||
3794 | 42 | Glib::ustring real_indentation((unsigned long) (indent * indent_level), ' '); | ||
3795 | 43 | out.writeUString(real_indentation); | ||
3796 | 44 | out.writeString("<!--"); | ||
3797 | 45 | out.writeString(content()); | ||
3798 | 46 | out.writeString("-->\n"); | ||
3799 | 47 | } | ||
3800 | 48 | |||
3801 | 40 | protected: | 49 | protected: |
3802 | 41 | SimpleNode *_duplicate(Document* doc) const { return new CommentNode(*this, doc); } | 50 | SimpleNode *_duplicate(Document* doc) const { return new CommentNode(*this, doc); } |
3803 | 42 | }; | 51 | }; |
3804 | 43 | 52 | ||
3805 | === modified file 'src/xml/node.h' | |||
3806 | --- src/xml/node.h 2014-12-16 14:05:47 +0000 | |||
3807 | +++ src/xml/node.h 2016-11-06 17:18:04 +0000 | |||
3808 | @@ -23,6 +23,11 @@ | |||
3809 | 23 | #include "util/list.h" | 23 | #include "util/list.h" |
3810 | 24 | 24 | ||
3811 | 25 | namespace Inkscape { | 25 | namespace Inkscape { |
3812 | 26 | |||
3813 | 27 | namespace IO { | ||
3814 | 28 | class Writer; | ||
3815 | 29 | } | ||
3816 | 30 | |||
3817 | 26 | namespace XML { | 31 | namespace XML { |
3818 | 27 | 32 | ||
3819 | 28 | struct AttributeRecord; | 33 | struct AttributeRecord; |
3820 | @@ -193,8 +198,17 @@ | |||
3821 | 193 | * @param value The node's new content | 198 | * @param value The node's new content |
3822 | 194 | */ | 199 | */ |
3823 | 195 | virtual void setContent(char const *value)=0; | 200 | virtual void setContent(char const *value)=0; |
3826 | 196 | 201 | ||
3827 | 197 | //@{ | 202 | /** |
3828 | 203 | * @brief Change a namespace of this node | ||
3829 | 204 | * | ||
3830 | 205 | * The strings passed to this method are copied, so you can free them after use. | ||
3831 | 206 | * | ||
3832 | 207 | * @param prefix namespace prefix | ||
3833 | 208 | * @param uri namespace uri | ||
3834 | 209 | */ | ||
3835 | 210 | virtual void setNamespace(Glib::ustring const &prefix, Glib::ustring const &uri) = 0; | ||
3836 | 211 | |||
3837 | 198 | /** | 212 | /** |
3838 | 199 | * @brief Change an attribute of this node | 213 | * @brief Change an attribute of this node |
3839 | 200 | * | 214 | * |
3840 | @@ -470,9 +484,18 @@ | |||
3841 | 470 | */ | 484 | */ |
3842 | 471 | virtual void synthesizeEvents(NodeEventVector const *vector, void *data)=0; | 485 | virtual void synthesizeEvents(NodeEventVector const *vector, void *data)=0; |
3843 | 472 | 486 | ||
3844 | 487 | /** | ||
3845 | 488 | * @brief serialize node and all descendants into given stream | ||
3846 | 489 | * @param out output writer | ||
3847 | 490 | * @param indent size of indent in characters | ||
3848 | 491 | * @param indent_level level of indent, every child should have indent_level of parent plus 1 | ||
3849 | 492 | * @param inline_attributes whether attributes should be printed in one line | ||
3850 | 493 | * @param preserve_spaces whether xml:space is set to 'preserve' | ||
3851 | 494 | * @param defaultPrefix prefix for default namespace | ||
3852 | 495 | */ | ||
3853 | 496 | virtual void serialize(Inkscape::IO::Writer& out, const Glib::ustring& defaultPrefix, int indent, int indent_level, bool inline_attributes, bool preserve_spaces) = 0; | ||
3854 | 497 | |||
3855 | 473 | virtual void recursivePrintTree(unsigned level)=0; | 498 | virtual void recursivePrintTree(unsigned level)=0; |
3856 | 474 | |||
3857 | 475 | /*@}*/ | ||
3858 | 476 | 499 | ||
3859 | 477 | protected: | 500 | protected: |
3860 | 478 | Node(Node const &) : Anchored() {} | 501 | Node(Node const &) : Anchored() {} |
3861 | 479 | 502 | ||
3862 | === modified file 'src/xml/pi-node.h' | |||
3863 | --- src/xml/pi-node.h 2014-10-08 02:22:03 +0000 | |||
3864 | +++ src/xml/pi-node.h 2016-11-06 17:18:04 +0000 | |||
3865 | @@ -15,6 +15,7 @@ | |||
3866 | 15 | #define SEEN_INKSCAPE_XML_PI_NODE_H | 15 | #define SEEN_INKSCAPE_XML_PI_NODE_H |
3867 | 16 | 16 | ||
3868 | 17 | #include "xml/simple-node.h" | 17 | #include "xml/simple-node.h" |
3869 | 18 | #include "io/inkscapestream.h" | ||
3870 | 18 | 19 | ||
3871 | 19 | namespace Inkscape { | 20 | namespace Inkscape { |
3872 | 20 | 21 | ||
3873 | @@ -34,6 +35,11 @@ | |||
3874 | 34 | 35 | ||
3875 | 35 | Inkscape::XML::NodeType type() const { return Inkscape::XML::PI_NODE; } | 36 | Inkscape::XML::NodeType type() const { return Inkscape::XML::PI_NODE; } |
3876 | 36 | 37 | ||
3877 | 38 | void serialize(Inkscape::IO::Writer& out, const Glib::ustring& defaultPrefix, int indent, int indent_level, bool inline_attributes, bool preserve_spaces) override { | ||
3878 | 39 | out.printf("<?%s %s?>\n", name(), content()); | ||
3879 | 40 | } | ||
3880 | 41 | |||
3881 | 42 | |||
3882 | 37 | protected: | 43 | protected: |
3883 | 38 | SimpleNode *_duplicate(Document* doc) const { return new PINode(*this, doc); } | 44 | SimpleNode *_duplicate(Document* doc) const { return new PINode(*this, doc); } |
3884 | 39 | }; | 45 | }; |
3885 | 40 | 46 | ||
3886 | === modified file 'src/xml/repr-io.cpp' | |||
3887 | --- src/xml/repr-io.cpp 2016-08-03 13:29:38 +0000 | |||
3888 | +++ src/xml/repr-io.cpp 2016-11-06 17:18:04 +0000 | |||
3889 | @@ -1,1091 +1,200 @@ | |||
3890 | 1 | /* | 1 | /* |
3892 | 2 | * Dirty DOM-like tree | 2 | * IO functions for svg files. |
3893 | 3 | * | 3 | * |
3894 | 4 | * Authors: | 4 | * Authors: |
3897 | 5 | * Lauris Kaplinski <lauris@kaplinski.com> | 5 | * Adrian Boguszewski |
3896 | 6 | * bulia byak <buliabyak@users.sf.net> | ||
3898 | 7 | * | 6 | * |
3900 | 8 | * Copyright (C) 1999-2002 Lauris Kaplinski | 7 | * Copyright (C) 2016 Adrian Boguszewski |
3901 | 9 | * | 8 | * |
3902 | 10 | * Released under GNU GPL, read the file 'COPYING' for more information | 9 | * Released under GNU GPL, read the file 'COPYING' for more information |
3903 | 11 | */ | 10 | */ |
3904 | 12 | 11 | ||
3905 | 13 | #ifdef HAVE_CONFIG_H | ||
3906 | 14 | #include <config.h> | ||
3907 | 15 | #endif | ||
3908 | 16 | |||
3909 | 17 | #include <cstring> | ||
3910 | 18 | #include <string> | ||
3911 | 19 | #include <stdexcept> | ||
3912 | 20 | |||
3913 | 21 | #include <libxml/parser.h> | ||
3914 | 22 | |||
3915 | 23 | #include "xml/repr.h" | ||
3916 | 24 | #include "xml/attribute-record.h" | ||
3917 | 25 | #include "xml/rebase-hrefs.h" | ||
3918 | 26 | #include "xml/simple-document.h" | ||
3919 | 27 | #include "xml/text-node.h" | ||
3920 | 28 | |||
3921 | 29 | #include "io/sys.h" | ||
3922 | 30 | #include "io/uristream.h" | 12 | #include "io/uristream.h" |
3923 | 31 | #include "io/stringstream.h" | 13 | #include "io/stringstream.h" |
3924 | 32 | #include "io/gzipstream.h" | 14 | #include "io/gzipstream.h" |
3925 | 33 | |||
3926 | 34 | #include "extension/extension.h" | ||
3927 | 35 | |||
3928 | 36 | #include "attribute-rel-util.h" | 15 | #include "attribute-rel-util.h" |
3929 | 37 | #include "attribute-sort-util.h" | 16 | #include "attribute-sort-util.h" |
3930 | 38 | |||
3931 | 39 | #include "preferences.h" | 17 | #include "preferences.h" |
3933 | 40 | 18 | #include "repr-io.h" | |
3934 | 19 | #include "rebase-hrefs.h" | ||
3935 | 20 | #include "simple-node.h" | ||
3936 | 21 | #include "util/list.h" | ||
3937 | 22 | #include "svg-parser.h" | ||
3938 | 23 | #include "xml/attribute-record.h" | ||
3939 | 24 | #include <boost/algorithm/string.hpp> | ||
3940 | 25 | #include <io/sys.h> | ||
3941 | 41 | #include <glibmm/miscutils.h> | 26 | #include <glibmm/miscutils.h> |
4324 | 42 | 27 | #include <giomm/file.h> | |
4325 | 43 | using Inkscape::IO::Writer; | 28 | |
4326 | 44 | using Inkscape::Util::List; | 29 | using namespace Inkscape::Util; |
3945 | 45 | using Inkscape::Util::cons; | ||
3946 | 46 | using Inkscape::XML::Document; | ||
3947 | 47 | using Inkscape::XML::SimpleDocument; | ||
3948 | 48 | using Inkscape::XML::Node; | ||
3949 | 49 | using Inkscape::XML::AttributeRecord; | ||
3950 | 50 | using Inkscape::XML::calc_abs_doc_base; | ||
3951 | 51 | using Inkscape::XML::rebase_href_attrs; | ||
3952 | 52 | |||
3953 | 53 | Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns); | ||
3954 | 54 | static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, std::map<std::string, std::string> &prefix_map); | ||
3955 | 55 | static gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar *default_ns, std::map<std::string, std::string> &prefix_map); | ||
3956 | 56 | static void sp_repr_write_stream_root_element(Node *repr, Writer &out, | ||
3957 | 57 | bool add_whitespace, gchar const *default_ns, | ||
3958 | 58 | int inlineattrs, int indent, | ||
3959 | 59 | gchar const *old_href_abs_base, | ||
3960 | 60 | gchar const *new_href_abs_base); | ||
3961 | 61 | |||
3962 | 62 | static void sp_repr_write_stream_element(Node *repr, Writer &out, | ||
3963 | 63 | gint indent_level, bool add_whitespace, | ||
3964 | 64 | Glib::QueryQuark elide_prefix, | ||
3965 | 65 | List<AttributeRecord const> attributes, | ||
3966 | 66 | int inlineattrs, int indent, | ||
3967 | 67 | gchar const *old_href_abs_base, | ||
3968 | 68 | gchar const *new_href_abs_base); | ||
3969 | 69 | |||
3970 | 70 | #ifdef HAVE_LIBWMF | ||
3971 | 71 | static xmlDocPtr sp_wmf_convert (const char * file_name); | ||
3972 | 72 | static char * sp_wmf_image_name (void * context); | ||
3973 | 73 | #endif /* HAVE_LIBWMF */ | ||
3974 | 74 | |||
3975 | 75 | |||
3976 | 76 | class XmlSource | ||
3977 | 77 | { | ||
3978 | 78 | public: | ||
3979 | 79 | XmlSource() | ||
3980 | 80 | : filename(0), | ||
3981 | 81 | encoding(0), | ||
3982 | 82 | fp(NULL), | ||
3983 | 83 | firstFewLen(0), | ||
3984 | 84 | LoadEntities(false), | ||
3985 | 85 | cachedData(), | ||
3986 | 86 | cachedPos(0), | ||
3987 | 87 | dummy("x"), | ||
3988 | 88 | instr(NULL), | ||
3989 | 89 | gzin(NULL) | ||
3990 | 90 | { | ||
3991 | 91 | for (int k=0;k<4;k++) | ||
3992 | 92 | { | ||
3993 | 93 | firstFew[k]=0; | ||
3994 | 94 | } | ||
3995 | 95 | } | ||
3996 | 96 | virtual ~XmlSource() | ||
3997 | 97 | { | ||
3998 | 98 | close(); | ||
3999 | 99 | if ( encoding ) { | ||
4000 | 100 | g_free(encoding); | ||
4001 | 101 | encoding = 0; | ||
4002 | 102 | } | ||
4003 | 103 | } | ||
4004 | 104 | |||
4005 | 105 | int setFile( char const * filename, bool load_entities ); | ||
4006 | 106 | |||
4007 | 107 | xmlDocPtr readXml(); | ||
4008 | 108 | |||
4009 | 109 | static int readCb( void * context, char * buffer, int len ); | ||
4010 | 110 | static int closeCb( void * context ); | ||
4011 | 111 | |||
4012 | 112 | char const* getEncoding() const { return encoding; } | ||
4013 | 113 | int read( char * buffer, int len ); | ||
4014 | 114 | int close(); | ||
4015 | 115 | private: | ||
4016 | 116 | const char* filename; | ||
4017 | 117 | char* encoding; | ||
4018 | 118 | FILE* fp; | ||
4019 | 119 | unsigned char firstFew[4]; | ||
4020 | 120 | int firstFewLen; | ||
4021 | 121 | bool LoadEntities; // Checks for SYSTEM Entities (requires cached data) | ||
4022 | 122 | std::string cachedData; | ||
4023 | 123 | unsigned int cachedPos; | ||
4024 | 124 | Inkscape::URI dummy; | ||
4025 | 125 | Inkscape::IO::UriInputStream* instr; | ||
4026 | 126 | Inkscape::IO::GzipInputStream* gzin; | ||
4027 | 127 | }; | ||
4028 | 128 | |||
4029 | 129 | int XmlSource::setFile(char const *filename, bool load_entities=false) | ||
4030 | 130 | { | ||
4031 | 131 | int retVal = -1; | ||
4032 | 132 | |||
4033 | 133 | this->filename = filename; | ||
4034 | 134 | |||
4035 | 135 | fp = Inkscape::IO::fopen_utf8name(filename, "r"); | ||
4036 | 136 | if ( fp ) { | ||
4037 | 137 | // First peek in the file to see what it is | ||
4038 | 138 | memset( firstFew, 0, sizeof(firstFew) ); | ||
4039 | 139 | |||
4040 | 140 | size_t some = fread( firstFew, 1, 4, fp ); | ||
4041 | 141 | if ( fp ) { | ||
4042 | 142 | // first check for compression | ||
4043 | 143 | if ( (some >= 2) && (firstFew[0] == 0x1f) && (firstFew[1] == 0x8b) ) { | ||
4044 | 144 | //g_message(" the file being read is gzip'd. extract it"); | ||
4045 | 145 | fclose(fp); | ||
4046 | 146 | fp = 0; | ||
4047 | 147 | fp = Inkscape::IO::fopen_utf8name(filename, "r"); | ||
4048 | 148 | instr = new Inkscape::IO::UriInputStream(fp, dummy); | ||
4049 | 149 | gzin = new Inkscape::IO::GzipInputStream(*instr); | ||
4050 | 150 | |||
4051 | 151 | memset( firstFew, 0, sizeof(firstFew) ); | ||
4052 | 152 | some = 0; | ||
4053 | 153 | int single = 0; | ||
4054 | 154 | while ( some < 4 && single >= 0 ) | ||
4055 | 155 | { | ||
4056 | 156 | single = gzin->get(); | ||
4057 | 157 | if ( single >= 0 ) { | ||
4058 | 158 | firstFew[some++] = 0x0ff & single; | ||
4059 | 159 | } else { | ||
4060 | 160 | break; | ||
4061 | 161 | } | ||
4062 | 162 | } | ||
4063 | 163 | } | ||
4064 | 164 | |||
4065 | 165 | int encSkip = 0; | ||
4066 | 166 | if ( (some >= 2) &&(firstFew[0] == 0xfe) && (firstFew[1] == 0xff) ) { | ||
4067 | 167 | encoding = g_strdup("UTF-16BE"); | ||
4068 | 168 | encSkip = 2; | ||
4069 | 169 | } else if ( (some >= 2) && (firstFew[0] == 0xff) && (firstFew[1] == 0xfe) ) { | ||
4070 | 170 | encoding = g_strdup("UTF-16LE"); | ||
4071 | 171 | encSkip = 2; | ||
4072 | 172 | } else if ( (some >= 3) && (firstFew[0] == 0xef) && (firstFew[1] == 0xbb) && (firstFew[2] == 0xbf) ) { | ||
4073 | 173 | encoding = g_strdup("UTF-8"); | ||
4074 | 174 | encSkip = 3; | ||
4075 | 175 | } | ||
4076 | 176 | |||
4077 | 177 | if ( encSkip ) { | ||
4078 | 178 | memmove( firstFew, firstFew + encSkip, (some - encSkip) ); | ||
4079 | 179 | some -= encSkip; | ||
4080 | 180 | } | ||
4081 | 181 | |||
4082 | 182 | firstFewLen = some; | ||
4083 | 183 | retVal = 0; // no error | ||
4084 | 184 | } | ||
4085 | 185 | } | ||
4086 | 186 | if(load_entities) { | ||
4087 | 187 | this->cachedData = std::string(""); | ||
4088 | 188 | this->cachedPos = 0; | ||
4089 | 189 | |||
4090 | 190 | // First get data from file in typical way (cache it all) | ||
4091 | 191 | char *buffer = new char [4096]; | ||
4092 | 192 | while(true) { | ||
4093 | 193 | int len = this->read(buffer, 4096); | ||
4094 | 194 | if(len <= 0) break; | ||
4095 | 195 | buffer[len] = 0; | ||
4096 | 196 | this->cachedData += buffer; | ||
4097 | 197 | } | ||
4098 | 198 | delete[] buffer; | ||
4099 | 199 | |||
4100 | 200 | // Check for SYSTEM or PUBLIC entities and remove them from the cache | ||
4101 | 201 | GMatchInfo *info; | ||
4102 | 202 | gint start, end; | ||
4103 | 203 | |||
4104 | 204 | GRegex *regex = g_regex_new( | ||
4105 | 205 | "<!ENTITY\\s+[^>\\s]+\\s+(SYSTEM|PUBLIC\\s+\"[^>\"]+\")\\s+\"[^>\"]+\"\\s*>", | ||
4106 | 206 | G_REGEX_CASELESS, G_REGEX_MATCH_NEWLINE_ANY, NULL); | ||
4107 | 207 | |||
4108 | 208 | g_regex_match (regex, this->cachedData.c_str(), G_REGEX_MATCH_NEWLINE_ANY, &info); | ||
4109 | 209 | |||
4110 | 210 | while (g_match_info_matches (info)) { | ||
4111 | 211 | if (g_match_info_fetch_pos (info, 1, &start, &end)) | ||
4112 | 212 | this->cachedData.erase(start, end - start); | ||
4113 | 213 | g_match_info_next (info, NULL); | ||
4114 | 214 | } | ||
4115 | 215 | g_match_info_free(info); | ||
4116 | 216 | g_regex_unref(regex); | ||
4117 | 217 | } | ||
4118 | 218 | // Do this after loading cache, so reads don't return cache to fill cache. | ||
4119 | 219 | this->LoadEntities = load_entities; | ||
4120 | 220 | return retVal; | ||
4121 | 221 | } | ||
4122 | 222 | |||
4123 | 223 | xmlDocPtr XmlSource::readXml() | ||
4124 | 224 | { | ||
4125 | 225 | int parse_options = XML_PARSE_HUGE | XML_PARSE_RECOVER; | ||
4126 | 226 | |||
4127 | 227 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); | ||
4128 | 228 | bool allowNetAccess = prefs->getBool("/options/externalresources/xml/allow_net_access", false); | ||
4129 | 229 | if (!allowNetAccess) parse_options |= XML_PARSE_NONET; | ||
4130 | 230 | |||
4131 | 231 | // Allow NOENT only if we're filtering out SYSTEM and PUBLIC entities | ||
4132 | 232 | if (LoadEntities) parse_options |= XML_PARSE_NOENT; | ||
4133 | 233 | |||
4134 | 234 | return xmlReadIO( readCb, closeCb, this, | ||
4135 | 235 | filename, getEncoding(), parse_options); | ||
4136 | 236 | } | ||
4137 | 237 | |||
4138 | 238 | int XmlSource::readCb( void * context, char * buffer, int len ) | ||
4139 | 239 | { | ||
4140 | 240 | int retVal = -1; | ||
4141 | 241 | |||
4142 | 242 | if ( context ) { | ||
4143 | 243 | XmlSource* self = static_cast<XmlSource*>(context); | ||
4144 | 244 | retVal = self->read( buffer, len ); | ||
4145 | 245 | } | ||
4146 | 246 | return retVal; | ||
4147 | 247 | } | ||
4148 | 248 | |||
4149 | 249 | int XmlSource::closeCb(void * context) | ||
4150 | 250 | { | ||
4151 | 251 | if ( context ) { | ||
4152 | 252 | XmlSource* self = static_cast<XmlSource*>(context); | ||
4153 | 253 | self->close(); | ||
4154 | 254 | } | ||
4155 | 255 | return 0; | ||
4156 | 256 | } | ||
4157 | 257 | |||
4158 | 258 | int XmlSource::read( char *buffer, int len ) | ||
4159 | 259 | { | ||
4160 | 260 | int retVal = 0; | ||
4161 | 261 | size_t got = 0; | ||
4162 | 262 | |||
4163 | 263 | if ( LoadEntities ) { | ||
4164 | 264 | if (cachedPos >= cachedData.length()) { | ||
4165 | 265 | return -1; | ||
4166 | 266 | } else { | ||
4167 | 267 | retVal = cachedData.copy(buffer, len, cachedPos); | ||
4168 | 268 | cachedPos += retVal; | ||
4169 | 269 | return retVal; // Do NOT continue. | ||
4170 | 270 | } | ||
4171 | 271 | } else if ( firstFewLen > 0 ) { | ||
4172 | 272 | int some = (len < firstFewLen) ? len : firstFewLen; | ||
4173 | 273 | memcpy( buffer, firstFew, some ); | ||
4174 | 274 | if ( len < firstFewLen ) { | ||
4175 | 275 | memmove( firstFew, firstFew + some, (firstFewLen - some) ); | ||
4176 | 276 | } | ||
4177 | 277 | firstFewLen -= some; | ||
4178 | 278 | got = some; | ||
4179 | 279 | } else if ( gzin ) { | ||
4180 | 280 | int single = 0; | ||
4181 | 281 | while ( (static_cast<int>(got) < len) && (single >= 0) ) | ||
4182 | 282 | { | ||
4183 | 283 | single = gzin->get(); | ||
4184 | 284 | if ( single >= 0 ) { | ||
4185 | 285 | buffer[got++] = 0x0ff & single; | ||
4186 | 286 | } else { | ||
4187 | 287 | break; | ||
4188 | 288 | } | ||
4189 | 289 | } | ||
4190 | 290 | } else { | ||
4191 | 291 | got = fread( buffer, 1, len, fp ); | ||
4192 | 292 | } | ||
4193 | 293 | |||
4194 | 294 | if ( feof(fp) ) { | ||
4195 | 295 | retVal = got; | ||
4196 | 296 | } else if ( ferror(fp) ) { | ||
4197 | 297 | retVal = -1; | ||
4198 | 298 | } else { | ||
4199 | 299 | retVal = got; | ||
4200 | 300 | } | ||
4201 | 301 | |||
4202 | 302 | return retVal; | ||
4203 | 303 | } | ||
4204 | 304 | |||
4205 | 305 | int XmlSource::close() | ||
4206 | 306 | { | ||
4207 | 307 | if ( gzin ) { | ||
4208 | 308 | gzin->close(); | ||
4209 | 309 | delete gzin; | ||
4210 | 310 | gzin = 0; | ||
4211 | 311 | } | ||
4212 | 312 | if ( instr ) { | ||
4213 | 313 | instr->close(); | ||
4214 | 314 | fp = 0; | ||
4215 | 315 | delete instr; | ||
4216 | 316 | instr = 0; | ||
4217 | 317 | } | ||
4218 | 318 | if ( fp ) { | ||
4219 | 319 | fclose(fp); | ||
4220 | 320 | fp = 0; | ||
4221 | 321 | } | ||
4222 | 322 | return 0; | ||
4223 | 323 | } | ||
4224 | 324 | |||
4225 | 325 | /** | ||
4226 | 326 | * Reads XML from a file, including WMF files, and returns the Document. | ||
4227 | 327 | * The default namespace can also be specified, if desired. | ||
4228 | 328 | */ | ||
4229 | 329 | Document *sp_repr_read_file (const gchar * filename, const gchar *default_ns) | ||
4230 | 330 | { | ||
4231 | 331 | // g_warning( "Reading file: %s", filename ); | ||
4232 | 332 | xmlDocPtr doc = 0; | ||
4233 | 333 | Document * rdoc = 0; | ||
4234 | 334 | |||
4235 | 335 | xmlSubstituteEntitiesDefault(1); | ||
4236 | 336 | |||
4237 | 337 | g_return_val_if_fail (filename != NULL, NULL); | ||
4238 | 338 | if (!Inkscape::IO::file_test( filename, G_FILE_TEST_EXISTS )) { | ||
4239 | 339 | g_warning("Can't open file: %s (doesn't exist)", filename); | ||
4240 | 340 | return NULL; | ||
4241 | 341 | } | ||
4242 | 342 | /* fixme: A file can disappear at any time, including between now and when we actually try to | ||
4243 | 343 | * open it. Get rid of the above test once we're sure that we correctly handle | ||
4244 | 344 | * non-existence. */ | ||
4245 | 345 | |||
4246 | 346 | // TODO: bulia, please look over | ||
4247 | 347 | gsize bytesRead = 0; | ||
4248 | 348 | gsize bytesWritten = 0; | ||
4249 | 349 | GError* error = NULL; | ||
4250 | 350 | // TODO: need to replace with our own fopen and reading | ||
4251 | 351 | gchar* localFilename = g_filename_from_utf8 ( filename, | ||
4252 | 352 | -1, &bytesRead, &bytesWritten, &error); | ||
4253 | 353 | g_return_val_if_fail( localFilename != NULL, NULL ); | ||
4254 | 354 | |||
4255 | 355 | Inkscape::IO::dump_fopen_call( filename, "N" ); | ||
4256 | 356 | |||
4257 | 357 | #ifdef HAVE_LIBWMF | ||
4258 | 358 | if (strlen (localFilename) > 4) { | ||
4259 | 359 | if ( (strcmp (localFilename + strlen (localFilename) - 4,".wmf") == 0) | ||
4260 | 360 | || (strcmp (localFilename + strlen (localFilename) - 4,".WMF") == 0)) { | ||
4261 | 361 | doc = sp_wmf_convert (localFilename); | ||
4262 | 362 | } | ||
4263 | 363 | } | ||
4264 | 364 | #endif // !HAVE_LIBWMF | ||
4265 | 365 | |||
4266 | 366 | if ( !doc ) { | ||
4267 | 367 | XmlSource src; | ||
4268 | 368 | |||
4269 | 369 | if ( (src.setFile(filename) == 0) ) { | ||
4270 | 370 | doc = src.readXml(); | ||
4271 | 371 | rdoc = sp_repr_do_read( doc, default_ns ); | ||
4272 | 372 | // For some reason, failed ns loading results in this | ||
4273 | 373 | // We try a system check version of load with NOENT for adobe | ||
4274 | 374 | if(rdoc && strcmp(rdoc->root()->name(), "ns:svg") == 0) { | ||
4275 | 375 | xmlFreeDoc( doc ); | ||
4276 | 376 | src.setFile(filename, true); | ||
4277 | 377 | doc = src.readXml(); | ||
4278 | 378 | rdoc = sp_repr_do_read( doc, default_ns ); | ||
4279 | 379 | } | ||
4280 | 380 | } | ||
4281 | 381 | } | ||
4282 | 382 | |||
4283 | 383 | |||
4284 | 384 | if ( doc ) { | ||
4285 | 385 | xmlFreeDoc( doc ); | ||
4286 | 386 | } | ||
4287 | 387 | |||
4288 | 388 | if ( localFilename ) { | ||
4289 | 389 | g_free( localFilename ); | ||
4290 | 390 | } | ||
4291 | 391 | |||
4292 | 392 | return rdoc; | ||
4293 | 393 | } | ||
4294 | 394 | |||
4295 | 395 | /** | ||
4296 | 396 | * Reads and parses XML from a buffer, returning it as an Document | ||
4297 | 397 | */ | ||
4298 | 398 | Document *sp_repr_read_mem (const gchar * buffer, gint length, const gchar *default_ns) | ||
4299 | 399 | { | ||
4300 | 400 | xmlDocPtr doc; | ||
4301 | 401 | Document * rdoc; | ||
4302 | 402 | |||
4303 | 403 | xmlSubstituteEntitiesDefault(1); | ||
4304 | 404 | |||
4305 | 405 | g_return_val_if_fail (buffer != NULL, NULL); | ||
4306 | 406 | |||
4307 | 407 | doc = xmlParseMemory (const_cast<gchar *>(buffer), length); | ||
4308 | 408 | |||
4309 | 409 | rdoc = sp_repr_do_read (doc, default_ns); | ||
4310 | 410 | if (doc) { | ||
4311 | 411 | xmlFreeDoc (doc); | ||
4312 | 412 | } | ||
4313 | 413 | return rdoc; | ||
4314 | 414 | } | ||
4315 | 415 | |||
4316 | 416 | /** | ||
4317 | 417 | * Reads and parses XML from a buffer, returning it as an Document | ||
4318 | 418 | */ | ||
4319 | 419 | Document *sp_repr_read_buf (const Glib::ustring &buf, const gchar *default_ns) | ||
4320 | 420 | { | ||
4321 | 421 | return sp_repr_read_mem(buf.c_str(), buf.size(), default_ns); | ||
4322 | 422 | } | ||
4323 | 423 | |||
4327 | 424 | 30 | ||
4328 | 425 | namespace Inkscape { | 31 | namespace Inkscape { |
4329 | 426 | 32 | ||
4542 | 427 | struct compare_quark_ids { | 33 | namespace XML { |
4543 | 428 | bool operator()(Glib::QueryQuark const &a, Glib::QueryQuark const &b) const { | 34 | |
4544 | 429 | return a.id() < b.id(); | 35 | using namespace Inkscape::IO; |
4545 | 430 | } | 36 | |
4546 | 431 | }; | 37 | static SVGParser _parser; |
4547 | 432 | 38 | ||
4548 | 433 | } | 39 | static void clean_attributes(Document *doc) { |
4549 | 434 | 40 | // Clean unnecessary attributes and style properties from SVG documents (controlled by preferences) | |
4338 | 435 | namespace { | ||
4339 | 436 | |||
4340 | 437 | typedef std::map<Glib::QueryQuark, Glib::QueryQuark, Inkscape::compare_quark_ids> PrefixMap; | ||
4341 | 438 | |||
4342 | 439 | Glib::QueryQuark qname_prefix(Glib::QueryQuark qname) { | ||
4343 | 440 | static PrefixMap prefix_map; | ||
4344 | 441 | PrefixMap::iterator iter = prefix_map.find(qname); | ||
4345 | 442 | if ( iter != prefix_map.end() ) { | ||
4346 | 443 | return (*iter).second; | ||
4347 | 444 | } else { | ||
4348 | 445 | gchar const *name_string=g_quark_to_string(qname); | ||
4349 | 446 | gchar const *prefix_end=strchr(name_string, ':'); | ||
4350 | 447 | if (prefix_end) { | ||
4351 | 448 | Glib::Quark prefix=Glib::ustring(name_string, prefix_end); | ||
4352 | 449 | prefix_map.insert(PrefixMap::value_type(qname, prefix)); | ||
4353 | 450 | return prefix; | ||
4354 | 451 | } else { | ||
4355 | 452 | return GQuark(0); | ||
4356 | 453 | } | ||
4357 | 454 | } | ||
4358 | 455 | } | ||
4359 | 456 | |||
4360 | 457 | } | ||
4361 | 458 | |||
4362 | 459 | namespace { | ||
4363 | 460 | |||
4364 | 461 | void promote_to_namespace(Node *repr, const gchar *prefix) { | ||
4365 | 462 | if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) { | ||
4366 | 463 | GQuark code = repr->code(); | ||
4367 | 464 | if (!qname_prefix(code).id()) { | ||
4368 | 465 | gchar *svg_name = g_strconcat(prefix, ":", g_quark_to_string(code), NULL); | ||
4369 | 466 | repr->setCodeUnsafe(g_quark_from_string(svg_name)); | ||
4370 | 467 | g_free(svg_name); | ||
4371 | 468 | } | ||
4372 | 469 | for ( Node *child = repr->firstChild() ; child ; child = child->next() ) { | ||
4373 | 470 | promote_to_namespace(child, prefix); | ||
4374 | 471 | } | ||
4375 | 472 | } | ||
4376 | 473 | } | ||
4377 | 474 | |||
4378 | 475 | } | ||
4379 | 476 | |||
4380 | 477 | /** | ||
4381 | 478 | * Reads in a XML file to create a Document | ||
4382 | 479 | */ | ||
4383 | 480 | Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns) | ||
4384 | 481 | { | ||
4385 | 482 | if (doc == NULL) { | ||
4386 | 483 | return NULL; | ||
4387 | 484 | } | ||
4388 | 485 | xmlNodePtr node=xmlDocGetRootElement (doc); | ||
4389 | 486 | if (node == NULL) { | ||
4390 | 487 | return NULL; | ||
4391 | 488 | } | ||
4392 | 489 | |||
4393 | 490 | std::map<std::string, std::string> prefix_map; | ||
4394 | 491 | |||
4395 | 492 | Document *rdoc = new Inkscape::XML::SimpleDocument(); | ||
4396 | 493 | |||
4397 | 494 | Node *root=NULL; | ||
4398 | 495 | for ( node = doc->children ; node != NULL ; node = node->next ) { | ||
4399 | 496 | if (node->type == XML_ELEMENT_NODE) { | ||
4400 | 497 | Node *repr=sp_repr_svg_read_node(rdoc, node, default_ns, prefix_map); | ||
4401 | 498 | rdoc->appendChild(repr); | ||
4402 | 499 | Inkscape::GC::release(repr); | ||
4403 | 500 | |||
4404 | 501 | if (!root) { | ||
4405 | 502 | root = repr; | ||
4406 | 503 | } else { | ||
4407 | 504 | root = NULL; | ||
4408 | 505 | break; | ||
4409 | 506 | } | ||
4410 | 507 | } else if ( node->type == XML_COMMENT_NODE || node->type == XML_PI_NODE ) { | ||
4411 | 508 | Node *repr=sp_repr_svg_read_node(rdoc, node, default_ns, prefix_map); | ||
4412 | 509 | rdoc->appendChild(repr); | ||
4413 | 510 | Inkscape::GC::release(repr); | ||
4414 | 511 | } | ||
4415 | 512 | } | ||
4416 | 513 | |||
4417 | 514 | if (root != NULL) { | ||
4418 | 515 | /* promote elements of some XML documents that don't use namespaces | ||
4419 | 516 | * into their default namespace */ | ||
4420 | 517 | if ( default_ns && !strchr(root->name(), ':') ) { | ||
4421 | 518 | if ( !strcmp(default_ns, SP_SVG_NS_URI) ) { | ||
4422 | 519 | promote_to_namespace(root, "svg"); | ||
4423 | 520 | } | ||
4424 | 521 | if ( !strcmp(default_ns, INKSCAPE_EXTENSION_URI) ) { | ||
4425 | 522 | promote_to_namespace(root, INKSCAPE_EXTENSION_NS_NC); | ||
4426 | 523 | } | ||
4427 | 524 | } | ||
4428 | 525 | |||
4429 | 526 | |||
4430 | 527 | // Clean unnecessary attributes and style properties from SVG documents. (Controlled by | ||
4431 | 528 | // preferences.) Note: internal Inkscape svg files will also be cleaned (filters.svg, | ||
4432 | 529 | // icons.svg). How can one tell if a file is internal? | ||
4433 | 530 | if ( !strcmp(root->name(), "svg:svg" ) ) { | ||
4434 | 531 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); | ||
4435 | 532 | bool clean = prefs->getBool("/options/svgoutput/check_on_reading"); | ||
4436 | 533 | if( clean ) { | ||
4437 | 534 | sp_attribute_clean_tree( root ); | ||
4438 | 535 | } | ||
4439 | 536 | } | ||
4440 | 537 | } | ||
4441 | 538 | |||
4442 | 539 | return rdoc; | ||
4443 | 540 | } | ||
4444 | 541 | |||
4445 | 542 | gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar */*default_ns*/, std::map<std::string, std::string> &prefix_map) | ||
4446 | 543 | { | ||
4447 | 544 | const xmlChar *prefix; | ||
4448 | 545 | if (ns){ | ||
4449 | 546 | if (ns->href ) { | ||
4450 | 547 | prefix = reinterpret_cast<const xmlChar*>( sp_xml_ns_uri_prefix(reinterpret_cast<const gchar*>(ns->href), | ||
4451 | 548 | reinterpret_cast<const char*>(ns->prefix)) ); | ||
4452 | 549 | prefix_map[reinterpret_cast<const char*>(prefix)] = reinterpret_cast<const char*>(ns->href); | ||
4453 | 550 | } | ||
4454 | 551 | else { | ||
4455 | 552 | prefix = NULL; | ||
4456 | 553 | } | ||
4457 | 554 | } | ||
4458 | 555 | else { | ||
4459 | 556 | prefix = NULL; | ||
4460 | 557 | } | ||
4461 | 558 | |||
4462 | 559 | if (prefix) { | ||
4463 | 560 | return g_snprintf (p, len, "%s:%s", reinterpret_cast<const gchar*>(prefix), name); | ||
4464 | 561 | } else { | ||
4465 | 562 | return g_snprintf (p, len, "%s", name); | ||
4466 | 563 | } | ||
4467 | 564 | } | ||
4468 | 565 | |||
4469 | 566 | static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, std::map<std::string, std::string> &prefix_map) | ||
4470 | 567 | { | ||
4471 | 568 | xmlAttrPtr prop; | ||
4472 | 569 | xmlNodePtr child; | ||
4473 | 570 | gchar c[256]; | ||
4474 | 571 | |||
4475 | 572 | if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) { | ||
4476 | 573 | |||
4477 | 574 | if (node->content == NULL || *(node->content) == '\0') { | ||
4478 | 575 | return NULL; // empty text node | ||
4479 | 576 | } | ||
4480 | 577 | |||
4481 | 578 | bool preserve = (xmlNodeGetSpacePreserve (node) == 1); | ||
4482 | 579 | |||
4483 | 580 | xmlChar *p; | ||
4484 | 581 | for (p = node->content; *p && g_ascii_isspace (*p) && !preserve; p++) | ||
4485 | 582 | ; // skip all whitespace | ||
4486 | 583 | |||
4487 | 584 | if (!(*p)) { // this is an all-whitespace node, and preserve == default | ||
4488 | 585 | return NULL; // we do not preserve all-whitespace nodes unless we are asked to | ||
4489 | 586 | } | ||
4490 | 587 | |||
4491 | 588 | // We keep track of original node type so that CDATA sections are preserved on output. | ||
4492 | 589 | return xml_doc->createTextNode(reinterpret_cast<gchar *>(node->content), | ||
4493 | 590 | node->type == XML_CDATA_SECTION_NODE ); | ||
4494 | 591 | } | ||
4495 | 592 | |||
4496 | 593 | if (node->type == XML_COMMENT_NODE) { | ||
4497 | 594 | return xml_doc->createComment(reinterpret_cast<gchar *>(node->content)); | ||
4498 | 595 | } | ||
4499 | 596 | |||
4500 | 597 | if (node->type == XML_PI_NODE) { | ||
4501 | 598 | return xml_doc->createPI(reinterpret_cast<const gchar *>(node->name), | ||
4502 | 599 | reinterpret_cast<const gchar *>(node->content)); | ||
4503 | 600 | } | ||
4504 | 601 | |||
4505 | 602 | if (node->type == XML_ENTITY_DECL) { | ||
4506 | 603 | return NULL; | ||
4507 | 604 | } | ||
4508 | 605 | |||
4509 | 606 | sp_repr_qualified_name (c, 256, node->ns, node->name, default_ns, prefix_map); | ||
4510 | 607 | Node *repr = xml_doc->createElement(c); | ||
4511 | 608 | /* TODO remember node->ns->prefix if node->ns != NULL */ | ||
4512 | 609 | |||
4513 | 610 | for (prop = node->properties; prop != NULL; prop = prop->next) { | ||
4514 | 611 | if (prop->children) { | ||
4515 | 612 | sp_repr_qualified_name (c, 256, prop->ns, prop->name, default_ns, prefix_map); | ||
4516 | 613 | repr->setAttribute(c, reinterpret_cast<gchar*>(prop->children->content)); | ||
4517 | 614 | /* TODO remember prop->ns->prefix if prop->ns != NULL */ | ||
4518 | 615 | } | ||
4519 | 616 | } | ||
4520 | 617 | |||
4521 | 618 | if (node->content) { | ||
4522 | 619 | repr->setContent(reinterpret_cast<gchar*>(node->content)); | ||
4523 | 620 | } | ||
4524 | 621 | |||
4525 | 622 | for (child = node->xmlChildrenNode; child != NULL; child = child->next) { | ||
4526 | 623 | Node *crepr = sp_repr_svg_read_node (xml_doc, child, default_ns, prefix_map); | ||
4527 | 624 | if (crepr) { | ||
4528 | 625 | repr->appendChild(crepr); | ||
4529 | 626 | Inkscape::GC::release(crepr); | ||
4530 | 627 | } | ||
4531 | 628 | } | ||
4532 | 629 | |||
4533 | 630 | return repr; | ||
4534 | 631 | } | ||
4535 | 632 | |||
4536 | 633 | |||
4537 | 634 | static void sp_repr_save_writer(Document *doc, Inkscape::IO::Writer *out, | ||
4538 | 635 | gchar const *default_ns, | ||
4539 | 636 | gchar const *old_href_abs_base, | ||
4540 | 637 | gchar const *new_href_abs_base) | ||
4541 | 638 | { | ||
4550 | 639 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); | 41 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); |
4645 | 640 | bool inlineattrs = prefs->getBool("/options/svgoutput/inlineattrs"); | 42 | bool clean = prefs->getBool("/options/svgoutput/check_on_reading"); |
4646 | 641 | int indent = prefs->getInt("/options/svgoutput/indent", 2); | 43 | if(clean) { |
4647 | 642 | 44 | sp_attribute_clean_tree(doc->root()); | |
4648 | 643 | /* fixme: do this The Right Way */ | 45 | } |
4649 | 644 | out->writeString( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" ); | 46 | } |
4650 | 645 | 47 | ||
4651 | 646 | const gchar *str = static_cast<Node *>(doc)->attribute("doctype"); | 48 | Document* read_svg_file(const Glib::ustring& filename, const bool& is_internal, const Glib::ustring& default_ns) { |
4652 | 647 | if (str) { | 49 | if (!Inkscape::IO::file_test(filename.c_str(), G_FILE_TEST_EXISTS)) { |
4653 | 648 | out->writeString( str ); | 50 | g_warning("Can't open file: %s (doesn't exist)", filename.c_str()); |
4654 | 649 | } | 51 | return nullptr; |
4655 | 650 | 52 | } | |
4656 | 651 | for (Node *repr = sp_repr_document_first_child(doc); | 53 | |
4657 | 652 | repr; repr = repr->next()) | 54 | Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filename); |
4658 | 653 | { | 55 | Glib::RefPtr<Gio::FileInfo> info = file->query_info(); |
4659 | 654 | Inkscape::XML::NodeType const node_type = repr->type(); | 56 | std::string content_type = info->get_content_type(); |
4660 | 655 | if ( node_type == Inkscape::XML::ELEMENT_NODE ) { | 57 | |
4661 | 656 | sp_repr_write_stream_root_element(repr, *out, TRUE, default_ns, inlineattrs, indent, | 58 | Document *doc; |
4662 | 657 | old_href_abs_base, new_href_abs_base); | 59 | if(content_type == "image/svg+xml" || content_type == "application/xml" || content_type == "text/html" |
4663 | 658 | } else { | 60 | // Windows doesn't support mime type |
4664 | 659 | sp_repr_write_stream(repr, *out, 0, TRUE, GQuark(0), inlineattrs, indent, | 61 | || content_type == ".svg" || content_type == ".xml" || content_type == ".inx" || content_type == ".htm" || content_type == ".html") { |
4665 | 660 | old_href_abs_base, new_href_abs_base); | 62 | doc = _parser.parseFile(filename, default_ns); |
4666 | 661 | if ( node_type == Inkscape::XML::COMMENT_NODE ) { | 63 | } else if (content_type == "image/svg+xml-compressed" || content_type == "application/x-gzip" |
4667 | 662 | out->writeChar('\n'); | 64 | // the same as above |
4668 | 663 | } | 65 | || content_type == ".svgz") { |
4669 | 664 | } | 66 | doc = _parser.parseCompressedFile(filename, default_ns); |
4670 | 665 | } | 67 | } else { |
4671 | 666 | } | 68 | g_warning("Unrecognized content type: %s.", content_type.c_str()); |
4672 | 667 | 69 | return nullptr; | |
4673 | 668 | 70 | } | |
4674 | 669 | Glib::ustring sp_repr_save_buf(Document *doc) | 71 | |
4675 | 670 | { | 72 | if(!is_internal) { |
4676 | 671 | Inkscape::IO::StringOutputStream souts; | 73 | clean_attributes(doc); |
4677 | 672 | Inkscape::IO::OutputStreamWriter outs(souts); | 74 | } |
4678 | 673 | 75 | return doc; | |
4679 | 674 | sp_repr_save_writer(doc, &outs, SP_INKSCAPE_NS_URI, 0, 0); | 76 | } |
4680 | 675 | 77 | ||
4681 | 676 | outs.close(); | 78 | Document* read_svg_buffer(const Glib::ustring& source, const bool& is_internal, const Glib::ustring& default_ns) { |
4682 | 677 | Glib::ustring buf = souts.getString(); | 79 | Document *doc = _parser.parseBuffer(source, default_ns); |
4683 | 678 | 80 | if(!is_internal) { | |
4684 | 679 | return buf; | 81 | clean_attributes(doc); |
4685 | 680 | } | 82 | } |
4686 | 681 | 83 | return doc; | |
4687 | 682 | 84 | } | |
4688 | 683 | void sp_repr_save_stream(Document *doc, FILE *fp, gchar const *default_ns, bool compress, | 85 | |
4689 | 684 | gchar const *const old_href_abs_base, | 86 | static void rebase_attributes(Node* node, const Glib::ustring& old_href_base, const Glib::ustring& new_href_base) { |
4690 | 685 | gchar const *const new_href_abs_base) | 87 | for(auto iter = rebase_href_attrs(old_href_base.c_str(), new_href_base.c_str(), node->attributeList()); iter; ++iter) { |
4691 | 686 | { | 88 | node->setAttribute(g_quark_to_string(iter->key), iter->value); |
4692 | 687 | Inkscape::URI dummy("x"); | 89 | } |
4693 | 688 | Inkscape::IO::UriOutputStream bout(fp, dummy); | 90 | |
4694 | 689 | Inkscape::IO::GzipOutputStream *gout = compress ? new Inkscape::IO::GzipOutputStream(bout) : NULL; | 91 | for (Node *child = node->firstChild(); child != nullptr; child = child->next()) { |
4695 | 690 | Inkscape::IO::OutputStreamWriter *out = compress ? new Inkscape::IO::OutputStreamWriter( *gout ) : new Inkscape::IO::OutputStreamWriter( bout ); | 92 | if (child->type() == ELEMENT_NODE) { |
4696 | 691 | 93 | rebase_attributes(child, old_href_base, new_href_base); | |
4697 | 692 | sp_repr_save_writer(doc, out, default_ns, old_href_abs_base, new_href_abs_base); | 94 | } |
4698 | 693 | 95 | } | |
4699 | 694 | delete out; | 96 | } |
4700 | 695 | delete gout; | 97 | |
4701 | 696 | } | 98 | static void add_namespace_to_doc(Document* doc, const Glib::ustring& node_name) { |
4702 | 697 | 99 | if (node_name.find(":") != Glib::ustring::npos) { | |
4703 | 698 | 100 | Glib::ustring prefix = node_name.substr(0, node_name.find(":")); | |
4704 | 699 | 101 | Glib::ustring extendedPrefix = "xmlns:" + prefix; | |
4705 | 700 | /** | 102 | SimpleNode* root = dynamic_cast<SimpleNode*>(doc->root()); |
4706 | 701 | * Returns true if file successfully saved. | 103 | if (root->namespaces().find(g_quark_from_string(extendedPrefix.c_str())) == root->namespaces().end() |
4707 | 702 | * | 104 | && prefix != "xml") { |
4708 | 703 | * \param filename The actual file to do I/O to, which might be a temp file. | 105 | root->setNamespace(extendedPrefix, sp_xml_ns_prefix_uri(prefix.c_str())); |
4709 | 704 | * | 106 | } |
4710 | 705 | * \param for_filename The base URI [actually filename] to assume for purposes of rewriting | 107 | } |
4711 | 706 | * xlink:href attributes. | 108 | } |
4712 | 707 | */ | 109 | |
4713 | 708 | bool sp_repr_save_rebased_file(Document *doc, gchar const *const filename, gchar const *default_ns, | 110 | static void find_all_prefixes(Document* doc, Node* node) { |
4714 | 709 | gchar const *old_base, gchar const *for_filename) | 111 | add_namespace_to_doc(doc, node->name()); |
4715 | 710 | { | 112 | for (List<AttributeRecord const> iter = node->attributeList(); iter; ++iter) { |
4716 | 711 | if (!filename) { | 113 | add_namespace_to_doc(doc, g_quark_to_string(iter->key)); |
4717 | 712 | return false; | 114 | } |
4718 | 713 | } | 115 | for (Node *child = node->firstChild(); child != nullptr; child = child->next()) { |
4719 | 714 | 116 | if (child->type() == ELEMENT_NODE) { | |
4720 | 715 | bool compress; | 117 | find_all_prefixes(doc, child); |
4721 | 716 | { | 118 | } |
4722 | 717 | size_t const filename_len = strlen(filename); | 119 | } |
4723 | 718 | compress = ( filename_len > 5 | 120 | } |
4724 | 719 | && strcasecmp(".svgz", filename + filename_len - 5) == 0 ); | 121 | |
4725 | 720 | } | 122 | static void serialize(Writer& writer, Document* doc, const Glib::ustring& default_ns, const Glib::ustring& old_base, const Glib::ustring& new_base) { |
4726 | 721 | 123 | if (old_base != "") { | |
4727 | 722 | Inkscape::IO::dump_fopen_call( filename, "B" ); | 124 | Glib::ustring old_href_abs_base = calc_abs_doc_base(old_base.c_str()); |
4728 | 723 | FILE *file = Inkscape::IO::fopen_utf8name(filename, "w"); | 125 | Glib::ustring new_href_abs_base; |
4729 | 724 | if (file == NULL) { | 126 | if (Glib::path_is_absolute(new_base)) { |
4730 | 725 | return false; | 127 | new_href_abs_base = Glib::path_get_dirname(new_base); |
4637 | 726 | } | ||
4638 | 727 | |||
4639 | 728 | Glib::ustring old_href_abs_base; | ||
4640 | 729 | Glib::ustring new_href_abs_base; | ||
4641 | 730 | if (for_filename) { | ||
4642 | 731 | old_href_abs_base = calc_abs_doc_base(old_base); | ||
4643 | 732 | if (Glib::path_is_absolute(for_filename)) { | ||
4644 | 733 | new_href_abs_base = Glib::path_get_dirname(for_filename); | ||
4731 | 734 | } else { | 128 | } else { |
4732 | 735 | Glib::ustring const cwd = Glib::get_current_dir(); | 129 | Glib::ustring const cwd = Glib::get_current_dir(); |
4734 | 736 | Glib::ustring const for_abs_filename = Glib::build_filename(cwd, for_filename); | 130 | Glib::ustring const for_abs_filename = Glib::build_filename(cwd, new_base); |
4735 | 737 | new_href_abs_base = Glib::path_get_dirname(for_abs_filename); | 131 | new_href_abs_base = Glib::path_get_dirname(for_abs_filename); |
4736 | 738 | } | 132 | } |
4878 | 739 | 133 | rebase_attributes(doc->root(), old_href_abs_base, new_href_abs_base); | |
4879 | 740 | /* effic: Once we're confident that we never need (or never want) to resort | 134 | } |
4880 | 741 | * to using sodipodi:absref instead of the xlink:href value, | 135 | |
4881 | 742 | * then we should do `if streq() { free them and set both to NULL; }'. */ | 136 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); |
4741 | 743 | } | ||
4742 | 744 | sp_repr_save_stream(doc, file, default_ns, compress, old_href_abs_base.c_str(), new_href_abs_base.c_str()); | ||
4743 | 745 | |||
4744 | 746 | if (fclose (file) != 0) { | ||
4745 | 747 | return false; | ||
4746 | 748 | } | ||
4747 | 749 | |||
4748 | 750 | return true; | ||
4749 | 751 | } | ||
4750 | 752 | |||
4751 | 753 | /** | ||
4752 | 754 | * Returns true iff file successfully saved. | ||
4753 | 755 | */ | ||
4754 | 756 | bool sp_repr_save_file(Document *doc, gchar const *const filename, gchar const *default_ns) | ||
4755 | 757 | { | ||
4756 | 758 | return sp_repr_save_rebased_file(doc, filename, default_ns, NULL, NULL); | ||
4757 | 759 | } | ||
4758 | 760 | |||
4759 | 761 | |||
4760 | 762 | /* (No doubt this function already exists elsewhere.) */ | ||
4761 | 763 | static void repr_quote_write (Writer &out, const gchar * val) | ||
4762 | 764 | { | ||
4763 | 765 | if (val) { | ||
4764 | 766 | for (; *val != '\0'; val++) { | ||
4765 | 767 | switch (*val) { | ||
4766 | 768 | case '"': out.writeString( """ ); break; | ||
4767 | 769 | case '&': out.writeString( "&" ); break; | ||
4768 | 770 | case '<': out.writeString( "<" ); break; | ||
4769 | 771 | case '>': out.writeString( ">" ); break; | ||
4770 | 772 | default: out.writeChar( *val ); break; | ||
4771 | 773 | } | ||
4772 | 774 | } | ||
4773 | 775 | } | ||
4774 | 776 | } | ||
4775 | 777 | |||
4776 | 778 | static void repr_write_comment( Writer &out, const gchar * val, bool addWhitespace, gint indentLevel, int indent ) | ||
4777 | 779 | { | ||
4778 | 780 | if ( indentLevel > 16 ) { | ||
4779 | 781 | indentLevel = 16; | ||
4780 | 782 | } | ||
4781 | 783 | if (addWhitespace && indent) { | ||
4782 | 784 | for (gint i = 0; i < indentLevel; i++) { | ||
4783 | 785 | for (gint j = 0; j < indent; j++) { | ||
4784 | 786 | out.writeString(" "); | ||
4785 | 787 | } | ||
4786 | 788 | } | ||
4787 | 789 | } | ||
4788 | 790 | |||
4789 | 791 | out.writeString("<!--"); | ||
4790 | 792 | // WARNING out.printf() and out.writeString() are *NOT* non-ASCII friendly. | ||
4791 | 793 | if (val) { | ||
4792 | 794 | for (const gchar* cur = val; *cur; cur++ ) { | ||
4793 | 795 | out.writeChar(*cur); | ||
4794 | 796 | } | ||
4795 | 797 | } else { | ||
4796 | 798 | out.writeString(" "); | ||
4797 | 799 | } | ||
4798 | 800 | out.writeString("-->"); | ||
4799 | 801 | |||
4800 | 802 | if (addWhitespace) { | ||
4801 | 803 | out.writeString("\n"); | ||
4802 | 804 | } | ||
4803 | 805 | } | ||
4804 | 806 | |||
4805 | 807 | namespace { | ||
4806 | 808 | |||
4807 | 809 | typedef std::map<Glib::QueryQuark, gchar const *, Inkscape::compare_quark_ids> LocalNameMap; | ||
4808 | 810 | typedef std::map<Glib::QueryQuark, Inkscape::Util::ptr_shared<char>, Inkscape::compare_quark_ids> NSMap; | ||
4809 | 811 | |||
4810 | 812 | gchar const *qname_local_name(Glib::QueryQuark qname) { | ||
4811 | 813 | static LocalNameMap local_name_map; | ||
4812 | 814 | LocalNameMap::iterator iter = local_name_map.find(qname); | ||
4813 | 815 | if ( iter != local_name_map.end() ) { | ||
4814 | 816 | return (*iter).second; | ||
4815 | 817 | } else { | ||
4816 | 818 | gchar const *name_string=g_quark_to_string(qname); | ||
4817 | 819 | gchar const *prefix_end=strchr(name_string, ':'); | ||
4818 | 820 | if (prefix_end) { | ||
4819 | 821 | return prefix_end + 1; | ||
4820 | 822 | } else { | ||
4821 | 823 | return name_string; | ||
4822 | 824 | } | ||
4823 | 825 | } | ||
4824 | 826 | } | ||
4825 | 827 | |||
4826 | 828 | void add_ns_map_entry(NSMap &ns_map, Glib::QueryQuark prefix) { | ||
4827 | 829 | using Inkscape::Util::ptr_shared; | ||
4828 | 830 | using Inkscape::Util::share_unsafe; | ||
4829 | 831 | |||
4830 | 832 | static const Glib::QueryQuark xml_prefix("xml"); | ||
4831 | 833 | |||
4832 | 834 | NSMap::iterator iter=ns_map.find(prefix); | ||
4833 | 835 | if ( iter == ns_map.end() ) { | ||
4834 | 836 | if (prefix.id()) { | ||
4835 | 837 | gchar const *uri=sp_xml_ns_prefix_uri(g_quark_to_string(prefix)); | ||
4836 | 838 | if (uri) { | ||
4837 | 839 | ns_map.insert(NSMap::value_type(prefix, share_unsafe(uri))); | ||
4838 | 840 | } else if ( prefix != xml_prefix ) { | ||
4839 | 841 | g_warning("No namespace known for normalized prefix %s", g_quark_to_string(prefix)); | ||
4840 | 842 | } | ||
4841 | 843 | } else { | ||
4842 | 844 | ns_map.insert(NSMap::value_type(prefix, ptr_shared<char>())); | ||
4843 | 845 | } | ||
4844 | 846 | } | ||
4845 | 847 | } | ||
4846 | 848 | |||
4847 | 849 | void populate_ns_map(NSMap &ns_map, Node &repr) { | ||
4848 | 850 | if ( repr.type() == Inkscape::XML::ELEMENT_NODE ) { | ||
4849 | 851 | add_ns_map_entry(ns_map, qname_prefix(repr.code())); | ||
4850 | 852 | for ( List<AttributeRecord const> iter=repr.attributeList() ; | ||
4851 | 853 | iter ; ++iter ) | ||
4852 | 854 | { | ||
4853 | 855 | Glib::QueryQuark prefix=qname_prefix(iter->key); | ||
4854 | 856 | if (prefix.id()) { | ||
4855 | 857 | add_ns_map_entry(ns_map, prefix); | ||
4856 | 858 | } | ||
4857 | 859 | } | ||
4858 | 860 | for ( Node *child=repr.firstChild() ; | ||
4859 | 861 | child ; child = child->next() ) | ||
4860 | 862 | { | ||
4861 | 863 | populate_ns_map(ns_map, *child); | ||
4862 | 864 | } | ||
4863 | 865 | } | ||
4864 | 866 | } | ||
4865 | 867 | |||
4866 | 868 | } | ||
4867 | 869 | |||
4868 | 870 | static void sp_repr_write_stream_root_element(Node *repr, Writer &out, | ||
4869 | 871 | bool add_whitespace, gchar const *default_ns, | ||
4870 | 872 | int inlineattrs, int indent, | ||
4871 | 873 | gchar const *const old_href_base, | ||
4872 | 874 | gchar const *const new_href_base) | ||
4873 | 875 | { | ||
4874 | 876 | using Inkscape::Util::ptr_shared; | ||
4875 | 877 | |||
4876 | 878 | g_assert(repr != NULL); | ||
4877 | 879 | |||
4882 | 880 | // Clean unnecessary attributes and stype properties. (Controlled by preferences.) | 137 | // Clean unnecessary attributes and stype properties. (Controlled by preferences.) |
4883 | 881 | Inkscape::Preferences *prefs = Inkscape::Preferences::get(); | ||
4884 | 882 | bool clean = prefs->getBool("/options/svgoutput/check_on_writing"); | 138 | bool clean = prefs->getBool("/options/svgoutput/check_on_writing"); |
4886 | 883 | if (clean) sp_attribute_clean_tree( repr ); | 139 | if (clean) { |
4887 | 140 | sp_attribute_clean_tree(doc->root()); | ||
4888 | 141 | } | ||
4889 | 142 | |||
4890 | 143 | // find all used prefixes | ||
4891 | 144 | find_all_prefixes(doc, doc->root()); | ||
4892 | 145 | if (default_ns != "") { | ||
4893 | 146 | doc->root()->setNamespace("xmlns", default_ns); | ||
4894 | 147 | } | ||
4895 | 148 | Glib::ustring defaultPrefix = sp_xml_ns_uri_prefix(default_ns.c_str(), ""); | ||
4896 | 149 | |||
4897 | 150 | bool inlineAttributes = prefs->getBool("/options/svgoutput/inlineattrs"); | ||
4898 | 151 | int indent = prefs->getInt("/options/svgoutput/indent", 2); | ||
4899 | 884 | 152 | ||
4900 | 885 | // Sort attributes in a canonical order (helps with "diffing" SVG files). | 153 | // Sort attributes in a canonical order (helps with "diffing" SVG files). |
4901 | 886 | bool sort = prefs->getBool("/options/svgoutput/sort_attributes"); | 154 | bool sort = prefs->getBool("/options/svgoutput/sort_attributes"); |
4902 | 887 | if (sort) sp_attribute_sort_tree( repr ); | ||
4903 | 888 | |||
4904 | 889 | Glib::QueryQuark xml_prefix=g_quark_from_static_string("xml"); | ||
4905 | 890 | |||
4906 | 891 | NSMap ns_map; | ||
4907 | 892 | populate_ns_map(ns_map, *repr); | ||
4908 | 893 | |||
4909 | 894 | Glib::QueryQuark elide_prefix=GQuark(0); | ||
4910 | 895 | if ( default_ns && ns_map.find(GQuark(0)) == ns_map.end() ) { | ||
4911 | 896 | elide_prefix = g_quark_from_string(sp_xml_ns_uri_prefix(default_ns, NULL)); | ||
4912 | 897 | } | ||
4913 | 898 | |||
4914 | 899 | List<AttributeRecord const> attributes=repr->attributeList(); | ||
4915 | 900 | for ( NSMap::iterator iter=ns_map.begin() ; iter != ns_map.end() ; ++iter ) | ||
4916 | 901 | { | ||
4917 | 902 | Glib::QueryQuark prefix=(*iter).first; | ||
4918 | 903 | ptr_shared<char> ns_uri=(*iter).second; | ||
4919 | 904 | |||
4920 | 905 | if (prefix.id()) { | ||
4921 | 906 | if ( prefix != xml_prefix ) { | ||
4922 | 907 | if ( elide_prefix == prefix ) { | ||
4923 | 908 | attributes = cons(AttributeRecord(g_quark_from_static_string("xmlns"), ns_uri), attributes); | ||
4924 | 909 | } | ||
4925 | 910 | |||
4926 | 911 | Glib::ustring attr_name="xmlns:"; | ||
4927 | 912 | attr_name.append(g_quark_to_string(prefix)); | ||
4928 | 913 | GQuark key = g_quark_from_string(attr_name.c_str()); | ||
4929 | 914 | attributes = cons(AttributeRecord(key, ns_uri), attributes); | ||
4930 | 915 | } | ||
4931 | 916 | } else { | ||
4932 | 917 | // if there are non-namespaced elements, we can't globally | ||
4933 | 918 | // use a default namespace | ||
4934 | 919 | elide_prefix = GQuark(0); | ||
4935 | 920 | } | ||
4936 | 921 | } | ||
4937 | 922 | |||
4938 | 923 | return sp_repr_write_stream_element(repr, out, 0, add_whitespace, elide_prefix, attributes, | ||
4939 | 924 | inlineattrs, indent, old_href_base, new_href_base); | ||
4940 | 925 | } | ||
4941 | 926 | |||
4942 | 927 | void sp_repr_write_stream( Node *repr, Writer &out, gint indent_level, | ||
4943 | 928 | bool add_whitespace, Glib::QueryQuark elide_prefix, | ||
4944 | 929 | int inlineattrs, int indent, | ||
4945 | 930 | gchar const *const old_href_base, | ||
4946 | 931 | gchar const *const new_href_base) | ||
4947 | 932 | { | ||
4948 | 933 | switch (repr->type()) { | ||
4949 | 934 | case Inkscape::XML::TEXT_NODE: { | ||
4950 | 935 | if( dynamic_cast<const Inkscape::XML::TextNode *>(repr)->is_CData() ) { | ||
4951 | 936 | // Preserve CDATA sections, not converting '&' to &, etc. | ||
4952 | 937 | out.printf( "<![CDATA[%s]]>", repr->content() ); | ||
4953 | 938 | } else { | ||
4954 | 939 | repr_quote_write( out, repr->content() ); | ||
4955 | 940 | } | ||
4956 | 941 | break; | ||
4957 | 942 | } | ||
4958 | 943 | case Inkscape::XML::COMMENT_NODE: { | ||
4959 | 944 | repr_write_comment( out, repr->content(), add_whitespace, indent_level, indent ); | ||
4960 | 945 | break; | ||
4961 | 946 | } | ||
4962 | 947 | case Inkscape::XML::PI_NODE: { | ||
4963 | 948 | out.printf( "<?%s %s?>", repr->name(), repr->content() ); | ||
4964 | 949 | break; | ||
4965 | 950 | } | ||
4966 | 951 | case Inkscape::XML::ELEMENT_NODE: { | ||
4967 | 952 | sp_repr_write_stream_element( repr, out, indent_level, | ||
4968 | 953 | add_whitespace, elide_prefix, | ||
4969 | 954 | repr->attributeList(), | ||
4970 | 955 | inlineattrs, indent, | ||
4971 | 956 | old_href_base, new_href_base); | ||
4972 | 957 | break; | ||
4973 | 958 | } | ||
4974 | 959 | case Inkscape::XML::DOCUMENT_NODE: { | ||
4975 | 960 | g_assert_not_reached(); | ||
4976 | 961 | break; | ||
4977 | 962 | } | ||
4978 | 963 | default: { | ||
4979 | 964 | g_assert_not_reached(); | ||
4980 | 965 | } | ||
4981 | 966 | } | ||
4982 | 967 | } | ||
4983 | 968 | |||
4984 | 969 | |||
4985 | 970 | void sp_repr_write_stream_element( Node * repr, Writer & out, | ||
4986 | 971 | gint indent_level, bool add_whitespace, | ||
4987 | 972 | Glib::QueryQuark elide_prefix, | ||
4988 | 973 | List<AttributeRecord const> attributes, | ||
4989 | 974 | int inlineattrs, int indent, | ||
4990 | 975 | gchar const *old_href_base, | ||
4991 | 976 | gchar const *new_href_base ) | ||
4992 | 977 | { | ||
4993 | 978 | Node *child = 0; | ||
4994 | 979 | bool loose = false; | ||
4995 | 980 | |||
4996 | 981 | g_return_if_fail (repr != NULL); | ||
4997 | 982 | |||
4998 | 983 | if ( indent_level > 16 ) { | ||
4999 | 984 | indent_level = 16; | ||
5000 | 985 | } |
The diff has been truncated for viewing.