LibreOffice Module ucb (master) 1
ucpexpand.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21#include <rtl/uri.hxx>
24#include <ucbhelper/content.hxx>
25#include <com/sun/star/uno/XComponentContext.hpp>
26#include <com/sun/star/lang/DisposedException.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/util/theMacroExpander.hpp>
29#include <com/sun/star/ucb/IllegalIdentifierException.hpp>
30#include <com/sun/star/ucb/XContentProvider.hpp>
32
33
34using namespace ::com::sun::star;
35
36namespace
37{
38
40 lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
41
42
43class ExpandContentProviderImpl : public t_impl_helper
44{
45 uno::Reference< uno::XComponentContext > m_xComponentContext;
46 uno::Reference< util::XMacroExpander > m_xMacroExpander;
47 OUString expandUri(
48 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
49
50protected:
51 void check() const;
52
53public:
54 explicit ExpandContentProviderImpl(
55 uno::Reference< uno::XComponentContext > const & xComponentContext )
56 : m_xComponentContext( xComponentContext ),
57 m_xMacroExpander( util::theMacroExpander::get(xComponentContext) )
58 {}
59
60 // XServiceInfo
61 virtual OUString SAL_CALL getImplementationName() override;
62 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
63 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
64
65 // XContentProvider
66 virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
67 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) override;
68 virtual sal_Int32 SAL_CALL compareContentIds(
69 uno::Reference< ucb::XContentIdentifier > const & xId1,
70 uno::Reference< ucb::XContentIdentifier > const & xId2 ) override;
71};
72
73
74void ExpandContentProviderImpl::check() const
75{
76 // xxx todo guard?
77// MutexGuard guard( m_mutex );
78 if (m_bDisposed)
79 {
80 throw lang::DisposedException(
81 "expand content provider instance has "
82 "already been disposed!",
83 const_cast< ExpandContentProviderImpl * >(this)->getXWeak() );
84 }
85}
86
87// XServiceInfo
88
89OUString ExpandContentProviderImpl::getImplementationName()
90{
91 check();
92 return "com.sun.star.comp.ucb.ExpandContentProvider";
93}
94
95
96uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
97{
98 check();
99 return {
100 "com.sun.star.ucb.ExpandContentProvider",
101 "com.sun.star.ucb.ContentProvider"
102 };
103}
104
105sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName )
106{
107 return cppu::supportsService(this, serviceName);
108}
109
110OUString ExpandContentProviderImpl::expandUri(
111 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
112{
113 OUString uri( xIdentifier->getContentIdentifier() );
114 if (!uri.startsWithIgnoreAsciiCase("vnd.sun.star.expand:", &uri))
115 {
116 throw ucb::IllegalIdentifierException(
117 "expected protocol vnd.sun.star.expand!",
118 const_cast< ExpandContentProviderImpl * >(this)->getXWeak() );
119 }
120 // decode uric class chars
121 OUString str = ::rtl::Uri::decode(uri, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
122 // expand macro string
123 return m_xMacroExpander->expandMacros( str );
124}
125
126// XContentProvider
127
128uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
129 uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
130{
131 check();
132 OUString uri( expandUri( xIdentifier ) );
133
134 ::ucbhelper::Content ucb_content;
136 uri, uno::Reference< ucb::XCommandEnvironment >(),
137 m_xComponentContext, ucb_content ))
138 {
139 return ucb_content.get();
140 }
141 else
142 {
143 return uno::Reference< ucb::XContent >();
144 }
145}
146
147
148sal_Int32 ExpandContentProviderImpl::compareContentIds(
149 uno::Reference< ucb::XContentIdentifier > const & xId1,
150 uno::Reference< ucb::XContentIdentifier > const & xId2 )
151{
152 check();
153 try
154 {
155 OUString uri1( expandUri( xId1 ) );
156 OUString uri2( expandUri( xId2 ) );
157 return uri1.compareTo( uri2 );
158 }
159 catch (const ucb::IllegalIdentifierException &)
160 {
161 TOOLS_WARN_EXCEPTION( "ucb", "" );
162 return -1;
163 }
164}
165
166
167}
168
169extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
171 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
172{
173 return cppu::acquire(new ExpandContentProviderImpl(context));
174}
175
176/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool create(const OUString &rURL, const css::uno::Reference< css::ucb::XCommandEnvironment > &rEnv, const css::uno::Reference< css::uno::XComponentContext > &rCtx, Content &rContent)
css::uno::Reference< css::ucb::XContent > get() const
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XComponentContext > const m_xComponentContext
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > > check(dp_misc::DescriptionInfoset const &infoset)
def expandUri(uri)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_expand_ExpandContentProviderImpl_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: ucpexpand.cxx:170