Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <documentbuilder.hxx>
21 :
22 : #include <string.h>
23 : #include <stdio.h>
24 : #include <stdarg.h>
25 :
26 : #include <libxml/xmlerror.h>
27 : #include <libxml/tree.h>
28 :
29 : #include <boost/shared_ptr.hpp>
30 :
31 : #include <rtl/alloc.h>
32 : #include <rtl/ustrbuf.hxx>
33 :
34 : #include <comphelper/processfactory.hxx>
35 : #include <cppuhelper/implbase1.hxx>
36 : #include <cppuhelper/supportsservice.hxx>
37 :
38 : #include <com/sun/star/xml/sax/SAXParseException.hpp>
39 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
40 : #include <com/sun/star/task/XInteractionHandler.hpp>
41 :
42 : #include <ucbhelper/content.hxx>
43 : #include <ucbhelper/commandenvironment.hxx>
44 :
45 : #include <node.hxx>
46 : #include <document.hxx>
47 :
48 : using namespace css::io;
49 : using namespace css::lang;
50 : using namespace css::ucb;
51 : using namespace css::uno;
52 : using namespace css::xml::dom;
53 : using namespace css::xml::sax;
54 : using namespace ucbhelper;
55 : using css::task::XInteractionHandler;
56 : using css::xml::sax::InputSource;
57 :
58 :
59 : namespace DOM
60 : {
61 :
62 692 : class CDefaultEntityResolver : public cppu::WeakImplHelper1< XEntityResolver >
63 : {
64 : public:
65 0 : virtual InputSource SAL_CALL resolveEntity( const OUString& sPublicId, const OUString& sSystemId )
66 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
67 : {
68 0 : InputSource is;
69 0 : is.sPublicId = sPublicId;
70 0 : is.sSystemId = sSystemId;
71 0 : is.sEncoding = OUString();
72 :
73 : try {
74 : Reference< XCommandEnvironment > aEnvironment(
75 : new CommandEnvironment(Reference< XInteractionHandler >(),
76 0 : Reference< XProgressHandler >() ));
77 0 : Content aContent(sSystemId, aEnvironment, comphelper::getProcessComponentContext());
78 :
79 0 : is.aInputStream = aContent.openStream();
80 0 : } catch (const css::uno::Exception&) {
81 : OSL_FAIL("exception in default entity resolver");
82 0 : is.aInputStream.clear();
83 : }
84 0 : return is;
85 : }
86 :
87 : };
88 :
89 232 : CDocumentBuilder::CDocumentBuilder(
90 : Reference< XMultiServiceFactory > const& xFactory)
91 : : m_xFactory(xFactory)
92 232 : , m_xEntityResolver(new CDefaultEntityResolver())
93 : {
94 : // init libxml. libxml will protect itself against multiple
95 : // initializations so there is no problem here if this gets
96 : // called multiple times.
97 232 : xmlInitParser();
98 232 : }
99 :
100 232 : Reference< XInterface > CDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
101 : {
102 232 : return static_cast< XDocumentBuilder* >(new CDocumentBuilder(rSMgr));
103 : }
104 :
105 : const char* CDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.DocumentBuilder";
106 : const char* CDocumentBuilder::aSupportedServiceNames[] = {
107 : "com.sun.star.xml.dom.DocumentBuilder",
108 : NULL
109 : };
110 :
111 810 : OUString CDocumentBuilder::_getImplementationName()
112 : {
113 810 : return OUString::createFromAscii(aImplementationName);
114 : }
115 232 : Sequence<OUString> CDocumentBuilder::_getSupportedServiceNames()
116 : {
117 232 : Sequence<OUString> aSequence;
118 464 : for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
119 232 : aSequence.realloc(i+1);
120 232 : aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
121 : }
122 232 : return aSequence;
123 : }
124 :
125 0 : Sequence< OUString > SAL_CALL CDocumentBuilder::getSupportedServiceNames()
126 : throw (RuntimeException, std::exception)
127 : {
128 0 : return CDocumentBuilder::_getSupportedServiceNames();
129 : }
130 :
131 0 : OUString SAL_CALL CDocumentBuilder::getImplementationName()
132 : throw (RuntimeException, std::exception)
133 : {
134 0 : return CDocumentBuilder::_getImplementationName();
135 : }
136 :
137 0 : sal_Bool SAL_CALL CDocumentBuilder::supportsService(const OUString& aServiceName)
138 : throw (RuntimeException, std::exception)
139 : {
140 0 : return cppu::supportsService(this, aServiceName);
141 : }
142 :
143 2 : Reference< XDOMImplementation > SAL_CALL CDocumentBuilder::getDOMImplementation()
144 : throw (RuntimeException, std::exception)
145 : {
146 :
147 2 : return Reference< XDOMImplementation >();
148 : }
149 :
150 48 : sal_Bool SAL_CALL CDocumentBuilder::isNamespaceAware()
151 : throw (RuntimeException, std::exception)
152 : {
153 48 : return sal_True;
154 : }
155 :
156 2 : sal_Bool SAL_CALL CDocumentBuilder::isValidating()
157 : throw (RuntimeException, std::exception)
158 : {
159 2 : return sal_False;
160 : }
161 :
162 18472 : Reference< XDocument > SAL_CALL CDocumentBuilder::newDocument()
163 : throw (RuntimeException, std::exception)
164 : {
165 18472 : ::osl::MutexGuard const g(m_Mutex);
166 :
167 : // create a new document
168 18472 : xmlDocPtr pDocument = xmlNewDoc((const xmlChar*)"1.0");
169 : Reference< XDocument > const xRet(
170 18472 : CDocument::CreateCDocument(pDocument).get());
171 18472 : return xRet;
172 : }
173 :
174 2 : static OUString make_error_message(xmlParserCtxtPtr ctxt)
175 : {
176 2 : OUStringBuffer buf;
177 2 : buf.appendAscii(ctxt->lastError.message);
178 2 : buf.appendAscii("Line: ");
179 2 : buf.append(static_cast<sal_Int32>(ctxt->lastError.line));
180 2 : buf.appendAscii("\nColumn: ");
181 2 : buf.append(static_cast<sal_Int32>(ctxt->lastError.int2));
182 2 : OUString msg = buf.makeStringAndClear();
183 2 : return msg;
184 : }
185 :
186 : // -- callbacks and context struct for parsing from stream
187 : // -- c-linkage, so the callbacks can be used by libxml
188 : extern "C" {
189 :
190 : // context struct passed to IO functions
191 22628 : typedef struct context {
192 : CDocumentBuilder *pBuilder;
193 : Reference< XInputStream > rInputStream;
194 : bool close;
195 : bool freeOnClose;
196 : } context_t;
197 :
198 30176 : static int xmlIO_read_func( void *context, char *buffer, int len)
199 : {
200 : // get the context...
201 30176 : context_t *pctx = static_cast<context_t*>(context);
202 30176 : if (!pctx->rInputStream.is())
203 0 : return -1;
204 : try {
205 : // try to read the requested number of bytes
206 30176 : Sequence< sal_Int8 > chunk(len);
207 30176 : int nread = pctx->rInputStream->readBytes(chunk, len);
208 :
209 : // copy bytes to the provided buffer
210 30176 : memcpy(buffer, chunk.getConstArray(), nread);
211 30176 : return nread;
212 0 : } catch (const css::uno::Exception& ex) {
213 : (void) ex;
214 : OSL_FAIL(OUStringToOString(ex.Message, RTL_TEXTENCODING_UTF8).getStr());
215 0 : return -1;
216 : }
217 : }
218 :
219 11314 : static int xmlIO_close_func(void* context)
220 : {
221 : // get the context...
222 11314 : context_t *pctx = static_cast<context_t*>(context);
223 11314 : if (!pctx->rInputStream.is())
224 0 : return 0;
225 : try
226 : {
227 11314 : if (pctx->close)
228 0 : pctx->rInputStream->closeInput();
229 11314 : if (pctx->freeOnClose)
230 0 : delete pctx;
231 11314 : return 0;
232 0 : } catch (const css::uno::Exception& ex) {
233 : (void) ex;
234 : OSL_FAIL(OUStringToOString(ex.Message, RTL_TEXTENCODING_UTF8).getStr());
235 0 : return -1;
236 : }
237 : }
238 :
239 0 : static xmlParserInputPtr resolve_func(void *ctx,
240 : const xmlChar *publicId,
241 : const xmlChar *systemId)
242 : {
243 : // get the CDocumentBuilder object
244 0 : xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr)ctx;
245 0 : CDocumentBuilder *builder = static_cast< CDocumentBuilder* >(ctxt->_private);
246 0 : Reference< XEntityResolver > resolver = builder->getEntityResolver();
247 0 : OUString sysid;
248 0 : if (systemId != 0)
249 0 : sysid = OUString((sal_Char*)systemId, strlen((char*)systemId), RTL_TEXTENCODING_UTF8);
250 0 : OUString pubid;
251 0 : if (publicId != 0)
252 0 : pubid = OUString((sal_Char*)publicId, strlen((char*)publicId), RTL_TEXTENCODING_UTF8);
253 :
254 : // resolve the entity
255 0 : InputSource src = resolver->resolveEntity(pubid, sysid);
256 :
257 : // create IO context on heap because this call will no longer be on the stack
258 : // when IO is actually performed through the callbacks. The close function must
259 : // free the memory which is indicated by the freeOnClose field in the context struct
260 0 : context_t *c = new context_t;
261 0 : c->pBuilder = builder;
262 0 : c->rInputStream = src.aInputStream;
263 0 : c->close = true;
264 0 : c->freeOnClose = true;
265 :
266 : // set up the inputBuffer and inputPtr for libxml
267 : xmlParserInputBufferPtr pBuffer =
268 0 : xmlParserInputBufferCreateIO(xmlIO_read_func, xmlIO_close_func, c, XML_CHAR_ENCODING_NONE);
269 : xmlParserInputPtr pInput =
270 0 : xmlNewIOInputStream(ctxt, pBuffer, XML_CHAR_ENCODING_NONE);
271 0 : return pInput;
272 : }
273 :
274 : #if 0
275 : static xmlParserInputPtr external_entity_loader(const char *URL, const char * /*ID*/, xmlParserCtxtPtr ctxt)
276 : {
277 : // just call our resolver function using the URL as systemId
278 : return resolve_func(ctxt, 0, (const xmlChar*)URL);
279 : }
280 : #endif
281 :
282 : // default warning handler does not trigger assertion
283 52 : static void warning_func(void * ctx, const char * /*msg*/, ...)
284 : {
285 : SAL_INFO(
286 : "unoxml",
287 : "libxml2 warning: "
288 : << make_error_message(static_cast<xmlParserCtxtPtr>(ctx)));
289 52 : }
290 :
291 : // default error handler triggers assertion
292 0 : static void error_func(void * ctx, const char * /*msg*/, ...)
293 : {
294 : SAL_WARN(
295 : "unoxml",
296 : "libxml2 error: "
297 : << make_error_message(static_cast<xmlParserCtxtPtr>(ctx)));
298 0 : }
299 :
300 : } // extern "C"
301 :
302 2 : void throwEx(xmlParserCtxtPtr ctxt)
303 : {
304 2 : css::xml::sax::SAXParseException saxex;
305 2 : saxex.Message = make_error_message(ctxt);
306 2 : saxex.LineNumber = static_cast<sal_Int32>(ctxt->lastError.line);
307 2 : saxex.ColumnNumber = static_cast<sal_Int32>(ctxt->lastError.int2);
308 2 : throw saxex;
309 : }
310 :
311 11316 : Reference< XDocument > SAL_CALL CDocumentBuilder::parse(const Reference< XInputStream >& is)
312 : throw (RuntimeException, SAXParseException, IOException, std::exception)
313 : {
314 11316 : if (!is.is()) {
315 2 : throw RuntimeException();
316 : }
317 :
318 11314 : ::osl::MutexGuard const g(m_Mutex);
319 :
320 : ::boost::shared_ptr<xmlParserCtxt> const pContext(
321 22628 : xmlNewParserCtxt(), xmlFreeParserCtxt);
322 :
323 : // register error functions to prevent errors being printed
324 : // on the console
325 11314 : pContext->_private = this;
326 11314 : pContext->sax->error = error_func;
327 11314 : pContext->sax->warning = warning_func;
328 11314 : pContext->sax->resolveEntity = resolve_func;
329 :
330 : // IO context struct
331 22628 : context_t c;
332 11314 : c.pBuilder = this;
333 11314 : c.rInputStream = is;
334 : // we did not open the stream, thus we do not close it.
335 11314 : c.close = false;
336 11314 : c.freeOnClose = false;
337 : xmlDocPtr const pDoc = xmlCtxtReadIO(pContext.get(),
338 11314 : xmlIO_read_func, xmlIO_close_func, &c, 0, 0, 0);
339 :
340 11314 : if (pDoc == 0) {
341 0 : throwEx(pContext.get());
342 : }
343 : Reference< XDocument > const xRet(
344 11314 : CDocument::CreateCDocument(pDoc).get());
345 22628 : return xRet;
346 : }
347 :
348 888 : Reference< XDocument > SAL_CALL CDocumentBuilder::parseURI(const OUString& sUri)
349 : throw (RuntimeException, SAXParseException, IOException, std::exception)
350 : {
351 888 : ::osl::MutexGuard const g(m_Mutex);
352 :
353 : ::boost::shared_ptr<xmlParserCtxt> const pContext(
354 1776 : xmlNewParserCtxt(), xmlFreeParserCtxt);
355 888 : pContext->_private = this;
356 888 : pContext->sax->error = error_func;
357 888 : pContext->sax->warning = warning_func;
358 888 : pContext->sax->resolveEntity = resolve_func;
359 : // xmlSetExternalEntityLoader(external_entity_loader);
360 1776 : OString oUri = OUStringToOString(sUri, RTL_TEXTENCODING_UTF8);
361 888 : char *uri = (char*) oUri.getStr();
362 888 : xmlDocPtr pDoc = xmlCtxtReadFile(pContext.get(), uri, 0, 0);
363 888 : if (pDoc == 0) {
364 2 : throwEx(pContext.get());
365 : }
366 : Reference< XDocument > const xRet(
367 886 : CDocument::CreateCDocument(pDoc).get());
368 1774 : return xRet;
369 : }
370 :
371 : void SAL_CALL
372 2 : CDocumentBuilder::setEntityResolver(Reference< XEntityResolver > const& xER)
373 : throw (RuntimeException, std::exception)
374 : {
375 2 : ::osl::MutexGuard const g(m_Mutex);
376 :
377 2 : m_xEntityResolver = xER;
378 2 : }
379 :
380 0 : Reference< XEntityResolver > SAL_CALL CDocumentBuilder::getEntityResolver()
381 : throw (RuntimeException)
382 : {
383 0 : ::osl::MutexGuard const g(m_Mutex);
384 :
385 0 : return m_xEntityResolver;
386 : }
387 :
388 : void SAL_CALL
389 6 : CDocumentBuilder::setErrorHandler(Reference< XErrorHandler > const& xEH)
390 : throw (RuntimeException, std::exception)
391 : {
392 6 : ::osl::MutexGuard const g(m_Mutex);
393 :
394 6 : m_xErrorHandler = xEH;
395 6 : }
396 : }
397 :
398 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|