Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / cppuhelper / source / unourl.cxx
blobcfd62ef23e6f08c59bb08be185b4340f087d25a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #include <rtl/uri.h>
27 #include <rtl/uri.hxx>
28 #include <rtl/ustring.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
32 #include <map>
33 #include <memory>
35 using cppu::UnoUrl;
36 using cppu::UnoUrlDescriptor;
38 namespace {
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
51 public:
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;
70 sal_Int32 nStart = 0;
71 rtl::OUString aKey;
72 for (sal_Int32 i = 0;; ++i)
74 bool bEnd = i == rDescriptor.getLength();
75 sal_Unicode c = bEnd ? 0 : rDescriptor[i];
76 switch (eState)
78 case STATE_NAME0:
79 if (bEnd || !isAlphanum(c))
80 throw rtl::MalformedUriException(
81 "UNO URL contains bad descriptor name");
82 nStart = i;
83 eState = STATE_NAME;
84 break;
86 case STATE_NAME:
87 if (bEnd || c == 0x2C) // ','
89 m_aName
90 = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
91 eState = STATE_KEY0;
93 else if (!isAlphanum(c))
94 throw rtl::MalformedUriException(
95 "UNO URL contains bad descriptor name");
96 break;
98 case STATE_KEY0:
99 if (bEnd || !isAlphanum(c))
100 throw rtl::MalformedUriException(
101 "UNO URL contains bad parameter key");
102 nStart = i;
103 eState = STATE_KEY;
104 break;
106 case STATE_KEY:
107 if (c == 0x3D) // '='
109 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
110 nStart = i + 1;
111 eState = STATE_VALUE;
113 else if (bEnd || !isAlphanum(c))
114 throw rtl::MalformedUriException(
115 "UNO URL contains bad parameter key");
116 break;
118 case STATE_VALUE:
119 if (bEnd || c == 0x2C) // ','
121 if (!m_aParameters.emplace(
122 aKey,
123 rtl::Uri::decode(rDescriptor.copy(nStart,
124 i - nStart),
125 rtl_UriDecodeWithCharset,
126 RTL_TEXTENCODING_UTF8)).second)
127 throw rtl::MalformedUriException(
128 "UNO URL contains duplicated parameter");
129 eState = STATE_KEY0;
131 break;
133 if (bEnd)
134 break;
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()
148 delete m_pImpl;
151 UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
153 std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
154 delete m_pImpl;
155 m_pImpl = newImpl.release();
156 return *this;
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;
182 class UnoUrl::Impl
184 public:
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);
195 private:
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);
211 if (j < 0)
212 throw rtl::MalformedUriException("UNO URL has too few semicolons");
213 rtl::OUString aConnection(rUrl.copy(i, j - i));
214 i = j + 1;
215 j = rUrl.indexOf(0x3B, i); // ';'
216 if (j < 0)
217 throw rtl::MalformedUriException("UNO URL has too few semicolons");
218 rtl::OUString aProtocol(rUrl.copy(i, j - i));
219 i = j + 1;
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 // '?', '@', '_'
231 && c != 0x7E) // '~'
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())
243 UnoUrl::~UnoUrl()
245 delete m_pImpl;
248 UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
250 std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
251 delete m_pImpl;
252 m_pImpl = newImpl.release();
253 return *this;
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: */