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