xref: /AOO41X/main/ucb/source/ucp/hierarchy/hierarchyuri.cxx (revision 2f86921c33504fdff5a030df6c0b258927045abb)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir                                 TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  *************************************************************************/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
34cdf0e10cSrcweir #include "osl/diagnose.h"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "hierarchyuri.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace hierarchy_ucp;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //=========================================================================
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define DEFAULT_DATA_SOURCE_SERVICE \
43cdf0e10cSrcweir                     "com.sun.star.ucb.DefaultHierarchyDataSource"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //=========================================================================
46cdf0e10cSrcweir //=========================================================================
47cdf0e10cSrcweir //
48cdf0e10cSrcweir // HierarchyUri Implementation.
49cdf0e10cSrcweir //
50cdf0e10cSrcweir //=========================================================================
51cdf0e10cSrcweir //=========================================================================
52cdf0e10cSrcweir 
init() const53cdf0e10cSrcweir void HierarchyUri::init() const
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     // Already inited?
56cdf0e10cSrcweir     if ( m_aUri.getLength() && !m_aPath.getLength() )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         // Note: Maybe it's a re-init, setUri only resets m_aPath!
59cdf0e10cSrcweir         m_aService = m_aParentUri = m_aName = rtl::OUString();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         // URI must match at least: <sheme>:
62cdf0e10cSrcweir         if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             // error, but remember that we did a init().
65cdf0e10cSrcweir             m_aPath = rtl::OUString::createFromAscii( "/" );
66cdf0e10cSrcweir             return;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         // Scheme is case insensitive.
70cdf0e10cSrcweir         rtl::OUString aScheme
71cdf0e10cSrcweir             = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
72cdf0e10cSrcweir         if ( aScheme.equalsAsciiL(
73cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM( HIERARCHY_URL_SCHEME ) ) )
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir             sal_Int32 nPos = 0;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             // If the URI has no service specifier, insert default service.
80cdf0e10cSrcweir             // This is for backward compatibility and for convenience.
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
83cdf0e10cSrcweir             {
84cdf0e10cSrcweir                 // root folder URI without path and service specifier.
85cdf0e10cSrcweir                 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
86cdf0e10cSrcweir                                 "//" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
87cdf0e10cSrcweir                 m_aService
88cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
89cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir                 nPos = m_aUri.getLength() - 1;
92cdf0e10cSrcweir             }
93cdf0e10cSrcweir             else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
94cdf0e10cSrcweir                       &&
95cdf0e10cSrcweir                       ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 1 ]
96cdf0e10cSrcweir                                                     == sal_Unicode( '/' ) ) )
97cdf0e10cSrcweir             {
98cdf0e10cSrcweir                 // root folder URI without service specifier.
99cdf0e10cSrcweir                 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
100cdf0e10cSrcweir                                 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
101cdf0e10cSrcweir                 m_aService
102cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
103cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir                 nPos = m_aUri.getLength() - 1;
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir             else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
108cdf0e10cSrcweir                       &&
109cdf0e10cSrcweir                       ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 2 ]
110cdf0e10cSrcweir                                                     != sal_Unicode( '/' ) ) )
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 // other (no root folder) URI without service specifier.
113cdf0e10cSrcweir                 m_aUri = m_aUri.replaceAt(
114cdf0e10cSrcweir                             HIERARCHY_URL_SCHEME_LENGTH + 2,
115cdf0e10cSrcweir                             0,
116cdf0e10cSrcweir                             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
117cdf0e10cSrcweir                                 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) ) );
118cdf0e10cSrcweir                 m_aService
119cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
120cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir                 nPos
123cdf0e10cSrcweir                     = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir             else
126cdf0e10cSrcweir             {
127cdf0e10cSrcweir                 // URI with service specifier.
128cdf0e10cSrcweir                 sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir                 // Here: - m_aUri has at least the form "<scheme>://"
131cdf0e10cSrcweir                 //       - nStart points to char after <scheme>://
132cdf0e10cSrcweir 
133cdf0e10cSrcweir                 // Only <scheme>:// ?
134cdf0e10cSrcweir                 if ( nStart == m_aUri.getLength() )
135cdf0e10cSrcweir                 {
136cdf0e10cSrcweir                     // error, but remember that we did a init().
137cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
138cdf0e10cSrcweir                     return;
139cdf0e10cSrcweir                 }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir                 // Empty path segments?
142cdf0e10cSrcweir                 if ( m_aUri.indexOf(
143cdf0e10cSrcweir                         rtl::OUString::createFromAscii( "//" ),
144cdf0e10cSrcweir                         nStart ) != -1 )
145cdf0e10cSrcweir                 {
146cdf0e10cSrcweir                     // error, but remember that we did a init().
147cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
148cdf0e10cSrcweir                     return;
149cdf0e10cSrcweir                 }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                 sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir                 // Only <scheme>:/// ?
154cdf0e10cSrcweir                 if ( nEnd == nStart )
155cdf0e10cSrcweir                 {
156cdf0e10cSrcweir                     // error, but remember that we did a init().
157cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
158cdf0e10cSrcweir                     return;
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir                 if ( nEnd == -1 )
162cdf0e10cSrcweir                 {
163cdf0e10cSrcweir                     // Trailing slash missing.
164cdf0e10cSrcweir                     nEnd = m_aUri.getLength();
165cdf0e10cSrcweir                     m_aUri += rtl::OUString::createFromAscii( "/" );
166cdf0e10cSrcweir                 }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                 m_aService = m_aUri.copy( nStart, nEnd - nStart );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                 nPos = nEnd;
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir             // Here: - m_aUri has at least the form "<scheme>://<service>/"
174cdf0e10cSrcweir             //       - m_aService was set
175cdf0e10cSrcweir             //       - m_aPath, m_aParentPath, m_aName not yet set
176cdf0e10cSrcweir             //       - nPos points to slash after service specifier
177cdf0e10cSrcweir 
178cdf0e10cSrcweir             // Remove trailing slash, if not a root folder URI.
179cdf0e10cSrcweir             sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
180cdf0e10cSrcweir             if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
181cdf0e10cSrcweir                 m_aUri = m_aUri.copy( 0, nEnd );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             // Path (includes leading slash)
184cdf0e10cSrcweir             m_aPath = m_aUri.copy( nPos );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             // parent URI + name
187cdf0e10cSrcweir             sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
188cdf0e10cSrcweir             if ( ( nLastSlash != -1 ) &&
189cdf0e10cSrcweir                  ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 m_aParentUri = m_aUri.copy( 0, nLastSlash );
192cdf0e10cSrcweir                 m_aName      = m_aUri.copy( nLastSlash + 1 );
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir             // success
196cdf0e10cSrcweir             m_bValid = true;
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         else
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             // error, but remember that we did a init().
201cdf0e10cSrcweir             m_aPath = rtl::OUString::createFromAscii( "/" );
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206