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 0 : inline bool isAlphanum(sal_Unicode c)
41 : {
42 0 : return (c >= 0x30 && c <= 0x39) // '0'--'9'
43 0 : || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
44 0 : || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
45 : }
46 :
47 : }
48 :
49 0 : 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 0 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
66 : {
67 0 : m_aDescriptor = rDescriptor;
68 : enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
69 0 : State eState = STATE_NAME0;
70 0 : sal_Int32 nStart = 0;
71 0 : rtl::OUString aKey;
72 0 : for (sal_Int32 i = 0;; ++i)
73 : {
74 0 : bool bEnd = i == rDescriptor.getLength();
75 0 : sal_Unicode c = bEnd ? 0 : rDescriptor[i];
76 0 : switch (eState)
77 : {
78 : case STATE_NAME0:
79 0 : if (bEnd || !isAlphanum(c))
80 : throw rtl::MalformedUriException(
81 0 : rtl::OUString("UNO URL contains bad descriptor name"));
82 0 : nStart = i;
83 0 : eState = STATE_NAME;
84 0 : break;
85 :
86 : case STATE_NAME:
87 0 : if (bEnd || c == 0x2C) // ','
88 : {
89 : m_aName
90 0 : = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
91 0 : eState = STATE_KEY0;
92 : }
93 0 : else if (!isAlphanum(c))
94 : throw rtl::MalformedUriException(
95 0 : rtl::OUString("UNO URL contains bad descriptor name"));
96 0 : break;
97 :
98 : case STATE_KEY0:
99 0 : if (bEnd || !isAlphanum(c))
100 : throw rtl::MalformedUriException(
101 0 : rtl::OUString("UNO URL contains bad parameter key"));
102 0 : nStart = i;
103 0 : eState = STATE_KEY;
104 0 : break;
105 :
106 : case STATE_KEY:
107 0 : if (c == 0x3D) // '='
108 : {
109 0 : aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
110 0 : nStart = i + 1;
111 0 : eState = STATE_VALUE;
112 : }
113 0 : else if (bEnd || !isAlphanum(c))
114 : throw rtl::MalformedUriException(
115 0 : rtl::OUString("UNO URL contains bad parameter key"));
116 0 : break;
117 :
118 : case STATE_VALUE:
119 0 : if (bEnd || c == 0x2C) // ','
120 : {
121 0 : if (!m_aParameters.insert(
122 : Parameters::value_type(
123 : aKey,
124 : rtl::Uri::decode(rDescriptor.copy(nStart,
125 : i - nStart),
126 : rtl_UriDecodeWithCharset,
127 0 : RTL_TEXTENCODING_UTF8))).second)
128 : throw rtl::MalformedUriException(
129 0 : rtl::OUString("UNO URL contains duplicated parameter"));
130 0 : eState = STATE_KEY0;
131 : }
132 0 : break;
133 : }
134 0 : if (bEnd)
135 0 : break;
136 0 : }
137 0 : }
138 :
139 0 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
140 0 : m_xImpl(new Impl(rDescriptor))
141 0 : {}
142 :
143 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
144 0 : UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
145 0 : m_xImpl(rImpl)
146 0 : {}
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 0 : UnoUrlDescriptor::~UnoUrlDescriptor()
154 0 : {}
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 0 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
163 : {
164 0 : return m_xImpl->m_aDescriptor;
165 : }
166 :
167 0 : rtl::OUString const & UnoUrlDescriptor::getName() const
168 : {
169 0 : return m_xImpl->m_aName;
170 : }
171 :
172 0 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
173 : {
174 0 : return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
175 0 : != m_xImpl->m_aParameters.end();
176 : }
177 :
178 0 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
179 : {
180 : Impl::Parameters::const_iterator
181 0 : aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
182 0 : return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
183 : }
184 :
185 0 : 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 0 : 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 0 : m_aObjectName(rObjectName)
206 0 : {}
207 : SAL_WNODEPRECATED_DECLARATIONS_POP
208 : };
209 :
210 0 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
211 : {
212 0 : if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
213 : throw rtl::MalformedUriException(
214 0 : rtl::OUString("UNO URL does not start with \"uno:\""));
215 0 : sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
216 0 : sal_Int32 j = rUrl.indexOf(';', i);
217 0 : if (j < 0)
218 : throw rtl::MalformedUriException(
219 0 : rtl::OUString("UNO URL has too few semicolons"));
220 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
221 : std::auto_ptr< UnoUrlDescriptor::Impl >
222 0 : xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
223 : SAL_WNODEPRECATED_DECLARATIONS_POP
224 0 : i = j + 1;
225 0 : j = rUrl.indexOf(0x3B, i); // ';'
226 0 : 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 0 : xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
232 : SAL_WNODEPRECATED_DECLARATIONS_POP
233 0 : i = j + 1;
234 0 : if (i == rUrl.getLength())
235 : throw rtl::MalformedUriException(
236 0 : rtl::OUString("UNO URL contains empty ObjectName"));
237 0 : for (j = i; j < rUrl.getLength(); ++j)
238 : {
239 0 : sal_Unicode c = rUrl[j];
240 0 : if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
241 0 : && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
242 0 : && c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
243 0 : && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
244 0 : && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
245 0 : && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
246 0 : && c != 0x7E) // '~'
247 : throw rtl::MalformedUriException(
248 0 : rtl::OUString("UNO URL contains invalid ObjectName"));
249 : }
250 0 : return new Impl(xConnection, xProtocol, rUrl.copy(i));
251 : }
252 :
253 0 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
254 0 : {}
255 :
256 0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
257 0 : {}
258 :
259 0 : UnoUrl::~UnoUrl()
260 0 : {}
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 0 : UnoUrlDescriptor const & UnoUrl::getConnection() const
269 : {
270 0 : return m_xImpl->m_aConnection;
271 : }
272 :
273 0 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
274 : {
275 0 : return m_xImpl->m_aProtocol;
276 : }
277 :
278 0 : rtl::OUString const & UnoUrl::getObjectName() const
279 : {
280 0 : return m_xImpl->m_aObjectName;
281 : }
282 :
283 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|