Bump version to 21.06.18.1
[LibreOffice.git] / stoc / source / uriproc / UriReference.cxx
blob75ed1abaa2a40b2263051790158f97cd44063fff
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <cassert>
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):
37 m_scheme(scheme),
38 m_authority(authority),
39 m_path(path),
40 m_query(query),
41 m_hasAuthority(bHasAuthority),
42 m_hasQuery(bHasQuery),
43 m_hasFragment(false)
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()) {
56 buf.append(m_scheme);
57 buf.append(':');
59 appendSchemeSpecificPart(buf);
60 if (m_hasFragment) {
61 buf.append('#');
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);
75 OUStringBuffer buf;
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);
92 return m_authority;
95 OUString UriReference::getPath() {
96 osl::MutexGuard g(m_mutex);
97 return m_path;
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()) {
110 return 0;
111 } else {
112 sal_Int32 n = m_path[0] == '/' ? 0 : 1;
113 for (sal_Int32 i = 0;; ++i) {
114 i = m_path.indexOf('/', i);
115 if (i < 0) {
116 break;
118 ++n;
120 return n;
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) {
129 if (index-- == 0) {
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);
134 if (i < 0) {
135 break;
139 return OUString();
142 bool UriReference::hasQuery() {
143 osl::MutexGuard g(m_mutex);
144 return m_hasQuery;
147 OUString UriReference::getQuery() {
148 osl::MutexGuard g(m_mutex);
149 return m_query;
152 bool UriReference::hasFragment() {
153 osl::MutexGuard g(m_mutex);
154 return m_hasFragment;
157 OUString UriReference::getFragment() {
158 osl::MutexGuard g(m_mutex);
159 return m_fragment;
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;
172 m_fragment.clear();
175 void UriReference::appendSchemeSpecificPart(OUStringBuffer & buffer) const
177 if (m_hasAuthority) {
178 buffer.append("//");
179 buffer.append(m_authority);
181 buffer.append(m_path);
182 if (m_hasQuery) {
183 buffer.append('?');
184 buffer.append(m_query);
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */