summaryrefslogtreecommitdiff
path: root/compiled/XMLParserWrapper.h
blob: 86a76c15ab7d476045491ab1b7a0aa27abb49d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2011  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef XMLPARSER_WRAPPER_H
#define XMLPARSER_WRAPPER_H

#include <string>
#include <memory>
#include <bindings/kolabformat-xcal.hxx>
#include <xsd/cxx/tree/error-handler.hxx>
#include <boost/scoped_ptr.hpp>
#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>

#if _XERCES_VERSION >= 30000
#  include <xercesc/dom/DOMLSParser.hpp>
#  include <xercesc/dom/DOMLSException.hpp>
#else
#  include <xercesc/dom/DOMBuilder.hpp>
#endif

#include <xercesc/framework/XMLGrammarPool.hpp>

/**
 * This wrapper controls the lifetime of the parser object.
 * 
 * Initializing the parser is much more expensive than parsing a single XML document, therefore the parser should be reused if possible.
 * 
 * It might make sense to use a singleton internally to keep the parser alive between usages. Alternatively this object can be kept alive for as long as it makes sense.
 * 
 * This class also encapsulates the initialization of the whole parser, which must be done manually because precomiled schemas are used (which greatly improves the initialization performance).
 * 
 * Writing the document is static and doesn't need any initialization and is therefore not wrapped by this object.
 * 
 */

class XMLParserWrapper {
public:
    XMLParserWrapper();
    ~XMLParserWrapper();
    
    /**
     * Threadsafe singleton. One Xerces instance is created per thread (threadlocal).
     * Access via singleton to reuse parser.
     */
    static XMLParserWrapper &inst();
    
    xml_schema::dom::auto_ptr<xercesc::DOMDocument> parseFile(const std::string &url);
    xml_schema::dom::auto_ptr<xercesc::DOMDocument> parseString(const std::string &s);
    xml_schema::dom::auto_ptr<xercesc::DOMDocument> parse(std::istream &ifs, const std::string &name);
private:
    void init();
    xsd::cxx::tree::error_handler<char> eh;
    xsd::cxx::xml::dom::bits::error_handler_proxy<char> ehp;

    #if _XERCES_VERSION >= 30000
        xercesc::DOMLSParser  *parser;
    #else
        xercesc::DOMBuilder  *parser;
    #endif
    xercesc::XMLGrammarPool *gp;
};


#endif