1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_stoc.hxx"
29 
30 #include "sal/config.h"
31 
32 #include "stocservices.hxx"
33 
34 #include <new>
35 
36 #include "com/sun/star/lang/IllegalArgumentException.hpp"
37 #include "com/sun/star/lang/XServiceInfo.hpp"
38 #include "com/sun/star/uno/Reference.hxx"
39 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include "com/sun/star/uno/Sequence.hxx"
41 #include "com/sun/star/uno/XInterface.hpp"
42 #include "com/sun/star/uri/XUriReference.hpp"
43 #include "com/sun/star/uri/XUriSchemeParser.hpp"
44 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
45 #include "com/sun/star/util/XMacroExpander.hpp"
46 #include "cppuhelper/implbase1.hxx"
47 #include "cppuhelper/implbase2.hxx"
48 #include "cppuhelper/weak.hxx"
49 #include "rtl/textenc.h"
50 #include "rtl/uri.h"
51 #include "rtl/uri.hxx"
52 #include "rtl/ustring.h"
53 #include "rtl/ustring.hxx"
54 #include "sal/types.h"
55 
56 #include "UriReference.hxx"
57 #include "supportsService.hxx"
58 
59 namespace {
60 
61 namespace css = ::com::sun::star;
62 
63 bool parseSchemeSpecificPart(::rtl::OUString const & part) {
64     // Liberally accepts both an empty opaque_part and an opaque_part that
65     // starts with a non-escaped "/":
66     return part.getLength() == 0
67         || ((::rtl::Uri::decode(
68                  part, ::rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8).
69              getLength())
70             != 0);
71 }
72 
73 class UrlReference:
74     public ::cppu::WeakImplHelper1< css::uri::XVndSunStarExpandUrlReference >
75 {
76 public:
77     UrlReference(::rtl::OUString const & scheme, ::rtl::OUString const & path):
78         base_(
79             scheme, false, false, ::rtl::OUString(), path, false,
80             ::rtl::OUString())
81     {}
82 
83     virtual ::rtl::OUString SAL_CALL getUriReference()
84         throw (css::uno::RuntimeException)
85     { return base_.getUriReference(); }
86 
87     virtual ::sal_Bool SAL_CALL isAbsolute() throw (css::uno::RuntimeException)
88     { return base_.isAbsolute(); }
89 
90     virtual ::rtl::OUString SAL_CALL getScheme()
91         throw (css::uno::RuntimeException)
92     { return base_.getScheme(); }
93 
94     virtual ::rtl::OUString SAL_CALL getSchemeSpecificPart()
95         throw (css::uno::RuntimeException)
96     { return base_.getSchemeSpecificPart(); }
97 
98     virtual ::sal_Bool SAL_CALL isHierarchical()
99         throw (css::uno::RuntimeException)
100     { return base_.isHierarchical(); }
101 
102     virtual ::sal_Bool SAL_CALL hasAuthority()
103         throw (css::uno::RuntimeException)
104     { return base_.hasAuthority(); }
105 
106     virtual ::rtl::OUString SAL_CALL getAuthority()
107         throw (css::uno::RuntimeException)
108     { return base_.getAuthority(); }
109 
110     virtual ::rtl::OUString SAL_CALL getPath()
111         throw (css::uno::RuntimeException)
112     { return base_.getPath(); }
113 
114     virtual ::sal_Bool SAL_CALL hasRelativePath()
115         throw (css::uno::RuntimeException)
116     { return base_.hasRelativePath(); }
117 
118     virtual ::sal_Int32 SAL_CALL getPathSegmentCount()
119         throw (css::uno::RuntimeException)
120     { return base_.getPathSegmentCount(); }
121 
122     virtual ::rtl::OUString SAL_CALL getPathSegment(sal_Int32 index)
123         throw (css::uno::RuntimeException)
124     { return base_.getPathSegment(index); }
125 
126     virtual ::sal_Bool SAL_CALL hasQuery() throw (css::uno::RuntimeException)
127     { return base_.hasQuery(); }
128 
129     virtual ::rtl::OUString SAL_CALL getQuery()
130         throw (css::uno::RuntimeException)
131     { return base_.getQuery(); }
132 
133     virtual ::sal_Bool SAL_CALL hasFragment() throw (css::uno::RuntimeException)
134     { return base_.hasFragment(); }
135 
136     virtual ::rtl::OUString SAL_CALL getFragment()
137         throw (css::uno::RuntimeException)
138     { return base_.getFragment(); }
139 
140     virtual void SAL_CALL setFragment(::rtl::OUString const & fragment)
141         throw (css::uno::RuntimeException)
142     { base_.setFragment(fragment); }
143 
144     virtual void SAL_CALL clearFragment() throw (css::uno::RuntimeException)
145     { base_.clearFragment(); }
146 
147     virtual ::rtl::OUString SAL_CALL expand(
148         css::uno::Reference< css::util::XMacroExpander > const & expander)
149         throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
150 
151 private:
152     UrlReference(UrlReference &); // not defined
153     void operator =(UrlReference); // not defined
154 
155     virtual ~UrlReference() {}
156 
157     stoc::uriproc::UriReference base_;
158 };
159 
160 ::rtl::OUString UrlReference::expand(
161     css::uno::Reference< css::util::XMacroExpander > const & expander)
162     throw (css::lang::IllegalArgumentException, css::uno::RuntimeException)
163 {
164     OSL_ASSERT(expander.is());
165     return expander->expandMacros(
166         ::rtl::Uri::decode(
167             getPath(), ::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8));
168 }
169 
170 class Parser: public ::cppu::WeakImplHelper2<
171     css::lang::XServiceInfo, css::uri::XUriSchemeParser >
172 {
173 public:
174     Parser() {}
175 
176     virtual ::rtl::OUString SAL_CALL getImplementationName()
177         throw (css::uno::RuntimeException);
178 
179     virtual ::sal_Bool SAL_CALL supportsService(
180         ::rtl::OUString const & serviceName)
181         throw (css::uno::RuntimeException);
182 
183     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL
184     getSupportedServiceNames() throw (css::uno::RuntimeException);
185 
186     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
187     parse(
188         ::rtl::OUString const & scheme,
189         ::rtl::OUString const & schemeSpecificPart)
190         throw (css::uno::RuntimeException);
191 
192 private:
193     Parser(Parser &); // not defined
194     void operator =(Parser); // not defined
195 
196     virtual ~Parser() {}
197 };
198 
199 ::rtl::OUString Parser::getImplementationName()
200     throw (css::uno::RuntimeException)
201 {
202     return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
203         getImplementationName();
204 }
205 
206 ::sal_Bool Parser::supportsService(::rtl::OUString const & serviceName)
207     throw (css::uno::RuntimeException)
208 {
209     return ::stoc::uriproc::supportsService(
210         getSupportedServiceNames(), serviceName);
211 }
212 
213 css::uno::Sequence< ::rtl::OUString > Parser::getSupportedServiceNames()
214     throw (css::uno::RuntimeException)
215 {
216     return ::stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTexpand::
217         getSupportedServiceNames();
218 }
219 
220 css::uno::Reference< css::uri::XUriReference > Parser::parse(
221     ::rtl::OUString const & scheme, ::rtl::OUString const & schemeSpecificPart)
222     throw (css::uno::RuntimeException)
223 {
224     if (!parseSchemeSpecificPart(schemeSpecificPart)) {
225         return css::uno::Reference< css::uri::XUriReference >();
226     }
227     try {
228         return new UrlReference(scheme, schemeSpecificPart);
229     } catch (::std::bad_alloc &) {
230         throw css::uno::RuntimeException(
231             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
232             css::uno::Reference< css::uno::XInterface >());
233     }
234 }
235 
236 }
237 
238 namespace stoc_services { namespace UriSchemeParser_vndDOTsunDOTstarDOTexpand {
239 
240 css::uno::Reference< css::uno::XInterface > create(
241     css::uno::Reference< css::uno::XComponentContext > const &)
242     SAL_THROW((css::uno::Exception))
243 {
244     //TODO: single instance
245     try {
246         return static_cast< ::cppu::OWeakObject * >(new Parser);
247     } catch (::std::bad_alloc &) {
248         throw css::uno::RuntimeException(
249             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
250             css::uno::Reference< css::uno::XInterface >());
251     }
252 }
253 
254 ::rtl::OUString getImplementationName() {
255     return ::rtl::OUString(
256         RTL_CONSTASCII_USTRINGPARAM(
257             "com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
258 }
259 
260 css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
261     css::uno::Sequence< ::rtl::OUString > s(1);
262     s[0] = ::rtl::OUString(
263         RTL_CONSTASCII_USTRINGPARAM(
264             "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTexpand"));
265     return s;
266 }
267 
268 } }
269