xref: /AOO41X/main/ucb/source/ucp/hierarchy/hierarchyuri.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**************************************************************************
32*cdf0e10cSrcweir                                 TODO
33*cdf0e10cSrcweir  **************************************************************************
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir  *************************************************************************/
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
38*cdf0e10cSrcweir #include "osl/diagnose.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "hierarchyuri.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace hierarchy_ucp;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //=========================================================================
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #define DEFAULT_DATA_SOURCE_SERVICE \
47*cdf0e10cSrcweir                     "com.sun.star.ucb.DefaultHierarchyDataSource"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //=========================================================================
50*cdf0e10cSrcweir //=========================================================================
51*cdf0e10cSrcweir //
52*cdf0e10cSrcweir // HierarchyUri Implementation.
53*cdf0e10cSrcweir //
54*cdf0e10cSrcweir //=========================================================================
55*cdf0e10cSrcweir //=========================================================================
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir void HierarchyUri::init() const
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     // Already inited?
60*cdf0e10cSrcweir     if ( m_aUri.getLength() && !m_aPath.getLength() )
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         // Note: Maybe it's a re-init, setUri only resets m_aPath!
63*cdf0e10cSrcweir         m_aService = m_aParentUri = m_aName = rtl::OUString();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         // URI must match at least: <sheme>:
66*cdf0e10cSrcweir         if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             // error, but remember that we did a init().
69*cdf0e10cSrcweir             m_aPath = rtl::OUString::createFromAscii( "/" );
70*cdf0e10cSrcweir             return;
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         // Scheme is case insensitive.
74*cdf0e10cSrcweir         rtl::OUString aScheme
75*cdf0e10cSrcweir             = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
76*cdf0e10cSrcweir         if ( aScheme.equalsAsciiL(
77*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM( HIERARCHY_URL_SCHEME ) ) )
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir             sal_Int32 nPos = 0;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir             // If the URI has no service specifier, insert default service.
84*cdf0e10cSrcweir             // This is for backward compatibility and for convenience.
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir             if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
87*cdf0e10cSrcweir             {
88*cdf0e10cSrcweir                 // root folder URI without path and service specifier.
89*cdf0e10cSrcweir                 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
90*cdf0e10cSrcweir                                 "//" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
91*cdf0e10cSrcweir                 m_aService
92*cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
93*cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir                 nPos = m_aUri.getLength() - 1;
96*cdf0e10cSrcweir             }
97*cdf0e10cSrcweir             else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
98*cdf0e10cSrcweir                       &&
99*cdf0e10cSrcweir                       ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 1 ]
100*cdf0e10cSrcweir                                                     == sal_Unicode( '/' ) ) )
101*cdf0e10cSrcweir             {
102*cdf0e10cSrcweir                 // root folder URI without service specifier.
103*cdf0e10cSrcweir                 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
104*cdf0e10cSrcweir                                 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
105*cdf0e10cSrcweir                 m_aService
106*cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
107*cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir                 nPos = m_aUri.getLength() - 1;
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir             else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
112*cdf0e10cSrcweir                       &&
113*cdf0e10cSrcweir                       ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 2 ]
114*cdf0e10cSrcweir                                                     != sal_Unicode( '/' ) ) )
115*cdf0e10cSrcweir             {
116*cdf0e10cSrcweir                 // other (no root folder) URI without service specifier.
117*cdf0e10cSrcweir                 m_aUri = m_aUri.replaceAt(
118*cdf0e10cSrcweir                             HIERARCHY_URL_SCHEME_LENGTH + 2,
119*cdf0e10cSrcweir                             0,
120*cdf0e10cSrcweir                             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
121*cdf0e10cSrcweir                                 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) ) );
122*cdf0e10cSrcweir                 m_aService
123*cdf0e10cSrcweir                     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
124*cdf0e10cSrcweir                                             DEFAULT_DATA_SOURCE_SERVICE ) );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir                 nPos
127*cdf0e10cSrcweir                     = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir             else
130*cdf0e10cSrcweir             {
131*cdf0e10cSrcweir                 // URI with service specifier.
132*cdf0e10cSrcweir                 sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                 // Here: - m_aUri has at least the form "<scheme>://"
135*cdf0e10cSrcweir                 //       - nStart points to char after <scheme>://
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                 // Only <scheme>:// ?
138*cdf0e10cSrcweir                 if ( nStart == m_aUri.getLength() )
139*cdf0e10cSrcweir                 {
140*cdf0e10cSrcweir                     // error, but remember that we did a init().
141*cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
142*cdf0e10cSrcweir                     return;
143*cdf0e10cSrcweir                 }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir                 // Empty path segments?
146*cdf0e10cSrcweir                 if ( m_aUri.indexOf(
147*cdf0e10cSrcweir                         rtl::OUString::createFromAscii( "//" ),
148*cdf0e10cSrcweir                         nStart ) != -1 )
149*cdf0e10cSrcweir                 {
150*cdf0e10cSrcweir                     // error, but remember that we did a init().
151*cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
152*cdf0e10cSrcweir                     return;
153*cdf0e10cSrcweir                 }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir                 sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir                 // Only <scheme>:/// ?
158*cdf0e10cSrcweir                 if ( nEnd == nStart )
159*cdf0e10cSrcweir                 {
160*cdf0e10cSrcweir                     // error, but remember that we did a init().
161*cdf0e10cSrcweir                     m_aPath = rtl::OUString::createFromAscii( "/" );
162*cdf0e10cSrcweir                     return;
163*cdf0e10cSrcweir                 }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir                 if ( nEnd == -1 )
166*cdf0e10cSrcweir                 {
167*cdf0e10cSrcweir                     // Trailing slash missing.
168*cdf0e10cSrcweir                     nEnd = m_aUri.getLength();
169*cdf0e10cSrcweir                     m_aUri += rtl::OUString::createFromAscii( "/" );
170*cdf0e10cSrcweir                 }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir                 m_aService = m_aUri.copy( nStart, nEnd - nStart );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir                 nPos = nEnd;
175*cdf0e10cSrcweir             }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir             // Here: - m_aUri has at least the form "<scheme>://<service>/"
178*cdf0e10cSrcweir             //       - m_aService was set
179*cdf0e10cSrcweir             //       - m_aPath, m_aParentPath, m_aName not yet set
180*cdf0e10cSrcweir             //       - nPos points to slash after service specifier
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir             // Remove trailing slash, if not a root folder URI.
183*cdf0e10cSrcweir             sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
184*cdf0e10cSrcweir             if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
185*cdf0e10cSrcweir                 m_aUri = m_aUri.copy( 0, nEnd );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             // Path (includes leading slash)
188*cdf0e10cSrcweir             m_aPath = m_aUri.copy( nPos );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             // parent URI + name
191*cdf0e10cSrcweir             sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
192*cdf0e10cSrcweir             if ( ( nLastSlash != -1 ) &&
193*cdf0e10cSrcweir                  ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                 m_aParentUri = m_aUri.copy( 0, nLastSlash );
196*cdf0e10cSrcweir                 m_aName      = m_aUri.copy( nLastSlash + 1 );
197*cdf0e10cSrcweir             }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir             // success
200*cdf0e10cSrcweir             m_bValid = true;
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir         else
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             // error, but remember that we did a init().
205*cdf0e10cSrcweir             m_aPath = rtl::OUString::createFromAscii( "/" );
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210