xref: /AOO41X/main/unoxml/source/xpath/xpathapi.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 <xpathapi.hxx>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <stdarg.h>
31*cdf0e10cSrcweir #include <string.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <libxml/tree.h>
34*cdf0e10cSrcweir #include <libxml/xmlerror.h>
35*cdf0e10cSrcweir #include <libxml/xpath.h>
36*cdf0e10cSrcweir #include <libxml/xpathInternals.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <nodelist.hxx>
41*cdf0e10cSrcweir #include <xpathobject.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "../dom/node.hxx"
44*cdf0e10cSrcweir #include "../dom/document.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace XPath
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 	// factory
53*cdf0e10cSrcweir     Reference< XInterface > CXPathAPI::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         return Reference< XInterface >(static_cast<XXPathAPI*>(new CXPathAPI(rSMgr)));
56*cdf0e10cSrcweir     }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	// ctor
59*cdf0e10cSrcweir     CXPathAPI::CXPathAPI(const Reference< XMultiServiceFactory >& rSMgr)
60*cdf0e10cSrcweir         : m_aFactory(rSMgr)
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir     }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     const char* CXPathAPI::aImplementationName = "com.sun.star.comp.xml.xpath.XPathAPI";
65*cdf0e10cSrcweir     const char* CXPathAPI::aSupportedServiceNames[] = {
66*cdf0e10cSrcweir         "com.sun.star.xml.xpath.XPathAPI",
67*cdf0e10cSrcweir         NULL
68*cdf0e10cSrcweir     };
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     OUString CXPathAPI::_getImplementationName()
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir 	    return OUString::createFromAscii(aImplementationName);
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     Sequence<OUString> CXPathAPI::_getSupportedServiceNames()
76*cdf0e10cSrcweir     {
77*cdf0e10cSrcweir 	    Sequence<OUString> aSequence;
78*cdf0e10cSrcweir 	    for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
79*cdf0e10cSrcweir 		    aSequence.realloc(i+1);
80*cdf0e10cSrcweir 		    aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
81*cdf0e10cSrcweir 	    }
82*cdf0e10cSrcweir 	    return aSequence;
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     Sequence< OUString > SAL_CALL CXPathAPI::getSupportedServiceNames()
86*cdf0e10cSrcweir         throw (RuntimeException)
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         return CXPathAPI::_getSupportedServiceNames();
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     OUString SAL_CALL CXPathAPI::getImplementationName()
92*cdf0e10cSrcweir         throw (RuntimeException)
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         return CXPathAPI::_getImplementationName();
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     sal_Bool SAL_CALL CXPathAPI::supportsService(const OUString& aServiceName)
98*cdf0e10cSrcweir         throw (RuntimeException)
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         Sequence< OUString > supported = CXPathAPI::_getSupportedServiceNames();
101*cdf0e10cSrcweir         for (sal_Int32 i=0; i<supported.getLength(); i++)
102*cdf0e10cSrcweir         {
103*cdf0e10cSrcweir             if (supported[i] == aServiceName) return sal_True;
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir         return sal_False;
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     // -------------------------------------------------------------------
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     void SAL_CALL CXPathAPI::registerNS(
111*cdf0e10cSrcweir 			const OUString& aPrefix,
112*cdf0e10cSrcweir 			const OUString& aURI)
113*cdf0e10cSrcweir         throw (RuntimeException)
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_Mutex);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         m_nsmap.insert(nsmap_t::value_type(aPrefix, aURI));
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     void SAL_CALL CXPathAPI::unregisterNS(
121*cdf0e10cSrcweir 			const OUString& aPrefix,
122*cdf0e10cSrcweir 			const OUString& aURI)
123*cdf0e10cSrcweir         throw (RuntimeException)
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_Mutex);
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         if ((m_nsmap.find(aPrefix))->second.equals(aURI)) {
128*cdf0e10cSrcweir             m_nsmap.erase(aPrefix);
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	// register all namespaces stored in the namespace list for this object
133*cdf0e10cSrcweir 	// with the current xpath evaluation context
134*cdf0e10cSrcweir     static void lcl_registerNamespaces(
135*cdf0e10cSrcweir 			xmlXPathContextPtr ctx,
136*cdf0e10cSrcweir 			const nsmap_t& nsmap)
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         nsmap_t::const_iterator i = nsmap.begin();
139*cdf0e10cSrcweir         OString oprefix, ouri;
140*cdf0e10cSrcweir         xmlChar *p, *u;
141*cdf0e10cSrcweir         while (i != nsmap.end())
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             oprefix = OUStringToOString(i->first,  RTL_TEXTENCODING_UTF8);
144*cdf0e10cSrcweir             ouri    = OUStringToOString(i->second, RTL_TEXTENCODING_UTF8);
145*cdf0e10cSrcweir             p = (xmlChar*)oprefix.getStr();
146*cdf0e10cSrcweir             u = (xmlChar*)ouri.getStr();
147*cdf0e10cSrcweir             xmlXPathRegisterNs(ctx, p, u);
148*cdf0e10cSrcweir             i++;
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     // get all ns decls on a node (and parent nodes, if any)
153*cdf0e10cSrcweir     static void lcl_collectNamespaces(
154*cdf0e10cSrcweir             nsmap_t & rNamespaces, Reference< XNode > const& xNamespaceNode)
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         DOM::CNode *const pCNode(DOM::CNode::GetImplementation(xNamespaceNode));
157*cdf0e10cSrcweir         if (!pCNode) { throw RuntimeException(); }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         ::osl::MutexGuard const g(pCNode->GetOwnerDocument().GetMutex());
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         xmlNodePtr pNode = pCNode->GetNodePtr();
162*cdf0e10cSrcweir 		while (pNode != 0) {
163*cdf0e10cSrcweir 			xmlNsPtr curDef = pNode->nsDef;
164*cdf0e10cSrcweir 			while (curDef != 0) {
165*cdf0e10cSrcweir 				const xmlChar* xHref = curDef->href;
166*cdf0e10cSrcweir 				OUString aURI((sal_Char*)xHref, strlen((char*)xHref), RTL_TEXTENCODING_UTF8);
167*cdf0e10cSrcweir 				const xmlChar* xPre = curDef->prefix;
168*cdf0e10cSrcweir 				OUString aPrefix((sal_Char*)xPre, strlen((char*)xPre), RTL_TEXTENCODING_UTF8);
169*cdf0e10cSrcweir                 // we could already have this prefix from a child node
170*cdf0e10cSrcweir                 if (rNamespaces.find(aPrefix) == rNamespaces.end())
171*cdf0e10cSrcweir                 {
172*cdf0e10cSrcweir                     rNamespaces.insert(::std::make_pair(aPrefix, aURI));
173*cdf0e10cSrcweir                 }
174*cdf0e10cSrcweir 				curDef = curDef->next;
175*cdf0e10cSrcweir 			}
176*cdf0e10cSrcweir 			pNode = pNode->parent;
177*cdf0e10cSrcweir 		}
178*cdf0e10cSrcweir 	}
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     static void lcl_collectRegisterNamespaces(
181*cdf0e10cSrcweir             CXPathAPI & rAPI, Reference< XNode > const& xNamespaceNode)
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         nsmap_t namespaces;
184*cdf0e10cSrcweir         lcl_collectNamespaces(namespaces, xNamespaceNode);
185*cdf0e10cSrcweir         for (nsmap_t::const_iterator iter = namespaces.begin();
186*cdf0e10cSrcweir                 iter != namespaces.end(); ++iter)
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             rAPI.registerNS(iter->first, iter->second);
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	// register function and variable lookup functions with the current
193*cdf0e10cSrcweir 	// xpath evaluation context
194*cdf0e10cSrcweir     static void lcl_registerExtensions(
195*cdf0e10cSrcweir 			xmlXPathContextPtr ctx,
196*cdf0e10cSrcweir 			const extensions_t& extensions)
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         extensions_t::const_iterator i = extensions.begin();
199*cdf0e10cSrcweir         while (i != extensions.end())
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             Libxml2ExtensionHandle aHandle = (*i)->getLibxml2ExtensionHandle();
202*cdf0e10cSrcweir 			if ( aHandle.functionLookupFunction != 0 )
203*cdf0e10cSrcweir 			{
204*cdf0e10cSrcweir                 xmlXPathRegisterFuncLookup(ctx,
205*cdf0e10cSrcweir                     reinterpret_cast<xmlXPathFuncLookupFunc>(
206*cdf0e10cSrcweir 						sal::static_int_cast<sal_IntPtr>(aHandle.functionLookupFunction)),
207*cdf0e10cSrcweir                     reinterpret_cast<void*>(
208*cdf0e10cSrcweir 						sal::static_int_cast<sal_IntPtr>(aHandle.functionData)));
209*cdf0e10cSrcweir 			}
210*cdf0e10cSrcweir 			if ( aHandle.variableLookupFunction != 0 )
211*cdf0e10cSrcweir 			{
212*cdf0e10cSrcweir                 xmlXPathRegisterVariableLookup(ctx,
213*cdf0e10cSrcweir                     reinterpret_cast<xmlXPathVariableLookupFunc>(
214*cdf0e10cSrcweir 						sal::static_int_cast<sal_IntPtr>(aHandle.variableLookupFunction)),
215*cdf0e10cSrcweir                     reinterpret_cast<void*>(
216*cdf0e10cSrcweir 						sal::static_int_cast<sal_IntPtr>(aHandle.variableData)));
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir             i++;
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     /**
223*cdf0e10cSrcweir      * Use an XPath string to select a nodelist.
224*cdf0e10cSrcweir      */
225*cdf0e10cSrcweir     Reference< XNodeList > SAL_CALL CXPathAPI::selectNodeList(
226*cdf0e10cSrcweir             const Reference< XNode >& contextNode,
227*cdf0e10cSrcweir             const OUString& expr)
228*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir 		Reference< XXPathObject > xobj = eval(contextNode, expr);
231*cdf0e10cSrcweir 		return xobj->getNodeList();
232*cdf0e10cSrcweir     }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir     /**
235*cdf0e10cSrcweir      * same as selectNodeList but registers all name space decalratiosn found on namespaceNode
236*cdf0e10cSrcweir      */
237*cdf0e10cSrcweir     Reference< XNodeList > SAL_CALL CXPathAPI::selectNodeListNS(
238*cdf0e10cSrcweir             const Reference< XNode >&  contextNode,
239*cdf0e10cSrcweir             const OUString& expr,
240*cdf0e10cSrcweir             const Reference< XNode >&  namespaceNode)
241*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         lcl_collectRegisterNamespaces(*this, namespaceNode);
244*cdf0e10cSrcweir         return selectNodeList(contextNode, expr);
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     /**
248*cdf0e10cSrcweir      * Same as selectNodeList but returns the first node (if any)
249*cdf0e10cSrcweir      */
250*cdf0e10cSrcweir     Reference< XNode > SAL_CALL CXPathAPI::selectSingleNode(
251*cdf0e10cSrcweir             const Reference< XNode >& contextNode,
252*cdf0e10cSrcweir             const OUString& expr)
253*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         Reference< XNodeList > aList = selectNodeList(contextNode, expr);
256*cdf0e10cSrcweir         Reference< XNode > aNode = aList->item(0);
257*cdf0e10cSrcweir         return aNode;
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     /**
261*cdf0e10cSrcweir 	 * Same as selectSingleNode but registers all namespaces declared on
262*cdf0e10cSrcweir      * namespaceNode
263*cdf0e10cSrcweir      */
264*cdf0e10cSrcweir     Reference< XNode > SAL_CALL CXPathAPI::selectSingleNodeNS(
265*cdf0e10cSrcweir             const Reference< XNode >& contextNode,
266*cdf0e10cSrcweir             const OUString& expr,
267*cdf0e10cSrcweir             const Reference< XNode >&  namespaceNode )
268*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
269*cdf0e10cSrcweir     {
270*cdf0e10cSrcweir         lcl_collectRegisterNamespaces(*this, namespaceNode);
271*cdf0e10cSrcweir         return selectSingleNode(contextNode, expr);
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir     static OUString make_error_message(xmlErrorPtr pError)
275*cdf0e10cSrcweir     {
276*cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
277*cdf0e10cSrcweir         if (pError->message) {
278*cdf0e10cSrcweir             buf.appendAscii(pError->message);
279*cdf0e10cSrcweir         }
280*cdf0e10cSrcweir         int line = pError->line;
281*cdf0e10cSrcweir         if (line) {
282*cdf0e10cSrcweir             buf.appendAscii("Line: ");
283*cdf0e10cSrcweir             buf.append(static_cast<sal_Int32>(line));
284*cdf0e10cSrcweir             buf.appendAscii("\n");
285*cdf0e10cSrcweir         }
286*cdf0e10cSrcweir         int column = pError->int2;
287*cdf0e10cSrcweir         if (column) {
288*cdf0e10cSrcweir             buf.appendAscii("Column: ");
289*cdf0e10cSrcweir             buf.append(static_cast<sal_Int32>(column));
290*cdf0e10cSrcweir             buf.appendAscii("\n");
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir         OUString msg = buf.makeStringAndClear();
293*cdf0e10cSrcweir         return msg;
294*cdf0e10cSrcweir     }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     extern "C" {
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir         static void generic_error_func(void *userData, const char *format, ...)
299*cdf0e10cSrcweir         {
300*cdf0e10cSrcweir             (void) userData;
301*cdf0e10cSrcweir             char str[1000];
302*cdf0e10cSrcweir             va_list args;
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir             va_start(args, format);
305*cdf0e10cSrcweir #ifdef _WIN32
306*cdf0e10cSrcweir #define vsnprintf _vsnprintf
307*cdf0e10cSrcweir #endif
308*cdf0e10cSrcweir             vsnprintf(str, sizeof(str), format, args);
309*cdf0e10cSrcweir             va_end(args);
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf(
312*cdf0e10cSrcweir                 OUString::createFromAscii("libxml2 error:\n"));
313*cdf0e10cSrcweir             buf.appendAscii(str);
314*cdf0e10cSrcweir             OString msg = OUStringToOString(buf.makeStringAndClear(),
315*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US);
316*cdf0e10cSrcweir             OSL_ENSURE(sal_False, msg.getStr());
317*cdf0e10cSrcweir         }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir         static void structured_error_func(void * userData, xmlErrorPtr error)
320*cdf0e10cSrcweir         {
321*cdf0e10cSrcweir             (void) userData;
322*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf(
323*cdf0e10cSrcweir                 OUString::createFromAscii("libxml2 error:\n"));
324*cdf0e10cSrcweir             if (error) {
325*cdf0e10cSrcweir                 buf.append(make_error_message(error));
326*cdf0e10cSrcweir             } else {
327*cdf0e10cSrcweir                 buf.append(OUString::createFromAscii("no error argument!"));
328*cdf0e10cSrcweir             }
329*cdf0e10cSrcweir             OString msg = OUStringToOString(buf.makeStringAndClear(),
330*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US);
331*cdf0e10cSrcweir             OSL_ENSURE(sal_False, msg.getStr());
332*cdf0e10cSrcweir         }
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir     } // extern "C"
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 	/**
337*cdf0e10cSrcweir 	 * evaluates an XPath string. relative XPath expressions are evaluated relative to
338*cdf0e10cSrcweir 	 * the context Node
339*cdf0e10cSrcweir 	 */
340*cdf0e10cSrcweir     Reference< XXPathObject > SAL_CALL CXPathAPI::eval(
341*cdf0e10cSrcweir             Reference< XNode > const& xContextNode,
342*cdf0e10cSrcweir 			const OUString& expr)
343*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
344*cdf0e10cSrcweir     {
345*cdf0e10cSrcweir         if (!xContextNode.is()) { throw RuntimeException(); }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir         nsmap_t nsmap;
348*cdf0e10cSrcweir         extensions_t extensions;
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir         {
351*cdf0e10cSrcweir             ::osl::MutexGuard const g(m_Mutex);
352*cdf0e10cSrcweir             nsmap = m_nsmap;
353*cdf0e10cSrcweir             extensions = m_extensions;
354*cdf0e10cSrcweir         }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir         // get the node and document
357*cdf0e10cSrcweir         ::rtl::Reference<DOM::CDocument> const pCDoc(
358*cdf0e10cSrcweir                 dynamic_cast<DOM::CDocument*>( DOM::CNode::GetImplementation(
359*cdf0e10cSrcweir                         xContextNode->getOwnerDocument())));
360*cdf0e10cSrcweir         if (!pCDoc.is()) { throw RuntimeException(); }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir         DOM::CNode *const pCNode = DOM::CNode::GetImplementation(xContextNode);
363*cdf0e10cSrcweir         if (!pCNode) { throw RuntimeException(); }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir         ::osl::MutexGuard const g(pCDoc->GetMutex()); // lock the document!
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir         xmlNodePtr const pNode = pCNode->GetNodePtr();
368*cdf0e10cSrcweir         if (!pNode) { throw RuntimeException(); }
369*cdf0e10cSrcweir         xmlDocPtr pDoc = pNode->doc;
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir         /* NB: workaround for #i87252#:
372*cdf0e10cSrcweir            libxml < 2.6.17 considers it an error if the context
373*cdf0e10cSrcweir            node is the empty document (i.e. its xpathCtx->doc has no
374*cdf0e10cSrcweir            children). libxml 2.6.17 does not consider it an error.
375*cdf0e10cSrcweir            Unfortunately, old libxml prints an error message to stderr,
376*cdf0e10cSrcweir            which (afaik) cannot be turned off in this case, so we handle it.
377*cdf0e10cSrcweir         */
378*cdf0e10cSrcweir         if (!pDoc->children) {
379*cdf0e10cSrcweir             throw XPathException();
380*cdf0e10cSrcweir         }
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir         /* Create xpath evaluation context */
383*cdf0e10cSrcweir         ::boost::shared_ptr<xmlXPathContext> const xpathCtx(
384*cdf0e10cSrcweir                 xmlXPathNewContext(pDoc), xmlXPathFreeContext);
385*cdf0e10cSrcweir         if (xpathCtx == NULL) { throw XPathException(); }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir         // set context node
388*cdf0e10cSrcweir         xpathCtx->node = pNode;
389*cdf0e10cSrcweir         // error handling
390*cdf0e10cSrcweir         xpathCtx->error = structured_error_func;
391*cdf0e10cSrcweir         xmlSetGenericErrorFunc(NULL, generic_error_func);
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 		// register namespaces and extension
394*cdf0e10cSrcweir         lcl_registerNamespaces(xpathCtx.get(), nsmap);
395*cdf0e10cSrcweir         lcl_registerExtensions(xpathCtx.get(), extensions);
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir 		/* run the query */
398*cdf0e10cSrcweir         OString o1 = OUStringToOString(expr, RTL_TEXTENCODING_UTF8);
399*cdf0e10cSrcweir         xmlChar *xStr = (xmlChar*)o1.getStr();
400*cdf0e10cSrcweir         ::boost::shared_ptr<xmlXPathObject> const xpathObj(
401*cdf0e10cSrcweir                 xmlXPathEval(xStr, xpathCtx.get()), xmlXPathFreeObject);
402*cdf0e10cSrcweir         if (0 == xpathObj) {
403*cdf0e10cSrcweir             // OSL_ENSURE(xpathCtx->lastError == NULL, xpathCtx->lastError->message);
404*cdf0e10cSrcweir             throw XPathException();
405*cdf0e10cSrcweir         }
406*cdf0e10cSrcweir         Reference<XXPathObject> const xObj(
407*cdf0e10cSrcweir                 new CXPathObject(pCDoc, pCDoc->GetMutex(), xpathObj));
408*cdf0e10cSrcweir         return xObj;
409*cdf0e10cSrcweir     }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 	/**
412*cdf0e10cSrcweir 	 * same as eval but registers all namespace declarations found on namespaceNode
413*cdf0e10cSrcweir 	 */
414*cdf0e10cSrcweir     Reference< XXPathObject > SAL_CALL CXPathAPI::evalNS(
415*cdf0e10cSrcweir 			const Reference< XNode >& contextNode,
416*cdf0e10cSrcweir 			const OUString& expr,
417*cdf0e10cSrcweir 			const Reference< XNode >& namespaceNode)
418*cdf0e10cSrcweir         throw (RuntimeException, XPathException)
419*cdf0e10cSrcweir     {
420*cdf0e10cSrcweir         lcl_collectRegisterNamespaces(*this, namespaceNode);
421*cdf0e10cSrcweir         return eval(contextNode, expr);
422*cdf0e10cSrcweir     }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir 	/**
425*cdf0e10cSrcweir 	 * uses the service manager to create an instance of the service denoted by aName.
426*cdf0e10cSrcweir 	 * If the returned object implements the XXPathExtension interface, it is added to the list
427*cdf0e10cSrcweir 	 * of extensions that are used when evaluating XPath strings with this XPathAPI instance
428*cdf0e10cSrcweir 	 */
429*cdf0e10cSrcweir     void SAL_CALL CXPathAPI::registerExtension(
430*cdf0e10cSrcweir 			const OUString& aName)
431*cdf0e10cSrcweir         throw (RuntimeException)
432*cdf0e10cSrcweir     {
433*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_Mutex);
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir         // get extension from service manager
436*cdf0e10cSrcweir         Reference< XXPathExtension > const xExtension(
437*cdf0e10cSrcweir                 m_aFactory->createInstance(aName), UNO_QUERY_THROW);
438*cdf0e10cSrcweir         m_extensions.push_back(xExtension);
439*cdf0e10cSrcweir     }
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 	/**
442*cdf0e10cSrcweir 	 * registers the given extension instance to be used by XPath evaluations performed through this
443*cdf0e10cSrcweir 	 * XPathAPI instance
444*cdf0e10cSrcweir 	 */
445*cdf0e10cSrcweir     void SAL_CALL CXPathAPI::registerExtensionInstance(
446*cdf0e10cSrcweir             Reference< XXPathExtension> const& xExtension)
447*cdf0e10cSrcweir         throw (RuntimeException)
448*cdf0e10cSrcweir     {
449*cdf0e10cSrcweir         if (!xExtension.is()) {
450*cdf0e10cSrcweir             throw RuntimeException();
451*cdf0e10cSrcweir         }
452*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_Mutex);
453*cdf0e10cSrcweir         m_extensions.push_back( xExtension );
454*cdf0e10cSrcweir     }
455*cdf0e10cSrcweir }
456