Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / uriproc / UriReference.cxx
blob2baa1f3b9b209854d212539f77685a283ec5a9b2
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 .
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):
36 m_scheme(scheme),
37 m_authority(authority),
38 m_path(path),
39 m_query(query),
40 m_isHierarchical(bIsHierarchical),
41 m_hasAuthority(bHasAuthority),
42 m_hasQuery(bHasQuery),
43 m_hasFragment(false)
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);
57 OUStringBuffer buf;
58 if (!m_scheme.isEmpty()) {
59 buf.append(m_scheme);
60 buf.append(':');
62 appendSchemeSpecificPart(buf);
63 if (m_hasFragment) {
64 buf.append('#');
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);
78 OUStringBuffer buf;
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);
95 return m_authority;
98 OUString UriReference::getPath() {
99 osl::MutexGuard g(m_mutex);
100 return m_path;
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()) {
113 return 0;
114 } else {
115 sal_Int32 n = m_path[0] == '/' ? 0 : 1;
116 for (sal_Int32 i = 0;; ++i) {
117 i = m_path.indexOf('/', i);
118 if (i < 0) {
119 break;
121 ++n;
123 return n;
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) {
132 if (index-- == 0) {
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);
137 if (i < 0) {
138 break;
142 return OUString();
145 bool UriReference::hasQuery() {
146 osl::MutexGuard g(m_mutex);
147 return m_hasQuery;
150 OUString UriReference::getQuery() {
151 osl::MutexGuard g(m_mutex);
152 return m_query;
155 bool UriReference::hasFragment() {
156 osl::MutexGuard g(m_mutex);
157 return m_hasFragment;
160 OUString UriReference::getFragment() {
161 osl::MutexGuard g(m_mutex);
162 return m_fragment;
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;
175 m_fragment.clear();
178 void UriReference::appendSchemeSpecificPart(OUStringBuffer & buffer) const
180 if (m_hasAuthority) {
181 buffer.append("//");
182 buffer.append(m_authority);
184 buffer.append(m_path);
185 if (m_hasQuery) {
186 buffer.append('?');
187 buffer.append(m_query);
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */