Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "cppuhelper/unourl.hxx"
22 :
23 : #include "osl/diagnose.h"
24 : #include "rtl/malformeduriexception.hxx"
25 : #include "rtl/string.h"
26 : #include "rtl/textenc.h"
27 : #include "rtl/uri.h"
28 : #include "rtl/uri.hxx"
29 : #include "rtl/ustring.h"
30 : #include "rtl/ustring.hxx"
31 : #include "sal/types.h"
32 :
33 : #include <map>
34 :
35 : using cppu::UnoUrl;
36 : using cppu::UnoUrlDescriptor;
37 :
38 : namespace {
39 :
40 2261 : inline bool isAlphanum(sal_Unicode c)
41 : {
42 2227 : return (c >= 0x30 && c <= 0x39) // '0'--'9'
43 2227 : || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
44 4372 : || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
45 : }
46 :
47 : }
48 :
49 279 : class UnoUrlDescriptor::Impl
50 : {
51 : public:
52 : typedef std::map< rtl::OUString, rtl::OUString > Parameters;
53 :
54 : rtl::OUString m_aDescriptor;
55 : rtl::OUString m_aName;
56 : Parameters m_aParameters;
57 :
58 : /** @exception rtl::MalformedUriException
59 : */
60 : explicit inline Impl(rtl::OUString const & m_aDescriptor);
61 :
62 0 : inline Impl * clone() const { return new Impl(*this); }
63 : };
64 :
65 301 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
66 : {
67 290 : m_aDescriptor = rDescriptor;
68 : enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
69 290 : State eState = STATE_NAME0;
70 290 : sal_Int32 nStart = 0;
71 290 : rtl::OUString aKey;
72 6094 : for (sal_Int32 i = 0;; ++i)
73 : {
74 6094 : bool bEnd = i == rDescriptor.getLength();
75 6094 : sal_Unicode c = bEnd ? 0 : rDescriptor.getStr()[i];
76 6094 : switch (eState)
77 : {
78 : case STATE_NAME0:
79 290 : if (bEnd || !isAlphanum(c))
80 : throw rtl::MalformedUriException(
81 1 : rtl::OUString("UNO URL contains bad descriptor name"));
82 289 : nStart = i;
83 289 : eState = STATE_NAME;
84 289 : break;
85 :
86 : case STATE_NAME:
87 959 : if (bEnd || c == 0x2C) // ','
88 : {
89 : m_aName
90 287 : = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
91 287 : eState = STATE_KEY0;
92 : }
93 672 : else if (!isAlphanum(c))
94 : throw rtl::MalformedUriException(
95 2 : rtl::OUString("UNO URL contains bad descriptor name"));
96 957 : break;
97 :
98 : case STATE_KEY0:
99 233 : if (bEnd || !isAlphanum(c))
100 : throw rtl::MalformedUriException(
101 5 : rtl::OUString("UNO URL contains bad parameter key"));
102 228 : nStart = i;
103 228 : eState = STATE_KEY;
104 228 : break;
105 :
106 : case STATE_KEY:
107 775 : if (c == 0x3D) // '='
108 : {
109 226 : aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
110 226 : nStart = i + 1;
111 226 : eState = STATE_VALUE;
112 : }
113 549 : else if (bEnd || !isAlphanum(c))
114 : throw rtl::MalformedUriException(
115 2 : rtl::OUString("UNO URL contains bad parameter key"));
116 773 : break;
117 :
118 : case STATE_VALUE:
119 3837 : if (bEnd || c == 0x2C) // ','
120 : {
121 452 : if (!m_aParameters.insert(
122 : Parameters::value_type(
123 : aKey,
124 : rtl::Uri::decode(rDescriptor.copy(nStart,
125 : i - nStart),
126 : rtl_UriDecodeWithCharset,
127 452 : RTL_TEXTENCODING_UTF8))).second)
128 : throw rtl::MalformedUriException(
129 1 : rtl::OUString("UNO URL contains duplicated parameter"));
130 225 : eState = STATE_KEY0;
131 : }
132 3836 : break;
133 : }
134 6083 : if (bEnd)
135 279 : break;
136 5815 : }
137 279 : }
138 :
139 192 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
140 202 : m_xImpl(new Impl(rDescriptor))
141 182 : {}
142 :
143 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
144 92 : UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
145 92 : m_xImpl(rImpl)
146 92 : {}
147 : SAL_WNODEPRECATED_DECLARATIONS_POP
148 :
149 0 : UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
150 0 : m_xImpl(rOther.m_xImpl->clone())
151 0 : {}
152 :
153 274 : UnoUrlDescriptor::~UnoUrlDescriptor()
154 274 : {}
155 :
156 0 : UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
157 : {
158 0 : m_xImpl.reset(rOther.m_xImpl->clone());
159 0 : return *this;
160 : }
161 :
162 64 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
163 : {
164 64 : return m_xImpl->m_aDescriptor;
165 : }
166 :
167 88 : rtl::OUString const & UnoUrlDescriptor::getName() const
168 : {
169 88 : return m_xImpl->m_aName;
170 : }
171 :
172 17 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
173 : {
174 34 : return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
175 51 : != m_xImpl->m_aParameters.end();
176 : }
177 :
178 111 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
179 : {
180 : Impl::Parameters::const_iterator
181 111 : aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
182 111 : return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
183 : }
184 :
185 46 : class UnoUrl::Impl
186 : {
187 : public:
188 : UnoUrlDescriptor m_aConnection;
189 : UnoUrlDescriptor m_aProtocol;
190 : rtl::OUString m_aObjectName;
191 :
192 0 : inline Impl * clone() const { return new Impl(*this); }
193 :
194 : /** @exception rtl::MalformedUriException
195 : */
196 : static inline Impl * create(rtl::OUString const & rUrl);
197 :
198 : private:
199 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
200 46 : 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 46 : m_aObjectName(rObjectName)
206 46 : {}
207 : SAL_WNODEPRECATED_DECLARATIONS_POP
208 : };
209 :
210 53 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
211 : {
212 53 : if (!rUrl.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("uno:"), 0))
213 : throw rtl::MalformedUriException(
214 3 : rtl::OUString("UNO URL does not start with \"uno:\""));
215 50 : sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
216 50 : sal_Int32 j = rUrl.indexOf(';', i);
217 50 : if (j < 0)
218 : throw rtl::MalformedUriException(
219 1 : rtl::OUString("UNO URL has too few semicolons"));
220 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
221 : std::auto_ptr< UnoUrlDescriptor::Impl >
222 49 : xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
223 : SAL_WNODEPRECATED_DECLARATIONS_POP
224 49 : i = j + 1;
225 49 : j = rUrl.indexOf(0x3B, i); // ';'
226 49 : if (j < 0)
227 : throw rtl::MalformedUriException(
228 0 : rtl::OUString("UNO URL has too few semicolons"));
229 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
230 : std::auto_ptr< UnoUrlDescriptor::Impl >
231 98 : xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
232 : SAL_WNODEPRECATED_DECLARATIONS_POP
233 48 : i = j + 1;
234 48 : if (i == rUrl.getLength())
235 : throw rtl::MalformedUriException(
236 1 : rtl::OUString("UNO URL contains empty ObjectName"));
237 568 : for (j = i; j < rUrl.getLength(); ++j)
238 : {
239 522 : sal_Unicode c = rUrl.getStr()[j];
240 1081 : if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
241 35 : && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
242 33 : && c != 0x28 && c != 0x2A && c != 0x2B // ')', '*', '+'
243 31 : && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
244 11 : && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
245 5 : && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
246 523 : && c != 0x7E) // '~'
247 : throw rtl::MalformedUriException(
248 1 : rtl::OUString("UNO URL contains invalid ObjectName"));
249 : }
250 95 : return new Impl(xConnection, xProtocol, rUrl.copy(i));
251 : }
252 :
253 53 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
254 46 : {}
255 :
256 0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
257 0 : {}
258 :
259 46 : UnoUrl::~UnoUrl()
260 46 : {}
261 :
262 0 : UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
263 : {
264 0 : m_xImpl.reset(rOther.m_xImpl->clone());
265 0 : return *this;
266 : }
267 :
268 23 : UnoUrlDescriptor const & UnoUrl::getConnection() const
269 : {
270 23 : return m_xImpl->m_aConnection;
271 : }
272 :
273 23 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
274 : {
275 23 : return m_xImpl->m_aProtocol;
276 : }
277 :
278 27 : rtl::OUString const & UnoUrl::getObjectName() const
279 : {
280 27 : return m_xImpl->m_aObjectName;
281 : }
282 :
283 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|