xref: /trunk/main/stoc/source/uriproc/UriReference.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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()
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() {
78     return m_scheme.getLength() != 0;
79 }
80 
getScheme()81 rtl::OUString UriReference::getScheme() {
82     return m_scheme;
83 }
84 
getSchemeSpecificPart()85 rtl::OUString UriReference::getSchemeSpecificPart()
86 {
87     osl::MutexGuard g(m_mutex);
88     rtl::OUStringBuffer buf;
89     appendSchemeSpecificPart(buf);
90     return buf.makeStringAndClear();
91 }
92 
isHierarchical()93 sal_Bool UriReference::isHierarchical() {
94     osl::MutexGuard g(m_mutex);
95     return m_isHierarchical;
96 }
97 
hasAuthority()98 sal_Bool UriReference::hasAuthority() {
99     osl::MutexGuard g(m_mutex);
100     return m_hasAuthority;
101 }
102 
getAuthority()103 rtl::OUString UriReference::getAuthority() {
104     osl::MutexGuard g(m_mutex);
105     return m_authority;
106 }
107 
getPath()108 rtl::OUString UriReference::getPath() {
109     osl::MutexGuard g(m_mutex);
110     return m_path;
111 }
112 
hasRelativePath()113 sal_Bool UriReference::hasRelativePath() {
114     osl::MutexGuard g(m_mutex);
115     return m_isHierarchical && !m_hasAuthority
116         && (m_path.getLength() == 0 || m_path[0] != '/');
117 }
118 
getPathSegmentCount()119 sal_Int32 UriReference::getPathSegmentCount()
120 {
121     osl::MutexGuard g(m_mutex);
122     if (!m_isHierarchical || m_path.getLength() == 0) {
123         return 0;
124     } else {
125         sal_Int32 n = m_path[0] == '/' ? 0 : 1;
126         for (sal_Int32 i = 0;; ++i) {
127             i = m_path.indexOf('/', i);
128             if (i < 0) {
129                 break;
130             }
131             ++n;
132         }
133         return n;
134     }
135 }
136 
getPathSegment(sal_Int32 index)137 rtl::OUString UriReference::getPathSegment(sal_Int32 index)
138 {
139     osl::MutexGuard g(m_mutex);
140     if (m_isHierarchical && m_path.getLength() != 0 && index >= 0) {
141         for (sal_Int32 i = m_path[0] == '/' ? 1 : 0;; ++i) {
142             if (index-- == 0) {
143                 sal_Int32 j = m_path.indexOf('/', i);
144                 return j < 0 ? m_path.copy(i) : m_path.copy(i, j - i);
145             }
146             i = m_path.indexOf('/', i);
147             if (i < 0) {
148                 break;
149             }
150         }
151     }
152     return rtl::OUString();
153 }
154 
hasQuery()155 sal_Bool UriReference::hasQuery() {
156     osl::MutexGuard g(m_mutex);
157     return m_hasQuery;
158 }
159 
getQuery()160 rtl::OUString UriReference::getQuery() {
161     osl::MutexGuard g(m_mutex);
162     return m_query;
163 }
164 
hasFragment()165 sal_Bool UriReference::hasFragment() {
166     osl::MutexGuard g(m_mutex);
167     return m_hasFragment;
168 }
169 
getFragment()170 rtl::OUString UriReference::getFragment() {
171     osl::MutexGuard g(m_mutex);
172     return m_fragment;
173 }
174 
setFragment(rtl::OUString const & fragment)175 void UriReference::setFragment(rtl::OUString const & fragment)
176 {
177     osl::MutexGuard g(m_mutex);
178     m_hasFragment = true;
179     m_fragment = fragment;
180 }
181 
clearFragment()182 void UriReference::clearFragment() {
183     osl::MutexGuard g(m_mutex);
184     m_hasFragment = false;
185     m_fragment = rtl::OUString();
186 }
187 
appendSchemeSpecificPart(rtl::OUStringBuffer & buffer) const188 void UriReference::appendSchemeSpecificPart(rtl::OUStringBuffer & buffer) const
189 {
190     if (m_hasAuthority) {
191         buffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("//"));
192         buffer.append(m_authority);
193     }
194     buffer.append(m_path);
195     if (m_hasQuery) {
196         buffer.append(static_cast< sal_Unicode >('?'));
197         buffer.append(m_query);
198     }
199 }
200