update dev300-m58
[ooovba.git] / ucb / source / ucp / hierarchy / hierarchyuri.cxx
blob7339e3434112dea7208698d736db48bceca07466
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hierarchyuri.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include "rtl/ustrbuf.hxx"
41 #include "osl/diagnose.h"
43 #include "hierarchyuri.hxx"
45 using namespace hierarchy_ucp;
47 //=========================================================================
49 #define DEFAULT_DATA_SOURCE_SERVICE \
50 "com.sun.star.ucb.DefaultHierarchyDataSource"
52 //=========================================================================
53 //=========================================================================
55 // HierarchyUri Implementation.
57 //=========================================================================
58 //=========================================================================
60 void HierarchyUri::init() const
62 // Already inited?
63 if ( m_aUri.getLength() && !m_aPath.getLength() )
65 // Note: Maybe it's a re-init, setUri only resets m_aPath!
66 m_aService = m_aParentUri = m_aName = rtl::OUString();
68 // URI must match at least: <sheme>:
69 if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
71 // error, but remember that we did a init().
72 m_aPath = rtl::OUString::createFromAscii( "/" );
73 return;
76 // Scheme is case insensitive.
77 rtl::OUString aScheme
78 = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
79 if ( aScheme.equalsAsciiL(
80 RTL_CONSTASCII_STRINGPARAM( HIERARCHY_URL_SCHEME ) ) )
82 m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
84 sal_Int32 nPos = 0;
86 // If the URI has no service specifier, insert default service.
87 // This is for backward compatibility and for convenience.
89 if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
91 // root folder URI without path and service specifier.
92 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
93 "//" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
94 m_aService
95 = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
96 DEFAULT_DATA_SOURCE_SERVICE ) );
98 nPos = m_aUri.getLength() - 1;
100 else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
102 ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 1 ]
103 == sal_Unicode( '/' ) ) )
105 // root folder URI without service specifier.
106 m_aUri += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
107 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
108 m_aService
109 = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
110 DEFAULT_DATA_SOURCE_SERVICE ) );
112 nPos = m_aUri.getLength() - 1;
114 else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
116 ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 2 ]
117 != sal_Unicode( '/' ) ) )
119 // other (no root folder) URI without service specifier.
120 m_aUri = m_aUri.replaceAt(
121 HIERARCHY_URL_SCHEME_LENGTH + 2,
123 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
124 "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) ) );
125 m_aService
126 = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
127 DEFAULT_DATA_SOURCE_SERVICE ) );
129 nPos
130 = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
132 else
134 // URI with service specifier.
135 sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
137 // Here: - m_aUri has at least the form "<scheme>://"
138 // - nStart points to char after <scheme>://
140 // Only <scheme>:// ?
141 if ( nStart == m_aUri.getLength() )
143 // error, but remember that we did a init().
144 m_aPath = rtl::OUString::createFromAscii( "/" );
145 return;
148 // Empty path segments?
149 if ( m_aUri.indexOf(
150 rtl::OUString::createFromAscii( "//" ),
151 nStart ) != -1 )
153 // error, but remember that we did a init().
154 m_aPath = rtl::OUString::createFromAscii( "/" );
155 return;
158 sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
160 // Only <scheme>:/// ?
161 if ( nEnd == nStart )
163 // error, but remember that we did a init().
164 m_aPath = rtl::OUString::createFromAscii( "/" );
165 return;
168 if ( nEnd == -1 )
170 // Trailing slash missing.
171 nEnd = m_aUri.getLength();
172 m_aUri += rtl::OUString::createFromAscii( "/" );
175 m_aService = m_aUri.copy( nStart, nEnd - nStart );
177 nPos = nEnd;
180 // Here: - m_aUri has at least the form "<scheme>://<service>/"
181 // - m_aService was set
182 // - m_aPath, m_aParentPath, m_aName not yet set
183 // - nPos points to slash after service specifier
185 // Remove trailing slash, if not a root folder URI.
186 sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
187 if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
188 m_aUri = m_aUri.copy( 0, nEnd );
190 // Path (includes leading slash)
191 m_aPath = m_aUri.copy( nPos );
193 // parent URI + name
194 sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
195 if ( ( nLastSlash != -1 ) &&
196 ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
198 m_aParentUri = m_aUri.copy( 0, nLastSlash );
199 m_aName = m_aUri.copy( nLastSlash + 1 );
202 // success
203 m_bValid = true;
205 else
207 // error, but remember that we did a init().
208 m_aPath = rtl::OUString::createFromAscii( "/" );