1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "cppuhelper/unourl.hxx"
23 #include "osl/diagnose.h"
24 #include "rtl/malformeduriexception.hxx"
25 #include "rtl/string.h"
26 #include "rtl/textenc.h"
28 #include "rtl/uri.hxx"
29 #include "rtl/ustring.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
36 using cppu::UnoUrlDescriptor
;
40 inline bool isAlphanum(sal_Unicode c
)
42 return (c
>= 0x30 && c
<= 0x39) // '0'--'9'
43 || (c
>= 0x41 && c
<= 0x5A) // 'A'--'Z'
44 || (c
>= 0x61 && c
<= 0x7A); // 'a'--'z'
49 class UnoUrlDescriptor::Impl
52 typedef std::map
< rtl::OUString
, rtl::OUString
> Parameters
;
54 rtl::OUString m_aDescriptor
;
55 rtl::OUString m_aName
;
56 Parameters m_aParameters
;
58 /** @exception rtl::MalformedUriException
60 explicit inline Impl(rtl::OUString
const & m_aDescriptor
);
62 inline Impl
* clone() const { return new Impl(*this); }
65 inline UnoUrlDescriptor::Impl::Impl(rtl::OUString
const & rDescriptor
)
67 m_aDescriptor
= rDescriptor
;
68 enum State
{ STATE_NAME0
, STATE_NAME
, STATE_KEY0
, STATE_KEY
, STATE_VALUE
};
69 State eState
= STATE_NAME0
;
72 for (sal_Int32 i
= 0;; ++i
)
74 bool bEnd
= i
== rDescriptor
.getLength();
75 sal_Unicode c
= bEnd
? 0 : rDescriptor
.getStr()[i
];
79 if (bEnd
|| !isAlphanum(c
))
80 throw rtl::MalformedUriException(
81 rtl::OUString("UNO URL contains bad descriptor name"));
87 if (bEnd
|| c
== 0x2C) // ','
90 = rDescriptor
.copy(nStart
, i
- nStart
).toAsciiLowerCase();
93 else if (!isAlphanum(c
))
94 throw rtl::MalformedUriException(
95 rtl::OUString("UNO URL contains bad descriptor name"));
99 if (bEnd
|| !isAlphanum(c
))
100 throw rtl::MalformedUriException(
101 rtl::OUString("UNO URL contains bad parameter key"));
107 if (c
== 0x3D) // '='
109 aKey
= rDescriptor
.copy(nStart
, i
- nStart
).toAsciiLowerCase();
111 eState
= STATE_VALUE
;
113 else if (bEnd
|| !isAlphanum(c
))
114 throw rtl::MalformedUriException(
115 rtl::OUString("UNO URL contains bad parameter key"));
119 if (bEnd
|| c
== 0x2C) // ','
121 if (!m_aParameters
.insert(
122 Parameters::value_type(
124 rtl::Uri::decode(rDescriptor
.copy(nStart
,
126 rtl_UriDecodeWithCharset
,
127 RTL_TEXTENCODING_UTF8
))).second
)
128 throw rtl::MalformedUriException(
129 rtl::OUString("UNO URL contains duplicated parameter"));
139 UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString
const & rDescriptor
):
140 m_xImpl(new Impl(rDescriptor
))
143 SAL_WNODEPRECATED_DECLARATIONS_PUSH
144 UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr
< Impl
> & rImpl
):
147 SAL_WNODEPRECATED_DECLARATIONS_POP
149 UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor
const & rOther
):
150 m_xImpl(rOther
.m_xImpl
->clone())
153 UnoUrlDescriptor::~UnoUrlDescriptor()
156 UnoUrlDescriptor
& UnoUrlDescriptor::operator =(UnoUrlDescriptor
const & rOther
)
158 m_xImpl
.reset(rOther
.m_xImpl
->clone());
162 rtl::OUString
const & UnoUrlDescriptor::getDescriptor() const
164 return m_xImpl
->m_aDescriptor
;
167 rtl::OUString
const & UnoUrlDescriptor::getName() const
169 return m_xImpl
->m_aName
;
172 bool UnoUrlDescriptor::hasParameter(rtl::OUString
const & rKey
) const
174 return m_xImpl
->m_aParameters
.find(rKey
.toAsciiLowerCase())
175 != m_xImpl
->m_aParameters
.end();
178 rtl::OUString
UnoUrlDescriptor::getParameter(rtl::OUString
const & rKey
) const
180 Impl::Parameters::const_iterator
181 aIt(m_xImpl
->m_aParameters
.find(rKey
.toAsciiLowerCase()));
182 return aIt
== m_xImpl
->m_aParameters
.end() ? rtl::OUString() : aIt
->second
;
188 UnoUrlDescriptor m_aConnection
;
189 UnoUrlDescriptor m_aProtocol
;
190 rtl::OUString m_aObjectName
;
192 inline Impl
* clone() const { return new Impl(*this); }
194 /** @exception rtl::MalformedUriException
196 static inline Impl
* create(rtl::OUString
const & rUrl
);
199 SAL_WNODEPRECATED_DECLARATIONS_PUSH
200 Impl(std::auto_ptr
< UnoUrlDescriptor::Impl
> & rConnection
,
201 std::auto_ptr
< UnoUrlDescriptor::Impl
> & rProtocol
,
202 rtl::OUString
const & rObjectName
):
203 m_aConnection(rConnection
),
204 m_aProtocol(rProtocol
),
205 m_aObjectName(rObjectName
)
207 SAL_WNODEPRECATED_DECLARATIONS_POP
210 inline UnoUrl::Impl
* UnoUrl::Impl::create(rtl::OUString
const & rUrl
)
212 if (!rUrl
.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("uno:"), 0))
213 throw rtl::MalformedUriException(
214 rtl::OUString("UNO URL does not start with \"uno:\""));
215 sal_Int32 i
= RTL_CONSTASCII_LENGTH("uno:");
216 sal_Int32 j
= rUrl
.indexOf(';', i
);
218 throw rtl::MalformedUriException(
219 rtl::OUString("UNO URL has too few semicolons"));
220 SAL_WNODEPRECATED_DECLARATIONS_PUSH
221 std::auto_ptr
< UnoUrlDescriptor::Impl
>
222 xConnection(new UnoUrlDescriptor::Impl(rUrl
.copy(i
, j
- i
)));
223 SAL_WNODEPRECATED_DECLARATIONS_POP
225 j
= rUrl
.indexOf(0x3B, i
); // ';'
227 throw rtl::MalformedUriException(
228 rtl::OUString("UNO URL has too few semicolons"));
229 SAL_WNODEPRECATED_DECLARATIONS_PUSH
230 std::auto_ptr
< UnoUrlDescriptor::Impl
>
231 xProtocol(new UnoUrlDescriptor::Impl(rUrl
.copy(i
, j
- i
)));
232 SAL_WNODEPRECATED_DECLARATIONS_POP
234 if (i
== rUrl
.getLength())
235 throw rtl::MalformedUriException(
236 rtl::OUString("UNO URL contains empty ObjectName"));
237 for (j
= i
; j
< rUrl
.getLength(); ++j
)
239 sal_Unicode c
= rUrl
.getStr()[j
];
240 if (!isAlphanum(c
) && c
!= 0x21 && c
!= 0x24 // '!', '$'
241 && c
!= 0x26 && c
!= 0x27 && c
!= 0x28 // '&', ''', '('
242 && c
!= 0x28 && c
!= 0x2A && c
!= 0x2B // ')', '*', '+'
243 && c
!= 0x2C && c
!= 0x2D && c
!= 0x2E // ',', '-', '.'
244 && c
!= 0x2F && c
!= 0x3A && c
!= 0x3D // '/', ':', '='
245 && c
!= 0x3F && c
!= 0x40 && c
!= 0x5F // '?', '@', '_'
247 throw rtl::MalformedUriException(
248 rtl::OUString("UNO URL contains invalid ObjectName"));
250 return new Impl(xConnection
, xProtocol
, rUrl
.copy(i
));
253 UnoUrl::UnoUrl(rtl::OUString
const & rUrl
): m_xImpl(Impl::create(rUrl
))
256 UnoUrl::UnoUrl(UnoUrl
const & rOther
): m_xImpl(rOther
.m_xImpl
->clone())
262 UnoUrl
& UnoUrl::operator =(UnoUrl
const & rOther
)
264 m_xImpl
.reset(rOther
.m_xImpl
->clone());
268 UnoUrlDescriptor
const & UnoUrl::getConnection() const
270 return m_xImpl
->m_aConnection
;
273 UnoUrlDescriptor
const & UnoUrl::getProtocol() const
275 return m_xImpl
->m_aProtocol
;
278 rtl::OUString
const & UnoUrl::getObjectName() const
280 return m_xImpl
->m_aObjectName
;
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */