Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "internal/xml_parser.hxx"
31 : : #include "internal/i_xml_parser_event_handler.hxx"
32 : :
33 : : #include <assert.h>
34 : :
35 : : namespace /* private */
36 : : {
37 : :
38 : : /* Extracts the local part of tag without
39 : : namespace decoration e.g. meta:creator -> creator */
40 : : const XML_Char COLON = (XML_Char)':';
41 : :
42 : 0 : const XML_Char* get_local_name(const XML_Char* rawname)
43 : : {
44 : 0 : const XML_Char* p = rawname;
45 : :
46 : : // go to the end
47 : 0 : while (*p) p++;
48 : :
49 : : // go back until the first ':'
50 : 0 : while (*p != COLON && p > rawname)
51 : 0 : p--;
52 : :
53 : : // if we are on a colon one step forward
54 : 0 : if (*p == COLON)
55 : 0 : p++;
56 : :
57 : 0 : return p;
58 : : }
59 : :
60 : 0 : inline xml_parser* get_parser_instance(void* data)
61 : : {
62 : : return reinterpret_cast<xml_parser*>(XML_GetUserData(
63 : 0 : reinterpret_cast<XML_Parser>(data)));
64 : : }
65 : :
66 : 0 : bool has_only_whitespaces(const XML_Char* s, int len)
67 : : {
68 : 0 : const XML_Char* p = s;
69 : 0 : for (int i = 0; i < len; i++)
70 : 0 : if (*p++ != ' ') return false;
71 : 0 : return true;
72 : : }
73 : : }
74 : :
75 : 0 : xml_parser::xml_parser(const XML_Char* EncodingName) :
76 : : document_handler_(0),
77 : 0 : xml_parser_(XML_ParserCreate(EncodingName))
78 : : {
79 : 0 : init();
80 : 0 : }
81 : :
82 : 0 : xml_parser::~xml_parser()
83 : : {
84 : 0 : XML_ParserFree(xml_parser_);
85 : 0 : }
86 : :
87 : : /* Callback functions will be called by the parser on
88 : : different events */
89 : :
90 : : extern "C"
91 : : {
92 : :
93 : 0 : static void xml_start_element_handler(void* UserData, const XML_Char* name, const XML_Char** atts)
94 : : {
95 : : assert(UserData != NULL);
96 : :
97 : 0 : xml_parser* pImpl = get_parser_instance(UserData);
98 : :
99 : 0 : i_xml_parser_event_handler* pDocHdl = pImpl->get_document_handler();
100 : 0 : if (pDocHdl)
101 : : {
102 : 0 : xml_tag_attribute_container_t attributes;
103 : :
104 : 0 : int i = 0;
105 : :
106 : 0 : while(atts[i])
107 : : {
108 : 0 : attributes[reinterpret_cast<const char_t*>(get_local_name(atts[i]))] = reinterpret_cast<const char_t*>(atts[i+1]);
109 : 0 : i += 2; // skip to next pair
110 : : }
111 : :
112 : : pDocHdl->start_element(
113 : 0 : reinterpret_cast<const char_t*>(name), reinterpret_cast<const char_t*>(get_local_name(name)), attributes);
114 : : }
115 : 0 : }
116 : :
117 : 0 : static void xml_end_element_handler(void* UserData, const XML_Char* name)
118 : : {
119 : : assert(UserData);
120 : :
121 : 0 : xml_parser* pImpl = get_parser_instance(UserData);
122 : 0 : i_xml_parser_event_handler* pDocHdl = pImpl->get_document_handler();
123 : 0 : if (pDocHdl)
124 : 0 : pDocHdl->end_element(reinterpret_cast<const char_t*>(name), reinterpret_cast<const char_t*>(get_local_name(name)));
125 : 0 : }
126 : :
127 : 0 : static void xml_character_data_handler(void* UserData, const XML_Char* s, int len)
128 : : {
129 : : assert(UserData);
130 : :
131 : 0 : xml_parser* pImpl = get_parser_instance(UserData);
132 : 0 : i_xml_parser_event_handler* pDocHdl = pImpl->get_document_handler();
133 : 0 : if (pDocHdl)
134 : : {
135 : 0 : if (has_only_whitespaces(s,len))
136 : 0 : pDocHdl->ignore_whitespace(string_t(reinterpret_cast<const char_t*>(s), len));
137 : : else
138 : 0 : pDocHdl->characters(string_t(reinterpret_cast<const char_t*>(s), len));
139 : : }
140 : 0 : }
141 : :
142 : 0 : static void xml_comment_handler(void* UserData, const XML_Char* Data)
143 : : {
144 : : assert(UserData);
145 : :
146 : 0 : xml_parser* pImpl = get_parser_instance(UserData);
147 : 0 : i_xml_parser_event_handler* pDocHdl = pImpl->get_document_handler();
148 : 0 : if (pDocHdl)
149 : 0 : pDocHdl->comment(reinterpret_cast<const char_t*>(Data));
150 : 0 : }
151 : :
152 : : } // extern "C"
153 : :
154 : 0 : void xml_parser::init()
155 : : {
156 : 0 : XML_SetUserData(xml_parser_, this);
157 : :
158 : : // we use the parser as handler argument,
159 : : // so we could use it if necessary, the
160 : : // UserData are usable anyway using
161 : : // XML_GetUserData(...)
162 : 0 : XML_UseParserAsHandlerArg(xml_parser_);
163 : :
164 : : XML_SetElementHandler(
165 : : xml_parser_,
166 : : xml_start_element_handler,
167 : 0 : xml_end_element_handler);
168 : :
169 : : XML_SetCharacterDataHandler(
170 : : xml_parser_,
171 : 0 : xml_character_data_handler);
172 : :
173 : : XML_SetCommentHandler(
174 : : xml_parser_,
175 : 0 : xml_comment_handler);
176 : 0 : }
177 : :
178 : 0 : void xml_parser::parse(const char* XmlData, size_t Length, bool IsFinal)
179 : : {
180 : 0 : if (XML_STATUS_ERROR ==
181 : 0 : XML_Parse(xml_parser_, XmlData, static_cast<int>(Length), IsFinal))
182 : : {
183 : : throw xml_parser_exception(
184 : 0 : (char*)XML_ErrorString(XML_GetErrorCode(xml_parser_)),
185 : 0 : (int)XML_GetErrorCode(xml_parser_),
186 : 0 : XML_GetCurrentLineNumber(xml_parser_),
187 : 0 : XML_GetCurrentColumnNumber(xml_parser_),
188 : 0 : XML_GetCurrentByteIndex(xml_parser_));
189 : : }
190 : 0 : }
191 : :
192 : 0 : void xml_parser::set_document_handler(
193 : : i_xml_parser_event_handler* event_handler)
194 : : {
195 : 0 : document_handler_ = event_handler;
196 : 0 : }
197 : :
198 : 0 : i_xml_parser_event_handler* xml_parser::get_document_handler() const
199 : : {
200 : 0 : return document_handler_;
201 : : }
202 : :
203 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|