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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dp_descriptioninfoset.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "dp_resource.h"
34*cdf0e10cSrcweir #include "sal/config.h"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "comphelper/sequence.hxx"
37*cdf0e10cSrcweir #include "comphelper/makesequence.hxx"
38*cdf0e10cSrcweir #include "comphelper/processfactory.hxx"
39*cdf0e10cSrcweir #include "boost/optional.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/beans/Optional.hpp"
41*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiComponentFactory.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/lang/Locale.hpp"
43*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
44*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
45*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
46*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
47*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
48*cdf0e10cSrcweir #include "com/sun/star/xml/dom/DOMException.hpp"
49*cdf0e10cSrcweir #include "com/sun/star/xml/dom/XNode.hpp"
50*cdf0e10cSrcweir #include "com/sun/star/xml/dom/XNodeList.hpp"
51*cdf0e10cSrcweir #include "com/sun/star/xml/dom/XDocumentBuilder.hpp"
52*cdf0e10cSrcweir #include "com/sun/star/xml/xpath/XXPathAPI.hpp"
53*cdf0e10cSrcweir #include "com/sun/star/ucb/InteractiveAugmentedIOException.hpp"
54*cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
55*cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx"
56*cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
57*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
58*cdf0e10cSrcweir #include "rtl/ustring.h"
59*cdf0e10cSrcweir #include "rtl/ustring.hxx"
60*cdf0e10cSrcweir #include "sal/types.h"
61*cdf0e10cSrcweir #include "ucbhelper/content.hxx"
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir namespace {
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir namespace css = ::com::sun::star;
66*cdf0e10cSrcweir using css::uno::Reference;
67*cdf0e10cSrcweir using ::rtl::OUString;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir class EmptyNodeList: public ::cppu::WeakImplHelper1< css::xml::dom::XNodeList >
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir public:
72*cdf0e10cSrcweir     EmptyNodeList();
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     virtual ~EmptyNodeList();
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL getLength() throw (css::uno::RuntimeException);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL
79*cdf0e10cSrcweir     item(::sal_Int32 index) throw (css::uno::RuntimeException);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir private:
82*cdf0e10cSrcweir     EmptyNodeList(EmptyNodeList &); // not defined
83*cdf0e10cSrcweir     void operator =(EmptyNodeList &); // not defined
84*cdf0e10cSrcweir };
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir EmptyNodeList::EmptyNodeList() {}
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir EmptyNodeList::~EmptyNodeList() {}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir ::sal_Int32 EmptyNodeList::getLength() throw (css::uno::RuntimeException) {
91*cdf0e10cSrcweir     return 0;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir css::uno::Reference< css::xml::dom::XNode > EmptyNodeList::item(::sal_Int32)
95*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     throw css::uno::RuntimeException(
98*cdf0e10cSrcweir         ::rtl::OUString(
99*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
100*cdf0e10cSrcweir                 "bad EmptyNodeList com.sun.star.xml.dom.XNodeList.item call")),
101*cdf0e10cSrcweir         static_cast< ::cppu::OWeakObject * >(this));
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir ::rtl::OUString getNodeValue(
105*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > const & node)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     OSL_ASSERT(node.is());
108*cdf0e10cSrcweir     try {
109*cdf0e10cSrcweir         return node->getNodeValue();
110*cdf0e10cSrcweir     } catch (css::xml::dom::DOMException & e) {
111*cdf0e10cSrcweir         throw css::uno::RuntimeException(
112*cdf0e10cSrcweir             (::rtl::OUString(
113*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
114*cdf0e10cSrcweir                     "com.sun.star.xml.dom.DOMException: ")) +
115*cdf0e10cSrcweir              e.Message),
116*cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >());
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir /**The class uses the UCB to access the description.xml file in an
121*cdf0e10cSrcweir    extension. The UCB must have been initialized already. It also
122*cdf0e10cSrcweir    requires that the extension has already be unzipped to a particular
123*cdf0e10cSrcweir    location.
124*cdf0e10cSrcweir  */
125*cdf0e10cSrcweir class ExtensionDescription
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir public:
128*cdf0e10cSrcweir     /**throws an exception if the description.xml is not
129*cdf0e10cSrcweir 		available, cannot be read, does not contain the expected data,
130*cdf0e10cSrcweir 		or any other error occured. Therefore it shoult only be used with
131*cdf0e10cSrcweir 		new extensions.
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         Throws com::sun::star::uno::RuntimeException,
134*cdf0e10cSrcweir         com::sun::star::deployment::DeploymentException,
135*cdf0e10cSrcweir         dp_registry::backend::bundle::NoDescriptionException.
136*cdf0e10cSrcweir      */
137*cdf0e10cSrcweir     ExtensionDescription(
138*cdf0e10cSrcweir         const css::uno::Reference<css::uno::XComponentContext>& xContext,
139*cdf0e10cSrcweir         const ::rtl::OUString& installDir,
140*cdf0e10cSrcweir         const css::uno::Reference< css::ucb::XCommandEnvironment >& xCmdEnv);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	~ExtensionDescription();
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	css::uno::Reference<css::xml::dom::XNode> getRootElement() const
145*cdf0e10cSrcweir 	{
146*cdf0e10cSrcweir 		return m_xRoot;
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 	::rtl::OUString getExtensionRootUrl() const
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 		return m_sExtensionRootUrl;
152*cdf0e10cSrcweir 	}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir private:
156*cdf0e10cSrcweir 	css::uno::Reference<css::xml::dom::XNode> m_xRoot;
157*cdf0e10cSrcweir 	::rtl::OUString m_sExtensionRootUrl;
158*cdf0e10cSrcweir };
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir class NoDescriptionException
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir };
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir class FileDoesNotExistFilter
165*cdf0e10cSrcweir     : public ::cppu::WeakImplHelper2< css::ucb::XCommandEnvironment,
166*cdf0e10cSrcweir                                       css::task::XInteractionHandler >
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     //css::uno::Reference<css::task::XInteractionHandler> m_xHandler;
170*cdf0e10cSrcweir 	bool m_bExist;
171*cdf0e10cSrcweir 	css::uno::Reference< css::ucb::XCommandEnvironment > m_xCommandEnv;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir public:
174*cdf0e10cSrcweir     virtual ~FileDoesNotExistFilter();
175*cdf0e10cSrcweir 	FileDoesNotExistFilter(
176*cdf0e10cSrcweir 		const css::uno::Reference< css::ucb::XCommandEnvironment >& xCmdEnv);
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	bool exist();
179*cdf0e10cSrcweir     // XCommandEnvironment
180*cdf0e10cSrcweir     virtual css::uno::Reference<css::task::XInteractionHandler > SAL_CALL
181*cdf0e10cSrcweir     getInteractionHandler() throw (css::uno::RuntimeException);
182*cdf0e10cSrcweir     virtual css::uno::Reference<css::ucb::XProgressHandler >
183*cdf0e10cSrcweir     SAL_CALL getProgressHandler() throw (css::uno::RuntimeException);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     // XInteractionHandler
186*cdf0e10cSrcweir     virtual void SAL_CALL handle(
187*cdf0e10cSrcweir         css::uno::Reference<css::task::XInteractionRequest > const & xRequest )
188*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
189*cdf0e10cSrcweir };
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir ExtensionDescription::ExtensionDescription(
192*cdf0e10cSrcweir     const Reference<css::uno::XComponentContext>& xContext,
193*cdf0e10cSrcweir     const OUString& installDir,
194*cdf0e10cSrcweir     const Reference< css::ucb::XCommandEnvironment >& xCmdEnv)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     try {
197*cdf0e10cSrcweir         m_sExtensionRootUrl = installDir;
198*cdf0e10cSrcweir         //may throw ::com::sun::star::ucb::ContentCreationException
199*cdf0e10cSrcweir         //If there is no description.xml then ucb will start an interaction which
200*cdf0e10cSrcweir         //brings up a dialog.We want to prevent this. Therefore we wrap the xCmdEnv
201*cdf0e10cSrcweir         //and filter the respective exception out.
202*cdf0e10cSrcweir         OUString sDescriptionUri(installDir + OUSTR("/description.xml"));
203*cdf0e10cSrcweir         Reference<css::ucb::XCommandEnvironment> xFilter =
204*cdf0e10cSrcweir             static_cast<css::ucb::XCommandEnvironment*>(
205*cdf0e10cSrcweir                 new FileDoesNotExistFilter(xCmdEnv));
206*cdf0e10cSrcweir         ::ucbhelper::Content descContent(sDescriptionUri, xFilter);
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         //throws an com::sun::star::uno::Exception if the file is not available
209*cdf0e10cSrcweir         Reference<css::io::XInputStream> xIn;
210*cdf0e10cSrcweir         try
211*cdf0e10cSrcweir         {	//throws com.sun.star.ucb.InteractiveAugmentedIOException
212*cdf0e10cSrcweir             xIn = descContent.openStream();
213*cdf0e10cSrcweir         }
214*cdf0e10cSrcweir         catch (css::uno::Exception& )
215*cdf0e10cSrcweir         {
216*cdf0e10cSrcweir             if ( ! static_cast<FileDoesNotExistFilter*>(xFilter.get())->exist())
217*cdf0e10cSrcweir                 throw NoDescriptionException();
218*cdf0e10cSrcweir             throw;
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir         if (!xIn.is())
221*cdf0e10cSrcweir         {
222*cdf0e10cSrcweir             throw css::uno::Exception(
223*cdf0e10cSrcweir                 OUSTR("Could not get XInputStream for description.xml of extension ") +
224*cdf0e10cSrcweir                 sDescriptionUri, 0);
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         //get root node of description.xml
228*cdf0e10cSrcweir         Reference<css::xml::dom::XDocumentBuilder> xDocBuilder(
229*cdf0e10cSrcweir             xContext->getServiceManager()->createInstanceWithContext(
230*cdf0e10cSrcweir                 OUSTR("com.sun.star.xml.dom.DocumentBuilder"),
231*cdf0e10cSrcweir                 xContext ), css::uno::UNO_QUERY);
232*cdf0e10cSrcweir         if (!xDocBuilder.is())
233*cdf0e10cSrcweir             throw css::uno::Exception(OUSTR(" Could not create service com.sun.star.xml.dom.DocumentBuilder"), 0);
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         if (xDocBuilder->isNamespaceAware() == sal_False)
236*cdf0e10cSrcweir         {
237*cdf0e10cSrcweir             throw css::uno::Exception(
238*cdf0e10cSrcweir                 OUSTR("Service com.sun.star.xml.dom.DocumentBuilder is not namespace aware."), 0);
239*cdf0e10cSrcweir         }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir         Reference<css::xml::dom::XDocument> xDoc = xDocBuilder->parse(xIn);
242*cdf0e10cSrcweir         if (!xDoc.is())
243*cdf0e10cSrcweir         {
244*cdf0e10cSrcweir             throw css::uno::Exception(sDescriptionUri + OUSTR(" contains data which cannot be parsed. "), 0);
245*cdf0e10cSrcweir         }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir         //check for proper root element and namespace
248*cdf0e10cSrcweir         Reference<css::xml::dom::XElement> xRoot = xDoc->getDocumentElement();
249*cdf0e10cSrcweir         if (!xRoot.is())
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             throw css::uno::Exception(
252*cdf0e10cSrcweir                 sDescriptionUri + OUSTR(" contains no root element."), 0);
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         if ( ! xRoot->getTagName().equals(OUSTR("description")))
256*cdf0e10cSrcweir         {
257*cdf0e10cSrcweir             throw css::uno::Exception(
258*cdf0e10cSrcweir                 sDescriptionUri + OUSTR(" does not contain the root element <description>."), 0);
259*cdf0e10cSrcweir         }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         m_xRoot = Reference<css::xml::dom::XNode>(
262*cdf0e10cSrcweir             xRoot, css::uno::UNO_QUERY_THROW);
263*cdf0e10cSrcweir         OUString nsDescription = xRoot->getNamespaceURI();
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         //check if this namespace is supported
266*cdf0e10cSrcweir         if ( ! nsDescription.equals(OUSTR("http://openoffice.org/extensions/description/2006")))
267*cdf0e10cSrcweir         {
268*cdf0e10cSrcweir             throw css::uno::Exception(sDescriptionUri + OUSTR(" contains a root element with an unsupported namespace. "), 0);
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir     } catch (css::uno::RuntimeException &) {
271*cdf0e10cSrcweir         throw;
272*cdf0e10cSrcweir     } catch (css::deployment::DeploymentException &) {
273*cdf0e10cSrcweir         throw;
274*cdf0e10cSrcweir     } catch (css::uno::Exception & e) {
275*cdf0e10cSrcweir         css::uno::Any a(cppu::getCaughtException());
276*cdf0e10cSrcweir         throw css::deployment::DeploymentException(
277*cdf0e10cSrcweir 			e.Message, Reference< css::uno::XInterface >(), a);
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir ExtensionDescription::~ExtensionDescription()
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir //======================================================================
286*cdf0e10cSrcweir FileDoesNotExistFilter::FileDoesNotExistFilter(
287*cdf0e10cSrcweir 	const Reference< css::ucb::XCommandEnvironment >& xCmdEnv):
288*cdf0e10cSrcweir 	m_bExist(true), m_xCommandEnv(xCmdEnv)
289*cdf0e10cSrcweir {}
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir FileDoesNotExistFilter::~FileDoesNotExistFilter()
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir };
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir bool FileDoesNotExistFilter::exist()
296*cdf0e10cSrcweir {
297*cdf0e10cSrcweir 	return m_bExist;
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir     // XCommandEnvironment
300*cdf0e10cSrcweir Reference<css::task::XInteractionHandler >
301*cdf0e10cSrcweir     FileDoesNotExistFilter::getInteractionHandler() throw (css::uno::RuntimeException)
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir 	return static_cast<css::task::XInteractionHandler*>(this);
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir Reference<css::ucb::XProgressHandler >
307*cdf0e10cSrcweir     FileDoesNotExistFilter::getProgressHandler() throw (css::uno::RuntimeException)
308*cdf0e10cSrcweir {
309*cdf0e10cSrcweir 	return m_xCommandEnv.is()
310*cdf0e10cSrcweir         ? m_xCommandEnv->getProgressHandler()
311*cdf0e10cSrcweir         : Reference<css::ucb::XProgressHandler>();
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir // XInteractionHandler
315*cdf0e10cSrcweir //If the interaction was caused by a non-existing file which is specified in the ctor
316*cdf0e10cSrcweir //of FileDoesNotExistFilter, then we do nothing
317*cdf0e10cSrcweir void  FileDoesNotExistFilter::handle(
318*cdf0e10cSrcweir         Reference<css::task::XInteractionRequest > const & xRequest )
319*cdf0e10cSrcweir         throw (css::uno::RuntimeException)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir 	css::uno::Any request( xRequest->getRequest() );
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 	css::ucb::InteractiveAugmentedIOException ioexc;
324*cdf0e10cSrcweir 	if ((request>>= ioexc) && ioexc.Code == css::ucb::IOErrorCode_NOT_EXISTING )
325*cdf0e10cSrcweir 	{
326*cdf0e10cSrcweir 		m_bExist = false;
327*cdf0e10cSrcweir 		return;
328*cdf0e10cSrcweir 	}
329*cdf0e10cSrcweir 	Reference<css::task::XInteractionHandler> xInteraction;
330*cdf0e10cSrcweir     if (m_xCommandEnv.is()) {
331*cdf0e10cSrcweir         xInteraction = m_xCommandEnv->getInteractionHandler();
332*cdf0e10cSrcweir     }
333*cdf0e10cSrcweir     if (xInteraction.is()) {
334*cdf0e10cSrcweir         xInteraction->handle(xRequest);
335*cdf0e10cSrcweir     }
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir namespace dp_misc {
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir DescriptionInfoset getDescriptionInfoset(OUString const & sExtensionFolderURL)
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     Reference< css::xml::dom::XNode > root;
345*cdf0e10cSrcweir     Reference<css::uno::XComponentContext> context =
346*cdf0e10cSrcweir         comphelper_getProcessComponentContext();
347*cdf0e10cSrcweir     OSL_ASSERT(context.is());
348*cdf0e10cSrcweir     try {
349*cdf0e10cSrcweir         root =
350*cdf0e10cSrcweir             ExtensionDescription(
351*cdf0e10cSrcweir                 context, sExtensionFolderURL,
352*cdf0e10cSrcweir                 Reference< css::ucb::XCommandEnvironment >()).
353*cdf0e10cSrcweir             getRootElement();
354*cdf0e10cSrcweir     } catch (NoDescriptionException &) {
355*cdf0e10cSrcweir     } catch (css::deployment::DeploymentException & e) {
356*cdf0e10cSrcweir         throw css::uno::RuntimeException(
357*cdf0e10cSrcweir             (OUString(
358*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
359*cdf0e10cSrcweir                     "com.sun.star.deployment.DeploymentException: ")) +
360*cdf0e10cSrcweir              e.Message), 0);
361*cdf0e10cSrcweir     }
362*cdf0e10cSrcweir     return DescriptionInfoset(context, root);
363*cdf0e10cSrcweir }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir DescriptionInfoset::DescriptionInfoset(
366*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const & context,
367*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > const & element):
368*cdf0e10cSrcweir     m_element(element)
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiComponentFactory > manager(
371*cdf0e10cSrcweir         context->getServiceManager(), css::uno::UNO_QUERY_THROW);
372*cdf0e10cSrcweir     if (m_element.is()) {
373*cdf0e10cSrcweir         m_xpath = css::uno::Reference< css::xml::xpath::XXPathAPI >(
374*cdf0e10cSrcweir             manager->createInstanceWithContext(
375*cdf0e10cSrcweir                 ::rtl::OUString(
376*cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM(
377*cdf0e10cSrcweir                         "com.sun.star.xml.xpath.XPathAPI")),
378*cdf0e10cSrcweir                 context),
379*cdf0e10cSrcweir             css::uno::UNO_QUERY_THROW);
380*cdf0e10cSrcweir         m_xpath->registerNS(
381*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc")),
382*cdf0e10cSrcweir             element->getNamespaceURI());
383*cdf0e10cSrcweir         m_xpath->registerNS(
384*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xlink")),
385*cdf0e10cSrcweir             ::rtl::OUString(
386*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("http://www.w3.org/1999/xlink")));
387*cdf0e10cSrcweir     }
388*cdf0e10cSrcweir }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir DescriptionInfoset::~DescriptionInfoset() {}
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir ::boost::optional< ::rtl::OUString > DescriptionInfoset::getIdentifier() const {
393*cdf0e10cSrcweir     return getOptionalValue(
394*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc:identifier/@value")));
395*cdf0e10cSrcweir }
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getNodeValueFromExpression(::rtl::OUString const & expression) const
398*cdf0e10cSrcweir {
399*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > n;
400*cdf0e10cSrcweir     if (m_element.is()) {
401*cdf0e10cSrcweir         try {
402*cdf0e10cSrcweir             n = m_xpath->selectSingleNode(m_element, expression);
403*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
404*cdf0e10cSrcweir             // ignore
405*cdf0e10cSrcweir         }
406*cdf0e10cSrcweir     }
407*cdf0e10cSrcweir     return n.is() ? getNodeValue(n) : ::rtl::OUString();
408*cdf0e10cSrcweir }
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getVersion() const
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir     return getNodeValueFromExpression( ::rtl::OUString(
414*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("desc:version/@value")));
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > DescriptionInfoset::getSupportedPlaforms() const
418*cdf0e10cSrcweir {
419*cdf0e10cSrcweir     //When there is no description.xml then we assume that we support all platforms
420*cdf0e10cSrcweir     if (! m_element.is())
421*cdf0e10cSrcweir     {
422*cdf0e10cSrcweir         return comphelper::makeSequence(
423*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("all")));
424*cdf0e10cSrcweir     }
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir     //Check if the <platform> element was provided. If not the default is "all" platforms
427*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > nodePlatform(
428*cdf0e10cSrcweir         m_xpath->selectSingleNode(m_element, ::rtl::OUString(
429*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("desc:platform"))));
430*cdf0e10cSrcweir     if (!nodePlatform.is())
431*cdf0e10cSrcweir     {
432*cdf0e10cSrcweir         return comphelper::makeSequence(
433*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("all")));
434*cdf0e10cSrcweir     }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir     //There is a platform element.
437*cdf0e10cSrcweir     const ::rtl::OUString value = getNodeValueFromExpression(::rtl::OUString(
438*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("desc:platform/@value")));
439*cdf0e10cSrcweir     //parse the string, it can contained multiple strings separated by commas
440*cdf0e10cSrcweir     ::std::vector< ::rtl::OUString> vec;
441*cdf0e10cSrcweir     sal_Int32 nIndex = 0;
442*cdf0e10cSrcweir     do
443*cdf0e10cSrcweir     {
444*cdf0e10cSrcweir         ::rtl::OUString aToken = value.getToken( 0, ',', nIndex );
445*cdf0e10cSrcweir         aToken = aToken.trim();
446*cdf0e10cSrcweir         if (aToken.getLength())
447*cdf0e10cSrcweir             vec.push_back(aToken);
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir     }
450*cdf0e10cSrcweir     while (nIndex >= 0);
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir     return comphelper::containerToSequence(vec);
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir css::uno::Reference< css::xml::dom::XNodeList >
456*cdf0e10cSrcweir DescriptionInfoset::getDependencies() const {
457*cdf0e10cSrcweir     if (m_element.is()) {
458*cdf0e10cSrcweir         try {
459*cdf0e10cSrcweir             return m_xpath->selectNodeList(m_element, ::rtl::OUString(
460*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM("desc:dependencies/*")));
461*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
462*cdf0e10cSrcweir             // ignore
463*cdf0e10cSrcweir         }
464*cdf0e10cSrcweir     }
465*cdf0e10cSrcweir     return new EmptyNodeList;
466*cdf0e10cSrcweir }
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString >
469*cdf0e10cSrcweir DescriptionInfoset::getUpdateInformationUrls() const {
470*cdf0e10cSrcweir     return getUrls(
471*cdf0e10cSrcweir         ::rtl::OUString(
472*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
473*cdf0e10cSrcweir                 "desc:update-information/desc:src/@xlink:href")));
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString >
477*cdf0e10cSrcweir DescriptionInfoset::getUpdateDownloadUrls() const
478*cdf0e10cSrcweir {
479*cdf0e10cSrcweir     return getUrls(
480*cdf0e10cSrcweir         ::rtl::OUString(
481*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
482*cdf0e10cSrcweir                 "desc:update-download/desc:src/@xlink:href")));
483*cdf0e10cSrcweir }
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getIconURL( sal_Bool bHighContrast ) const
486*cdf0e10cSrcweir {
487*cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > aStrList = getUrls( ::rtl::OUString(
488*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM( "desc:icon/desc:default/@xlink:href")));
489*cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > aStrListHC = getUrls( ::rtl::OUString(
490*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM( "desc:icon/desc:high-contrast/@xlink:href")));
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir     if ( bHighContrast && aStrListHC.hasElements() && aStrListHC[0].getLength() )
493*cdf0e10cSrcweir         return aStrListHC[0];
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir     if ( aStrList.hasElements() && aStrList[0].getLength() )
496*cdf0e10cSrcweir         return aStrList[0];
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir     return ::rtl::OUString();
499*cdf0e10cSrcweir }
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir ::boost::optional< ::rtl::OUString > DescriptionInfoset::getLocalizedUpdateWebsiteURL()
502*cdf0e10cSrcweir     const
503*cdf0e10cSrcweir {
504*cdf0e10cSrcweir     bool bParentExists = false;
505*cdf0e10cSrcweir     const ::rtl::OUString sURL (getLocalizedHREFAttrFromChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
506*cdf0e10cSrcweir         "/desc:description/desc:update-website")), &bParentExists ));
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir     if (sURL.getLength() > 0)
509*cdf0e10cSrcweir         return ::boost::optional< ::rtl::OUString >(sURL);
510*cdf0e10cSrcweir     else
511*cdf0e10cSrcweir         return bParentExists ? ::boost::optional< ::rtl::OUString >(::rtl::OUString()) :
512*cdf0e10cSrcweir             ::boost::optional< ::rtl::OUString >();
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir ::boost::optional< ::rtl::OUString > DescriptionInfoset::getOptionalValue(
516*cdf0e10cSrcweir     ::rtl::OUString const & expression) const
517*cdf0e10cSrcweir {
518*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > n;
519*cdf0e10cSrcweir     if (m_element.is()) {
520*cdf0e10cSrcweir         try {
521*cdf0e10cSrcweir             n = m_xpath->selectSingleNode(m_element, expression);
522*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
523*cdf0e10cSrcweir             // ignore
524*cdf0e10cSrcweir         }
525*cdf0e10cSrcweir     }
526*cdf0e10cSrcweir     return n.is()
527*cdf0e10cSrcweir         ? ::boost::optional< ::rtl::OUString >(getNodeValue(n))
528*cdf0e10cSrcweir         : ::boost::optional< ::rtl::OUString >();
529*cdf0e10cSrcweir }
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > DescriptionInfoset::getUrls(
532*cdf0e10cSrcweir     ::rtl::OUString const & expression) const
533*cdf0e10cSrcweir {
534*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNodeList > ns;
535*cdf0e10cSrcweir     if (m_element.is()) {
536*cdf0e10cSrcweir         try {
537*cdf0e10cSrcweir             ns = m_xpath->selectNodeList(m_element, expression);
538*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
539*cdf0e10cSrcweir             // ignore
540*cdf0e10cSrcweir         }
541*cdf0e10cSrcweir     }
542*cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > urls(ns.is() ? ns->getLength() : 0);
543*cdf0e10cSrcweir     for (::sal_Int32 i = 0; i < urls.getLength(); ++i) {
544*cdf0e10cSrcweir         urls[i] = getNodeValue(ns->item(i));
545*cdf0e10cSrcweir     }
546*cdf0e10cSrcweir     return urls;
547*cdf0e10cSrcweir }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir ::std::pair< ::rtl::OUString, ::rtl::OUString > DescriptionInfoset::getLocalizedPublisherNameAndURL() const
550*cdf0e10cSrcweir {
551*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > node =
552*cdf0e10cSrcweir         getLocalizedChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc:publisher")));
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     ::rtl::OUString sPublisherName;
555*cdf0e10cSrcweir     ::rtl::OUString sURL;
556*cdf0e10cSrcweir     if (node.is())
557*cdf0e10cSrcweir     {
558*cdf0e10cSrcweir         const ::rtl::OUString exp1(RTL_CONSTASCII_USTRINGPARAM("text()"));
559*cdf0e10cSrcweir         css::uno::Reference< css::xml::dom::XNode > xPathName;
560*cdf0e10cSrcweir         try {
561*cdf0e10cSrcweir             xPathName = m_xpath->selectSingleNode(node, exp1);
562*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
563*cdf0e10cSrcweir             // ignore
564*cdf0e10cSrcweir         }
565*cdf0e10cSrcweir         OSL_ASSERT(xPathName.is());
566*cdf0e10cSrcweir         if (xPathName.is())
567*cdf0e10cSrcweir             sPublisherName = xPathName->getNodeValue();
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir         const ::rtl::OUString exp2(RTL_CONSTASCII_USTRINGPARAM("@xlink:href"));
570*cdf0e10cSrcweir         css::uno::Reference< css::xml::dom::XNode > xURL;
571*cdf0e10cSrcweir         try {
572*cdf0e10cSrcweir             xURL = m_xpath->selectSingleNode(node, exp2);
573*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
574*cdf0e10cSrcweir             // ignore
575*cdf0e10cSrcweir         }
576*cdf0e10cSrcweir         OSL_ASSERT(xURL.is());
577*cdf0e10cSrcweir         if (xURL.is())
578*cdf0e10cSrcweir            sURL = xURL->getNodeValue();
579*cdf0e10cSrcweir     }
580*cdf0e10cSrcweir     return ::std::make_pair(sPublisherName, sURL);
581*cdf0e10cSrcweir }
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getLocalizedReleaseNotesURL() const
584*cdf0e10cSrcweir {
585*cdf0e10cSrcweir     return getLocalizedHREFAttrFromChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
586*cdf0e10cSrcweir         "/desc:description/desc:release-notes")), NULL);
587*cdf0e10cSrcweir }
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getLocalizedDisplayName() const
590*cdf0e10cSrcweir {
591*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > node =
592*cdf0e10cSrcweir         getLocalizedChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc:display-name")));
593*cdf0e10cSrcweir     if (node.is())
594*cdf0e10cSrcweir     {
595*cdf0e10cSrcweir         const ::rtl::OUString exp(RTL_CONSTASCII_USTRINGPARAM("text()"));
596*cdf0e10cSrcweir         css::uno::Reference< css::xml::dom::XNode > xtext;
597*cdf0e10cSrcweir         try {
598*cdf0e10cSrcweir             xtext = m_xpath->selectSingleNode(node, exp);
599*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
600*cdf0e10cSrcweir             // ignore
601*cdf0e10cSrcweir         }
602*cdf0e10cSrcweir         if (xtext.is())
603*cdf0e10cSrcweir             return xtext->getNodeValue();
604*cdf0e10cSrcweir     }
605*cdf0e10cSrcweir     return ::rtl::OUString();
606*cdf0e10cSrcweir }
607*cdf0e10cSrcweir 
608*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getLocalizedLicenseURL() const
609*cdf0e10cSrcweir {
610*cdf0e10cSrcweir     return getLocalizedHREFAttrFromChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
611*cdf0e10cSrcweir         "/desc:description/desc:registration/desc:simple-license")), NULL);
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir }
614*cdf0e10cSrcweir 
615*cdf0e10cSrcweir ::boost::optional<SimpleLicenseAttributes>
616*cdf0e10cSrcweir DescriptionInfoset::getSimpleLicenseAttributes() const
617*cdf0e10cSrcweir {
618*cdf0e10cSrcweir     //Check if the node exist
619*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > n;
620*cdf0e10cSrcweir     if (m_element.is()) {
621*cdf0e10cSrcweir         try {
622*cdf0e10cSrcweir             n = m_xpath->selectSingleNode(m_element,
623*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
624*cdf0e10cSrcweir                 "/desc:description/desc:registration/desc:simple-license/@accept-by")));
625*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
626*cdf0e10cSrcweir             // ignore
627*cdf0e10cSrcweir         }
628*cdf0e10cSrcweir         if (n.is())
629*cdf0e10cSrcweir         {
630*cdf0e10cSrcweir             SimpleLicenseAttributes attributes;
631*cdf0e10cSrcweir             attributes.acceptBy =
632*cdf0e10cSrcweir                 getNodeValueFromExpression(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
633*cdf0e10cSrcweir                 "/desc:description/desc:registration/desc:simple-license/@accept-by")));
634*cdf0e10cSrcweir 
635*cdf0e10cSrcweir             ::boost::optional< ::rtl::OUString > suppressOnUpdate = getOptionalValue(
636*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
637*cdf0e10cSrcweir                 "/desc:description/desc:registration/desc:simple-license/@suppress-on-update")));
638*cdf0e10cSrcweir             if (suppressOnUpdate)
639*cdf0e10cSrcweir                 attributes.suppressOnUpdate = (*suppressOnUpdate).trim().equalsIgnoreAsciiCase(
640*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("true")));
641*cdf0e10cSrcweir             else
642*cdf0e10cSrcweir                 attributes.suppressOnUpdate = false;
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir             ::boost::optional< ::rtl::OUString > suppressIfRequired = getOptionalValue(
645*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
646*cdf0e10cSrcweir                 "/desc:description/desc:registration/desc:simple-license/@suppress-if-required")));
647*cdf0e10cSrcweir             if (suppressIfRequired)
648*cdf0e10cSrcweir                 attributes.suppressIfRequired = (*suppressIfRequired).trim().equalsIgnoreAsciiCase(
649*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("true")));
650*cdf0e10cSrcweir             else
651*cdf0e10cSrcweir                 attributes.suppressIfRequired = false;
652*cdf0e10cSrcweir 
653*cdf0e10cSrcweir             return ::boost::optional<SimpleLicenseAttributes>(attributes);
654*cdf0e10cSrcweir         }
655*cdf0e10cSrcweir     }
656*cdf0e10cSrcweir     return ::boost::optional<SimpleLicenseAttributes>();
657*cdf0e10cSrcweir }
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getLocalizedDescriptionURL() const
660*cdf0e10cSrcweir {
661*cdf0e10cSrcweir     return getLocalizedHREFAttrFromChild(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
662*cdf0e10cSrcweir         "/desc:description/desc:extension-description")), NULL);
663*cdf0e10cSrcweir }
664*cdf0e10cSrcweir 
665*cdf0e10cSrcweir css::uno::Reference< css::xml::dom::XNode >
666*cdf0e10cSrcweir DescriptionInfoset::getLocalizedChild( const ::rtl::OUString & sParent) const
667*cdf0e10cSrcweir {
668*cdf0e10cSrcweir     if ( ! m_element.is() || !sParent.getLength())
669*cdf0e10cSrcweir         return css::uno::Reference< css::xml::dom::XNode > ();
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > xParent;
672*cdf0e10cSrcweir     try {
673*cdf0e10cSrcweir         xParent = m_xpath->selectSingleNode(m_element, sParent);
674*cdf0e10cSrcweir     } catch (css::xml::xpath::XPathException &) {
675*cdf0e10cSrcweir         // ignore
676*cdf0e10cSrcweir     }
677*cdf0e10cSrcweir     css::uno::Reference<css::xml::dom::XNode> nodeMatch;
678*cdf0e10cSrcweir     if (xParent.is())
679*cdf0e10cSrcweir     {
680*cdf0e10cSrcweir         const ::rtl::OUString sLocale = getOfficeLocaleString();
681*cdf0e10cSrcweir         nodeMatch = matchFullLocale(xParent, sLocale);
682*cdf0e10cSrcweir 
683*cdf0e10cSrcweir         //office: en-DE, en, en-DE-altmark
684*cdf0e10cSrcweir         if (! nodeMatch.is())
685*cdf0e10cSrcweir         {
686*cdf0e10cSrcweir             const css::lang::Locale officeLocale = getOfficeLocale();
687*cdf0e10cSrcweir             nodeMatch = matchCountryAndLanguage(xParent, officeLocale);
688*cdf0e10cSrcweir             if ( ! nodeMatch.is())
689*cdf0e10cSrcweir             {
690*cdf0e10cSrcweir                 nodeMatch = matchLanguage(xParent, officeLocale);
691*cdf0e10cSrcweir                 if (! nodeMatch.is())
692*cdf0e10cSrcweir                     nodeMatch = getChildWithDefaultLocale(xParent);
693*cdf0e10cSrcweir             }
694*cdf0e10cSrcweir         }
695*cdf0e10cSrcweir     }
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir     return nodeMatch;
698*cdf0e10cSrcweir }
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XNode>
701*cdf0e10cSrcweir DescriptionInfoset::matchFullLocale(css::uno::Reference< css::xml::dom::XNode >
702*cdf0e10cSrcweir                                     const & xParent, ::rtl::OUString const & sLocale) const
703*cdf0e10cSrcweir {
704*cdf0e10cSrcweir     OSL_ASSERT(xParent.is());
705*cdf0e10cSrcweir     const ::rtl::OUString exp1(
706*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*[@lang=\""))
707*cdf0e10cSrcweir         + sLocale +
708*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"]")));
709*cdf0e10cSrcweir     try {
710*cdf0e10cSrcweir         return m_xpath->selectSingleNode(xParent, exp1);
711*cdf0e10cSrcweir     } catch (css::xml::xpath::XPathException &) {
712*cdf0e10cSrcweir         // ignore
713*cdf0e10cSrcweir         return 0;
714*cdf0e10cSrcweir     }
715*cdf0e10cSrcweir }
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XNode>
718*cdf0e10cSrcweir DescriptionInfoset::matchCountryAndLanguage(
719*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > const & xParent, css::lang::Locale const & officeLocale) const
720*cdf0e10cSrcweir {
721*cdf0e10cSrcweir     OSL_ASSERT(xParent.is());
722*cdf0e10cSrcweir     css::uno::Reference<css::xml::dom::XNode> nodeMatch;
723*cdf0e10cSrcweir 
724*cdf0e10cSrcweir     if (officeLocale.Country.getLength())
725*cdf0e10cSrcweir     {
726*cdf0e10cSrcweir         const ::rtl::OUString sLangCountry(officeLocale.Language +
727*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-")) +
728*cdf0e10cSrcweir             officeLocale.Country);
729*cdf0e10cSrcweir         //first try exact match for lang-country
730*cdf0e10cSrcweir         const ::rtl::OUString exp1(
731*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*[@lang=\""))
732*cdf0e10cSrcweir             + sLangCountry +
733*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"]")));
734*cdf0e10cSrcweir         try {
735*cdf0e10cSrcweir             nodeMatch = m_xpath->selectSingleNode(xParent, exp1);
736*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
737*cdf0e10cSrcweir             // ignore
738*cdf0e10cSrcweir         }
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir         //try to match in strings that also have a variant, for example en-US matches in
741*cdf0e10cSrcweir         //en-US-montana
742*cdf0e10cSrcweir         if (!nodeMatch.is())
743*cdf0e10cSrcweir         {
744*cdf0e10cSrcweir             const ::rtl::OUString exp2(
745*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*[starts-with(@lang,\""))
746*cdf0e10cSrcweir                 + sLangCountry +
747*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-\")]")));
748*cdf0e10cSrcweir             try {
749*cdf0e10cSrcweir                 nodeMatch = m_xpath->selectSingleNode(xParent, exp2);
750*cdf0e10cSrcweir             } catch (css::xml::xpath::XPathException &) {
751*cdf0e10cSrcweir                 // ignore
752*cdf0e10cSrcweir             }
753*cdf0e10cSrcweir         }
754*cdf0e10cSrcweir     }
755*cdf0e10cSrcweir 
756*cdf0e10cSrcweir     return nodeMatch;
757*cdf0e10cSrcweir }
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir 
760*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XNode>
761*cdf0e10cSrcweir DescriptionInfoset::matchLanguage(
762*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > const & xParent, css::lang::Locale const & officeLocale) const
763*cdf0e10cSrcweir {
764*cdf0e10cSrcweir     OSL_ASSERT(xParent.is());
765*cdf0e10cSrcweir     css::uno::Reference<css::xml::dom::XNode> nodeMatch;
766*cdf0e10cSrcweir 
767*cdf0e10cSrcweir     //first try exact match for lang
768*cdf0e10cSrcweir     const ::rtl::OUString exp1(
769*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*[@lang=\""))
770*cdf0e10cSrcweir         + officeLocale.Language
771*cdf0e10cSrcweir         + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"]")));
772*cdf0e10cSrcweir     try {
773*cdf0e10cSrcweir         nodeMatch = m_xpath->selectSingleNode(xParent, exp1);
774*cdf0e10cSrcweir     } catch (css::xml::xpath::XPathException &) {
775*cdf0e10cSrcweir         // ignore
776*cdf0e10cSrcweir     }
777*cdf0e10cSrcweir 
778*cdf0e10cSrcweir     //try to match in strings that also have a country and/orvariant, for example en  matches in
779*cdf0e10cSrcweir     //en-US-montana, en-US, en-montana
780*cdf0e10cSrcweir     if (!nodeMatch.is())
781*cdf0e10cSrcweir     {
782*cdf0e10cSrcweir         const ::rtl::OUString exp2(
783*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*[starts-with(@lang,\""))
784*cdf0e10cSrcweir             + officeLocale.Language
785*cdf0e10cSrcweir             + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-\")]")));
786*cdf0e10cSrcweir         try {
787*cdf0e10cSrcweir             nodeMatch = m_xpath->selectSingleNode(xParent, exp2);
788*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
789*cdf0e10cSrcweir             // ignore
790*cdf0e10cSrcweir         }
791*cdf0e10cSrcweir     }
792*cdf0e10cSrcweir     return nodeMatch;
793*cdf0e10cSrcweir }
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XNode>
796*cdf0e10cSrcweir DescriptionInfoset::getChildWithDefaultLocale(css::uno::Reference< css::xml::dom::XNode >
797*cdf0e10cSrcweir                                     const & xParent) const
798*cdf0e10cSrcweir {
799*cdf0e10cSrcweir     OSL_ASSERT(xParent.is());
800*cdf0e10cSrcweir     if (xParent->getNodeName().equals(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("simple-license"))))
801*cdf0e10cSrcweir     {
802*cdf0e10cSrcweir         css::uno::Reference<css::xml::dom::XNode> nodeDefault;
803*cdf0e10cSrcweir         try {
804*cdf0e10cSrcweir             nodeDefault = m_xpath->selectSingleNode(xParent, ::rtl::OUString(
805*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("@default-license-id")));
806*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
807*cdf0e10cSrcweir             // ignore
808*cdf0e10cSrcweir         }
809*cdf0e10cSrcweir         if (nodeDefault.is())
810*cdf0e10cSrcweir         {
811*cdf0e10cSrcweir             //The old way
812*cdf0e10cSrcweir             const ::rtl::OUString exp1(
813*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("desc:license-text[@license-id = \""))
814*cdf0e10cSrcweir                 + nodeDefault->getNodeValue()
815*cdf0e10cSrcweir                 + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\"]")));
816*cdf0e10cSrcweir             try {
817*cdf0e10cSrcweir                 return m_xpath->selectSingleNode(xParent, exp1);
818*cdf0e10cSrcweir             } catch (css::xml::xpath::XPathException &) {
819*cdf0e10cSrcweir                 // ignore
820*cdf0e10cSrcweir             }
821*cdf0e10cSrcweir         }
822*cdf0e10cSrcweir     }
823*cdf0e10cSrcweir 
824*cdf0e10cSrcweir     const ::rtl::OUString exp2(RTL_CONSTASCII_USTRINGPARAM("*[1]"));
825*cdf0e10cSrcweir     try {
826*cdf0e10cSrcweir         return m_xpath->selectSingleNode(xParent, exp2);
827*cdf0e10cSrcweir     } catch (css::xml::xpath::XPathException &) {
828*cdf0e10cSrcweir         // ignore
829*cdf0e10cSrcweir         return 0;
830*cdf0e10cSrcweir     }
831*cdf0e10cSrcweir }
832*cdf0e10cSrcweir 
833*cdf0e10cSrcweir ::rtl::OUString DescriptionInfoset::getLocalizedHREFAttrFromChild(
834*cdf0e10cSrcweir     ::rtl::OUString const & sXPathParent, bool * out_bParentExists)
835*cdf0e10cSrcweir     const
836*cdf0e10cSrcweir {
837*cdf0e10cSrcweir     css::uno::Reference< css::xml::dom::XNode > node =
838*cdf0e10cSrcweir         getLocalizedChild(sXPathParent);
839*cdf0e10cSrcweir 
840*cdf0e10cSrcweir     ::rtl::OUString sURL;
841*cdf0e10cSrcweir     if (node.is())
842*cdf0e10cSrcweir     {
843*cdf0e10cSrcweir         if (out_bParentExists)
844*cdf0e10cSrcweir             *out_bParentExists = true;
845*cdf0e10cSrcweir         const ::rtl::OUString exp(RTL_CONSTASCII_USTRINGPARAM("@xlink:href"));
846*cdf0e10cSrcweir         css::uno::Reference< css::xml::dom::XNode > xURL;
847*cdf0e10cSrcweir         try {
848*cdf0e10cSrcweir             xURL = m_xpath->selectSingleNode(node, exp);
849*cdf0e10cSrcweir         } catch (css::xml::xpath::XPathException &) {
850*cdf0e10cSrcweir             // ignore
851*cdf0e10cSrcweir         }
852*cdf0e10cSrcweir         OSL_ASSERT(xURL.is());
853*cdf0e10cSrcweir         if (xURL.is())
854*cdf0e10cSrcweir             sURL = xURL->getNodeValue();
855*cdf0e10cSrcweir     }
856*cdf0e10cSrcweir     else
857*cdf0e10cSrcweir     {
858*cdf0e10cSrcweir         if (out_bParentExists)
859*cdf0e10cSrcweir             *out_bParentExists = false;
860*cdf0e10cSrcweir     }
861*cdf0e10cSrcweir     return sURL;
862*cdf0e10cSrcweir }
863*cdf0e10cSrcweir 
864*cdf0e10cSrcweir }
865