update dev300-m58
[ooovba.git] / cppuhelper / source / unourl.cxx
blob5ea199682d1acc3bcc76a9aef5a7af14b90980ea
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unourl.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
34 #include "cppuhelper/unourl.hxx"
36 #include "osl/diagnose.h"
37 #include "rtl/malformeduriexception.hxx"
38 #include "rtl/string.h"
39 #include "rtl/textenc.h"
40 #include "rtl/uri.h"
41 #include "rtl/uri.hxx"
42 #include "rtl/ustring.h"
43 #include "rtl/ustring.hxx"
44 #include "sal/types.h"
46 #include <map>
48 using cppu::UnoUrl;
49 using cppu::UnoUrlDescriptor;
51 namespace {
53 inline bool isAlphanum(sal_Unicode c)
55 return (c >= 0x30 && c <= 0x39) // '0'--'9'
56 || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
57 || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
62 class UnoUrlDescriptor::Impl
64 public:
65 typedef std::map< rtl::OUString, rtl::OUString > Parameters;
67 rtl::OUString m_aDescriptor;
68 rtl::OUString m_aName;
69 Parameters m_aParameters;
71 /** @exception rtl::MalformedUriException
73 explicit inline Impl(rtl::OUString const & m_aDescriptor);
75 inline Impl * clone() const { return new Impl(*this); }
78 inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
80 m_aDescriptor = rDescriptor;
81 enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
82 State eState = STATE_NAME0;
83 sal_Int32 nStart = 0;
84 rtl::OUString aKey;
85 for (sal_Int32 i = 0;; ++i)
87 bool bEnd = i == rDescriptor.getLength();
88 sal_Unicode c = bEnd ? 0 : rDescriptor.getStr()[i];
89 switch (eState)
91 case STATE_NAME0:
92 if (bEnd || !isAlphanum(c))
93 throw rtl::MalformedUriException(
94 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
95 "UNO URL contains bad descriptor name")));
96 nStart = i;
97 eState = STATE_NAME;
98 break;
100 case STATE_NAME:
101 if (bEnd || c == 0x2C) // ','
103 m_aName
104 = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
105 eState = STATE_KEY0;
107 else if (!isAlphanum(c))
108 throw rtl::MalformedUriException(
109 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
110 "UNO URL contains bad descriptor name")));
111 break;
113 case STATE_KEY0:
114 if (bEnd || !isAlphanum(c))
115 throw rtl::MalformedUriException(
116 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
117 "UNO URL contains bad parameter key")));
118 nStart = i;
119 eState = STATE_KEY;
120 break;
122 case STATE_KEY:
123 if (c == 0x3D) // '='
125 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
126 nStart = i + 1;
127 eState = STATE_VALUE;
129 else if (bEnd || !isAlphanum(c))
130 throw rtl::MalformedUriException(
131 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
132 "UNO URL contains bad parameter key")));
133 break;
135 case STATE_VALUE:
136 if (bEnd || c == 0x2C) // ','
138 if (!m_aParameters.insert(
139 Parameters::value_type(
140 aKey,
141 rtl::Uri::decode(rDescriptor.copy(nStart,
142 i - nStart),
143 rtl_UriDecodeWithCharset,
144 RTL_TEXTENCODING_UTF8))).second)
145 throw rtl::MalformedUriException(
146 rtl::OUString(
147 RTL_CONSTASCII_USTRINGPARAM(
148 "UNO URL contains duplicated parameter")));
149 eState = STATE_KEY0;
151 break;
153 if (bEnd)
154 break;
158 UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
159 m_xImpl(new Impl(rDescriptor))
162 UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
163 m_xImpl(rImpl)
166 UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
167 m_xImpl(rOther.m_xImpl->clone())
170 UnoUrlDescriptor::~UnoUrlDescriptor()
173 UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
175 m_xImpl.reset(rOther.m_xImpl->clone());
176 return *this;
179 rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
181 return m_xImpl->m_aDescriptor;
184 rtl::OUString const & UnoUrlDescriptor::getName() const
186 return m_xImpl->m_aName;
189 bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
191 return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
192 != m_xImpl->m_aParameters.end();
195 rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
197 Impl::Parameters::const_iterator
198 aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
199 return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
202 class UnoUrl::Impl
204 public:
205 UnoUrlDescriptor m_aConnection;
206 UnoUrlDescriptor m_aProtocol;
207 rtl::OUString m_aObjectName;
209 inline Impl * clone() const { return new Impl(*this); }
211 /** @exception rtl::MalformedUriException
213 static inline Impl * create(rtl::OUString const & rUrl);
215 private:
216 inline Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection,
217 std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol,
218 rtl::OUString const & rObjectName);
221 inline UnoUrl::Impl::Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection,
222 std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol,
223 rtl::OUString const & rObjectName):
224 m_aConnection(rConnection),
225 m_aProtocol(rProtocol),
226 m_aObjectName(rObjectName)
229 inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
231 if (!rUrl.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("uno:"), 0))
232 throw rtl::MalformedUriException(
233 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
234 "UNO URL does not start with \"uno:\"")));
235 sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
236 sal_Int32 j = rUrl.indexOf(';', i);
237 if (j < 0)
238 throw rtl::MalformedUriException(
239 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
240 "UNO URL has too few semicolons")));
241 std::auto_ptr< UnoUrlDescriptor::Impl >
242 xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
243 i = j + 1;
244 j = rUrl.indexOf(0x3B, i); // ';'
245 if (j < 0)
246 throw rtl::MalformedUriException(
247 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
248 "UNO URL has too few semicolons")));
249 std::auto_ptr< UnoUrlDescriptor::Impl >
250 xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
251 i = j + 1;
252 if (i == rUrl.getLength())
253 throw rtl::MalformedUriException(
254 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
255 "UNO URL contains empty ObjectName")));
256 for (j = i; j < rUrl.getLength(); ++j)
258 sal_Unicode c = rUrl.getStr()[j];
259 if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
260 && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
261 && c != 0x28 && c != 0x2A && c != 0x2B // ')', '*', '+'
262 && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
263 && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
264 && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
265 && c != 0x7E) // '~'
266 throw rtl::MalformedUriException(
267 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
268 "UNO URL contains invalid ObjectName")));
270 return new Impl(xConnection, xProtocol, rUrl.copy(i));
273 UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
276 UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
279 UnoUrl::~UnoUrl()
282 UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
284 m_xImpl.reset(rOther.m_xImpl->clone());
285 return *this;
288 UnoUrlDescriptor const & UnoUrl::getConnection() const
290 return m_xImpl->m_aConnection;
293 UnoUrlDescriptor const & UnoUrl::getProtocol() const
295 return m_xImpl->m_aProtocol;
298 rtl::OUString const & UnoUrl::getObjectName() const
300 return m_xImpl->m_aObjectName;