1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26 
27 #include "stocservices.hxx"
28 
29 #include "supportsService.hxx"
30 
31 #include "com/sun/star/lang/XServiceInfo.hpp"
32 #include "com/sun/star/uno/Exception.hpp"
33 #include "com/sun/star/uno/Reference.hxx"
34 #include "com/sun/star/uno/RuntimeException.hpp"
35 #include "com/sun/star/uno/Sequence.hxx"
36 #include "com/sun/star/uno/XComponentContext.hpp"
37 #include "com/sun/star/uno/XInterface.hpp"
38 #include "com/sun/star/uri/UriReferenceFactory.hpp"
39 #include "com/sun/star/uri/XUriReference.hpp"
40 #include "com/sun/star/uri/XUriReferenceFactory.hpp"
41 #include "com/sun/star/uri/XVndSunStarPkgUrlReferenceFactory.hpp"
42 #include "cppuhelper/implbase2.hxx"
43 #include "cppuhelper/weak.hxx"
44 #include "rtl/string.h"
45 #include "rtl/textenc.h"
46 #include "rtl/uri.h"
47 #include "rtl/uri.hxx"
48 #include "rtl/ustrbuf.hxx"
49 #include "rtl/ustring.hxx"
50 #include "sal/types.h"
51 
52 #include <new>
53 
54 namespace css = com::sun::star;
55 
56 namespace {
57 
58 class Factory: public cppu::WeakImplHelper2<
59     css::lang::XServiceInfo, css::uri::XVndSunStarPkgUrlReferenceFactory >
60 {
61 public:
Factory(css::uno::Reference<css::uno::XComponentContext> const & context)62     explicit Factory(
63         css::uno::Reference< css::uno::XComponentContext > const & context):
64         m_context(context) {}
65 
66     virtual rtl::OUString SAL_CALL getImplementationName()
67         throw (css::uno::RuntimeException);
68 
69     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
70         throw (css::uno::RuntimeException);
71 
72     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
73     getSupportedServiceNames() throw (css::uno::RuntimeException);
74 
75     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
76     createVndSunStarPkgUrlReference(
77         css::uno::Reference< css::uri::XUriReference > const & authority)
78         throw (css::uno::RuntimeException);
79 
80 private:
81     Factory(Factory &); // not implemented
82     void operator =(Factory); // not implemented
83 
~Factory()84     virtual ~Factory() {}
85 
86     css::uno::Reference< css::uno::XComponentContext > m_context;
87 };
88 
getImplementationName()89 rtl::OUString Factory::getImplementationName()
90     throw (css::uno::RuntimeException)
91 {
92     return
93         stoc_services::VndSunStarPkgUrlReferenceFactory::
94         getImplementationName();
95 }
96 
supportsService(rtl::OUString const & serviceName)97 sal_Bool Factory::supportsService(rtl::OUString const & serviceName)
98     throw (css::uno::RuntimeException)
99 {
100     return stoc::uriproc::supportsService(
101         getSupportedServiceNames(), serviceName);
102 }
103 
getSupportedServiceNames()104 css::uno::Sequence< rtl::OUString > Factory::getSupportedServiceNames()
105     throw (css::uno::RuntimeException)
106 {
107     return stoc_services::VndSunStarPkgUrlReferenceFactory::
108         getSupportedServiceNames();
109 }
110 
111 css::uno::Reference< css::uri::XUriReference >
createVndSunStarPkgUrlReference(css::uno::Reference<css::uri::XUriReference> const & authority)112 Factory::createVndSunStarPkgUrlReference(
113     css::uno::Reference< css::uri::XUriReference > const & authority)
114     throw (css::uno::RuntimeException)
115 {
116     OSL_ASSERT(authority.is());
117     if (authority->isAbsolute() && !authority->hasFragment()) {
118         rtl::OUStringBuffer buf;
119         buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.pkg://"));
120         buf.append(
121             rtl::Uri::encode(
122                 authority->getUriReference(), rtl_UriCharClassRegName,
123                 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8));
124         css::uno::Reference< css::uri::XUriReference > uriRef(
125             css::uri::UriReferenceFactory::create(m_context)->parse(
126                 buf.makeStringAndClear()));
127         OSL_ASSERT(uriRef.is());
128         return uriRef;
129     } else {
130         return css::uno::Reference< css::uri::XUriReference >();
131     }
132 }
133 
134 }
135 
136 namespace stoc_services { namespace VndSunStarPkgUrlReferenceFactory
137 {
138 
create(css::uno::Reference<css::uno::XComponentContext> const & context)139 css::uno::Reference< css::uno::XInterface > create(
140     css::uno::Reference< css::uno::XComponentContext > const & context)
141     SAL_THROW((css::uno::Exception))
142 {
143     try {
144         return static_cast< cppu::OWeakObject * >(new Factory(context));
145     } catch (std::bad_alloc &) {
146         throw css::uno::RuntimeException(
147             rtl::OUString::createFromAscii("std::bad_alloc"), 0);
148     }
149 }
150 
getImplementationName()151 rtl::OUString getImplementationName() {
152     return rtl::OUString::createFromAscii(
153         "com.sun.star.comp.uri.VndSunStarPkgUrlReferenceFactory");
154 }
155 
getSupportedServiceNames()156 css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
157     css::uno::Sequence< rtl::OUString > s(1);
158     s[0] = rtl::OUString::createFromAscii(
159         "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory");
160     return s;
161 }
162 
163 } }
164