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 .
20 #include <sal/config.h>
24 #include "UriReference.hxx"
26 #include <osl/mutex.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <rtl/ustring.hxx>
29 #include <sal/types.h>
31 using stoc::uriproc::UriReference
;
33 UriReference::UriReference(
34 OUString
const & scheme
, bool bHasAuthority
,
35 OUString
const & authority
, OUString
const & path
,
36 bool bHasQuery
, OUString
const & query
):
38 m_authority(authority
),
41 m_hasAuthority(bHasAuthority
),
42 m_hasQuery(bHasQuery
),
45 assert(authority
.isEmpty() || bHasAuthority
);
46 assert(query
.isEmpty() || bHasQuery
);
49 UriReference::~UriReference() {}
51 OUString
UriReference::getUriReference()
53 osl::MutexGuard
g(m_mutex
);
54 OUStringBuffer
buf(128);
55 if (!m_scheme
.isEmpty()) {
59 appendSchemeSpecificPart(buf
);
62 buf
.append(m_fragment
);
64 return buf
.makeStringAndClear();
67 bool UriReference::isAbsolute() const {
68 return !m_scheme
.isEmpty();
72 OUString
UriReference::getSchemeSpecificPart()
74 osl::MutexGuard
g(m_mutex
);
76 appendSchemeSpecificPart(buf
);
77 return buf
.makeStringAndClear();
80 bool UriReference::isHierarchical() {
81 osl::MutexGuard
g(m_mutex
);
82 return m_scheme
.isEmpty() || m_hasAuthority
|| m_path
.startsWith("/");
85 bool UriReference::hasAuthority() {
86 osl::MutexGuard
g(m_mutex
);
87 return m_hasAuthority
;
90 OUString
UriReference::getAuthority() {
91 osl::MutexGuard
g(m_mutex
);
95 OUString
UriReference::getPath() {
96 osl::MutexGuard
g(m_mutex
);
100 bool UriReference::hasRelativePath() {
101 osl::MutexGuard
g(m_mutex
);
102 return !m_hasAuthority
103 && (m_path
.isEmpty() || m_path
[0] != '/');
106 sal_Int32
UriReference::getPathSegmentCount()
108 osl::MutexGuard
g(m_mutex
);
109 if (m_path
.isEmpty()) {
112 sal_Int32 n
= m_path
[0] == '/' ? 0 : 1;
113 for (sal_Int32 i
= 0;; ++i
) {
114 i
= m_path
.indexOf('/', i
);
124 OUString
UriReference::getPathSegment(sal_Int32 index
)
126 osl::MutexGuard
g(m_mutex
);
127 if (!m_path
.isEmpty() && index
>= 0) {
128 for (sal_Int32 i
= m_path
[0] == '/' ? 1 : 0;; ++i
) {
130 sal_Int32 j
= m_path
.indexOf('/', i
);
131 return j
< 0 ? m_path
.copy(i
) : m_path
.copy(i
, j
- i
);
133 i
= m_path
.indexOf('/', i
);
142 bool UriReference::hasQuery() {
143 osl::MutexGuard
g(m_mutex
);
147 OUString
UriReference::getQuery() {
148 osl::MutexGuard
g(m_mutex
);
152 bool UriReference::hasFragment() {
153 osl::MutexGuard
g(m_mutex
);
154 return m_hasFragment
;
157 OUString
UriReference::getFragment() {
158 osl::MutexGuard
g(m_mutex
);
162 void UriReference::setFragment(OUString
const & fragment
)
164 osl::MutexGuard
g(m_mutex
);
165 m_hasFragment
= true;
166 m_fragment
= fragment
;
169 void UriReference::clearFragment() {
170 osl::MutexGuard
g(m_mutex
);
171 m_hasFragment
= false;
175 void UriReference::appendSchemeSpecificPart(OUStringBuffer
& buffer
) const
177 if (m_hasAuthority
) {
179 buffer
.append(m_authority
);
181 buffer
.append(m_path
);
184 buffer
.append(m_query
);
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */