1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /**************************************************************************
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
50 if ( !m_aUri
.isEmpty() && m_aPath
.isEmpty() )
52 // Note: Maybe it's a re-init, setUri only resets m_aPath!
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().
65 // Scheme is case insensitive.
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
);
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
;
107 = HIERARCHY_URL_SCHEME_LENGTH
+ 3 + m_aService
.getLength();
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().
125 // Empty path segments?
130 // error, but remember that we did a init().
135 sal_Int32 nEnd
= m_aUri
.indexOf( '/', nStart
);
137 // Only <scheme>:/// ?
138 if ( nEnd
== nStart
)
140 // error, but remember that we did a init().
147 // Trailing slash missing.
148 nEnd
= m_aUri
.getLength();
152 m_aService
= m_aUri
.copy( nStart
, nEnd
- nStart
);
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
);
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 );
184 // error, but remember that we did a init().
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */