1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UriReference.cxx,v $
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_stoc.hxx"
34 #include "UriReference.hxx"
36 #include "osl/diagnose.h"
37 #include "osl/mutex.hxx"
38 #include "rtl/string.h"
39 #include "rtl/ustrbuf.hxx"
40 #include "rtl/ustring.hxx"
41 #include "sal/types.h"
43 namespace css
= com::sun::star
;
44 using stoc::uriproc::UriReference
;
46 UriReference::UriReference(
47 rtl::OUString
const & scheme
, bool bIsHierarchical
, bool bHasAuthority
,
48 rtl::OUString
const & authority
, rtl::OUString
const & path
,
49 bool bHasQuery
, rtl::OUString
const & query
):
51 m_authority(authority
),
54 m_isHierarchical(bIsHierarchical
),
55 m_hasAuthority(bHasAuthority
),
56 m_hasQuery(bHasQuery
),
59 OSL_ASSERT(scheme
.getLength() != 0 || bIsHierarchical
);
60 OSL_ASSERT(!bHasAuthority
|| bIsHierarchical
);
61 OSL_ASSERT(authority
.getLength() == 0 || bHasAuthority
);
62 OSL_ASSERT(!bHasQuery
|| bIsHierarchical
);
63 OSL_ASSERT(query
.getLength() == 0 || bHasQuery
);
66 UriReference::~UriReference() {}
68 rtl::OUString
UriReference::getUriReference() throw (css::uno::RuntimeException
)
70 osl::MutexGuard
g(m_mutex
);
71 rtl::OUStringBuffer buf
;
72 if (m_scheme
.getLength() != 0) {
74 buf
.append(static_cast< sal_Unicode
>(':'));
76 appendSchemeSpecificPart(buf
);
78 buf
.append(static_cast< sal_Unicode
>('#'));
79 buf
.append(m_fragment
);
81 return buf
.makeStringAndClear();
84 sal_Bool
UriReference::isAbsolute() throw (css::uno::RuntimeException
) {
85 return m_scheme
.getLength() != 0;
88 rtl::OUString
UriReference::getScheme() throw (css::uno::RuntimeException
) {
92 rtl::OUString
UriReference::getSchemeSpecificPart()
93 throw (css::uno::RuntimeException
)
95 osl::MutexGuard
g(m_mutex
);
96 rtl::OUStringBuffer buf
;
97 appendSchemeSpecificPart(buf
);
98 return buf
.makeStringAndClear();
101 sal_Bool
UriReference::isHierarchical() throw (css::uno::RuntimeException
) {
102 osl::MutexGuard
g(m_mutex
);
103 return m_isHierarchical
;
106 sal_Bool
UriReference::hasAuthority() throw (css::uno::RuntimeException
) {
107 osl::MutexGuard
g(m_mutex
);
108 return m_hasAuthority
;
111 rtl::OUString
UriReference::getAuthority() throw (css::uno::RuntimeException
) {
112 osl::MutexGuard
g(m_mutex
);
116 rtl::OUString
UriReference::getPath() throw (css::uno::RuntimeException
) {
117 osl::MutexGuard
g(m_mutex
);
121 sal_Bool
UriReference::hasRelativePath() throw (css::uno::RuntimeException
) {
122 osl::MutexGuard
g(m_mutex
);
123 return m_isHierarchical
&& !m_hasAuthority
124 && (m_path
.getLength() == 0 || m_path
[0] != '/');
127 sal_Int32
UriReference::getPathSegmentCount() throw (css::uno::RuntimeException
)
129 osl::MutexGuard
g(m_mutex
);
130 if (!m_isHierarchical
|| m_path
.getLength() == 0) {
133 sal_Int32 n
= m_path
[0] == '/' ? 0 : 1;
134 for (sal_Int32 i
= 0;; ++i
) {
135 i
= m_path
.indexOf('/', i
);
145 rtl::OUString
UriReference::getPathSegment(sal_Int32 index
)
146 throw (css::uno::RuntimeException
)
148 osl::MutexGuard
g(m_mutex
);
149 if (m_isHierarchical
&& m_path
.getLength() != 0 && index
>= 0) {
150 for (sal_Int32 i
= m_path
[0] == '/' ? 1 : 0;; ++i
) {
152 sal_Int32 j
= m_path
.indexOf('/', i
);
153 return j
< 0 ? m_path
.copy(i
) : m_path
.copy(i
, j
- i
);
155 i
= m_path
.indexOf('/', i
);
161 return rtl::OUString();
164 sal_Bool
UriReference::hasQuery() throw (css::uno::RuntimeException
) {
165 osl::MutexGuard
g(m_mutex
);
169 rtl::OUString
UriReference::getQuery() throw (css::uno::RuntimeException
) {
170 osl::MutexGuard
g(m_mutex
);
174 sal_Bool
UriReference::hasFragment() throw (css::uno::RuntimeException
) {
175 osl::MutexGuard
g(m_mutex
);
176 return m_hasFragment
;
179 rtl::OUString
UriReference::getFragment() throw (css::uno::RuntimeException
) {
180 osl::MutexGuard
g(m_mutex
);
184 void UriReference::setFragment(rtl::OUString
const & fragment
)
185 throw (css::uno::RuntimeException
)
187 osl::MutexGuard
g(m_mutex
);
188 m_hasFragment
= true;
189 m_fragment
= fragment
;
192 void UriReference::clearFragment() throw (css::uno::RuntimeException
) {
193 osl::MutexGuard
g(m_mutex
);
194 m_hasFragment
= false;
195 m_fragment
= rtl::OUString();
198 void UriReference::appendSchemeSpecificPart(rtl::OUStringBuffer
& buffer
) const
200 if (m_hasAuthority
) {
201 buffer
.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
202 buffer
.append(m_authority
);
204 buffer
.append(m_path
);
206 buffer
.append(static_cast< sal_Unicode
>('?'));
207 buffer
.append(m_query
);