1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26
27 #include "UriReference.hxx"
28
29 #include "osl/diagnose.h"
30 #include "osl/mutex.hxx"
31 #include "rtl/string.h"
32 #include "rtl/ustrbuf.hxx"
33 #include "rtl/ustring.hxx"
34 #include "sal/types.h"
35
36 namespace css = com::sun::star;
37 using stoc::uriproc::UriReference;
38
UriReference(rtl::OUString const & scheme,bool bIsHierarchical,bool bHasAuthority,rtl::OUString const & authority,rtl::OUString const & path,bool bHasQuery,rtl::OUString const & query)39 UriReference::UriReference(
40 rtl::OUString const & scheme, bool bIsHierarchical, bool bHasAuthority,
41 rtl::OUString const & authority, rtl::OUString const & path,
42 bool bHasQuery, rtl::OUString const & query):
43 m_scheme(scheme),
44 m_authority(authority),
45 m_path(path),
46 m_query(query),
47 m_isHierarchical(bIsHierarchical),
48 m_hasAuthority(bHasAuthority),
49 m_hasQuery(bHasQuery),
50 m_hasFragment(false)
51 {
52 OSL_ASSERT(scheme.getLength() != 0 || bIsHierarchical);
53 OSL_ASSERT(!bHasAuthority || bIsHierarchical);
54 OSL_ASSERT(authority.getLength() == 0 || bHasAuthority);
55 OSL_ASSERT(!bHasQuery || bIsHierarchical);
56 OSL_ASSERT(query.getLength() == 0 || bHasQuery);
57 }
58
~UriReference()59 UriReference::~UriReference() {}
60
getUriReference()61 rtl::OUString UriReference::getUriReference() throw (css::uno::RuntimeException)
62 {
63 osl::MutexGuard g(m_mutex);
64 rtl::OUStringBuffer buf;
65 if (m_scheme.getLength() != 0) {
66 buf.append(m_scheme);
67 buf.append(static_cast< sal_Unicode >(':'));
68 }
69 appendSchemeSpecificPart(buf);
70 if (m_hasFragment) {
71 buf.append(static_cast< sal_Unicode >('#'));
72 buf.append(m_fragment);
73 }
74 return buf.makeStringAndClear();
75 }
76
isAbsolute()77 sal_Bool UriReference::isAbsolute() throw (css::uno::RuntimeException) {
78 return m_scheme.getLength() != 0;
79 }
80
getScheme()81 rtl::OUString UriReference::getScheme() throw (css::uno::RuntimeException) {
82 return m_scheme;
83 }
84
getSchemeSpecificPart()85 rtl::OUString UriReference::getSchemeSpecificPart()
86 throw (css::uno::RuntimeException)
87 {
88 osl::MutexGuard g(m_mutex);
89 rtl::OUStringBuffer buf;
90 appendSchemeSpecificPart(buf);
91 return buf.makeStringAndClear();
92 }
93
isHierarchical()94 sal_Bool UriReference::isHierarchical() throw (css::uno::RuntimeException) {
95 osl::MutexGuard g(m_mutex);
96 return m_isHierarchical;
97 }
98
hasAuthority()99 sal_Bool UriReference::hasAuthority() throw (css::uno::RuntimeException) {
100 osl::MutexGuard g(m_mutex);
101 return m_hasAuthority;
102 }
103
getAuthority()104 rtl::OUString UriReference::getAuthority() throw (css::uno::RuntimeException) {
105 osl::MutexGuard g(m_mutex);
106 return m_authority;
107 }
108
getPath()109 rtl::OUString UriReference::getPath() throw (css::uno::RuntimeException) {
110 osl::MutexGuard g(m_mutex);
111 return m_path;
112 }
113
hasRelativePath()114 sal_Bool UriReference::hasRelativePath() throw (css::uno::RuntimeException) {
115 osl::MutexGuard g(m_mutex);
116 return m_isHierarchical && !m_hasAuthority
117 && (m_path.getLength() == 0 || m_path[0] != '/');
118 }
119
getPathSegmentCount()120 sal_Int32 UriReference::getPathSegmentCount() throw (css::uno::RuntimeException)
121 {
122 osl::MutexGuard g(m_mutex);
123 if (!m_isHierarchical || m_path.getLength() == 0) {
124 return 0;
125 } else {
126 sal_Int32 n = m_path[0] == '/' ? 0 : 1;
127 for (sal_Int32 i = 0;; ++i) {
128 i = m_path.indexOf('/', i);
129 if (i < 0) {
130 break;
131 }
132 ++n;
133 }
134 return n;
135 }
136 }
137
getPathSegment(sal_Int32 index)138 rtl::OUString UriReference::getPathSegment(sal_Int32 index)
139 throw (css::uno::RuntimeException)
140 {
141 osl::MutexGuard g(m_mutex);
142 if (m_isHierarchical && m_path.getLength() != 0 && index >= 0) {
143 for (sal_Int32 i = m_path[0] == '/' ? 1 : 0;; ++i) {
144 if (index-- == 0) {
145 sal_Int32 j = m_path.indexOf('/', i);
146 return j < 0 ? m_path.copy(i) : m_path.copy(i, j - i);
147 }
148 i = m_path.indexOf('/', i);
149 if (i < 0) {
150 break;
151 }
152 }
153 }
154 return rtl::OUString();
155 }
156
hasQuery()157 sal_Bool UriReference::hasQuery() throw (css::uno::RuntimeException) {
158 osl::MutexGuard g(m_mutex);
159 return m_hasQuery;
160 }
161
getQuery()162 rtl::OUString UriReference::getQuery() throw (css::uno::RuntimeException) {
163 osl::MutexGuard g(m_mutex);
164 return m_query;
165 }
166
hasFragment()167 sal_Bool UriReference::hasFragment() throw (css::uno::RuntimeException) {
168 osl::MutexGuard g(m_mutex);
169 return m_hasFragment;
170 }
171
getFragment()172 rtl::OUString UriReference::getFragment() throw (css::uno::RuntimeException) {
173 osl::MutexGuard g(m_mutex);
174 return m_fragment;
175 }
176
setFragment(rtl::OUString const & fragment)177 void UriReference::setFragment(rtl::OUString const & fragment)
178 throw (css::uno::RuntimeException)
179 {
180 osl::MutexGuard g(m_mutex);
181 m_hasFragment = true;
182 m_fragment = fragment;
183 }
184
clearFragment()185 void UriReference::clearFragment() throw (css::uno::RuntimeException) {
186 osl::MutexGuard g(m_mutex);
187 m_hasFragment = false;
188 m_fragment = rtl::OUString();
189 }
190
appendSchemeSpecificPart(rtl::OUStringBuffer & buffer) const191 void UriReference::appendSchemeSpecificPart(rtl::OUStringBuffer & buffer) const
192 {
193 if (m_hasAuthority) {
194 buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
195 buffer.append(m_authority);
196 }
197 buffer.append(m_path);
198 if (m_hasQuery) {
199 buffer.append(static_cast< sal_Unicode >('?'));
200 buffer.append(m_query);
201 }
202 }
203