1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647a425cSAndrew Rist  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647a425cSAndrew Rist  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19*647a425cSAndrew Rist  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew Rist 
23cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
24cdf0e10cSrcweir #include "precompiled_stoc.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "sal/config.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "stocservices.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <new>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "com/sun/star/lang/IllegalArgumentException.hpp"
33cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
34cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
35cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
36cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
37cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
38cdf0e10cSrcweir #include "com/sun/star/uri/XUriReference.hpp"
39cdf0e10cSrcweir #include "com/sun/star/uri/XUriSchemeParser.hpp"
40cdf0e10cSrcweir #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
41cdf0e10cSrcweir #include "com/sun/star/util/XMacroExpander.hpp"
42cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
43cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx"
44cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
45cdf0e10cSrcweir #include "rtl/textenc.h"
46cdf0e10cSrcweir #include "rtl/uri.h"
47cdf0e10cSrcweir #include "rtl/uri.hxx"
48cdf0e10cSrcweir #include "rtl/ustring.h"
49cdf0e10cSrcweir #include "rtl/ustring.hxx"
50cdf0e10cSrcweir #include "sal/types.h"
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include "UriReference.hxx"
53cdf0e10cSrcweir #include "supportsService.hxx"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace css = ::com::sun::star;
58cdf0e10cSrcweir 
parseSchemeSpecificPart(::rtl::OUString const & part)59cdf0e10cSrcweir bool parseSchemeSpecificPart(::rtl::OUString const & part) {
60cdf0e10cSrcweir     // Liberally accepts both an empty opaque_part and an opaque_part that
61cdf0e10cSrcweir     // starts with a non-escaped "/":
62cdf0e10cSrcweir     return part.getLength() == 0
63cdf0e10cSrcweir         || ((::rtl::Uri::decode(
64cdf0e10cSrcweir                  part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).
65cdf0e10cSrcweir              getLength())
66cdf0e10cSrcweir             != 0);
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir class UrlReference:
70cdf0e10cSrcweir     public ::cppu::WeakImplHelper1< css::uri::XVndSunStarExpandUrlReference >
71cdf0e10cSrcweir {
72cdf0e10cSrcweir public:
UrlReference(::rtl::OUString const & scheme,::rtl::OUString const & path)73cdf0e10cSrcweir     UrlReference(::rtl::OUString const & scheme, ::rtl::OUString const & path):
74cdf0e10cSrcweir         base_(
75cdf0e10cSrcweir             scheme, false, false, ::rtl::OUString(), path, false,
76cdf0e10cSrcweir             ::rtl::OUString())
77cdf0e10cSrcweir     {}
78cdf0e10cSrcweir 
getUriReference()79cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getUriReference()
80cdf0e10cSrcweir         throw (css::uno::RuntimeException)
81cdf0e10cSrcweir     { return base_.getUriReference(); }
82cdf0e10cSrcweir 
isAbsolute()83cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL isAbsolute() throw (css::uno::RuntimeException)
84cdf0e10cSrcweir     { return base_.isAbsolute(); }
85cdf0e10cSrcweir 
getScheme()86cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getScheme()
87cdf0e10cSrcweir         throw (css::uno::RuntimeException)
88cdf0e10cSrcweir     { return base_.getScheme(); }
89cdf0e10cSrcweir 
getSchemeSpecificPart()90cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getSchemeSpecificPart()
91cdf0e10cSrcweir         throw (css::uno::RuntimeException)
92cdf0e10cSrcweir     { return base_.getSchemeSpecificPart(); }
93cdf0e10cSrcweir 
isHierarchical()94cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL isHierarchical()
95cdf0e10cSrcweir         throw (css::uno::RuntimeException)
96cdf0e10cSrcweir     { return base_.isHierarchical(); }
97cdf0e10cSrcweir 
hasAuthority()98cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasAuthority()
99cdf0e10cSrcweir         throw (css::uno::RuntimeException)
100cdf0e10cSrcweir     { return base_.hasAuthority(); }
101cdf0e10cSrcweir 
getAuthority()102cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getAuthority()
103cdf0e10cSrcweir         throw (css::uno::RuntimeException)
104cdf0e10cSrcweir     { return base_.getAuthority(); }
105cdf0e10cSrcweir 
getPath()106cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getPath()
107cdf0e10cSrcweir         throw (css::uno::RuntimeException)
108cdf0e10cSrcweir     { return base_.getPath(); }
109cdf0e10cSrcweir 
hasRelativePath()110cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasRelativePath()
111cdf0e10cSrcweir         throw (css::uno::RuntimeException)
112cdf0e10cSrcweir     { return base_.hasRelativePath(); }
113cdf0e10cSrcweir 
getPathSegmentCount()114cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL getPathSegmentCount()
115cdf0e10cSrcweir         throw (css::uno::RuntimeException)
116cdf0e10cSrcweir     { return base_.getPathSegmentCount(); }
117cdf0e10cSrcweir 
getPathSegment(sal_Int32 index)118cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getPathSegment(sal_Int32 index)
119cdf0e10cSrcweir         throw (css::uno::RuntimeException)
120cdf0e10cSrcweir     { return base_.getPathSegment(index); }
121cdf0e10cSrcweir 
hasQuery()122cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasQuery() throw (css::uno::RuntimeException)
123cdf0e10cSrcweir     { return base_.hasQuery(); }
124cdf0e10cSrcweir 
getQuery()125cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getQuery()
126cdf0e10cSrcweir         throw (css::uno::RuntimeException)
127cdf0e10cSrcweir     { return base_.getQuery(); }
128cdf0e10cSrcweir 
hasFragment()129cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasFragment() throw (css::uno::RuntimeException)
130cdf0e10cSrcweir     { return base_.hasFragment(); }
131cdf0e10cSrcweir 
getFragment()132cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getFragment()
133cdf0e10cSrcweir         throw (css::uno::RuntimeException)
134cdf0e10cSrcweir     { return base_.getFragment(); }
135cdf0e10cSrcweir 
setFragment(::rtl::OUString const & fragment)136cdf0e10cSrcweir     virtual void SAL_CALL setFragment(::rtl::OUString const & fragment)
137cdf0e10cSrcweir         throw (css::uno::RuntimeException)
138cdf0e10cSrcweir     { base_.setFragment(fragment); }
139cdf0e10cSrcweir 
clearFragment()140cdf0e10cSrcweir     virtual void SAL_CALL clearFragment() throw (css::uno::RuntimeException)
141cdf0e10cSrcweir     { base_.clearFragment(); }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL expand(
144cdf0e10cSrcweir         css::uno::Reference< css::util::XMacroExpander > const & expander)
145cdf0e10cSrcweir         throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir private:
148cdf0e10cSrcweir     UrlReference(UrlReference &); // not defined
149cdf0e10cSrcweir     void operator =(UrlReference); // not defined
150cdf0e10cSrcweir 
~UrlReference()151cdf0e10cSrcweir     virtual ~UrlReference() {}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     stoc::uriproc::UriReference base_;
154cdf0e10cSrcweir };
155cdf0e10cSrcweir 
expand(css::uno::Reference<css::util::XMacroExpander> const & expander)156cdf0e10cSrcweir ::rtl::OUString UrlReference::expand(
157cdf0e10cSrcweir     css::uno::Reference< css::util::XMacroExpander > const & expander)
158cdf0e10cSrcweir     throw (css::lang::IllegalArgumentException, css::uno::RuntimeException)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     OSL_ASSERT(expander.is());
161cdf0e10cSrcweir     return expander->expandMacros(
162cdf0e10cSrcweir         ::rtl::Uri::decode(
163cdf0e10cSrcweir             getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir class Parser: public ::cppu::WeakImplHelper2<
167cdf0e10cSrcweir     css::lang::XServiceInfo, css::uri::XUriSchemeParser >
168cdf0e10cSrcweir {
169cdf0e10cSrcweir public:
Parser()170cdf0e10cSrcweir     Parser() {}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName()
173cdf0e10cSrcweir         throw (css::uno::RuntimeException);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService(
176cdf0e10cSrcweir         ::rtl::OUString const & serviceName)
177cdf0e10cSrcweir         throw (css::uno::RuntimeException);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL
180cdf0e10cSrcweir     getSupportedServiceNames() throw (css::uno::RuntimeException);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
183cdf0e10cSrcweir     parse(
184cdf0e10cSrcweir         ::rtl::OUString const & scheme,
185cdf0e10cSrcweir         ::rtl::OUString const & schemeSpecificPart)
186cdf0e10cSrcweir         throw (css::uno::RuntimeException);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir private:
189cdf0e10cSrcweir     Parser(Parser &); // not defined
190cdf0e10cSrcweir     void operator =(Parser); // not defined
191cdf0e10cSrcweir 
~Parser()192cdf0e10cSrcweir     virtual ~Parser() {}
193cdf0e10cSrcweir };
194cdf0e10cSrcweir 
getImplementationName()195cdf0e10cSrcweir ::rtl::OUString Parser::getImplementationName()
196cdf0e10cSrcweir     throw (css::uno::RuntimeException)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir     return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
199cdf0e10cSrcweir         getImplementationName();
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
supportsService(::rtl::OUString const & serviceName)202cdf0e10cSrcweir ::sal_Bool Parser::supportsService(::rtl::OUString const & serviceName)
203cdf0e10cSrcweir     throw (css::uno::RuntimeException)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     return ::stoc::uriproc::supportsService(
206cdf0e10cSrcweir         getSupportedServiceNames(), serviceName);
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
getSupportedServiceNames()209cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > Parser::getSupportedServiceNames()
210cdf0e10cSrcweir     throw (css::uno::RuntimeException)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
213cdf0e10cSrcweir         getSupportedServiceNames();
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
parse(::rtl::OUString const & scheme,::rtl::OUString const & schemeSpecificPart)216cdf0e10cSrcweir css::uno::Reference< css::uri::XUriReference > Parser::parse(
217cdf0e10cSrcweir     ::rtl::OUString const & scheme, ::rtl::OUString const & schemeSpecificPart)
218cdf0e10cSrcweir     throw (css::uno::RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     if (!parseSchemeSpecificPart(schemeSpecificPart)) {
221cdf0e10cSrcweir         return css::uno::Reference< css::uri::XUriReference >();
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir     try {
224cdf0e10cSrcweir         return new UrlReference(scheme, schemeSpecificPart);
225cdf0e10cSrcweir     } catch (::std::bad_alloc &) {
226cdf0e10cSrcweir         throw css::uno::RuntimeException(
227cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
228cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >());
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir namespace stoc_services { namespace UriSchemeParser_vndDOTsunDOTstarDOTexpand {
235cdf0e10cSrcweir 
create(css::uno::Reference<css::uno::XComponentContext> const &)236cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > create(
237cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const &)
238cdf0e10cSrcweir     SAL_THROW((css::uno::Exception))
239cdf0e10cSrcweir {
240cdf0e10cSrcweir     //TODO: single instance
241cdf0e10cSrcweir     try {
242cdf0e10cSrcweir         return static_cast< ::cppu::OWeakObject * >(new Parser);
243cdf0e10cSrcweir     } catch (::std::bad_alloc &) {
244cdf0e10cSrcweir         throw css::uno::RuntimeException(
245cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
246cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >());
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
getImplementationName()250cdf0e10cSrcweir ::rtl::OUString getImplementationName() {
251cdf0e10cSrcweir     return ::rtl::OUString(
252cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
253cdf0e10cSrcweir             "com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
getSupportedServiceNames()256cdf0e10cSrcweir css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
257cdf0e10cSrcweir     css::uno::Sequence< ::rtl::OUString > s(1);
258cdf0e10cSrcweir     s[0] = ::rtl::OUString(
259cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
260cdf0e10cSrcweir             "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
261cdf0e10cSrcweir     return s;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir } }
265