45 |
46 | int main (int argc, char *argv[]) {
47 | if (argc != 2) {
48 | std::cerr << argv[0] << ": you must give one and only one XML file name\n";
49 | return 1;
50 | }
51 |
52 | try {
53 |
54 | xml::tree_parser parser(argv[1]);
55 |
56 | xml::node &root = parser.get_document().get_root_node();
57 | std::cout << "root node is '" << root.get_name() << "'\n";
58 |
59 | for (auto const& child : root) {
60 | if (child.is_text()) continue;
61 | std::cout << "child node '" << child.get_name() << "'\n";
62 | }
63 |
64 | } catch (std::exception &e) {
65 | std::cerr << argv[0] << ": " << e.what() << "\n";
66 | return 1;
67 | }
68 |
69 | return 0;
70 | }
71 |
--------------------------------------------------------------------------------
/docs/manual/tips.doxygen:
--------------------------------------------------------------------------------
1 | /**
2 |
3 | @page tips Tips, Tricks and Warnings
4 |
5 | @section tips_lifetime Iterators and Object Lifetimes
6 |
7 | Because of the way xmlwrapp is implemented, you must be careful
8 | with pointers and references that you take from xmlwrapp
9 | iterators. The pointers and references will only be valid as long
10 | as the iterator is valid.
11 |
12 | From the xmlwrapp-users mailing list:
13 |
14 |
15 | From: Peter Jones
16 | Subject: Re: [xmlwrapp-users] Potential bug in xml::node::iterator dereferencing?
17 | Date: Fri, 21 Mar 2003 07:47:05 -0800
18 |
19 | It does seem strange on the surface that xml::node::iterator has an
20 | xml::node as a member variable. However, xml::node::iterator is really a
21 | shell class that uses the pimpl idiom to contain another class, and that
22 | class is what holds the xml::node.
23 |
24 | I would much prefer to emulate the standard containers in this respect,
25 | and I would love to hear any suggestions on how this might be done.
26 |
27 | Let me explain the problem as I see it, and maybe you and others can
28 | point me in the right direction.
29 |
30 | xml::node is somewhat like a std::list. From the outside it looks as if
31 | xml::node is a container for other xml::node objects, however, this is
32 | not true. The objects that xml::node contains are really pointers to
33 | libxml2 data structures.
34 |
35 | I decided early on that, unlike other libxml2 C++ libraries, xmlwrapp
36 | should not try to duplicate the XML tree using C++ containers, but
37 | instead emulate that container interface and adapt it to the libxml2
38 | function calls.
39 |
40 | It is quite easy for the standard containers to return a valid
41 | pointer/reference to their contained objects since those objects are
42 | already in memory, and will be around until the container goes away or
43 | an operation is preformed on that container that would invalidate those
44 | pointers/references.
45 |
46 | But, xml::node is keeping pointers to xmlNode structs, not xml::node
47 | classes. This means that the xml::node::iterators could return pointers
48 | to xmlNode structs that out live the iterator, but that is not what
49 | users would expect.
50 |
51 | Currently, the xml::node::iterator will use its xml::node member
52 | variable as a way to pretend that xml::node objects are containers for
53 | other xml::node objects. This is why the lifetime of the xml::node is
54 | limited to the lifetime of the xml::node::iterator.
55 |
56 |
57 | @section tips_debugging Debugging
58 |
59 | Any xml::node object can be easily examined by dumping it to a string using its
60 | xml::node::node_to_string() method or by outputting it into any standard
61 | stream:
62 | @code
63 | xml::node n = ...;
64 | std::cout << "Contents of the node: " << n;
65 | @endcode
66 |
67 | Notice that the node_to_string() function can even be called from a debugger to
68 | see the contents of the node during a debugging session.
69 |
70 | */
71 |
--------------------------------------------------------------------------------
/src/libxml/ait_impl.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #ifndef _xmlwrapp_ait_impl_h_
34 | #define _xmlwrapp_ait_impl_h_
35 |
36 | // xmlwrapp includes
37 | #include "xmlwrapp/attributes.h"
38 |
39 | // libxml2 includes
40 | #include
41 |
42 | namespace xml
43 | {
44 |
45 | namespace impl
46 | {
47 |
48 | // The class that does all the work behind xml::attributes::iterator and
49 | // xml::attributes::const_iterator.
50 | class ait_impl
51 | {
52 | public:
53 | ait_impl(xmlNodePtr node, xmlAttrPtr prop);
54 | ait_impl(const char *name, const char *value, bool);
55 | ait_impl(const ait_impl& other);
56 | ait_impl& operator=(const ait_impl& other);
57 |
58 | attributes::attr* get();
59 | xmlAttrPtr get_raw_attr();
60 |
61 | ait_impl& operator++();
62 | ait_impl operator++(int);
63 |
64 | friend bool operator==(const ait_impl& lhs, const ait_impl& rhs);
65 | friend bool operator!=(const ait_impl& lhs, const ait_impl& rhs);
66 |
67 | private:
68 | xmlNodePtr xmlnode_;
69 | xmlAttrPtr xmlattr_;
70 | attributes::attr attr_;
71 | bool fake_;
72 | };
73 |
74 | // a couple helper functions
75 | xmlAttrPtr find_prop(xmlNodePtr xmlnode, const char *name);
76 | xmlAttributePtr find_default_prop(xmlNodePtr xmlnode, const char *name);
77 |
78 | } // namespace impl
79 |
80 | } // namespace xml
81 |
82 | #endif // _xmlwrapp_ait_impl_h_
83 |
--------------------------------------------------------------------------------
/src/libxml/dtd_impl.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #ifndef _xmlwrapp_dtd_impl_h_
34 | #define _xmlwrapp_dtd_impl_h_
35 |
36 | // standard includes
37 | #include
38 |
39 | // libxml2 includes
40 | #include
41 | #include
42 | #include
43 |
44 | namespace xml
45 | {
46 |
47 | namespace impl
48 | {
49 |
50 | class dtd_impl
51 | {
52 | public:
53 | // load the given DTD. you should check to see if error_ is empty or
54 | // not, which will tell you if the DTD was loaded okay.
55 | explicit dtd_impl(const char *filename);
56 |
57 | // Don't load a DTD
58 | dtd_impl();
59 |
60 | ~dtd_impl();
61 |
62 | // check the document against the loaded DTD, or in the case where no
63 | // DTD is loaded try to use the one inside the document.
64 | bool validate(xmlDocPtr xmldoc);
65 |
66 | // return the dtd pointer and never again free it.
67 | xmlDtdPtr release();
68 |
69 | // count of DTD parsing/validating warnings
70 | int warnings_;
71 |
72 | // last DTD parsing/validating error message
73 | std::string error_;
74 |
75 | private:
76 | xmlValidCtxt vctxt_;
77 | xmlDtdPtr dtd_;
78 |
79 | dtd_impl(const dtd_impl&) = delete;
80 | dtd_impl& operator=(const dtd_impl&) = delete;
81 | void init_ctxt();
82 | };
83 |
84 | } // namespace impl
85 |
86 | } // namespace xml
87 |
88 | #endif // _xmlwrapp_dtd_impl_h_
89 |
--------------------------------------------------------------------------------
/examples/02-event_parsing/example.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /*
34 | * The following code demonstrates the usage of the xml::event_parser class.
35 | * This class must be derived from and the new subclass must implement the
36 | * pure virtual member functions from xml::event_parser. These member
37 | * functions are called when parsing events occur.
38 | */
39 |
40 | // xmlwrapp include
41 | #include
42 |
43 | // standard includes
44 | #include
45 | #include
46 |
47 | /*
48 | * Here we create a class that will receive the parsing events.
49 | */
50 | class myparser : public xml::event_parser {
51 | public:
52 | myparser ()
53 | { std::cout << "myparser constructor\n"; }
54 |
55 | ~myparser () override
56 | { std::cout << "myparser destructor\n"; }
57 | private:
58 | bool start_element (const std::string &name, const attrs_type&) override
59 | { std::cout << "begin tag '" << name << "'\n"; return true; }
60 |
61 | bool end_element (const std::string &name) override
62 | { std::cout << "end tag '" << name << "'\n"; return true; }
63 |
64 | bool text (const std::string&) override
65 | { return true; }
66 | };
67 |
68 | /*
69 | * And here is good ol' main.
70 | */
71 | int main (int argc, char *argv[]) {
72 | if (argc != 2) {
73 | std::cerr << argv[0] << ": you must give one and only one XML file name\n";
74 | return 1;
75 | }
76 |
77 | myparser parser;
78 |
79 | if (!parser.parse_file(argv[1])) {
80 | std::cerr << argv[0] << ": error parsing XML file\n";
81 | return 1;
82 | }
83 |
84 | return 0;
85 | }
86 |
--------------------------------------------------------------------------------
/docs/manual/prepare.doxygen:
--------------------------------------------------------------------------------
1 | /**
2 |
3 | @page prepare Preparing To Use xmlwrapp
4 |
5 | Before you use xmlwrapp there are a few things you will need to
6 | know. This chapter will give you all the background you need to
7 | understand the rest of this book.
8 |
9 | @section prepare_headers Header Files
10 |
11 | When you install xmlwrapp, a set of header files will be installed in the
12 | include directory of your choosing. If you don't pick a location for these file
13 | to go, they will be installed in /usr/local/include by default. All of the
14 | xmlwrapp header files will be placed into a @c xmlwrapp subdirectory.
15 |
16 | If you are using the @c pkg-config script, you don't have to worry about where
17 | the xmlwrapp header files were installed. @c "pkg-config xmlwrapp" will add the
18 | appropriate directory to the compiler's search path.
19 |
20 | When using the xmlwrapp header files, you can either include each
21 | file you need or use the master include file to include all
22 | xmlwrapp header files. The choice is yours and mainly depends on
23 | your style and the project you are working on.
24 |
25 | @subsection using_win32_dll Using DLLs under Microsoft Windows systems
26 |
27 | If xmlwrapp was built as a DLL, you must predefine @c XMLWRAPP_USE_DLL before
28 | including any of the library headers, e.g. typically in the project build
29 | options. Similarly, @c XSLTWRAPP_USE_DLL must be defined when using xsltwrapp
30 | as a DLL.
31 |
32 | Note that this it is not necessary to do this when using GNU toolset, as GNU
33 | linker can automatically import symbols from the DLLs as needed. However this
34 | is required when using other toolsets, such as Microsoft C++ one, and may be
35 | slightly more efficient than relying on auto-linking even with GNU linker.
36 |
37 | @subsection prepare_headers_example1 Include xmlwrapp Header Files
38 |
39 | @code
40 | #include
41 | #include
42 | @endcode
43 |
44 | @subsection prepare_headers_example2 Include All xmlwrapp Header Files
45 |
46 | @code
47 | #include
48 | @endcode
49 |
50 | @section prepare_namespace The xmlwrapp Namespace
51 |
52 | To prevent pollution of the global namespace, xmlwrapp places all classes and
53 | functions in the xml namespace. For clarity, this document will always use the
54 | xml namespace prefix when showing examples.
55 |
56 | It is your choice if you want to use the xml namespace in your code, or use
57 | one of the many forms of the "using namespace" statement.
58 |
59 |
60 | @section prepare_init Initializing the XML Parser
61 |
62 | Starting with xmlwrapp version 0.6.0, you don't have to explicitly initialize
63 | the library, it is automatically initialized at startup time, in a thread-safe
64 | way.
65 |
66 | You may want to configure the libxml2 parser to behave differently from
67 | its default behavior. The xml::init class exists for this purpose and provides
68 | several static methods for configuring various aspects of libxml2. If you
69 | decide to tweak the defaults, it is generally good idea to do it before
70 | first use of xmlwrapp, as setting the defaults is @em not thread-safe and
71 | has global effect.
72 |
73 | @code
74 | int main() {
75 | xml::init::remove_whitespace(true);
76 | ...
77 | return 0;
78 | }
79 | @endcode
80 |
81 |
82 | */
83 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 0. Introduction
2 |
3 | xmlwrapp is a modern style C++ library for working with XML data, built atop
4 | the venerable libxml2 C library.
5 |
6 | Additional resources, bug reports, latest sources etc. can be found on the
7 | [project page](https://vslavik.github.io/xmlwrapp/).
8 |
9 | Packages tarballs can be downloaded from the same location or from the
10 | [release page](https://github.com/vslavik/xmlwrapp/releases).
11 |
12 | If you need help or want to discuss xmlwrapp, feel free to join the discussion
13 | group hosted at [Google Groups](https://groups.google.com/group/xmlwrapp)
14 | or email directly to xmlwrapp@googlegroups.com.
15 |
16 |
17 | ## 1. Requirements
18 |
19 | In order to build xmlwrapp, you need libxml2 version 2.4.28 or newer. When
20 | building with XSLT support, libxslt 1.1.6 or newer is required. Both libraries
21 | are available from [libxml2 home page](https://xmlsoft.org).
22 |
23 |
24 | ## 2. Building on Unix
25 |
26 | On Unix, either autotools-based build system or CMake can be used.
27 |
28 | ### Building using Autotools
29 |
30 | Building xmlwrapp is usually as simple as running the following three commands:
31 |
32 | $ ./configure
33 | $ make
34 | $ make install
35 |
36 | See the output of `./configure --help` for additional settings and options.
37 |
38 | Cross-compiling, notably for Windows, is also supported using the usual
39 | `--host` option, e.g. `--host=x86_64-w64-mingw32`.
40 |
41 | Note that if you obtained the library sources from Git, and not from the
42 | release archives, `configure` script won't exist and you will need to run
43 |
44 | $ autoreconf -i
45 |
46 | to create it and other build files.
47 |
48 | ### Building using CMake
49 |
50 | Building xmlwrapp using CMake is also straightforward. Just do the following:
51 |
52 | $ cmake -S . -B build-dir
53 | $ cmake --build build-dir
54 | $ cmake --install build-dir
55 |
56 | The usual CMake `BUILD_SHARED_LIBS` option is supported for selecting whether
57 | shared (default) or static libraries should be built, use `cmake -L` to see the
58 | other available options.
59 |
60 |
61 | ## 3. Building on Windows
62 |
63 | The library can be built either with Microsoft Visual C++ compiler, using the
64 | project files from located in platform/Win32 directory or in the same way as
65 | under Unix, i.e. with configure and make, if you have Cygwin installed (note
66 | that MinGW can be used as a cross-compiler under Cygwin, producing native
67 | libraries without any Cygwin dependencies). In either case, you will need to
68 | either build libxml and libxslt for Windows yourself or download the already
69 | built versions from http://www.zlatkovic.com/libxml.en.html or elsewhere.
70 |
71 |
72 | ## Using xmlwrapp
73 |
74 | On Unix, you should use pkg-config to get compiler flags for xmlwrapp or
75 | xsltwrapp libraries:
76 |
77 | $ c++ -c `pkg-config --cflags xmlwrapp` ...
78 | $ c++ -o ... `pkg-config --libs xmlwrapp`
79 |
80 | On Windows, you need to link against xmlwrapp libraries and add the include/
81 | directory to compiler's headers search path.
82 |
83 | Under either platform you may consume the library using CMake in the usual way,
84 | i.e. using either `find_package(xmlwrapp)` or `add_subdirectory(xmlwrapp)` and
85 | then linking to the `xmlwrapp::xmlwrapp` target.
86 |
--------------------------------------------------------------------------------
/platform/Win32/xmlwrapp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 17
4 | VisualStudioVersion = 17.0.31919.166
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xmlwrapp", "xmlwrapp.vcxproj", "{27D0067A-8534-56E2-A734-799F4A6CE894}"
7 | EndProject
8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xsltwrapp", "xsltwrapp.vcxproj", "{30E912D3-2B6D-5358-81F3-7CF96781F735}"
9 | ProjectSection(ProjectDependencies) = postProject
10 | {27D0067A-8534-56E2-A734-799F4A6CE894} = {27D0067A-8534-56E2-A734-799F4A6CE894}
11 | EndProjectSection
12 | EndProject
13 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{C824FD57-A9D0-5288-9677-F80D927BC942}"
14 | ProjectSection(ProjectDependencies) = postProject
15 | {27D0067A-8534-56E2-A734-799F4A6CE894} = {27D0067A-8534-56E2-A734-799F4A6CE894}
16 | {30E912D3-2B6D-5358-81F3-7CF96781F735} = {30E912D3-2B6D-5358-81F3-7CF96781F735}
17 | EndProjectSection
18 | EndProject
19 | Global
20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
21 | Debug|Win32 = Debug|Win32
22 | Debug|x64 = Debug|x64
23 | Release|Win32 = Release|Win32
24 | Release|x64 = Release|x64
25 | EndGlobalSection
26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
27 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Debug|Win32.ActiveCfg = Debug|Win32
28 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Debug|Win32.Build.0 = Debug|Win32
29 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Debug|x64.ActiveCfg = Debug|x64
30 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Debug|x64.Build.0 = Debug|x64
31 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Release|Win32.ActiveCfg = Release|Win32
32 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Release|Win32.Build.0 = Release|Win32
33 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Release|x64.ActiveCfg = Release|x64
34 | {27D0067A-8534-56E2-A734-799F4A6CE894}.Release|x64.Build.0 = Release|x64
35 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Debug|Win32.ActiveCfg = Debug|Win32
36 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Debug|Win32.Build.0 = Debug|Win32
37 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Debug|x64.ActiveCfg = Debug|x64
38 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Debug|x64.Build.0 = Debug|x64
39 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Release|Win32.ActiveCfg = Release|Win32
40 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Release|Win32.Build.0 = Release|Win32
41 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Release|x64.ActiveCfg = Release|x64
42 | {30E912D3-2B6D-5358-81F3-7CF96781F735}.Release|x64.Build.0 = Release|x64
43 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Debug|Win32.ActiveCfg = Debug|Win32
44 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Debug|Win32.Build.0 = Debug|Win32
45 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Debug|x64.ActiveCfg = Debug|x64
46 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Debug|x64.Build.0 = Debug|x64
47 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Release|Win32.ActiveCfg = Release|Win32
48 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Release|Win32.Build.0 = Release|Win32
49 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Release|x64.ActiveCfg = Release|x64
50 | {C824FD57-A9D0-5288-9677-F80D927BC942}.Release|x64.Build.0 = Release|x64
51 | EndGlobalSection
52 | GlobalSection(SolutionProperties) = preSolution
53 | HideSolutionNode = FALSE
54 | EndGlobalSection
55 | EndGlobal
56 |
--------------------------------------------------------------------------------
/src/libxslt/init.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the implementation of the xslt::init class.
37 | */
38 |
39 | #include "xsltwrapp/init.h"
40 |
41 | #include
42 | #include
43 | #include
44 | #include
45 | #include
46 |
47 | extern "C"
48 | {
49 |
50 | static void xslt_error(void *, const char*, ...)
51 | {
52 | // don't do anything; we install context-specific error handler to
53 | // catch errors while applying a stylesheet
54 | }
55 |
56 | } // extern "C"
57 |
58 |
59 | int xslt::init::ms_counter = 0;
60 |
61 |
62 | xslt::init::init()
63 | {
64 | if ( ms_counter++ == 0 )
65 | init_library();
66 | }
67 |
68 |
69 | xslt::init::~init()
70 | {
71 | if ( --ms_counter == 0 )
72 | shutdown_library();
73 | }
74 |
75 |
76 | void xslt::init::init_library()
77 | {
78 | // xsltInit() was added after 1.1.15 release, so don't use it with the
79 | // ancient libxslt version (which is the only one we have under Solaris).
80 | #if LIBXSLT_VERSION > 10115
81 | xsltInit();
82 | #endif
83 |
84 | // set some defautls
85 | process_xincludes(true);
86 |
87 | // keep libxslt silent; we install context-specific error handler to
88 | // catch errors while applying a stylesheet
89 | xsltSetGenericErrorFunc(nullptr, xslt_error);
90 | xsltSetGenericDebugFunc(nullptr, xslt_error);
91 |
92 | // load EXSLT
93 | exsltRegisterAll();
94 | }
95 |
96 |
97 | void xslt::init::shutdown_library()
98 | {
99 | xsltCleanupGlobals();
100 | }
101 |
102 |
103 | void xslt::init::process_xincludes(bool flag)
104 | {
105 | xsltSetXIncludeDefault(flag ? 1 : 0);
106 | }
107 |
--------------------------------------------------------------------------------
/examples/03-xml_generation/example.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /*
34 | * The following code demonstrates how to use the xml::node class to build
35 | * an XML tree and then convert it to XML text.
36 | *
37 | * Here is what we want to create:
38 | *
39 | *
40 | *
41 | * pjones@pmade.org
42 | *
43 | * 555-1212
44 | *
45 | *
46 | */
47 |
48 | // xmlwrapp include
49 | #include
50 |
51 | // standard includes
52 | #include
53 | #include
54 |
55 | int main () {
56 | // create a new XML document and set the root node
57 | xml::document xmldoc(xml::node("abook"));
58 | xml::node &root = xmldoc.get_root_node();
59 |
60 | // add a child to the root node,
61 | xml::node::iterator it = root.insert(root.begin(), xml::node("person"));
62 |
63 | /*
64 | * set the attributes for this new element using the
65 | * xml::attributes object returned from xml::node::get_attributes
66 | */
67 | it->get_attributes().insert("id", "01");
68 | it->get_attributes().insert("name", "Peter Jones");
69 |
70 | // add a node and set the content for that new node
71 | it->push_back(xml::node("email", "pjones@pmade.org"));
72 |
73 | // add an XML comment
74 | it->push_back(xml::node(xml::node::comment(" Fake Phone Number ")));
75 |
76 | // build a node one member function at a time
77 | it = it->insert(xml::node("phone"));
78 | it->get_attributes().insert("type", "home");
79 | it->set_content("555-1212");
80 |
81 | // set some document settings
82 | xmldoc.set_is_standalone(true);
83 | xmldoc.set_encoding("ISO-8859-1");
84 |
85 | // convert the document to XML
86 | std::cout << xmldoc;
87 | return 0;
88 | }
89 |
--------------------------------------------------------------------------------
/tests/schema/test_schema.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Vaclav Slavik (vslavik@gmail.com)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #include "../test.h"
34 |
35 | TEST_CASE_METHOD( SrcdirConfig, "schema/load_non_schema_file", "[schema]" )
36 | {
37 | xml::document sch_doc =
38 | xml::tree_parser(test_file_path("schema/data/valid.xml").c_str()).get_document();
39 |
40 | CHECK_THROWS_AS
41 | (
42 | xml::schema(sch_doc),
43 | xml::exception
44 | );
45 |
46 | xml::error_messages log;
47 | // throw even when collecting errors into log
48 | CHECK_THROWS_AS
49 | (
50 | xml::schema(sch_doc, log),
51 | xml::exception
52 | );
53 | CHECK( log.has_errors() );
54 | }
55 |
56 |
57 | TEST_CASE_METHOD( SrcdirConfig, "schema/validate_ok", "[schema]" )
58 | {
59 | xml::document sch_doc =
60 | xml::tree_parser(test_file_path("schema/data/schema.xsd").c_str()).get_document();
61 | xml::schema sch(sch_doc);
62 |
63 | xml::document doc =
64 | xml::tree_parser(test_file_path("schema/data/valid.xml").c_str()).get_document();
65 |
66 | CHECK( sch.validate(doc) );
67 |
68 | // And the same with logging:
69 | xml::error_messages log;
70 | CHECK( sch.validate(doc) );
71 | CHECK( !log.has_errors() );
72 | CHECK( !log.has_warnings() );
73 | }
74 |
75 |
76 | TEST_CASE_METHOD( SrcdirConfig, "schema/validate_fail", "[schema]" )
77 | {
78 | xml::document sch_doc =
79 | xml::tree_parser(test_file_path("schema/data/schema.xsd").c_str()).get_document();
80 | xml::schema sch(sch_doc);
81 |
82 | xml::document doc =
83 | xml::tree_parser(test_file_path("schema/data/invalid.xml").c_str()).get_document();
84 |
85 | CHECK_THROWS_AS
86 | (
87 | sch.validate(doc),
88 | xml::exception
89 | );
90 |
91 | // And the same with logging:
92 | xml::error_messages log;
93 | CHECK( !sch.validate(doc, log) );
94 | CHECK( log.has_errors() );
95 | }
96 |
--------------------------------------------------------------------------------
/docs/manual/attributes.doxygen:
--------------------------------------------------------------------------------
1 | /**
2 |
3 | @page attr Node Attributes
4 |
5 | It is possible for every element type node in an XML node tree to have
6 | attributes. Attributes are nothing more than a collection of name value pairs.
7 | In xmlwrapp, attributes are accessed using the xml::attributes class. This
8 | chapter will show you how to work with objects of this class.
9 |
10 |
11 | @section attr_it Accessing Individual Attributes
12 |
13 | The xml::attributes class is a container of node attributes. It would be pretty
14 | useless if there was no way to access the individual attributes inside of it.
15 | Iterators come to the rescue again. Just like the other xmlwrapp classes,
16 | iterators are used to access the attributes stored inside the xml::attributes
17 | class.
18 |
19 | @subsection attr_it_it Attribute Iterators
20 |
21 | The xml::attributes::iterator and xml::attributes::const_iterator classes point
22 | to a xml::attributes::attr object. This object is what gives you access to the
23 | name and value of a given attribute. In future versions of xmlwrapp, you will
24 | be able to use this class to access the attribute's namespace.
25 |
26 | @code
27 | xml::attributes attrs;
28 |
29 | ...
30 |
31 | xml::attributes::iterator i(attrs.begin());
32 |
33 | if (i != attrs.end())
34 | {
35 | std::cout << " name: " << i->get_name() << "\n";
36 | std::cout << "value: " << i->get_value() << "\n";
37 | }
38 | @endcode
39 |
40 | @subsection attr_it_begin Begin and End
41 |
42 | If you just wanted to iterate through the attributes, you can use the
43 | xml::attributes::begin() and the xml::attributes::end() member functions. They
44 | both return either a xml::attributes::iterator or
45 | xml::attributes::const_iterator object depending on whether the xml::attributes
46 | object is const or not.
47 |
48 | @subsection attr_it_find Finding Attributes
49 |
50 | When you want to locate an attribute with a given name, you can use the
51 | xml::attributes::find() member function. It will return an iterator that points
52 | to the found attribute or an iterator that is equal to the iterators that the
53 | xml::attributes::end() function returns.
54 |
55 | There is a small difference between the xml::attributes::find() function and
56 | just using the xml::attributes::begin() function to iterate over the
57 | attributes. If the attributes belong to a xml::document object that has been
58 | validated, the xml::attributes::find() function may return an attribute that
59 | was not given in the XML document but contains a default value in the DTD.
60 |
61 | If you are asking the xml::attributes::find() function to find an attribute
62 | that has a default value, but was present in the XML document, the document
63 | version of the attribute is returned.
64 |
65 |
66 | @section attr_add Adding and Replacing Attributes
67 |
68 | Since only one attribute with a given name can exist in a node at any one time,
69 | the same function can be used to both add and replace attributes. When
70 | attempting to add an attribute who has the same name as another attribute in
71 | the container, the old attribute will be removed before the new one is
72 | inserted.
73 |
74 | To add or replace an attribute, you can use the xml::attributes::insert()
75 | member function. It takes the name and value for the attribute to insert.
76 |
77 |
78 | @section attr_remove Removing Attributes
79 |
80 | There are two ways to remove an attribute from a xml::attributes object. Both
81 | involve a call to the xml::attributes::erase() member function. You can remove
82 | an attribute by name, or using an iterator that points to the attribute you
83 | wish to remove.
84 |
85 | */
86 |
--------------------------------------------------------------------------------
/src/libxslt/result.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008 Vadim Zeitlin (vz-xmlwrapp@zeitlins.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the declaration of the xslt::result class.
37 | */
38 |
39 | #ifndef _xsltwrapp_result_h_
40 | #define _xsltwrapp_result_h_
41 |
42 | // standard includes
43 | #include
44 |
45 | // forward declarations
46 | typedef struct _xmlDoc *xmlDocPtr;
47 |
48 | namespace xslt
49 | {
50 |
51 | namespace impl
52 | {
53 |
54 | /**
55 | @internal
56 |
57 | The xslt::result class is used as a callback by xml::document to allow
58 | special treatment of XML documents which were created by XSLT.
59 |
60 | This class is only meant to be used internally by the library and is
61 | necessary to avoid the dependency of xml::document, which is part of
62 | libxmlwrapp, on libxslt which should be only a dependency of libxsltwrapp
63 | as this precludes calling the XSLT functions which must be used with such
64 | "result" documents directly from xml::document code.
65 | */
66 | class result
67 | {
68 | public:
69 | /**
70 | Save the contents of the given XML document in the provided string.
71 |
72 | @param s The string to place the XML text data.
73 | */
74 | virtual void save_to_string(std::string &s) const = 0;
75 |
76 | /**
77 | Save the contents of the given XML document in the provided filename.
78 |
79 | @param filename
80 | The name of the file to place the XML text data into.
81 | @param compression_level
82 | 0 is no compression, 1-9 allowed, where 1 is for better speed,
83 | and 9 is for smaller size
84 | @return True if the data was saved successfully, false otherwise.
85 | */
86 | virtual bool save_to_file(const char *filename,
87 | int compression_level) const = 0;
88 |
89 | /// Trivial but virtual base class destructor.
90 | virtual ~result() = default;
91 | };
92 |
93 | } // end impl namespace
94 |
95 | } // end xslt namespace
96 |
97 | #endif // _xsltwrapp_result_h_
98 |
--------------------------------------------------------------------------------
/include/xsltwrapp/init.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the definition of the xslt::init class.
37 | */
38 |
39 | #ifndef _xsltwrapp_init_h_
40 | #define _xsltwrapp_init_h_
41 |
42 | // xmlwrapp includes
43 | #include "xmlwrapp/init.h"
44 | #include "xmlwrapp/export.h"
45 |
46 | /// XSLT library namespace
47 | namespace xslt
48 | {
49 |
50 | /**
51 | The xslt::init class is used to configure the XSLT engine.
52 |
53 | If you want to use any of the xslt::init member functions, do so before
54 | you start any threads or use any other part of xsltwrapp. The member
55 | functions may alter global and/or static variables. In other words, this
56 | class is not thread safe.
57 |
58 | @note In xmlwrapp versions prior to 0.6.0, this class was used to initialize
59 | the library and exactly one instance had to be created before first
60 | use. This is no longer true: user code doesn't have to create any
61 | instances, but it @em can create as many instances as it wants.
62 | */
63 | class init : public xml::init
64 | {
65 | public:
66 | XSLTWRAPP_API init();
67 | XSLTWRAPP_API ~init();
68 |
69 | /**
70 | This function controls whether or not the XSLT engine will process
71 | XInclusions by default while parsing the stylesheet. The default is
72 | true.
73 |
74 | @param flag True to enable XInclusing processing; False otherwise.
75 | */
76 | XSLTWRAPP_API static void process_xincludes(bool flag);
77 |
78 | private:
79 | init(const init&) = delete;
80 | init& operator=(const init&) = delete;
81 |
82 | void init_library();
83 | void shutdown_library();
84 |
85 | static int ms_counter;
86 | }; // end xslt::init class
87 |
88 |
89 | // use a "nifty counter" to ensure that any source file that uses xsltwrapp
90 | // will initialize the library prior to its first use
91 | namespace
92 | {
93 | xslt::init g_xsltwrapp_initializer;
94 | }
95 |
96 | } // end xslt namespace
97 |
98 | #endif // _xsltwrapp_init_h_
99 |
--------------------------------------------------------------------------------
/src/libxml/dtd_impl.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | // xmlwrapp includes
34 | #include "dtd_impl.h"
35 | #include "utility.h"
36 |
37 | // standard includes
38 | #include
39 | #include
40 |
41 | // libxml2 includes
42 | #include
43 | #include
44 | #include
45 |
46 | using namespace xml;
47 | using namespace xml::impl;
48 |
49 | namespace
50 | {
51 |
52 | extern "C" void dtd_error(void *ctxt, const char *message, ...)
53 | {
54 | auto *dtd = static_cast(ctxt);
55 |
56 | va_list ap;
57 | va_start(ap, message);
58 | printf2string(dtd->error_, message, ap);
59 | va_end(ap);
60 | }
61 |
62 | extern "C" void dtd_warning(void *ctxt, const char*, ...)
63 | {
64 | auto *dtd = static_cast(ctxt);
65 | ++dtd->warnings_;
66 | }
67 |
68 | } // anonymous namespace
69 |
70 |
71 | dtd_impl::dtd_impl(const char *filename)
72 | : warnings_(0), dtd_(nullptr)
73 | {
74 | if ( (dtd_ = xmlParseDTD(nullptr, reinterpret_cast(filename))) == nullptr)
75 | {
76 | error_ = "unable to parse DTD ";
77 | error_ += filename;
78 | }
79 | }
80 |
81 |
82 | dtd_impl::dtd_impl() : warnings_(0), dtd_(nullptr)
83 | {
84 | }
85 |
86 |
87 | dtd_impl::~dtd_impl()
88 | {
89 | if (dtd_)
90 | xmlFreeDtd(dtd_);
91 | }
92 |
93 |
94 | void dtd_impl::init_ctxt()
95 | {
96 | std::memset(&vctxt_, 0, sizeof(vctxt_));
97 |
98 | vctxt_.userData = this;
99 | vctxt_.error = dtd_error;
100 | vctxt_.warning = dtd_warning;
101 | }
102 |
103 |
104 | bool dtd_impl::validate(xmlDocPtr xmldoc)
105 | {
106 | init_ctxt();
107 |
108 | if (dtd_)
109 | return xmlValidateDtd(&vctxt_, xmldoc, dtd_) != 0;
110 | else
111 | return xmlValidateDocument(&vctxt_, xmldoc) != 0;
112 | }
113 |
114 |
115 | xmlDtdPtr dtd_impl::release()
116 | {
117 | xmlDtdPtr xmldtd = dtd_;
118 | dtd_ = nullptr;
119 | return xmldtd;
120 | }
121 |
--------------------------------------------------------------------------------
/src/libxml/init.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 |
34 | // xmlwrapp includes
35 | #include "xmlwrapp/init.h"
36 |
37 | // libxml includes
38 | #include
39 | #include
40 | #include
41 |
42 | namespace
43 | {
44 |
45 | bool change_flag_and_return_old_value(int* flag, bool new_value)
46 | {
47 | const bool old_value = *flag != 0;
48 | *flag = new_value ? 1 : 0;
49 | return old_value;
50 | }
51 |
52 | } // anonymous namespace
53 |
54 | namespace xml
55 | {
56 |
57 | int init::ms_counter = 0;
58 |
59 | init::init()
60 | {
61 | if ( ms_counter++ == 0 )
62 | init_library();
63 | }
64 |
65 |
66 | init::~init()
67 | {
68 | if ( --ms_counter == 0 )
69 | shutdown_library();
70 | }
71 |
72 |
73 | void init::init_library()
74 | {
75 | // init the parser (keeps libxml2 thread safe)
76 | xmlInitParser();
77 |
78 | // set some libxml global variables
79 | indent_output(true);
80 | remove_whitespace(false);
81 | substitute_entities(true);
82 | load_external_subsets(true);
83 | validate_xml(false);
84 | }
85 |
86 |
87 | void init::shutdown_library()
88 | {
89 | xmlCleanupParser();
90 | }
91 |
92 |
93 | bool init::indent_output(bool flag)
94 | {
95 | return change_flag_and_return_old_value(&xmlIndentTreeOutput, flag);
96 | }
97 |
98 |
99 | bool init::remove_whitespace(bool flag)
100 | {
101 | return !change_flag_and_return_old_value(&xmlKeepBlanksDefaultValue, !flag);
102 | }
103 |
104 |
105 | bool init::substitute_entities(bool flag)
106 | {
107 | return change_flag_and_return_old_value(&xmlSubstituteEntitiesDefaultValue, flag);
108 | }
109 |
110 |
111 | bool init::load_external_subsets(bool flag)
112 | {
113 | return change_flag_and_return_old_value(&xmlLoadExtDtdDefaultValue, flag);
114 | }
115 |
116 |
117 | bool init::validate_xml(bool flag)
118 | {
119 | return change_flag_and_return_old_value(&xmlDoValidityCheckingDefaultValue, flag);
120 | }
121 |
122 | } // namespace xml
123 |
--------------------------------------------------------------------------------
/src/libxml/node_manip.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the definition of the xml::node manipulation functions.
37 | */
38 |
39 | #ifndef _xmlwrapp_node_manip_h_
40 | #define _xmlwrapp_node_manip_h_
41 |
42 | // libxml includes
43 | #include
44 |
45 | namespace xml
46 | {
47 |
48 | namespace impl
49 | {
50 |
51 | /**
52 | @internal
53 |
54 | Insert a node somewhere in the child list of a parent node.
55 |
56 | @param parent The parent who's child list will be inserted into.
57 | @param before Insert @a to_add before this node, or, if this node is
58 | 0 (null), insert at the end of the child list.
59 | @param to_add The node to be copied and then inserted into the child list.
60 |
61 | @return The new node that was inserted into the child list.
62 | */
63 | xmlNodePtr node_insert(xmlNodePtr parent, xmlNodePtr before, xmlNodePtr to_add);
64 |
65 | /**
66 | @internal
67 |
68 | Replace a node with another one. The node being replaced will be
69 | freed from memory.
70 |
71 | @param old_node The old node to remove and free.
72 | @param new_node The new node to copy and insert where old node was.
73 |
74 | @return The new node that was crated from copying @a new_node and inserted
75 | into the child list where @a old_node was.
76 | */
77 | xmlNodePtr node_replace(xmlNodePtr old_node, xmlNodePtr new_node);
78 |
79 | /**
80 | @internal
81 |
82 | Erase a node from the child list, and then free it from memory.
83 |
84 | @param to_erase The node to remove and free.
85 |
86 | @return The node that was after to_erase (may be 0 (null) if @a to_erase
87 | was the last node in the list)
88 | */
89 | xmlNodePtr node_erase(xmlNodePtr to_erase);
90 |
91 | /**
92 | @internal
93 |
94 | Set namespace for this node and all its children not using any namespace.
95 | */
96 | void node_set_ns_recursively(xmlNodePtr node, xmlNsPtr ns);
97 |
98 | } // namespace impl
99 |
100 | } // namespace xml
101 |
102 | #endif // _xmlwrapp_node_manip_h_
103 |
--------------------------------------------------------------------------------
/include/xmlwrapp/schema.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Vaclav Slavik
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the definition of the xml::schema class.
37 | */
38 |
39 | #ifndef _xmlwrapp_schema_h_
40 | #define _xmlwrapp_schema_h_
41 |
42 | // xmlwrapp includes
43 | #include "xmlwrapp/init.h"
44 | #include "xmlwrapp/export.h"
45 | #include "xmlwrapp/errors.h"
46 |
47 | #include
48 |
49 | XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
50 |
51 | namespace xml
52 | {
53 |
54 | // forward declarations
55 | class document;
56 | class error_messages;
57 |
58 | namespace impl
59 | {
60 | struct schema_impl;
61 | }
62 |
63 | /**
64 | XML Schema.
65 |
66 | This class is used to validate documents against XML Schema.
67 |
68 | @since 0.7.0
69 | */
70 | class XMLWRAPP_API schema
71 | {
72 | public:
73 | /**
74 | Parses XML Schema document and creates schema instance from it.
75 |
76 | Errors are handled by @a on_error handler; by default, xml::exception
77 | is thrown on errors. If there's a fatal error that prevents the schema
78 | from being loaded and the error handler doesn't throw an exception, the
79 | constructor will throw xml::exception anyway.
80 | */
81 | explicit schema(const document& doc, error_handler& on_error = throw_on_error);
82 |
83 | /// Destructor
84 | ~schema();
85 |
86 | /**
87 | Validates the document @a doc against the schema.
88 |
89 | Errors are handled by @a on_error handler; by default, xml::exception
90 | is thrown on errors.
91 |
92 | @return `true` if the document is valid with regard to the schema,
93 | `false` otherwise.
94 | */
95 | bool validate(const document& doc, error_handler& on_error = throw_on_error) const;
96 |
97 | private:
98 | std::unique_ptr pimpl_;
99 |
100 | // Schema class is not copyable
101 | schema(const schema&) = delete;
102 | schema& operator=(const schema&) = delete;
103 | };
104 |
105 | } // namespace xml
106 |
107 | XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
108 |
109 | #endif // _xmlwrapp_schema_h_
110 |
--------------------------------------------------------------------------------
/src/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Configure shared library with the given name and (full) ELF version.
2 | #
3 | # Set properties for using hidden ELF visibility, defining proper DLL-related
4 | # symbols and use $ORIGIN in the runpath.
5 | #
6 | # Note that this does nothing when shared libraries are not used.
7 | function(setup_shared_library target version)
8 | if (XMLWRAPP_SHARED)
9 | # Construct XMLWRAPP_USE_DLL or XSLTWRAPP_USE_DLL symbol name.
10 | string(TOUPPER "${target}_USE_DLL" use_dll_symbol)
11 | target_compile_definitions(${target}
12 | PRIVATE
13 | HAVE_VISIBILITY
14 | INTERFACE
15 | ${use_dll_symbol}
16 | )
17 |
18 | # Set SOVERSION to the major VERSION component.
19 | string(REPLACE "." ";" version_as_list ${version})
20 | list(GET version_as_list 0 soversion)
21 |
22 | set(output_name "${target}")
23 | if( XMLWRAPP_NAME_PREFIX )
24 | set(output_name "${XMLWRAPP_NAME_PREFIX}${output_name}")
25 | endif()
26 | if( XMLWRAPP_NAME_SUFFIX )
27 | set(output_name "${output_name}${XMLWRAPP_NAME_SUFFIX}")
28 | endif()
29 |
30 | set_target_properties(${target} PROPERTIES
31 | OUTPUT_NAME ${output_name}
32 | BUILD_RPATH_USE_ORIGIN ON
33 | DEFINE_SYMBOL "DLL_EXPORT"
34 | CXX_VISIBILITY_PRESET hidden
35 | VISIBILITY_INLINES_HIDDEN ON
36 | VERSION ${version}
37 | SOVERSION ${soversion}
38 | )
39 | endif()
40 | endfunction()
41 |
42 | add_library(xmlwrapp ${XMLWRAPP_LIB_TYPE})
43 | target_sources(xmlwrapp
44 | PRIVATE
45 | libxml/ait_impl.cxx
46 | libxml/ait_impl.h
47 | libxml/attributes.cxx
48 | libxml/document.cxx
49 | libxml/dtd_impl.cxx
50 | libxml/dtd_impl.h
51 | libxml/errors.cxx
52 | libxml/errors_impl.h
53 | libxml/event_parser.cxx
54 | libxml/init.cxx
55 | libxml/node.cxx
56 | libxml/node_iterator.cxx
57 | libxml/node_iterator.h
58 | libxml/node_manip.cxx
59 | libxml/node_manip.h
60 | libxml/nodes_view.cxx
61 | libxml/relaxng.cxx
62 | libxml/schema.cxx
63 | libxml/tree_parser.cxx
64 | libxml/utility.cxx
65 | libxml/utility.h
66 | libxml/version.cxx
67 | libxml/xpath.cxx
68 | )
69 |
70 | target_include_directories(xmlwrapp
71 | PRIVATE
72 | ${LIBXML2_INCLUDE_DIRS}
73 | PUBLIC
74 | $
75 | $
76 | )
77 | target_link_options(xmlwrapp
78 | PUBLIC
79 | ${LIBXML2_LDFLAGS}
80 | )
81 | target_link_libraries(xmlwrapp
82 | PUBLIC
83 | ${LIBXML2_LIBRARIES}
84 | )
85 | setup_shared_library(xmlwrapp 6.0.0)
86 | install (TARGETS xmlwrapp EXPORT XmlwrappTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})
87 |
88 | add_library(xmlwrapp::xmlwrapp ALIAS xmlwrapp)
89 |
90 | if( XMLWRAPP_WITH_LIBXSLT )
91 | add_library(xsltwrapp ${XMLWRAPP_LIB_TYPE})
92 | target_sources(xsltwrapp
93 | PRIVATE
94 | libxslt/init.cxx
95 | libxslt/stylesheet.cxx
96 | libxslt/result.h
97 | )
98 | target_include_directories(xsltwrapp
99 | PRIVATE
100 | ${LIBXSLT_INCLUDE_DIRS}
101 | ${LIBEXSLT_INCLUDE_DIRS}
102 | PUBLIC
103 | $
104 | $
105 | )
106 | target_link_options(xsltwrapp
107 | PUBLIC
108 | ${LIBXSLT_LDFLAGS}
109 | ${LIBEXSLT_LDFLAGS}
110 | )
111 | target_link_libraries(xsltwrapp
112 | PUBLIC
113 | xmlwrapp
114 | ${LIBXSLT_LIBRARIES}
115 | ${LIBEXSLT_LIBRARIES}
116 | )
117 | setup_shared_library(xsltwrapp 4.0.0)
118 | install (TARGETS xsltwrapp EXPORT XmlwrappTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})
119 |
120 | add_library(xmlwrapp::xsltwrapp ALIAS xsltwrapp)
121 | endif( XMLWRAPP_WITH_LIBXSLT )
122 |
--------------------------------------------------------------------------------
/include/xmlwrapp/relaxng.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Vadim Zeitlin
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | /**
34 | @file
35 |
36 | This file contains the definition of the xml::relaxng class.
37 | */
38 |
39 | #ifndef _xmlwrapp_relaxng_h_
40 | #define _xmlwrapp_relaxng_h_
41 |
42 | // xmlwrapp includes
43 | #include "xmlwrapp/init.h"
44 | #include "xmlwrapp/export.h"
45 | #include "xmlwrapp/errors.h"
46 |
47 | #include
48 |
49 | XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
50 |
51 | namespace xml
52 | {
53 |
54 | // forward declarations
55 | class document;
56 | class error_messages;
57 |
58 | namespace impl
59 | {
60 | struct relaxng_impl;
61 | }
62 |
63 | /**
64 | XML validator using RelaxNG.
65 |
66 | This class is used to validate documents against RelaxNG schemas expressed
67 | in XML syntax (compact RelaxNG syntax is not supported).
68 |
69 | @since 0.9.0
70 | */
71 | class XMLWRAPP_API relaxng
72 | {
73 | public:
74 | /**
75 | Parses XML RelaxNG document and creates relaxng instance from it.
76 |
77 | Errors are handled by @a on_error handler; by default, xml::exception
78 | is thrown on errors. If there's a fatal error that prevents the relaxng
79 | from being loaded and the error handler doesn't throw an exception, the
80 | constructor will throw xml::exception anyway.
81 | */
82 | explicit relaxng(const document& doc, error_handler& on_error = throw_on_error);
83 |
84 | /// Destructor
85 | ~relaxng();
86 |
87 | /**
88 | Validates the document @a doc against the relaxng.
89 |
90 | Errors are handled by @a on_error handler; by default, xml::exception
91 | is thrown on errors.
92 |
93 | @return `true` if the document is valid with regard to the relaxng,
94 | `false` otherwise.
95 | */
96 | bool validate(const document& doc, error_handler& on_error = throw_on_error) const;
97 |
98 | private:
99 | std::unique_ptr pimpl_;
100 |
101 | // This class is not copyable
102 | relaxng(const relaxng&) = delete;
103 | relaxng& operator=(const relaxng&) = delete;
104 | };
105 |
106 | } // namespace xml
107 |
108 | XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
109 |
110 | #endif // _xmlwrapp_relaxng_h_
111 |
--------------------------------------------------------------------------------
/src/libxml/node_iterator.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * 2009 Vaclav Slavik
4 | * All Rights Reserved
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | *
10 | * 1. Redistributions of source code must retain the above copyright
11 | * notice, this list of conditions and the following disclaimer.
12 | * 2. Redistributions in binary form must reproduce the above copyright
13 | * notice, this list of conditions and the following disclaimer in
14 | * the documentation and/or other materials provided with the
15 | * distribution.
16 | * 3. Neither the name of the Author nor the names of its contributors
17 | * may be used to endorse or promote products derived from this software
18 | * without specific prior written permission.
19 | *
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
24 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 | * SUCH DAMAGE.
32 | */
33 |
34 | #ifndef _xmlwrapp_node_iterator_h_
35 | #define _xmlwrapp_node_iterator_h_
36 |
37 | // xmlwrapp includes
38 | #include "xmlwrapp/node.h"
39 |
40 | // libxml includes
41 | #include
42 |
43 | namespace xml
44 | {
45 |
46 | namespace impl
47 | {
48 |
49 | // helper to obtain the next node in "filtering" iterators (as used by
50 | // nodes_view and const_nodes_view)
51 | //
52 | // Note: This class is reference-counted; don't delete instance of it, use
53 | // dec_ref() and inc_ref(). Newly created instance has reference count
54 | // of 1.
55 | class iter_advance_functor
56 | {
57 | public:
58 | iter_advance_functor() = default;
59 |
60 | void inc_ref()
61 | {
62 | refcnt_++;
63 | }
64 |
65 | void dec_ref()
66 | {
67 | if ( --refcnt_ == 0 )
68 | delete this;
69 | }
70 |
71 | virtual xmlNodePtr operator()(xmlNodePtr node) const = 0;
72 |
73 | protected:
74 | // use inc_ref(), dec_ref() instead of using the dtor explicitly
75 | virtual ~iter_advance_functor() = default;
76 |
77 | private:
78 | // no copy ctor or assignment
79 | iter_advance_functor(const iter_advance_functor& other) = delete;
80 | iter_advance_functor& operator=(const iter_advance_functor& other) = delete;
81 |
82 | private:
83 | int refcnt_{1};
84 | };
85 |
86 | // base iterator class
87 | class node_iterator
88 | {
89 | public:
90 | node_iterator() : fake_node_(0), node_(nullptr) {}
91 |
92 | node_iterator(node &parent)
93 | : fake_node_(0),
94 | node_(reinterpret_cast(parent.get_node_data()))
95 | {
96 | }
97 |
98 | node_iterator(xmlNodePtr xmlnode) : fake_node_(0), node_(xmlnode) {}
99 | node_iterator(const node_iterator& other) : fake_node_(0), node_(other.node_) {}
100 | node_iterator& operator=(const node_iterator& other)
101 | { node_ = other.node_; return *this;}
102 |
103 | node *get() const;
104 | xmlNodePtr get_raw_node() { return node_; }
105 |
106 | void advance() { node_ = node_->next; }
107 | void advance(iter_advance_functor& next) { node_ = next(node_); }
108 |
109 | private:
110 | mutable node fake_node_;
111 | xmlNodePtr node_;
112 | };
113 |
114 | } // namespace impl
115 |
116 | } // namespace xml
117 |
118 | #endif // _xmlwrapp_node_iterator_h_
119 |
--------------------------------------------------------------------------------
/tests/relaxng/test_relaxng.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Vadim Zeitlin
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #include "../test.h"
34 |
35 | TEST_CASE_METHOD( SrcdirConfig, "relaxng/load_non_relaxng_file", "[relaxng]" )
36 | {
37 | xml::document sch_doc =
38 | xml::tree_parser(test_file_path("relaxng/data/valid.xml").c_str()).get_document();
39 |
40 | CHECK_THROWS_AS
41 | (
42 | xml::relaxng(sch_doc),
43 | xml::exception
44 | );
45 |
46 | xml::error_messages log;
47 | // throw even when collecting errors into log
48 | CHECK_THROWS_AS
49 | (
50 | xml::relaxng(sch_doc, log),
51 | xml::exception
52 | );
53 | CHECK( log.has_errors() );
54 | }
55 |
56 |
57 | TEST_CASE_METHOD( SrcdirConfig, "relaxng/validate_ok", "[relaxng]" )
58 | {
59 | xml::document sch_doc =
60 | xml::tree_parser(test_file_path("relaxng/data/schema.rng").c_str()).get_document();
61 | xml::relaxng sch(sch_doc);
62 |
63 | xml::document doc =
64 | xml::tree_parser(test_file_path("relaxng/data/valid.xml").c_str()).get_document();
65 |
66 | CHECK( sch.validate(doc) );
67 |
68 | // And the same with logging:
69 | xml::error_messages log;
70 | CHECK( sch.validate(doc) );
71 | CHECK( !log.has_errors() );
72 | CHECK( !log.has_warnings() );
73 | }
74 |
75 |
76 | TEST_CASE_METHOD( SrcdirConfig, "relaxng/validate_invalid", "[relaxng]" )
77 | {
78 | xml::document sch_doc =
79 | xml::tree_parser(test_file_path("relaxng/data/schema.rng").c_str()).get_document();
80 | xml::relaxng sch(sch_doc);
81 |
82 | xml::document doc =
83 | xml::tree_parser(test_file_path("relaxng/data/invalid.xml").c_str()).get_document();
84 |
85 | CHECK_THROWS_AS
86 | (
87 | sch.validate(doc),
88 | xml::exception
89 | );
90 |
91 | // And the same with logging:
92 | xml::error_messages log;
93 | CHECK( !sch.validate(doc, log) );
94 | CHECK( log.has_errors() );
95 | }
96 |
97 |
98 | TEST_CASE_METHOD( SrcdirConfig, "relaxng/validate_nonvalid", "[relaxng]" )
99 | {
100 | xml::document sch_doc =
101 | xml::tree_parser(test_file_path("relaxng/data/schema.rng").c_str()).get_document();
102 | xml::relaxng sch(sch_doc);
103 |
104 | xml::document doc =
105 | xml::tree_parser(test_file_path("relaxng/data/nonvalid.xml").c_str()).get_document();
106 |
107 | CHECK_THROWS_AS
108 | (
109 | sch.validate(doc),
110 | xml::exception
111 | );
112 |
113 | // And the same with logging:
114 | xml::error_messages log;
115 | CHECK( !sch.validate(doc, log) );
116 | CHECK( log.has_errors() );
117 |
118 | // The error message is "Did not expect element CCC there" because it's not
119 | // supposed to occur before "BBB".
120 | CHECK( log.print().find("CCC") != std::string::npos );
121 | }
122 |
--------------------------------------------------------------------------------
/tests/xpath/test_xpath.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Jonas Weber
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #include "../test.h"
34 |
35 | #include
36 |
37 |
38 | TEST_CASE_METHOD( SrcdirConfig, "xpath/create_context", "[xpath]" )
39 | {
40 | xml::tree_parser parser(test_file_path("xpath/data/01.xml").c_str());
41 | xml::xpath_context ctxt(parser.get_document());
42 | }
43 |
44 | TEST_CASE_METHOD( SrcdirConfig, "xpath/evaluate", "[xpath]" )
45 | {
46 | xml::tree_parser parser(test_file_path("xpath/data/01.xml").c_str());
47 | xml::xpath_context ctxt(parser.get_document());
48 | xml::const_nodes_view ns = ctxt.evaluate("//child");
49 |
50 | CHECK( !ns.empty() );
51 | }
52 |
53 | TEST_CASE_METHOD( SrcdirConfig, "xpath/evaluate_with_ns", "[xpath]" )
54 | {
55 | xml::tree_parser parser(test_file_path("xpath/data/02.xml").c_str());
56 | xml::xpath_context ctxt(parser.get_document());
57 |
58 | xml::const_nodes_view ns1 = ctxt.evaluate("//child");
59 | CHECK( ns1.empty() );
60 |
61 | ctxt.register_namespace("p", "href");
62 | xml::const_nodes_view ns2 = ctxt.evaluate("//p:child");
63 | CHECK( !ns2.empty() );
64 | CHECK( std::distance(ns2.begin(), ns2.end()) == 3 );
65 | }
66 |
67 | TEST_CASE_METHOD( SrcdirConfig, "xpath/node_set_iterators", "[xpath]" )
68 | {
69 | xml::tree_parser parser(test_file_path("xpath/data/02.xml").c_str());
70 | xml::xpath_context ctxt(parser.get_document());
71 | ctxt.register_namespace("p", "href");
72 | xml::const_nodes_view ns = ctxt.evaluate("//p:child");
73 |
74 | CHECK( ns.begin() == ns.begin() );
75 | REQUIRE( !ns.empty() );
76 | CHECK( ns.begin() != ns.end() );
77 |
78 | xml::const_nodes_view::iterator it = ns.begin();
79 | CHECK( it++ == ns.begin() );
80 | CHECK( it != ns.begin() );
81 | it = ns.begin();
82 | CHECK( ++it != ns.begin() );
83 |
84 |
85 | for (auto const& n : ns)
86 | {
87 | CHECK( n.get_name() );
88 | }
89 | }
90 |
91 | TEST_CASE_METHOD( SrcdirConfig, "xpath/node_set_contains", "[xpath]" )
92 | {
93 | xml::tree_parser parser(test_file_path("xpath/data/02.xml").c_str());
94 | xml::xpath_context ctxt(parser.get_document());
95 |
96 | ctxt.register_namespace("p", "href");
97 |
98 | xml::const_nodes_view ns = ctxt.evaluate("//p:child");
99 | CHECK( std::distance(ns.begin(), ns.end()) == 3 );
100 | }
101 |
102 | TEST_CASE_METHOD( SrcdirConfig, "xpath/illegal_xpath", "[xpath]" )
103 | {
104 | xml::tree_parser parser(test_file_path("xpath/data/02.xml").c_str());
105 | xml::xpath_context ctxt(parser.get_document());
106 |
107 | xml::const_nodes_view ns = ctxt.evaluate("ILLEGAL XPATH-QUERY", xml::ignore_errors);
108 | CHECK( ns.begin() == ns.end() );
109 |
110 | CHECK_THROWS_AS
111 | (
112 | ctxt.evaluate("ANOTHER ILLEGAL QUERY"),
113 | xml::exception
114 | );
115 | }
116 |
--------------------------------------------------------------------------------
/src/libxml/utility.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #ifndef _xmlwrapp_utility_h_
34 | #define _xmlwrapp_utility_h_
35 |
36 | #include
37 |
38 | // standard includes
39 | #include
40 | #include
41 | #include
42 |
43 | // libxml2 includes
44 | #include
45 |
46 | namespace xml
47 | {
48 |
49 | namespace impl
50 | {
51 |
52 | // Helper for holding libxml objects with guaranteed freeing
53 | template
54 | class xml_scoped_ptr
55 | {
56 | public:
57 | explicit xml_scoped_ptr(TPtr ptr) : ptr_(ptr) {}
58 |
59 | ~xml_scoped_ptr()
60 | {
61 | if (ptr_)
62 | FreeFunc(ptr_);
63 | }
64 |
65 | TPtr operator->() const { return ptr_; }
66 | TPtr get() const { return ptr_; }
67 | operator TPtr() const { return ptr_; }
68 |
69 | private:
70 | TPtr ptr_;
71 | };
72 |
73 | // exception safe wrapper around xmlChar*s that are returned from some
74 | // of the libxml functions that the user must free.
75 | class xmlchar_helper
76 | {
77 | public:
78 | xmlchar_helper(xmlChar *ptr) : ptr_(ptr) {}
79 |
80 | ~xmlchar_helper()
81 | { if (ptr_) xmlFree(ptr_); }
82 |
83 | const char* get() const
84 | { return reinterpret_cast(ptr_); }
85 |
86 | private:
87 | xmlChar *ptr_;
88 | };
89 |
90 | inline const xmlChar* xml_string(const std::string& s)
91 | {
92 | return reinterpret_cast(s.c_str());
93 | }
94 |
95 | // Formats given message with arguments into a std::string
96 | void printf2string(std::string& s, const char *message, va_list ap);
97 |
98 | // Sun CC uses ancient C++ standard library that doesn't have standard
99 | // std::distance(). Work around it here
100 | #if defined(__SUNPRO_CC) && !defined(_STLPORT_VERSION)
101 | template
102 | inline size_t distance(T a, const T&b)
103 | {
104 | size_t n = 0;
105 | for ( ; a != b; ++a )
106 | ++n;
107 | return n;
108 | }
109 | #endif // defined(__SUNPRO_CC) && !defined(_STLPORT_VERSION)
110 |
111 | // Cast size_t to int safely, i.e. throw an exception in case of an overflow.
112 | inline int checked_int_cast(std::size_t len)
113 | {
114 | if ( len > INT_MAX )
115 | throw std::invalid_argument("data too long");
116 |
117 | return static_cast(len);
118 | }
119 |
120 | // Cast int to size_t safely, checking that it's non-negative (we assume that
121 | // size_t is always big enough to contain INT_MAX, which is true for all
122 | // currently existing architectures).
123 | inline std::size_t checked_size_t_cast(int len)
124 | {
125 | if ( len < 0 )
126 | throw std::runtime_error("length value unexpectedly negative");
127 |
128 | return static_cast(len);
129 | }
130 |
131 | } // namespace impl
132 |
133 | } // namespace xml
134 |
135 |
136 | #endif // _xmlwrapp_utility_h_
137 |
--------------------------------------------------------------------------------
/src/libxml/relaxng.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Vadim Zeitlin
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the relaxngation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | // xmlwrapp includes
34 | #include "xmlwrapp/relaxng.h"
35 | #include "xmlwrapp/document.h"
36 | #include "xmlwrapp/errors.h"
37 |
38 | #include "errors_impl.h"
39 |
40 | // libxml includes
41 | #include
42 |
43 | namespace xml
44 | {
45 |
46 | using namespace impl;
47 |
48 | // ------------------------------------------------------------------------
49 | // xml::impl::relaxng_impl
50 | // ------------------------------------------------------------------------
51 |
52 | namespace impl
53 | {
54 |
55 | struct relaxng_impl
56 | {
57 | relaxng_impl(xmlDocPtr xmldoc, error_handler& on_error)
58 | {
59 | impl::errors_collector err;
60 |
61 | xmlRelaxNGParserCtxtPtr ctxt = xmlRelaxNGNewDocParserCtxt(xmldoc);
62 | if ( !ctxt )
63 | throw std::bad_alloc();
64 | xmlRelaxNGSetParserErrors(ctxt,
65 | cb_messages_error, cb_messages_warning,
66 | &err);
67 |
68 | relaxng_ = xmlRelaxNGParse(ctxt);
69 | xmlRelaxNGFreeParserCtxt(ctxt);
70 |
71 | if ( !relaxng_ )
72 | {
73 | err.replay(on_error);
74 | // if the handler didn't throw, do it ourselves -- it's the only
75 | // way to signal fatal errors from a ctor:
76 | throw exception(err);
77 | }
78 | }
79 |
80 | ~relaxng_impl()
81 | {
82 | if (relaxng_)
83 | xmlRelaxNGFree(relaxng_);
84 | }
85 |
86 | xmlRelaxNGPtr relaxng_{nullptr};
87 | };
88 |
89 | } // namespace impl
90 |
91 |
92 | // ------------------------------------------------------------------------
93 | // xml::relaxng
94 | // ------------------------------------------------------------------------
95 |
96 | relaxng::relaxng(const document& doc, error_handler& on_error)
97 | {
98 | // Note that, unlike xmlSchemaNewDocParserCtxt(), we don't need to make a
99 | // copy of the document here as xmlRelaxNGNewDocParserCtxt() already does
100 | // it internally.
101 | auto xmldoc = static_cast(doc.get_doc_data_read_only());
102 |
103 | pimpl_.reset(new relaxng_impl(xmldoc, on_error));
104 | }
105 |
106 | relaxng::~relaxng() = default;
107 |
108 |
109 | bool relaxng::validate(const document& doc, error_handler& on_error) const
110 | {
111 | auto xmldoc = static_cast(doc.get_doc_data_read_only());
112 |
113 | xmlRelaxNGValidCtxtPtr ctxt = xmlRelaxNGNewValidCtxt(pimpl_->relaxng_);
114 | if ( !ctxt )
115 | throw std::bad_alloc();
116 |
117 | impl::errors_collector err;
118 | xmlRelaxNGSetValidErrors(ctxt,
119 | cb_messages_error, cb_messages_warning,
120 | &err);
121 |
122 | int ret = xmlRelaxNGValidateDoc(ctxt, xmldoc);
123 |
124 | xmlRelaxNGFreeValidCtxt(ctxt);
125 |
126 | if ( ret == -1 )
127 | throw xml::exception("internal validation error");
128 |
129 | err.replay(on_error);
130 |
131 | return ret == 0;
132 | }
133 |
134 | } // namespace xml
135 |
--------------------------------------------------------------------------------
/src/libxml/schema.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Vaclav Slavik
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the schemaation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | // xmlwrapp includes
34 | #include "xmlwrapp/schema.h"
35 | #include "xmlwrapp/document.h"
36 | #include "xmlwrapp/errors.h"
37 |
38 | #include "errors_impl.h"
39 |
40 | // libxml includes
41 | #include
42 |
43 | namespace xml
44 | {
45 |
46 | using namespace impl;
47 |
48 | // ------------------------------------------------------------------------
49 | // xml::impl::schema_impl
50 | // ------------------------------------------------------------------------
51 |
52 | namespace impl
53 | {
54 |
55 | struct schema_impl
56 | {
57 | schema_impl(xmlDocPtr xmldoc, error_handler& on_error)
58 | {
59 | impl::errors_collector err;
60 |
61 | xmlSchemaParserCtxtPtr ctxt = xmlSchemaNewDocParserCtxt(xmldoc);
62 | if ( !ctxt )
63 | throw std::bad_alloc();
64 | xmlSchemaSetParserErrors(ctxt,
65 | cb_messages_error, cb_messages_warning,
66 | &err);
67 |
68 | schema_ = xmlSchemaParse(ctxt);
69 | xmlSchemaFreeParserCtxt(ctxt);
70 |
71 | if ( !schema_ )
72 | {
73 | err.replay(on_error);
74 | // if the handler didn't throw, do it ourselves -- it's the only
75 | // way to signal fatal errors from a ctor:
76 | throw exception(err);
77 | }
78 | }
79 |
80 | ~schema_impl()
81 | {
82 | if (schema_)
83 | xmlSchemaFree(schema_);
84 | if (retainDoc_)
85 | xmlFreeDoc(retainDoc_);
86 | }
87 |
88 | xmlSchemaPtr schema_{nullptr};
89 | xmlDocPtr retainDoc_{nullptr};
90 | };
91 |
92 | } // namespace impl
93 |
94 |
95 | // ------------------------------------------------------------------------
96 | // xml::schema
97 | // ------------------------------------------------------------------------
98 |
99 | schema::schema(const document& doc, error_handler& on_error)
100 | {
101 | // xmlSchemaNewDocParserCtxt() modifies the document during parsing,
102 | // so we have to make a private copy first
103 | document mydoc(doc);
104 |
105 | auto xmldoc = static_cast(mydoc.get_doc_data_read_only());
106 | pimpl_.reset(new schema_impl(xmldoc, on_error));
107 |
108 | // xmldoc is still used in the schema_, but it's not owned (and thus freed)
109 | // by it, we have to manage its lifetime outselves. Transfer the ownership
110 | // here and clear 'mydoc' of any data.
111 | //
112 | // Note that the document was already modified by libxml2 in the process of
113 | // parsing it, so it's not usable by the caller anymore.
114 | mydoc.release_doc_data();
115 | pimpl_->retainDoc_ = xmldoc;
116 | }
117 |
118 | schema::~schema() = default;
119 |
120 |
121 | bool schema::validate(const document& doc, error_handler& on_error) const
122 | {
123 | auto xmldoc = static_cast(doc.get_doc_data_read_only());
124 |
125 | xmlSchemaValidCtxtPtr ctxt = xmlSchemaNewValidCtxt(pimpl_->schema_);
126 | if ( !ctxt )
127 | throw std::bad_alloc();
128 |
129 | impl::errors_collector err;
130 | xmlSchemaSetValidErrors(ctxt,
131 | cb_messages_error, cb_messages_warning,
132 | &err);
133 |
134 | int ret = xmlSchemaValidateDoc(ctxt, xmldoc);
135 |
136 | xmlSchemaFreeValidCtxt(ctxt);
137 |
138 | if ( ret == -1 )
139 | throw xml::exception("internal validation error");
140 |
141 | err.replay(on_error);
142 |
143 | return ret == 0;
144 | }
145 |
146 | } // namespace xml
147 |
--------------------------------------------------------------------------------
/src/libxml/errors_impl.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2013 Vaclav Slavik
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #ifndef _xmlwrapp_errors_impl_h_
34 | #define _xmlwrapp_errors_impl_h_
35 |
36 | #include
37 | #include
38 | #include
39 |
40 | namespace xml
41 | {
42 |
43 | namespace impl
44 | {
45 |
46 | // This handler collects all error & warning messages from libxml2 callbacks,
47 | // without throwing any exceptions, and then replays them, in order, to the
48 | // "real" error handler.
49 | class XMLWRAPP_API errors_collector : public error_messages
50 | {
51 | public:
52 | // replay all errors into target handler
53 | void replay(error_handler& dest);
54 |
55 | protected:
56 | std::string format_for_print(const error_message& msg) const override;
57 | };
58 |
59 | // RAII helper installing the given error collector as the global error sink
60 | // for libxml2 error messages.
61 | class XMLWRAPP_API global_errors_installer
62 | {
63 | public:
64 | // The object given as argument must have lifetime greater than that of
65 | // this object itself.
66 | explicit global_errors_installer(error_messages& on_error);
67 | ~global_errors_installer();
68 |
69 | private:
70 | global_errors_installer(const global_errors_installer&) = delete;
71 | global_errors_installer& operator=(const global_errors_installer&) = delete;
72 |
73 | xmlGenericErrorFunc xml_generic_error_orig_;
74 | void *xml_generic_error_context_orig_;
75 |
76 | xmlStructuredErrorFunc xml_structured_error_orig_;
77 | void *xml_structured_error_context_orig_;
78 | };
79 |
80 | // This class behaves like error_collector but also installs itself as handler
81 | // for global libxml2 errors, i.e. those that happen outside of any other
82 | // context.
83 | class global_errors_collector :
84 | public errors_collector,
85 | private global_errors_installer
86 | {
87 | public:
88 | global_errors_collector() :
89 | global_errors_installer(static_cast(*this))
90 | {
91 | }
92 |
93 | private:
94 | global_errors_collector(const global_errors_collector&) = delete;
95 | global_errors_collector& operator=(const global_errors_collector&) = delete;
96 | };
97 |
98 | // These functions can be used as error callbacks in various libxml2 functions.
99 | // They collect messages into errors_collector structure.
100 | // Usage: pass the pointer to errors_collector as libxml2's void* ctx argument
101 | extern "C"
102 | {
103 |
104 | #if LIBXML_VERSION >= 21200
105 | XMLWRAPP_API void cb_messages_structured_error(void *out, const xmlError *error);
106 | #else
107 | XMLWRAPP_API void cb_messages_structured_error(void *out, xmlErrorPtr error);
108 | #endif
109 |
110 | XMLWRAPP_API void cb_messages_warning(void *out, const char *message, ...);
111 | XMLWRAPP_API void cb_messages_error(void *out, const char *message, ...);
112 |
113 | XMLWRAPP_API void cb_messages_warning_v(void *out, const char *message, va_list ap);
114 | XMLWRAPP_API void cb_messages_error_v(void *out, const char *message, va_list ap);
115 |
116 | #define CALL_CB_MESSAGES(cb, out, message) \
117 | va_list ap; \
118 | va_start(ap, message); \
119 | cb(out, message, ap); \
120 | va_end(ap)
121 |
122 | #define CALL_CB_MESSAGES_ERROR(out, message) \
123 | CALL_CB_MESSAGES(cb_messages_error_v, out, message)
124 |
125 | #define CALL_CB_MESSAGES_WARNING(out, message) \
126 | CALL_CB_MESSAGES(cb_messages_warning_v, out, message)
127 |
128 | } // extern "C"
129 |
130 | } // namespace impl
131 |
132 | } // namespace xml
133 |
134 |
135 | #endif // _xmlwrapp_errors_impl_h_
136 |
--------------------------------------------------------------------------------
/src/libxml/nodes_view.cxx:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009 Vaclav Slavik
3 | * All Rights Reserved
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | *
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in
13 | * the documentation and/or other materials provided with the
14 | * distribution.
15 | * 3. Neither the name of the Author nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 |
34 | // xmlwrapp includes
35 | #include "xmlwrapp/nodes_view.h"
36 |
37 | // definition include
38 | #include "node_iterator.h"
39 | #include "node_manip.h"
40 | #include "utility.h"
41 |
42 | using namespace xml::impl;
43 |
44 | namespace xml
45 | {
46 |
47 | // ------------------------------------------------------------------------
48 | // xml::const_nodes_view
49 | // ------------------------------------------------------------------------
50 |
51 | const_nodes_view::const_nodes_view(const const_nodes_view& other)
52 | : data_begin_(other.data_begin_),
53 | advance_func_(other.advance_func_)
54 | {
55 | if ( advance_func_ )
56 | advance_func_->inc_ref();
57 | }
58 |
59 |
60 | const_nodes_view::const_nodes_view(const nodes_view& other)
61 | : data_begin_(other.data_begin_),
62 | advance_func_(other.advance_func_)
63 | {
64 | if ( advance_func_ )
65 | advance_func_->inc_ref();
66 | }
67 |
68 |
69 | const_nodes_view::~const_nodes_view()
70 | {
71 | if ( advance_func_ )
72 | advance_func_->dec_ref();
73 | }
74 |
75 |
76 | const_nodes_view& const_nodes_view::operator=(const const_nodes_view& other)
77 | {
78 | if ( advance_func_ )
79 | advance_func_->dec_ref();
80 |
81 | data_begin_ = other.data_begin_;
82 | advance_func_ = other.advance_func_;
83 |
84 | if ( advance_func_ )
85 | advance_func_->inc_ref();
86 |
87 | return *this;
88 | }
89 |
90 |
91 | const_nodes_view& const_nodes_view::operator=(const nodes_view& other)
92 | {
93 | if ( advance_func_ )
94 | advance_func_->dec_ref();
95 |
96 | data_begin_ = other.data_begin_;
97 | advance_func_ = other.advance_func_;
98 |
99 | if ( advance_func_ )
100 | advance_func_->inc_ref();
101 |
102 | return *this;
103 | }
104 |
105 |
106 | const_nodes_view::size_type const_nodes_view::size() const
107 | {
108 | using namespace std;
109 | return checked_size_t_cast(distance(begin(), end()));
110 | }
111 |
112 |
113 | // ------------------------------------------------------------------------
114 | // xml::nodes_view
115 | // ------------------------------------------------------------------------
116 |
117 | nodes_view::nodes_view(const nodes_view& other)
118 | : data_begin_(other.data_begin_),
119 | advance_func_(other.advance_func_)
120 | {
121 | if ( advance_func_ )
122 | advance_func_->inc_ref();
123 | }
124 |
125 |
126 | nodes_view::~nodes_view()
127 | {
128 | if ( advance_func_ )
129 | advance_func_->dec_ref();
130 | }
131 |
132 |
133 | nodes_view& nodes_view::operator=(const nodes_view& other)
134 | {
135 | if ( advance_func_ )
136 | advance_func_->dec_ref();
137 |
138 | data_begin_ = other.data_begin_;
139 | advance_func_ = other.advance_func_;
140 |
141 | if ( advance_func_ )
142 | advance_func_->inc_ref();
143 |
144 | return *this;
145 | }
146 |
147 |
148 | nodes_view::size_type nodes_view::size() const
149 | {
150 | using namespace std;
151 | return checked_size_t_cast(distance(begin(), end()));
152 | }
153 |
154 |
155 | nodes_view::iterator nodes_view::erase(const nodes_view::iterator& to_erase)
156 | {
157 | iterator next(to_erase);
158 | ++next;
159 | xml::impl::node_erase(static_cast(to_erase.get_raw_node()));
160 | return next;
161 | }
162 |
163 |
164 | nodes_view::iterator nodes_view::erase(nodes_view::iterator first,
165 | const nodes_view::iterator& last)
166 | {
167 | while (first != last)
168 | first = erase(first);
169 | return first;
170 | }
171 |
172 | } // namespace xml
173 |
--------------------------------------------------------------------------------
/admin/visibility.m4:
--------------------------------------------------------------------------------
1 | dnl visibility.m4 serial 1 (gettext-0.15)
2 | dnl Copyright (C) 2005 Free Software Foundation, Inc.
3 | dnl This file is free software; the Free Software Foundation
4 | dnl gives unlimited permission to copy and/or distribute it,
5 | dnl with or without modifications, as long as this notice is preserved.
6 |
7 | dnl From Bruno Haible.
8 |
9 | dnl Modified for use in wxWidgets by Vaclav Slavik:
10 | dnl - don't define HAVE_VISIBILITY (=0) if not supported
11 | dnl - use -fvisibility-inlines-hidden too
12 | dnl - test in C++ mode
13 |
14 | dnl Tests whether the compiler supports the command-line option
15 | dnl -fvisibility=hidden and the function and variable attributes
16 | dnl __attribute__((__visibility__("hidden"))) and
17 | dnl __attribute__((__visibility__("default"))).
18 | dnl Does *not* test for __visibility__("protected") - which has tricky
19 | dnl semantics (see the 'vismain' test in glibc) and does not exist e.g. on
20 | dnl MacOS X.
21 | dnl Does *not* test for __visibility__("internal") - which has processor
22 | dnl dependent semantics.
23 | dnl Does *not* test for #pragma GCC visibility push(hidden) - which is
24 | dnl "really only recommended for legacy code".
25 | dnl Set the variable CFLAG_VISIBILITY.
26 | dnl Defines and sets the variable HAVE_VISIBILITY.
27 |
28 | AC_DEFUN([XMLWRAPP_VISIBILITY],
29 | [
30 | AC_REQUIRE([AC_PROG_CC])
31 | if test -n "$GCC"; then
32 | CFLAGS_VISIBILITY="-fvisibility=hidden"
33 | CXXFLAGS_VISIBILITY="-fvisibility=hidden -fvisibility-inlines-hidden"
34 | AC_MSG_CHECKING([for symbols visibility support])
35 | AC_CACHE_VAL(wx_cv_cc_visibility, [
36 | wx_save_CXXFLAGS="$CXXFLAGS"
37 | CXXFLAGS="$CXXFLAGS $CXXFLAGS_VISIBILITY"
38 | AC_LANG_PUSH(C++)
39 | AC_COMPILE_IFELSE(
40 | [AC_LANG_PROGRAM([], [
41 | /* we need gcc >= 4.0, older versions with visibility support
42 | didn't have class visibility: */
43 | #if defined(__GNUC__) && __GNUC__ < 4
44 | error this gcc is too old;
45 | #endif
46 |
47 | /* visibility only makes sense for ELF shared libs: */
48 | #if !defined(__ELF__) && !defined(__APPLE__)
49 | error this platform has no visibility;
50 | #endif
51 |
52 | extern __attribute__((__visibility__("hidden"))) int hiddenvar;
53 | extern __attribute__((__visibility__("default"))) int exportedvar;
54 | extern __attribute__((__visibility__("hidden"))) int hiddenfunc (void);
55 | extern __attribute__((__visibility__("default"))) int exportedfunc (void);
56 | class __attribute__((__visibility__("default"))) Foo {
57 | Foo() {}
58 | };
59 | ])],
60 | wx_cv_cc_visibility=yes,
61 | wx_cv_cc_visibility=no)
62 | AC_LANG_POP()
63 | CXXFLAGS="$wx_save_CXXFLAGS"])
64 | AC_MSG_RESULT([$wx_cv_cc_visibility])
65 | if test $wx_cv_cc_visibility = yes; then
66 | dnl we do have basic visibility support, now check if we can use it:
67 | dnl
68 | dnl Debian/Ubuntu's gcc 4.1 is affected:
69 | dnl https://bugs.launchpad.net/ubuntu/+source/gcc-4.1/+bug/109262
70 | AC_MSG_CHECKING([for broken libstdc++ visibility])
71 | AC_CACHE_VAL(wx_cv_cc_broken_libstdcxx_visibility, [
72 | wx_save_CXXFLAGS="$CXXFLAGS"
73 | wx_save_LDFLAGS="$LDFLAGS"
74 | CXXFLAGS="$CXXFLAGS $CXXFLAGS_VISIBILITY"
75 | LDFLAGS="$LDFLAGS -shared -fPIC"
76 | AC_LANG_PUSH(C++)
77 | AC_LINK_IFELSE(
78 | [AC_LANG_PROGRAM(
79 | [
80 | #include
81 | ],
82 | [
83 | std::string s("hello");
84 | return s.length();
85 | ])],
86 | wx_cv_cc_broken_libstdcxx_visibility=no,
87 | wx_cv_cc_broken_libstdcxx_visibility=yes)
88 | AC_LANG_POP()
89 | CXXFLAGS="$wx_save_CXXFLAGS"
90 | LDFLAGS="$wx_save_LDFLAGS"])
91 | AC_MSG_RESULT([$wx_cv_cc_broken_libstdcxx_visibility])
92 |
93 | if test $wx_cv_cc_broken_libstdcxx_visibility = yes; then
94 | AC_MSG_CHECKING([whether we can work around it])
95 | AC_CACHE_VAL(wx_cv_cc_visibility_workaround, [
96 | AC_LANG_PUSH(C++)
97 | AC_LINK_IFELSE(
98 | [AC_LANG_PROGRAM(
99 | [
100 | #pragma GCC visibility push(default)
101 | #include
102 | #pragma GCC visibility pop
103 | ],
104 | [
105 | std::string s("hello");
106 | return s.length();
107 | ])],
108 | wx_cv_cc_visibility_workaround=no,
109 | wx_cv_cc_visibility_workaround=yes)
110 | AC_LANG_POP()
111 | ])
112 | AC_MSG_RESULT([$wx_cv_cc_visibility_workaround])
113 |
114 | if test $wx_cv_cc_visibility_workaround = no; then
115 | dnl we can't use visibility at all then
116 | wx_cv_cc_visibility=no
117 | fi
118 | fi
119 | fi
120 |
121 | if test $wx_cv_cc_visibility = yes; then
122 | AC_DEFINE([HAVE_VISIBILITY])
123 | if test $wx_cv_cc_broken_libstdcxx_visibility = yes; then
124 | AC_DEFINE([HAVE_BROKEN_LIBSTDCXX_VISIBILITY])
125 | fi
126 | else
127 | CFLAGS_VISIBILITY=""
128 | CXXFLAGS_VISIBILITY=""
129 | fi
130 | AC_SUBST([CFLAGS_VISIBILITY])
131 | AC_SUBST([CXXFLAGS_VISIBILITY])
132 | fi
133 | ])
134 |
--------------------------------------------------------------------------------