Branch data Line data Source code
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 : : #include "sal/config.h"
21 : :
22 : : #include "stocservices.hxx"
23 : :
24 : : #include <new>
25 : :
26 : : #include "com/sun/star/lang/IllegalArgumentException.hpp"
27 : : #include "com/sun/star/lang/XServiceInfo.hpp"
28 : : #include "com/sun/star/uno/Reference.hxx"
29 : : #include "com/sun/star/uno/RuntimeException.hpp"
30 : : #include "com/sun/star/uno/Sequence.hxx"
31 : : #include "com/sun/star/uno/XInterface.hpp"
32 : : #include "com/sun/star/uri/XUriReference.hpp"
33 : : #include "com/sun/star/uri/XUriSchemeParser.hpp"
34 : : #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
35 : : #include "com/sun/star/util/XMacroExpander.hpp"
36 : : #include "cppuhelper/implbase1.hxx"
37 : : #include "cppuhelper/implbase2.hxx"
38 : : #include "cppuhelper/weak.hxx"
39 : : #include "rtl/textenc.h"
40 : : #include "rtl/uri.h"
41 : : #include "rtl/uri.hxx"
42 : : #include "rtl/ustring.h"
43 : : #include "rtl/ustring.hxx"
44 : : #include "sal/types.h"
45 : :
46 : : #include "UriReference.hxx"
47 : : #include "supportsService.hxx"
48 : :
49 : : namespace {
50 : :
51 : : namespace css = ::com::sun::star;
52 : :
53 : 4 : bool parseSchemeSpecificPart(::rtl::OUString const & part) {
54 : : // Liberally accepts both an empty opaque_part and an opaque_part that
55 : : // starts with a non-escaped "/":
56 : 4 : return part.isEmpty()
57 [ + - ][ + - ]: 4 : || (!::rtl::Uri::decode(part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).isEmpty());
[ + - ]
58 : : }
59 : :
60 : : class UrlReference:
61 : : public ::cppu::WeakImplHelper1< css::uri::XVndSunStarExpandUrlReference >
62 : : {
63 : : public:
64 : 4 : UrlReference(::rtl::OUString const & scheme, ::rtl::OUString const & path):
65 : : base_(
66 : : scheme, false, false, ::rtl::OUString(), path, false,
67 [ + - ]: 4 : ::rtl::OUString())
68 : 4 : {}
69 : :
70 : 0 : virtual ::rtl::OUString SAL_CALL getUriReference()
71 : : throw (css::uno::RuntimeException)
72 : 0 : { return base_.getUriReference(); }
73 : :
74 : 0 : virtual ::sal_Bool SAL_CALL isAbsolute() throw (css::uno::RuntimeException)
75 : 0 : { return base_.isAbsolute(); }
76 : :
77 : 0 : virtual ::rtl::OUString SAL_CALL getScheme()
78 : : throw (css::uno::RuntimeException)
79 : 0 : { return base_.getScheme(); }
80 : :
81 : 0 : virtual ::rtl::OUString SAL_CALL getSchemeSpecificPart()
82 : : throw (css::uno::RuntimeException)
83 : 0 : { return base_.getSchemeSpecificPart(); }
84 : :
85 : 0 : virtual ::sal_Bool SAL_CALL isHierarchical()
86 : : throw (css::uno::RuntimeException)
87 : 0 : { return base_.isHierarchical(); }
88 : :
89 : 0 : virtual ::sal_Bool SAL_CALL hasAuthority()
90 : : throw (css::uno::RuntimeException)
91 : 0 : { return base_.hasAuthority(); }
92 : :
93 : 0 : virtual ::rtl::OUString SAL_CALL getAuthority()
94 : : throw (css::uno::RuntimeException)
95 : 0 : { return base_.getAuthority(); }
96 : :
97 : 4 : virtual ::rtl::OUString SAL_CALL getPath()
98 : : throw (css::uno::RuntimeException)
99 : 4 : { return base_.getPath(); }
100 : :
101 : 0 : virtual ::sal_Bool SAL_CALL hasRelativePath()
102 : : throw (css::uno::RuntimeException)
103 : 0 : { return base_.hasRelativePath(); }
104 : :
105 : 0 : virtual ::sal_Int32 SAL_CALL getPathSegmentCount()
106 : : throw (css::uno::RuntimeException)
107 : 0 : { return base_.getPathSegmentCount(); }
108 : :
109 : 0 : virtual ::rtl::OUString SAL_CALL getPathSegment(sal_Int32 index)
110 : : throw (css::uno::RuntimeException)
111 : 0 : { return base_.getPathSegment(index); }
112 : :
113 : 0 : virtual ::sal_Bool SAL_CALL hasQuery() throw (css::uno::RuntimeException)
114 : 0 : { return base_.hasQuery(); }
115 : :
116 : 0 : virtual ::rtl::OUString SAL_CALL getQuery()
117 : : throw (css::uno::RuntimeException)
118 : 0 : { return base_.getQuery(); }
119 : :
120 : 0 : virtual ::sal_Bool SAL_CALL hasFragment() throw (css::uno::RuntimeException)
121 : 0 : { return base_.hasFragment(); }
122 : :
123 : 0 : virtual ::rtl::OUString SAL_CALL getFragment()
124 : : throw (css::uno::RuntimeException)
125 : 0 : { return base_.getFragment(); }
126 : :
127 : 0 : virtual void SAL_CALL setFragment(::rtl::OUString const & fragment)
128 : : throw (css::uno::RuntimeException)
129 : 0 : { base_.setFragment(fragment); }
130 : :
131 : 0 : virtual void SAL_CALL clearFragment() throw (css::uno::RuntimeException)
132 : 0 : { base_.clearFragment(); }
133 : :
134 : : virtual ::rtl::OUString SAL_CALL expand(
135 : : css::uno::Reference< css::util::XMacroExpander > const & expander)
136 : : throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
137 : :
138 : : private:
139 : : UrlReference(UrlReference &); // not defined
140 : : void operator =(UrlReference); // not defined
141 : :
142 [ + - ][ - + ]: 8 : virtual ~UrlReference() {}
143 : :
144 : : stoc::uriproc::UriReference base_;
145 : : };
146 : :
147 : 4 : ::rtl::OUString UrlReference::expand(
148 : : css::uno::Reference< css::util::XMacroExpander > const & expander)
149 : : throw (css::lang::IllegalArgumentException, css::uno::RuntimeException)
150 : : {
151 : : OSL_ASSERT(expander.is());
152 : 4 : return expander->expandMacros(
153 : : ::rtl::Uri::decode(
154 [ + - ]: 4 : getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
155 : : }
156 : :
157 : : class Parser: public ::cppu::WeakImplHelper2<
158 : : css::lang::XServiceInfo, css::uri::XUriSchemeParser >
159 : : {
160 : : public:
161 : 4 : Parser() {}
162 : :
163 : : virtual ::rtl::OUString SAL_CALL getImplementationName()
164 : : throw (css::uno::RuntimeException);
165 : :
166 : : virtual ::sal_Bool SAL_CALL supportsService(
167 : : ::rtl::OUString const & serviceName)
168 : : throw (css::uno::RuntimeException);
169 : :
170 : : virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL
171 : : getSupportedServiceNames() throw (css::uno::RuntimeException);
172 : :
173 : : virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
174 : : parse(
175 : : ::rtl::OUString const & scheme,
176 : : ::rtl::OUString const & schemeSpecificPart)
177 : : throw (css::uno::RuntimeException);
178 : :
179 : : private:
180 : : Parser(Parser &); // not defined
181 : : void operator =(Parser); // not defined
182 : :
183 [ - + ]: 8 : virtual ~Parser() {}
184 : : };
185 : :
186 : 0 : ::rtl::OUString Parser::getImplementationName()
187 : : throw (css::uno::RuntimeException)
188 : : {
189 : : return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
190 : 0 : getImplementationName();
191 : : }
192 : :
193 : 0 : ::sal_Bool Parser::supportsService(::rtl::OUString const & serviceName)
194 : : throw (css::uno::RuntimeException)
195 : : {
196 : : return ::stoc::uriproc::supportsService(
197 [ # # ]: 0 : getSupportedServiceNames(), serviceName);
198 : : }
199 : :
200 : 0 : css::uno::Sequence< ::rtl::OUString > Parser::getSupportedServiceNames()
201 : : throw (css::uno::RuntimeException)
202 : : {
203 : : return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
204 : 0 : getSupportedServiceNames();
205 : : }
206 : :
207 : 4 : css::uno::Reference< css::uri::XUriReference > Parser::parse(
208 : : ::rtl::OUString const & scheme, ::rtl::OUString const & schemeSpecificPart)
209 : : throw (css::uno::RuntimeException)
210 : : {
211 [ - + ]: 4 : if (!parseSchemeSpecificPart(schemeSpecificPart)) {
212 : 0 : return css::uno::Reference< css::uri::XUriReference >();
213 : : }
214 : : try {
215 [ + - ][ + - ]: 4 : return new UrlReference(scheme, schemeSpecificPart);
[ + - ]
216 [ # # ]: 0 : } catch (::std::bad_alloc &) {
217 : : throw css::uno::RuntimeException(
218 : : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
219 [ # # # # ]: 0 : css::uno::Reference< css::uno::XInterface >());
220 : : }
221 : : }
222 : :
223 : : }
224 : :
225 : : namespace stoc_services { namespace UriSchemeParser_vndDOTsunDOTstarDOTexpand {
226 : :
227 : 4 : css::uno::Reference< css::uno::XInterface > create(
228 : : SAL_UNUSED_PARAMETER css::uno::Reference< css::uno::XComponentContext >
229 : : const &)
230 : : SAL_THROW((css::uno::Exception))
231 : : {
232 : : //TODO: single instance
233 : : try {
234 [ + - ][ + - ]: 4 : return static_cast< ::cppu::OWeakObject * >(new Parser);
235 [ # # ]: 0 : } catch (::std::bad_alloc &) {
236 : : throw css::uno::RuntimeException(
237 : : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
238 [ # # # # ]: 0 : css::uno::Reference< css::uno::XInterface >());
239 : : }
240 : : }
241 : :
242 : 377 : ::rtl::OUString getImplementationName() {
243 : : return ::rtl::OUString(
244 : : RTL_CONSTASCII_USTRINGPARAM(
245 : 377 : "com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
246 : : }
247 : :
248 : 2 : css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
249 : 2 : css::uno::Sequence< ::rtl::OUString > s(1);
250 [ + - ]: 2 : s[0] = ::rtl::OUString(
251 : : RTL_CONSTASCII_USTRINGPARAM(
252 [ + - ]: 4 : "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
253 : 2 : return s;
254 : : }
255 : :
256 : : } }
257 : :
258 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|