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