Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "UriReference.hxx"
22 :
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"
29 :
30 : using stoc::uriproc::UriReference;
31 :
32 5599 : UriReference::UriReference(
33 : rtl::OUString const & scheme, bool bIsHierarchical, bool bHasAuthority,
34 : rtl::OUString const & authority, rtl::OUString const & path,
35 : bool bHasQuery, rtl::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 5599 : m_hasFragment(false)
44 : {
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);
50 5599 : }
51 :
52 5599 : UriReference::~UriReference() {}
53 :
54 1486 : rtl::OUString UriReference::getUriReference() throw (css::uno::RuntimeException)
55 : {
56 1486 : osl::MutexGuard g(m_mutex);
57 1486 : rtl::OUStringBuffer buf;
58 1486 : if (!m_scheme.isEmpty()) {
59 1462 : buf.append(m_scheme);
60 1462 : buf.append(static_cast< sal_Unicode >(':'));
61 : }
62 1486 : appendSchemeSpecificPart(buf);
63 1486 : if (m_hasFragment) {
64 8 : buf.append(static_cast< sal_Unicode >('#'));
65 8 : buf.append(m_fragment);
66 : }
67 1486 : return buf.makeStringAndClear();
68 : }
69 :
70 2302 : sal_Bool UriReference::isAbsolute() throw (css::uno::RuntimeException) {
71 2302 : return !m_scheme.isEmpty();
72 : }
73 :
74 1187 : rtl::OUString UriReference::getScheme() throw (css::uno::RuntimeException) {
75 1187 : return m_scheme;
76 : }
77 :
78 0 : rtl::OUString UriReference::getSchemeSpecificPart()
79 : throw (css::uno::RuntimeException)
80 : {
81 0 : osl::MutexGuard g(m_mutex);
82 0 : rtl::OUStringBuffer buf;
83 0 : appendSchemeSpecificPart(buf);
84 0 : return buf.makeStringAndClear();
85 : }
86 :
87 1189 : sal_Bool UriReference::isHierarchical() throw (css::uno::RuntimeException) {
88 1189 : osl::MutexGuard g(m_mutex);
89 1189 : return m_isHierarchical;
90 : }
91 :
92 3389 : sal_Bool UriReference::hasAuthority() throw (css::uno::RuntimeException) {
93 3389 : osl::MutexGuard g(m_mutex);
94 3389 : return m_hasAuthority;
95 : }
96 :
97 1151 : rtl::OUString UriReference::getAuthority() throw (css::uno::RuntimeException) {
98 1151 : osl::MutexGuard g(m_mutex);
99 1151 : return m_authority;
100 : }
101 :
102 3364 : rtl::OUString UriReference::getPath() throw (css::uno::RuntimeException) {
103 3364 : osl::MutexGuard g(m_mutex);
104 3364 : return m_path;
105 : }
106 :
107 1143 : sal_Bool UriReference::hasRelativePath() throw (css::uno::RuntimeException) {
108 1143 : osl::MutexGuard g(m_mutex);
109 1143 : return m_isHierarchical && !m_hasAuthority
110 2286 : && (m_path.isEmpty() || m_path[0] != '/');
111 : }
112 :
113 2331 : sal_Int32 UriReference::getPathSegmentCount() throw (css::uno::RuntimeException)
114 : {
115 2331 : osl::MutexGuard g(m_mutex);
116 2331 : if (!m_isHierarchical || m_path.isEmpty()) {
117 0 : return 0;
118 : } else {
119 2331 : sal_Int32 n = m_path[0] == '/' ? 0 : 1;
120 5408 : for (sal_Int32 i = 0;; ++i) {
121 5408 : i = m_path.indexOf('/', i);
122 5408 : if (i < 0) {
123 2331 : break;
124 : }
125 3077 : ++n;
126 : }
127 2331 : return n;
128 2331 : }
129 : }
130 :
131 5363 : rtl::OUString UriReference::getPathSegment(sal_Int32 index)
132 : throw (css::uno::RuntimeException)
133 : {
134 5363 : osl::MutexGuard g(m_mutex);
135 5363 : if (m_isHierarchical && !m_path.isEmpty() && index >= 0) {
136 8886 : for (sal_Int32 i = m_path[0] == '/' ? 1 : 0;; ++i) {
137 8886 : if (index-- == 0) {
138 5363 : sal_Int32 j = m_path.indexOf('/', i);
139 5363 : return j < 0 ? m_path.copy(i) : m_path.copy(i, j - i);
140 : }
141 3523 : i = m_path.indexOf('/', i);
142 3523 : if (i < 0) {
143 0 : break;
144 : }
145 : }
146 : }
147 0 : return rtl::OUString();
148 : }
149 :
150 1147 : sal_Bool UriReference::hasQuery() throw (css::uno::RuntimeException) {
151 1147 : osl::MutexGuard g(m_mutex);
152 1147 : return m_hasQuery;
153 : }
154 :
155 4 : rtl::OUString UriReference::getQuery() throw (css::uno::RuntimeException) {
156 4 : osl::MutexGuard g(m_mutex);
157 4 : return m_query;
158 : }
159 :
160 1147 : sal_Bool UriReference::hasFragment() throw (css::uno::RuntimeException) {
161 1147 : osl::MutexGuard g(m_mutex);
162 1147 : return m_hasFragment;
163 : }
164 :
165 10 : rtl::OUString UriReference::getFragment() throw (css::uno::RuntimeException) {
166 10 : osl::MutexGuard g(m_mutex);
167 10 : return m_fragment;
168 : }
169 :
170 20 : void UriReference::setFragment(rtl::OUString const & fragment)
171 : throw (css::uno::RuntimeException)
172 : {
173 20 : osl::MutexGuard g(m_mutex);
174 20 : m_hasFragment = true;
175 20 : m_fragment = fragment;
176 20 : }
177 :
178 1421 : void UriReference::clearFragment() throw (css::uno::RuntimeException) {
179 1421 : osl::MutexGuard g(m_mutex);
180 1421 : m_hasFragment = false;
181 1421 : m_fragment = rtl::OUString();
182 1421 : }
183 :
184 1486 : void UriReference::appendSchemeSpecificPart(rtl::OUStringBuffer & buffer) const
185 : {
186 1486 : if (m_hasAuthority) {
187 1458 : buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
188 1458 : buffer.append(m_authority);
189 : }
190 1486 : buffer.append(m_path);
191 1486 : if (m_hasQuery) {
192 4 : buffer.append(static_cast< sal_Unicode >('?'));
193 4 : buffer.append(m_query);
194 : }
195 1486 : }
196 :
197 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|