bump product version to 5.0.4.1
[LibreOffice.git] / ucb / source / ucp / hierarchy / hierarchyuri.cxx
blob0c4b2595711463c1ee0086d580bddfe4023f9e50
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 /**************************************************************************
22 TODO
23 **************************************************************************
25 *************************************************************************/
27 #include "rtl/ustrbuf.hxx"
28 #include "osl/diagnose.h"
30 #include "hierarchyuri.hxx"
32 using namespace hierarchy_ucp;
36 #define DEFAULT_DATA_SOURCE_SERVICE \
37 "com.sun.star.ucb.DefaultHierarchyDataSource"
42 // HierarchyUri Implementation.
47 void HierarchyUri::init() const
49 // Already inited?
50 if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
52 // Note: Maybe it's a re-init, setUri only resets m_aPath!
53 m_aService.clear();
54 m_aParentUri.clear();
55 m_aName.clear();
57 // URI must match at least: <sheme>:
58 if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
60 // error, but remember that we did a init().
61 m_aPath = "/";
62 return;
65 // Scheme is case insensitive.
66 OUString aScheme
67 = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
68 if ( aScheme == HIERARCHY_URL_SCHEME )
70 m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
72 sal_Int32 nPos = 0;
74 // If the URI has no service specifier, insert default service.
75 // This is for backward compatibility and for convenience.
77 if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
79 // root folder URI without path and service specifier.
80 m_aUri += "//" DEFAULT_DATA_SOURCE_SERVICE "/";
81 m_aService = DEFAULT_DATA_SOURCE_SERVICE ;
83 nPos = m_aUri.getLength() - 1;
85 else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
87 ( m_aUri[ HIERARCHY_URL_SCHEME_LENGTH + 1 ] == '/' ) )
89 // root folder URI without service specifier.
90 m_aUri += "/" DEFAULT_DATA_SOURCE_SERVICE "/";
91 m_aService = DEFAULT_DATA_SOURCE_SERVICE;
93 nPos = m_aUri.getLength() - 1;
95 else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
97 ( m_aUri[ HIERARCHY_URL_SCHEME_LENGTH + 2 ] != '/' ) )
99 // other (no root folder) URI without service specifier.
100 m_aUri = m_aUri.replaceAt(
101 HIERARCHY_URL_SCHEME_LENGTH + 2,
103 OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
104 m_aService = DEFAULT_DATA_SOURCE_SERVICE;
106 nPos
107 = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
109 else
111 // URI with service specifier.
112 sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
114 // Here: - m_aUri has at least the form "<scheme>://"
115 // - nStart points to char after <scheme>:
117 // Only <scheme>:// ?
118 if ( nStart == m_aUri.getLength() )
120 // error, but remember that we did a init().
121 m_aPath = "/";
122 return;
125 // Empty path segments?
126 if ( m_aUri.indexOf(
127 OUString("//"),
128 nStart ) != -1 )
130 // error, but remember that we did a init().
131 m_aPath = "/";
132 return;
135 sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
137 // Only <scheme>:/// ?
138 if ( nEnd == nStart )
140 // error, but remember that we did a init().
141 m_aPath = "/";
142 return;
145 if ( nEnd == -1 )
147 // Trailing slash missing.
148 nEnd = m_aUri.getLength();
149 m_aUri += "/";
152 m_aService = m_aUri.copy( nStart, nEnd - nStart );
154 nPos = nEnd;
157 // Here: - m_aUri has at least the form "<scheme>://<service>/"
158 // - m_aService was set
159 // - m_aPath, m_aParentPath, m_aName not yet set
160 // - nPos points to slash after service specifier
162 // Remove trailing slash, if not a root folder URI.
163 sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
164 if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
165 m_aUri = m_aUri.copy( 0, nEnd );
167 // Path (includes leading slash)
168 m_aPath = m_aUri.copy( nPos );
170 // parent URI + name
171 sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
172 if ( ( nLastSlash != -1 ) &&
173 ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
175 m_aParentUri = m_aUri.copy( 0, nLastSlash );
176 m_aName = m_aUri.copy( nLastSlash + 1 );
179 // success
180 m_bValid = true;
182 else
184 // error, but remember that we did a init().
185 m_aPath = "/";
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */