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 <rtl/malformeduriexception.hxx>
24 #include <rtl/string.h>
25 #include <rtl/textenc.h>
27 #include <rtl/uri.hxx>
28 #include <rtl/ustring.h>
29 #include <rtl/ustring.hxx>
30 #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 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
[i
];
79 if (bEnd
|| !isAlphanum(c
))
80 throw rtl::MalformedUriException(
81 "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 "UNO URL contains bad descriptor name");
99 if (bEnd
|| !isAlphanum(c
))
100 throw rtl::MalformedUriException(
101 "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 "UNO URL contains bad parameter key");
119 if (bEnd
|| c
== 0x2C) // ','
121 if (!m_aParameters
.emplace(
123 rtl::Uri::decode(rDescriptor
.copy(nStart
,
125 rtl_UriDecodeWithCharset
,
126 RTL_TEXTENCODING_UTF8
)).second
)
127 throw rtl::MalformedUriException(
128 "UNO URL contains duplicated parameter");
138 UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString
const & rDescriptor
):
139 m_pImpl(new Impl(rDescriptor
))
142 UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor
const & rOther
):
143 m_pImpl(rOther
.m_pImpl
->clone())
146 UnoUrlDescriptor::~UnoUrlDescriptor()
151 UnoUrlDescriptor
& UnoUrlDescriptor::operator =(UnoUrlDescriptor
const & rOther
)
153 std::unique_ptr
<Impl
> newImpl(rOther
.m_pImpl
->clone());
155 m_pImpl
= newImpl
.release();
159 rtl::OUString
const & UnoUrlDescriptor::getDescriptor() const
161 return m_pImpl
->m_aDescriptor
;
164 rtl::OUString
const & UnoUrlDescriptor::getName() const
166 return m_pImpl
->m_aName
;
169 bool UnoUrlDescriptor::hasParameter(rtl::OUString
const & rKey
) const
171 return m_pImpl
->m_aParameters
.find(rKey
.toAsciiLowerCase())
172 != m_pImpl
->m_aParameters
.end();
175 rtl::OUString
UnoUrlDescriptor::getParameter(rtl::OUString
const & rKey
) const
177 Impl::Parameters::const_iterator
178 aIt(m_pImpl
->m_aParameters
.find(rKey
.toAsciiLowerCase()));
179 return aIt
== m_pImpl
->m_aParameters
.end() ? rtl::OUString() : aIt
->second
;
185 UnoUrlDescriptor m_aConnection
;
186 UnoUrlDescriptor m_aProtocol
;
187 rtl::OUString m_aObjectName
;
189 Impl
* clone() const { return new Impl(*this); }
191 /** @exception rtl::MalformedUriException
193 static inline Impl
* create(rtl::OUString
const & rUrl
);
196 Impl(rtl::OUString
const & rConnectionDescriptor
,
197 rtl::OUString
const & rProtocolDescriptor
,
198 rtl::OUString
const & rObjectName
):
199 m_aConnection(rConnectionDescriptor
),
200 m_aProtocol(rProtocolDescriptor
),
201 m_aObjectName(rObjectName
)
205 inline UnoUrl::Impl
* UnoUrl::Impl::create(rtl::OUString
const & rUrl
)
207 if (!rUrl
.startsWithIgnoreAsciiCase("uno:"))
208 throw rtl::MalformedUriException("UNO URL does not start with \"uno:\"");
209 sal_Int32 i
= RTL_CONSTASCII_LENGTH("uno:");
210 sal_Int32 j
= rUrl
.indexOf(';', i
);
212 throw rtl::MalformedUriException("UNO URL has too few semicolons");
213 rtl::OUString
aConnection(rUrl
.copy(i
, j
- i
));
215 j
= rUrl
.indexOf(0x3B, i
); // ';'
217 throw rtl::MalformedUriException("UNO URL has too few semicolons");
218 rtl::OUString
aProtocol(rUrl
.copy(i
, j
- i
));
220 if (i
== rUrl
.getLength())
221 throw rtl::MalformedUriException("UNO URL contains empty ObjectName");
222 for (j
= i
; j
< rUrl
.getLength(); ++j
)
224 sal_Unicode c
= rUrl
[j
];
225 if (!isAlphanum(c
) && c
!= 0x21 && c
!= 0x24 // '!', '$'
226 && c
!= 0x26 && c
!= 0x27 && c
!= 0x28 // '&', ''', '('
227 && c
!= 0x29 && c
!= 0x2A && c
!= 0x2B // ')', '*', '+'
228 && c
!= 0x2C && c
!= 0x2D && c
!= 0x2E // ',', '-', '.'
229 && c
!= 0x2F && c
!= 0x3A && c
!= 0x3D // '/', ':', '='
230 && c
!= 0x3F && c
!= 0x40 && c
!= 0x5F // '?', '@', '_'
232 throw rtl::MalformedUriException("UNO URL contains invalid ObjectName");
234 return new Impl(aConnection
, aProtocol
, rUrl
.copy(i
));
237 UnoUrl::UnoUrl(rtl::OUString
const & rUrl
): m_pImpl(Impl::create(rUrl
))
240 UnoUrl::UnoUrl(UnoUrl
const & rOther
): m_pImpl(rOther
.m_pImpl
->clone())
248 UnoUrl
& UnoUrl::operator =(UnoUrl
const & rOther
)
250 std::unique_ptr
<Impl
> newImpl(rOther
.m_pImpl
->clone());
252 m_pImpl
= newImpl
.release();
256 UnoUrlDescriptor
const & UnoUrl::getConnection() const
258 return m_pImpl
->m_aConnection
;
261 UnoUrlDescriptor
const & UnoUrl::getProtocol() const
263 return m_pImpl
->m_aProtocol
;
266 rtl::OUString
const & UnoUrl::getObjectName() const
268 return m_pImpl
->m_aObjectName
;
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */