~ubuntu-branches/ubuntu/trusty/mapnik/trusty

« back to all changes in this revision

Viewing changes to src/libxml2_loader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2009-05-20 15:39:58 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090520153958-cf6z1ql9zva4y4dq
Tags: 0.6.0-1ubuntu1
* Merge from debian unstable (LP: #378819), remaining changes:
  - debian/control:
    + Change bdeps from python2.5-dev to python-all-dev (>= 2.5)
    + Change XS-Python-Version from 2.5 to >= 2.5
  - debian/rules:
    + Various changes to enable python2.5 and python2.6 builds
* debian/patches/libtool2_2.diff Dropped. Included upsteam.
* Removed quilt support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
using boost::property_tree::ptree;
38
38
using namespace std;
39
39
 
 
40
#define DEFAULT_OPTIONS (XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS | XML_PARSE_DTDLOAD)
 
41
 
40
42
namespace mapnik 
41
43
{
42
44
    class libxml2_loader : boost::noncopyable
43
45
    {
44
46
        public:
45
 
            libxml2_loader() :
46
 
                ctx_( 0 )
 
47
            libxml2_loader(const char *encoding = NULL, int options = DEFAULT_OPTIONS, const char *url = NULL) :
 
48
                ctx_( 0 ),
 
49
                encoding_( encoding ),
 
50
                options_( options ),
 
51
                url_( url )
47
52
            {
48
53
                LIBXML_TEST_VERSION;
 
54
                ctx_ = xmlNewParserCtxt();
 
55
                if ( ! ctx_ )
 
56
                {
 
57
                    throw std::runtime_error("Failed to create parser context.");
 
58
                }
49
59
            }
50
60
 
51
61
            ~libxml2_loader()
52
62
            {
53
 
                if (ctx_ && ctx_->myDoc)
54
 
                {
55
 
                    xmlFreeDoc( ctx_->myDoc );
56
 
                }
57
63
                if (ctx_)
58
64
                {
59
65
                    xmlFreeParserCtxt(ctx_);    
67
73
                    throw config_error(string("Could not load map file '") +
68
74
                            filename + "': File does not exist");
69
75
                }
70
 
                ctx_ = xmlCreateFileParserCtxt( filename.c_str() );
71
 
 
72
 
                if ( ! ctx_ ) 
73
 
                {
74
 
                    throw std::runtime_error("Failed to create parser context.");
75
 
                }
76
 
 
77
 
                ctx_->replaceEntities = true;
78
 
                ctx_->keepBlanks = false;
79
 
                xmlCtxtUseOptions( ctx_, XML_PARSE_NOERROR | XML_PARSE_NOENT | XML_PARSE_NOBLANKS |
80
 
                                         XML_PARSE_DTDLOAD);
81
 
                xmlParseDocument( ctx_ );
82
 
 
83
 
                if ( ! ctx_->wellFormed ) 
 
76
 
 
77
                xmlDocPtr doc = xmlCtxtReadFile(ctx_, filename.c_str(), encoding_, options_);
 
78
 
 
79
                if ( !doc )
84
80
                {
85
81
                    xmlError * error = xmlCtxtGetLastError( ctx_ );
86
82
                    std::ostringstream os;
109
105
                   << std::endl;
110
106
                   }
111
107
                 */
112
 
 
113
 
                xmlNode * root( 0 );
114
 
                root = xmlDocGetRootElement( ctx_->myDoc );
 
108
                load(doc, pt);
 
109
            }
 
110
 
 
111
            void load( const int fd, ptree & pt )
 
112
            {
 
113
                xmlDocPtr doc = xmlCtxtReadFd(ctx_, fd, url_, encoding_, options_);
 
114
                load(doc, pt);
 
115
            }
 
116
 
 
117
            void load_string( const std::string & buffer, ptree & pt )
 
118
            {
 
119
                xmlDocPtr doc = xmlCtxtReadMemory(ctx_, buffer.data(), buffer.length(), url_, encoding_, options_);
 
120
                load(doc, pt);
 
121
            }
 
122
 
 
123
            void load( const xmlDocPtr doc, ptree & pt )
 
124
            {
 
125
                if ( !doc )
 
126
                {
 
127
                    xmlError * error = xmlCtxtGetLastError( ctx_ );
 
128
                    std::ostringstream os;
 
129
                    os << "XML document not well formed";
 
130
                    if (error)
 
131
                    {
 
132
                        os << ": " << std::endl << error->message;
 
133
                    }
 
134
                    throw config_error(os.str());
 
135
                }
 
136
 
 
137
                xmlNode * root = xmlDocGetRootElement( doc );
115
138
                if ( ! root ) {
116
139
                    throw config_error("XML document is empty.");
117
140
                }
118
141
 
119
142
                populate_tree( root, pt );
120
 
 
121
143
            }
122
144
 
123
145
        private:
171
193
            }
172
194
 
173
195
            xmlParserCtxtPtr ctx_;
 
196
            const char *encoding_;
 
197
            int options_;
 
198
            const char *url_;
174
199
    };
175
200
 
176
201
    void read_xml2( std::string const & filename, boost::property_tree::ptree & pt)
178
203
        libxml2_loader loader;
179
204
        loader.load( filename, pt );
180
205
    }
 
206
    void read_xml2_string( std::string const & str, boost::property_tree::ptree & pt)
 
207
    {
 
208
        libxml2_loader loader;
 
209
        loader.load_string( str, pt );
 
210
    }
181
211
 
182
212
} // end of namespace mapnik