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