1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <tools/urlobj.hxx>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/util/URL.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
namespace {
class URLTransformer : public ::cppu::WeakImplHelper< css::util::XURLTransformer, css::lang::XServiceInfo>
{
public:
URLTransformer() {}
virtual OUString SAL_CALL getImplementationName() override
{
return "com.sun.star.comp.framework.URLTransformer";
}
virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
{
return cppu::supportsService(this, ServiceName);
}
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
{
return {"com.sun.star.util.URLTransformer"};
}
virtual sal_Bool SAL_CALL parseStrict( css::util::URL& aURL ) override;
virtual sal_Bool SAL_CALL parseSmart( css::util::URL& aURL, const OUString& sSmartProtocol ) override;
virtual sal_Bool SAL_CALL assemble( css::util::URL& aURL ) override;
virtual OUString SAL_CALL getPresentation( const css::util::URL& aURL, sal_Bool bWithPassword ) override;
};
void lcl_ParserHelper(INetURLObject& _rParser, css::util::URL& _rURL,bool _bUseIntern)
{
// Get all information about this URL.
_rURL.Protocol = INetURLObject::GetScheme( _rParser.GetProtocol() );
_rURL.User = _rParser.GetUser ( INetURLObject::DecodeMechanism::WithCharset );
_rURL.Password = _rParser.GetPass ( INetURLObject::DecodeMechanism::WithCharset );
_rURL.Server = _rParser.GetHost ( INetURLObject::DecodeMechanism::WithCharset );
_rURL.Port = static_cast<sal_Int16>(_rParser.GetPort());
sal_Int32 nCount = _rParser.getSegmentCount( false );
if ( nCount > 0 )
{
// Don't add last segment as it is the name!
--nCount;
OUStringBuffer aPath(128);
for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ )
{
aPath.append( '/');
aPath.append( _rParser.getName( nIndex, false, INetURLObject::DecodeMechanism::NONE ));
}
if ( nCount > 0 )
aPath.append( '/' ); // final slash!
_rURL.Path = aPath.makeStringAndClear();
_rURL.Name = _rParser.getName( INetURLObject::LAST_SEGMENT, false, INetURLObject::DecodeMechanism::NONE );
}
else
{
_rURL.Path = _rParser.GetURLPath( INetURLObject::DecodeMechanism::NONE );
_rURL.Name = _rParser.GetLastName();
}
_rURL.Arguments = _rParser.GetParam();
_rURL.Mark = _rParser.GetMark( INetURLObject::DecodeMechanism::WithCharset );
// INetURLObject supports only an intelligent method of parsing URL's. So write
// back Complete to have a valid encoded URL in all cases!
_rURL.Complete = _rParser.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( _bUseIntern )
_rURL.Complete = _rURL.Complete.intern();
_rParser.SetMark ( OUString() );
_rParser.SetParam( OUString() );
_rURL.Main = _rParser.GetMainURL( INetURLObject::DecodeMechanism::NONE );
}
// XURLTransformer
sal_Bool SAL_CALL URLTransformer::parseStrict( css::util::URL& aURL )
{
// Safe impossible cases.
if ( aURL.Complete.isEmpty() )
{
return false;
}
// Try to extract the protocol
sal_Int32 nURLIndex = aURL.Complete.indexOf( ':' );
OUString aProtocol;
if ( nURLIndex > 1 )
{
aProtocol = aURL.Complete.copy( 0, nURLIndex+1 );
// If INetURLObject knows this protocol let it parse
if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INetProtocol::NotValid )
{
// Initialize parser with given URL.
INetURLObject aParser( aURL.Complete );
// Get all information about this URL.
INetProtocol eINetProt = aParser.GetProtocol();
if ( eINetProt == INetProtocol::NotValid )
{
return false;
}
else if ( !aParser.HasError() )
{
lcl_ParserHelper(aParser,aURL,false);
// Return "URL is parsed".
return true;
}
}
else
{
// Minimal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
// in framework!
aURL.Protocol = aProtocol;
aURL.Main = aURL.Complete;
aURL.Path = aURL.Complete.copy( nURLIndex+1 );
// Return "URL is parsed".
return true;
}
}
return false;
}
// XURLTransformer
sal_Bool SAL_CALL URLTransformer::parseSmart( css::util::URL& aURL,
const OUString& sSmartProtocol )
{
// Safe impossible cases.
if ( aURL.Complete.isEmpty() )
{
return false;
}
// Initialize parser with given URL.
INetURLObject aParser;
aParser.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol ));
bool bOk = aParser.SetSmartURL( aURL.Complete );
if ( bOk )
{
lcl_ParserHelper(aParser,aURL,true);
// Return "URL is parsed".
return true;
}
else
{
// Minimal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
// in framework!
if ( INetURLObject::CompareProtocolScheme( sSmartProtocol ) == INetProtocol::NotValid )
{
// Try to extract the protocol
sal_Int32 nIndex = aURL.Complete.indexOf( ':' );
OUString aProtocol;
if ( nIndex > 1 )
{
aProtocol = aURL.Complete.copy( 0, nIndex+1 );
// If INetURLObject knows this protocol something is wrong as detected before =>
// give up and return false!
if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INetProtocol::NotValid )
return false;
else
aURL.Protocol = aProtocol;
}
else
return false;
aURL.Main = aURL.Complete;
aURL.Path = aURL.Complete.copy( nIndex+1 );
return true;
}
else
return false;
}
}
// XURLTransformer
sal_Bool SAL_CALL URLTransformer::assemble( css::util::URL& aURL )
{
// Initialize parser.
INetURLObject aParser;
if ( INetURLObject::CompareProtocolScheme( aURL.Protocol ) != INetProtocol::NotValid )
{
OUStringBuffer aCompletePath( aURL.Path );
// Concat the name if it is provided, just support a final slash
if ( !aURL.Name.isEmpty() )
{
sal_Int32 nIndex = aURL.Path.lastIndexOf( '/' );
if ( nIndex == ( aURL.Path.getLength() -1 ))
aCompletePath.append( aURL.Name );
else
{
aCompletePath.append( '/' );
aCompletePath.append( aURL.Name );
}
}
bool bResult = aParser.ConcatData(
INetURLObject::CompareProtocolScheme( aURL.Protocol ) ,
aURL.User ,
aURL.Password ,
aURL.Server ,
aURL.Port ,
aCompletePath.makeStringAndClear() );
if ( !bResult )
return false;
// First parse URL WITHOUT ...
aURL.Main = aParser.GetMainURL( INetURLObject::DecodeMechanism::NONE );
// ...and then WITH parameter and mark.
aParser.SetParam( aURL.Arguments);
aParser.SetMark ( aURL.Mark, INetURLObject::EncodeMechanism::All );
aURL.Complete = aParser.GetMainURL( INetURLObject::DecodeMechanism::NONE );
// Return "URL is assembled".
return true;
}
else if ( !aURL.Protocol.isEmpty() )
{
// Minimal support for unknown protocols
aURL.Complete = aURL.Protocol + aURL.Path;
aURL.Main = aURL.Complete;
return true;
}
return false;
}
// XURLTransformer
OUString SAL_CALL URLTransformer::getPresentation( const css::util::URL& aURL,
sal_Bool bWithPassword )
{
// Safe impossible cases.
if ( aURL.Complete.isEmpty() )
{
return OUString();
}
// Check given URL
css::util::URL aTestURL = aURL;
bool bParseResult = parseSmart( aTestURL, aTestURL.Protocol );
if ( bParseResult )
{
if ( !bWithPassword && !aTestURL.Password.isEmpty() )
{
// Exchange password text with other placeholder string
aTestURL.Password = "<******>";
assemble( aTestURL );
}
// Convert internal URLs to "praesentation"-URLs!
OUString sPraesentationURL;
INetURLObject::translateToExternal( aTestURL.Complete, sPraesentationURL, INetURLObject::DecodeMechanism::Unambiguous );
return sPraesentationURL;
}
else
return OUString();
}
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_framework_URLTransformer_get_implementation(<--- The function 'com_sun_star_comp_framework_URLTransformer_get_implementation' is never used.
css::uno::XComponentContext *,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new URLTransformer());
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|