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