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 "hierarchyuri.hxx"
29 using namespace hierarchy_ucp
;
31 constexpr OUString HIERARCHY_URL_SCHEME
= u
"vnd.sun.star.hier"_ustr
;
32 constexpr OUString DEFAULT_DATA_SOURCE_SERVICE
=
33 u
"com.sun.star.ucb.DefaultHierarchyDataSource"_ustr
;
36 // HierarchyUri Implementation.
39 void HierarchyUri::init() const
42 if ( m_aUri
.isEmpty() || !m_aPath
.isEmpty() )
45 // Note: Maybe it's a re-init, setUri only resets m_aPath!
49 // URI must match at least: <scheme>:
50 if ( m_aUri
.getLength() < HIERARCHY_URL_SCHEME
.getLength() + 1 )
52 // error, but remember that we did an init().
57 // Scheme is case insensitive.
59 = m_aUri
.copy( 0, HIERARCHY_URL_SCHEME
.getLength() ).toAsciiLowerCase();
60 if ( aScheme
== HIERARCHY_URL_SCHEME
)
62 m_aUri
= m_aUri
.replaceAt( 0, aScheme
.getLength(), aScheme
);
66 // If the URI has no service specifier, insert default service.
67 // This is for backward compatibility and for convenience.
69 if ( m_aUri
.getLength() == HIERARCHY_URL_SCHEME
.getLength() + 1 )
71 // root folder URI without path and service specifier.
72 m_aUri
+= "//" + DEFAULT_DATA_SOURCE_SERVICE
+ "/";
73 m_aService
= DEFAULT_DATA_SOURCE_SERVICE
;
75 nPos
= m_aUri
.getLength() - 1;
77 else if ( ( m_aUri
.getLength() == HIERARCHY_URL_SCHEME
.getLength() + 2 )
79 ( m_aUri
[ HIERARCHY_URL_SCHEME
.getLength() + 1 ] == '/' ) )
81 // root folder URI without service specifier.
82 m_aUri
+= "/" + DEFAULT_DATA_SOURCE_SERVICE
+ "/";
83 m_aService
= DEFAULT_DATA_SOURCE_SERVICE
;
85 nPos
= m_aUri
.getLength() - 1;
87 else if ( ( m_aUri
.getLength() > HIERARCHY_URL_SCHEME
.getLength() + 2 )
89 ( m_aUri
[ HIERARCHY_URL_SCHEME
.getLength() + 2 ] != '/' ) )
91 // other (no root folder) URI without service specifier.
92 m_aUri
= m_aUri
.replaceAt(
93 HIERARCHY_URL_SCHEME
.getLength() + 2,
95 rtl::Concat2View("/" + DEFAULT_DATA_SOURCE_SERVICE
+ "/") );
96 m_aService
= DEFAULT_DATA_SOURCE_SERVICE
;
99 = HIERARCHY_URL_SCHEME
.getLength() + 3 + m_aService
.getLength();
103 // URI with service specifier.
104 sal_Int32 nStart
= HIERARCHY_URL_SCHEME
.getLength() + 3;
106 // Here: - m_aUri has at least the form "<scheme>://"
107 // - nStart points to char after <scheme>:
109 // Only <scheme>:// ?
110 if ( nStart
== m_aUri
.getLength() )
112 // error, but remember that we did an init().
117 // Empty path segments?
118 if ( m_aUri
.indexOf("//", nStart
) != -1 )
120 // error, but remember that we did an init().
125 sal_Int32 nEnd
= m_aUri
.indexOf( '/', nStart
);
127 // Only <scheme>:/// ?
128 if ( nEnd
== nStart
)
130 // error, but remember that we did an init().
137 // Trailing slash missing.
138 nEnd
= m_aUri
.getLength();
142 m_aService
= m_aUri
.copy( nStart
, nEnd
- nStart
);
147 // Here: - m_aUri has at least the form "<scheme>://<service>/"
148 // - m_aService was set
149 // - m_aPath, m_aParentPath, m_aName not yet set
150 // - nPos points to slash after service specifier
152 // Remove trailing slash, if not a root folder URI.
153 sal_Int32 nEnd
= m_aUri
.lastIndexOf( '/' );
154 if ( ( nEnd
> nPos
) && ( nEnd
== ( m_aUri
.getLength() - 1 ) ) )
155 m_aUri
= m_aUri
.copy( 0, nEnd
);
157 // Path (includes leading slash)
158 m_aPath
= m_aUri
.copy( nPos
);
161 sal_Int32 nLastSlash
= m_aUri
.lastIndexOf( '/' );
162 if ( ( nLastSlash
!= -1 ) &&
163 ( nLastSlash
!= m_aUri
.getLength() - 1 ) ) // root
165 m_aParentUri
= m_aUri
.copy( 0, nLastSlash
);
173 // error, but remember that we did an init().
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */