Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / hierarchy / hierarchyuri.cxx
blob8d6d051177f124a88ec00dc4cf717a99283ebe1e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 /**************************************************************************
31 TODO
32 **************************************************************************
34 *************************************************************************/
36 #include "rtl/ustrbuf.hxx"
37 #include "osl/diagnose.h"
39 #include "hierarchyuri.hxx"
41 using namespace hierarchy_ucp;
43 //=========================================================================
45 #define DEFAULT_DATA_SOURCE_SERVICE \
46 "com.sun.star.ucb.DefaultHierarchyDataSource"
48 //=========================================================================
49 //=========================================================================
51 // HierarchyUri Implementation.
53 //=========================================================================
54 //=========================================================================
56 void HierarchyUri::init() const
58 // Already inited?
59 if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
61 // Note: Maybe it's a re-init, setUri only resets m_aPath!
62 m_aService = m_aParentUri = m_aName = rtl::OUString();
64 // URI must match at least: <sheme>:
65 if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
67 // error, but remember that we did a init().
68 m_aPath = rtl::OUString("/");
69 return;
72 // Scheme is case insensitive.
73 rtl::OUString aScheme
74 = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
75 if ( aScheme == HIERARCHY_URL_SCHEME )
77 m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
79 sal_Int32 nPos = 0;
81 // If the URI has no service specifier, insert default service.
82 // This is for backward compatibility and for convenience.
84 if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
86 // root folder URI without path and service specifier.
87 m_aUri += rtl::OUString( "//" DEFAULT_DATA_SOURCE_SERVICE "/" );
88 m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
90 nPos = m_aUri.getLength() - 1;
92 else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
94 ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 1 ]
95 == sal_Unicode( '/' ) ) )
97 // root folder URI without service specifier.
98 m_aUri += rtl::OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" );
99 m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
101 nPos = m_aUri.getLength() - 1;
103 else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
105 ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 2 ]
106 != sal_Unicode( '/' ) ) )
108 // other (no root folder) URI without service specifier.
109 m_aUri = m_aUri.replaceAt(
110 HIERARCHY_URL_SCHEME_LENGTH + 2,
112 rtl::OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
113 m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
115 nPos
116 = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
118 else
120 // URI with service specifier.
121 sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
123 // Here: - m_aUri has at least the form "<scheme>://"
124 // - nStart points to char after <scheme>://
126 // Only <scheme>:// ?
127 if ( nStart == m_aUri.getLength() )
129 // error, but remember that we did a init().
130 m_aPath = rtl::OUString("/");
131 return;
134 // Empty path segments?
135 if ( m_aUri.indexOf(
136 rtl::OUString("//"),
137 nStart ) != -1 )
139 // error, but remember that we did a init().
140 m_aPath = rtl::OUString("/");
141 return;
144 sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
146 // Only <scheme>:/// ?
147 if ( nEnd == nStart )
149 // error, but remember that we did a init().
150 m_aPath = rtl::OUString("/");
151 return;
154 if ( nEnd == -1 )
156 // Trailing slash missing.
157 nEnd = m_aUri.getLength();
158 m_aUri += rtl::OUString("/");
161 m_aService = m_aUri.copy( nStart, nEnd - nStart );
163 nPos = nEnd;
166 // Here: - m_aUri has at least the form "<scheme>://<service>/"
167 // - m_aService was set
168 // - m_aPath, m_aParentPath, m_aName not yet set
169 // - nPos points to slash after service specifier
171 // Remove trailing slash, if not a root folder URI.
172 sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
173 if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
174 m_aUri = m_aUri.copy( 0, nEnd );
176 // Path (includes leading slash)
177 m_aPath = m_aUri.copy( nPos );
179 // parent URI + name
180 sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
181 if ( ( nLastSlash != -1 ) &&
182 ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
184 m_aParentUri = m_aUri.copy( 0, nLastSlash );
185 m_aName = m_aUri.copy( nLastSlash + 1 );
188 // success
189 m_bValid = true;
191 else
193 // error, but remember that we did a init().
194 m_aPath = rtl::OUString("/");
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */