Files
libreoffice/cppuhelper/source/unourl.cxx

274 lines
8.0 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2002-10-02 14:31:56 +00:00
#include <cppuhelper/unourl.hxx>
#include <osl/diagnose.h>
#include <rtl/malformeduriexception.hxx>
#include <rtl/string.h>
#include <rtl/textenc.h>
#include <rtl/uri.h>
#include <rtl/uri.hxx>
#include <rtl/ustring.h>
#include <rtl/ustring.hxx>
#include <sal/types.h>
2002-10-02 14:31:56 +00:00
#include <map>
#include <memory>
2002-10-02 14:31:56 +00:00
using cppu::UnoUrl;
using cppu::UnoUrlDescriptor;
namespace {
inline bool isAlphanum(sal_Unicode c)
{
CWS-TOOLING: integrate CWS cmcfixes51 2008-12-08 10:12:55 +0100 cmc r264975 : #i96203# protect with ifdefs to avoid unused symbol on mac 2008-12-05 12:23:47 +0100 cmc r264898 : CWS-TOOLING: rebase CWS cmcfixes51 to trunk@264807 (milestone: DEV300:m37) 2008-12-01 14:45:17 +0100 cmc r264606 : #i76655# ehlos apparently required 2008-11-28 17:49:30 +0100 cmc r264567 : #i96655# remove newly unused method 2008-11-28 10:41:28 +0100 cmc r264531 : #i96647# better ppc-bridges flushCode impl 2008-11-27 12:58:40 +0100 cmc r264478 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:32:49 +0100 cmc r264476 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:26:02 +0100 cmc r264475 : #i96655# redundant old table export helpers 2008-11-27 11:49:06 +0100 cmc r264473 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:38:35 +0100 cmc r264471 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:14:21 +0100 cmc r264467 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:06:22 +0100 cmc r264464 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:58:18 +0100 cmc r264462 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:41:44 +0100 cmc r264461 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:19:24 +0100 cmc r264460 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:13:39 +0100 cmc r264459 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:06:14 +0100 cmc r264458 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:59:54 +0100 cmc r264457 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:52:51 +0100 cmc r264456 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:48:26 +0100 cmc r264454 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:40:20 +0100 cmc r264452 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:35:26 +0100 cmc r264451 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:31:00 +0100 cmc r264450 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:24:08 +0100 cmc r264449 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:26:15 +0100 cmc r264443 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:21:01 +0100 cmc r264442 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:09:40 +0100 cmc r264441 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:51:56 +0100 cmc r264440 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:49:09 +0100 cmc r264439 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:09:54 +0100 cmc r264432 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:07:40 +0100 cmc r264431 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:28:02 +0100 cmc r264429 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:27:39 +0100 cmc r264428 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:18:36 +0100 cmc r264426 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 16:22:16 +0100 cmc r264415 : #i96624# make implicit braces and brackets explicit to avoid warnings 2008-11-26 16:00:23 +0100 cmc r264409 : #i90426# remove warnings from svtools 2008-11-26 15:59:17 +0100 cmc r264408 : #i90426# remove warnings 2008-11-26 15:47:32 +0100 cmc r264404 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:46:57 +0100 cmc r264394 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:19:50 +0100 cmc r264387 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:15:26 +0100 cmc r264386 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:11:26 +0100 cmc r264384 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:44:23 +0100 cmc r264380 : #i96084# comfirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:12:24 +0100 cmc r264372 : #i96604# silence new warnings 2008-11-26 12:35:02 +0100 cmc r264369 : #i96203# make qstarter work in 3-layer land 2008-11-26 12:33:04 +0100 cmc r264368 : #i96170# ensure gtypes are up and running
2008-12-11 07:05:03 +00:00
return (c >= 0x30 && c <= 0x39) // '0'--'9'
|| (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
|| (c >= 0x61 && c <= 0x7A); // 'a'--'z'
2002-10-02 14:31:56 +00:00
}
}
class UnoUrlDescriptor::Impl
{
public:
typedef std::map< rtl::OUString, rtl::OUString > Parameters;
rtl::OUString m_aDescriptor;
rtl::OUString m_aName;
Parameters m_aParameters;
/** @exception rtl::MalformedUriException
*/
explicit inline Impl(rtl::OUString const & m_aDescriptor);
inline Impl * clone() const { return new Impl(*this); }
};
inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
{
m_aDescriptor = rDescriptor;
enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
State eState = STATE_NAME0;
sal_Int32 nStart = 0;
2002-10-02 14:31:56 +00:00
rtl::OUString aKey;
for (sal_Int32 i = 0;; ++i)
{
bool bEnd = i == rDescriptor.getLength();
sal_Unicode c = bEnd ? 0 : rDescriptor[i];
2002-10-02 14:31:56 +00:00
switch (eState)
{
case STATE_NAME0:
if (bEnd || !isAlphanum(c))
throw rtl::MalformedUriException(
"UNO URL contains bad descriptor name");
2002-10-02 14:31:56 +00:00
nStart = i;
eState = STATE_NAME;
break;
case STATE_NAME:
if (bEnd || c == 0x2C) // ','
{
m_aName
= rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
eState = STATE_KEY0;
}
else if (!isAlphanum(c))
throw rtl::MalformedUriException(
"UNO URL contains bad descriptor name");
2002-10-02 14:31:56 +00:00
break;
case STATE_KEY0:
if (bEnd || !isAlphanum(c))
throw rtl::MalformedUriException(
"UNO URL contains bad parameter key");
2002-10-02 14:31:56 +00:00
nStart = i;
eState = STATE_KEY;
break;
case STATE_KEY:
if (c == 0x3D) // '='
{
aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
nStart = i + 1;
eState = STATE_VALUE;
}
else if (bEnd || !isAlphanum(c))
throw rtl::MalformedUriException(
"UNO URL contains bad parameter key");
2002-10-02 14:31:56 +00:00
break;
case STATE_VALUE:
if (bEnd || c == 0x2C) // ','
{
if (!m_aParameters.insert(
Parameters::value_type(
aKey,
rtl::Uri::decode(rDescriptor.copy(nStart,
i - nStart),
rtl_UriDecodeWithCharset,
RTL_TEXTENCODING_UTF8))).second)
throw rtl::MalformedUriException(
"UNO URL contains duplicated parameter");
2002-10-02 14:31:56 +00:00
eState = STATE_KEY0;
}
break;
}
if (bEnd)
break;
}
}
UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
m_pImpl(new Impl(rDescriptor))
2002-10-02 14:31:56 +00:00
{}
UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
m_pImpl(rOther.m_pImpl->clone())
2002-10-02 14:31:56 +00:00
{}
UnoUrlDescriptor::~UnoUrlDescriptor()
{
delete m_pImpl;
}
2002-10-02 14:31:56 +00:00
UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
{
std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
delete m_pImpl;
m_pImpl = newImpl.release();
2002-10-02 14:31:56 +00:00
return *this;
}
rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
{
return m_pImpl->m_aDescriptor;
2002-10-02 14:31:56 +00:00
}
rtl::OUString const & UnoUrlDescriptor::getName() const
{
return m_pImpl->m_aName;
2002-10-02 14:31:56 +00:00
}
bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
{
return m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase())
!= m_pImpl->m_aParameters.end();
2002-10-02 14:31:56 +00:00
}
rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
{
Impl::Parameters::const_iterator
aIt(m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
return aIt == m_pImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
2002-10-02 14:31:56 +00:00
}
class UnoUrl::Impl
{
public:
UnoUrlDescriptor m_aConnection;
UnoUrlDescriptor m_aProtocol;
rtl::OUString m_aObjectName;
inline Impl * clone() const { return new Impl(*this); }
/** @exception rtl::MalformedUriException
*/
static inline Impl * create(rtl::OUString const & rUrl);
private:
Impl(rtl::OUString const & rConnectionDescriptor,
rtl::OUString const & rProtocolDescriptor,
rtl::OUString const & rObjectName):
m_aConnection(rConnectionDescriptor),
m_aProtocol(rProtocolDescriptor),
m_aObjectName(rObjectName)
{}
2002-10-02 14:31:56 +00:00
};
inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
{
if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
throw rtl::MalformedUriException("UNO URL does not start with \"uno:\"");
2002-10-02 14:31:56 +00:00
sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
sal_Int32 j = rUrl.indexOf(';', i);
if (j < 0)
throw rtl::MalformedUriException("UNO URL has too few semicolons");
rtl::OUString aConnection(rUrl.copy(i, j - i));
2002-10-02 14:31:56 +00:00
i = j + 1;
j = rUrl.indexOf(0x3B, i); // ';'
if (j < 0)
throw rtl::MalformedUriException("UNO URL has too few semicolons");
rtl::OUString aProtocol(rUrl.copy(i, j - i));
2002-10-02 14:31:56 +00:00
i = j + 1;
if (i == rUrl.getLength())
throw rtl::MalformedUriException("UNO URL contains empty ObjectName");
2002-10-02 14:31:56 +00:00
for (j = i; j < rUrl.getLength(); ++j)
{
sal_Unicode c = rUrl[j];
2002-10-02 14:31:56 +00:00
if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
&& c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
&& c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
2002-10-02 14:31:56 +00:00
&& c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
&& c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
&& c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
&& c != 0x7E) // '~'
throw rtl::MalformedUriException("UNO URL contains invalid ObjectName");
2002-10-02 14:31:56 +00:00
}
return new Impl(aConnection, aProtocol, rUrl.copy(i));
2002-10-02 14:31:56 +00:00
}
UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_pImpl(Impl::create(rUrl))
2002-10-02 14:31:56 +00:00
{}
UnoUrl::UnoUrl(UnoUrl const & rOther): m_pImpl(rOther.m_pImpl->clone())
2002-10-02 14:31:56 +00:00
{}
UnoUrl::~UnoUrl()
{
delete m_pImpl;
}
2002-10-02 14:31:56 +00:00
UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
{
std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
delete m_pImpl;
m_pImpl = newImpl.release();
2002-10-02 14:31:56 +00:00
return *this;
}
UnoUrlDescriptor const & UnoUrl::getConnection() const
{
return m_pImpl->m_aConnection;
2002-10-02 14:31:56 +00:00
}
UnoUrlDescriptor const & UnoUrl::getProtocol() const
{
return m_pImpl->m_aProtocol;
2002-10-02 14:31:56 +00:00
}
rtl::OUString const & UnoUrl::getObjectName() const
{
return m_pImpl->m_aObjectName;
2002-10-02 14:31:56 +00:00
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */