1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "UriReference.hxx"
23 #include <osl/diagnose.h>
24 #include <osl/mutex.hxx>
25 #include <rtl/string.h>
26 #include <rtl/ustrbuf.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
30 using stoc::uriproc::UriReference
;
32 UriReference::UriReference(
33 OUString
const & scheme
, bool bIsHierarchical
, bool bHasAuthority
,
34 OUString
const & authority
, OUString
const & path
,
35 bool bHasQuery
, OUString
const & query
):
37 m_authority(authority
),
40 m_isHierarchical(bIsHierarchical
),
41 m_hasAuthority(bHasAuthority
),
42 m_hasQuery(bHasQuery
),
45 OSL_ASSERT(!scheme
.isEmpty() || bIsHierarchical
);
46 OSL_ASSERT(!bHasAuthority
|| bIsHierarchical
);
47 OSL_ASSERT(authority
.isEmpty() || bHasAuthority
);
48 OSL_ASSERT(!bHasQuery
|| bIsHierarchical
);
49 OSL_ASSERT(query
.isEmpty() || bHasQuery
);
52 UriReference::~UriReference() {}
54 OUString
UriReference::getUriReference()
56 osl::MutexGuard
g(m_mutex
);
58 if (!m_scheme
.isEmpty()) {
62 appendSchemeSpecificPart(buf
);
65 buf
.append(m_fragment
);
67 return buf
.makeStringAndClear();
70 bool UriReference::isAbsolute() {
71 return !m_scheme
.isEmpty();
75 OUString
UriReference::getSchemeSpecificPart()
77 osl::MutexGuard
g(m_mutex
);
79 appendSchemeSpecificPart(buf
);
80 return buf
.makeStringAndClear();
83 bool UriReference::isHierarchical() {
84 osl::MutexGuard
g(m_mutex
);
85 return m_isHierarchical
;
88 bool UriReference::hasAuthority() {
89 osl::MutexGuard
g(m_mutex
);
90 return m_hasAuthority
;
93 OUString
UriReference::getAuthority() {
94 osl::MutexGuard
g(m_mutex
);
98 OUString
UriReference::getPath() {
99 osl::MutexGuard
g(m_mutex
);
103 bool UriReference::hasRelativePath() {
104 osl::MutexGuard
g(m_mutex
);
105 return m_isHierarchical
&& !m_hasAuthority
106 && (m_path
.isEmpty() || m_path
[0] != '/');
109 sal_Int32
UriReference::getPathSegmentCount()
111 osl::MutexGuard
g(m_mutex
);
112 if (!m_isHierarchical
|| m_path
.isEmpty()) {
115 sal_Int32 n
= m_path
[0] == '/' ? 0 : 1;
116 for (sal_Int32 i
= 0;; ++i
) {
117 i
= m_path
.indexOf('/', i
);
127 OUString
UriReference::getPathSegment(sal_Int32 index
)
129 osl::MutexGuard
g(m_mutex
);
130 if (m_isHierarchical
&& !m_path
.isEmpty() && index
>= 0) {
131 for (sal_Int32 i
= m_path
[0] == '/' ? 1 : 0;; ++i
) {
133 sal_Int32 j
= m_path
.indexOf('/', i
);
134 return j
< 0 ? m_path
.copy(i
) : m_path
.copy(i
, j
- i
);
136 i
= m_path
.indexOf('/', i
);
145 bool UriReference::hasQuery() {
146 osl::MutexGuard
g(m_mutex
);
150 OUString
UriReference::getQuery() {
151 osl::MutexGuard
g(m_mutex
);
155 bool UriReference::hasFragment() {
156 osl::MutexGuard
g(m_mutex
);
157 return m_hasFragment
;
160 OUString
UriReference::getFragment() {
161 osl::MutexGuard
g(m_mutex
);
165 void UriReference::setFragment(OUString
const & fragment
)
167 osl::MutexGuard
g(m_mutex
);
168 m_hasFragment
= true;
169 m_fragment
= fragment
;
172 void UriReference::clearFragment() {
173 osl::MutexGuard
g(m_mutex
);
174 m_hasFragment
= false;
178 void UriReference::appendSchemeSpecificPart(OUStringBuffer
& buffer
) const
180 if (m_hasAuthority
) {
182 buffer
.append(m_authority
);
184 buffer
.append(m_path
);
187 buffer
.append(m_query
);
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */