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