xref: /trunk/main/stoc/source/uriproc/VndSunStarPkgUrlReferenceFactory.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
68     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName);
69 
70     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
71     getSupportedServiceNames();
72 
73     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
74     createVndSunStarPkgUrlReference(
75         css::uno::Reference< css::uri::XUriReference > const & authority);
76 
77 private:
78     Factory(Factory &); // not implemented
79     void operator =(Factory); // not implemented
80 
~Factory()81     virtual ~Factory() {}
82 
83     css::uno::Reference< css::uno::XComponentContext > m_context;
84 };
85 
getImplementationName()86 rtl::OUString Factory::getImplementationName()
87 {
88     return
89         stoc_services::VndSunStarPkgUrlReferenceFactory::
90         getImplementationName();
91 }
92 
supportsService(rtl::OUString const & serviceName)93 sal_Bool Factory::supportsService(rtl::OUString const & serviceName)
94 {
95     return stoc::uriproc::supportsService(
96         getSupportedServiceNames(), serviceName);
97 }
98 
getSupportedServiceNames()99 css::uno::Sequence< rtl::OUString > Factory::getSupportedServiceNames()
100 {
101     return stoc_services::VndSunStarPkgUrlReferenceFactory::
102         getSupportedServiceNames();
103 }
104 
105 css::uno::Reference< css::uri::XUriReference >
createVndSunStarPkgUrlReference(css::uno::Reference<css::uri::XUriReference> const & authority)106 Factory::createVndSunStarPkgUrlReference(
107     css::uno::Reference< css::uri::XUriReference > const & authority)
108 {
109     OSL_ASSERT(authority.is());
110     if (authority->isAbsolute() && !authority->hasFragment()) {
111         rtl::OUStringBuffer buf;
112         buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.pkg://"));
113         buf.append(
114             rtl::Uri::encode(
115                 authority->getUriReference(), rtl_UriCharClassRegName,
116                 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8));
117         css::uno::Reference< css::uri::XUriReference > uriRef(
118             css::uri::UriReferenceFactory::create(m_context)->parse(
119                 buf.makeStringAndClear()));
120         OSL_ASSERT(uriRef.is());
121         return uriRef;
122     } else {
123         return css::uno::Reference< css::uri::XUriReference >();
124     }
125 }
126 
127 }
128 
129 namespace stoc_services { namespace VndSunStarPkgUrlReferenceFactory
130 {
131 
create(css::uno::Reference<css::uno::XComponentContext> const & context)132 css::uno::Reference< css::uno::XInterface > create(
133     css::uno::Reference< css::uno::XComponentContext > const & context)
134 {
135     try {
136         return static_cast< cppu::OWeakObject * >(new Factory(context));
137     } catch (std::bad_alloc &) {
138         throw css::uno::RuntimeException(
139             rtl::OUString::createFromAscii("std::bad_alloc"), 0);
140     }
141 }
142 
getImplementationName()143 rtl::OUString getImplementationName() {
144     return rtl::OUString::createFromAscii(
145         "com.sun.star.comp.uri.VndSunStarPkgUrlReferenceFactory");
146 }
147 
getSupportedServiceNames()148 css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
149     css::uno::Sequence< rtl::OUString > s(1);
150     s[0] = rtl::OUString::createFromAscii(
151         "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory");
152     return s;
153 }
154 
155 } }
156