| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
/************************************************************************** |
| 22 |
TODO |
| 23 |
************************************************************************** |
| 24 |
|
| 25 |
*************************************************************************/ |
| 26 |
|
| 27 |
#include "hierarchyuri.hxx" |
| 28 |
|
| 29 |
using namespace hierarchy_ucp; |
| 30 |
|
| 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; |
| 34 |
|
| 35 |
|
| 36 |
// HierarchyUri Implementation. |
| 37 |
|
| 38 |
|
| 39 |
void HierarchyUri::init() const |
| 40 |
{ |
| 41 |
// Already inited? |
| 42 |
if ( m_aUri.isEmpty() || !m_aPath.isEmpty() ) |
| 43 |
return; |
| 44 |
|
| 45 |
// Note: Maybe it's a re-init, setUri only resets m_aPath! |
| 46 |
m_aService.clear(); |
| 47 |
m_aParentUri.clear(); |
| 48 |
|
| 49 |
// URI must match at least: <scheme>: |
| 50 |
if ( m_aUri.getLength() < HIERARCHY_URL_SCHEME.getLength() + 1 ) |
| 51 |
{ |
| 52 |
// error, but remember that we did an init(). |
| 53 |
m_aPath = "/"; |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Scheme is case insensitive. |
| 58 |
OUString aScheme |
| 59 |
= m_aUri.copy( 0, HIERARCHY_URL_SCHEME.getLength() ).toAsciiLowerCase(); |
| 60 |
if ( aScheme == HIERARCHY_URL_SCHEME ) |
| 61 |
{ |
| 62 |
m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme ); |
| 63 |
|
| 64 |
sal_Int32 nPos = 0; |
| 65 |
|
| 66 |
// If the URI has no service specifier, insert default service. |
| 67 |
// This is for backward compatibility and for convenience. |
| 68 |
|
| 69 |
if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME.getLength() + 1 ) |
| 70 |
{ |
| 71 |
// root folder URI without path and service specifier. |
| 72 |
m_aUri += "//" + DEFAULT_DATA_SOURCE_SERVICE + "/"; |
| 73 |
m_aService = DEFAULT_DATA_SOURCE_SERVICE ; |
| 74 |
|
| 75 |
nPos = m_aUri.getLength() - 1; |
| 76 |
} |
| 77 |
else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME.getLength() + 2 ) |
| 78 |
&& |
| 79 |
( m_aUri[ HIERARCHY_URL_SCHEME.getLength() + 1 ] == '/' ) ) |
| 80 |
{ |
| 81 |
// root folder URI without service specifier. |
| 82 |
m_aUri += "/" + DEFAULT_DATA_SOURCE_SERVICE + "/"; |
| 83 |
m_aService = DEFAULT_DATA_SOURCE_SERVICE; |
| 84 |
|
| 85 |
nPos = m_aUri.getLength() - 1; |
| 86 |
} |
| 87 |
else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME.getLength() + 2 ) |
| 88 |
&& |
| 89 |
( m_aUri[ HIERARCHY_URL_SCHEME.getLength() + 2 ] != '/' ) ) |
| 90 |
{ |
| 91 |
// other (no root folder) URI without service specifier. |
| 92 |
m_aUri = m_aUri.replaceAt( |
| 93 |
HIERARCHY_URL_SCHEME.getLength() + 2, |
| 94 |
0, |
| 95 |
rtl::Concat2View("/" + DEFAULT_DATA_SOURCE_SERVICE + "/") ); |
| 96 |
m_aService = DEFAULT_DATA_SOURCE_SERVICE; |
| 97 |
|
| 98 |
nPos |
| 99 |
= HIERARCHY_URL_SCHEME.getLength() + 3 + m_aService.getLength(); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
// URI with service specifier. |
| 104 |
sal_Int32 nStart = HIERARCHY_URL_SCHEME.getLength() + 3; |
| 105 |
|
| 106 |
// Here: - m_aUri has at least the form "<scheme>://" |
| 107 |
// - nStart points to char after <scheme>: |
| 108 |
|
| 109 |
// Only <scheme>:// ? |
| 110 |
if ( nStart == m_aUri.getLength() ) |
| 111 |
{ |
| 112 |
// error, but remember that we did an init(). |
| 113 |
m_aPath = "/"; |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// Empty path segments? |
| 118 |
if ( m_aUri.indexOf("//", nStart) != -1 ) |
| 119 |
{ |
| 120 |
// error, but remember that we did an init(). |
| 121 |
m_aPath = "/"; |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
sal_Int32 nEnd = m_aUri.indexOf( '/', nStart ); |
| 126 |
|
| 127 |
// Only <scheme>:/// ? |
| 128 |
if ( nEnd == nStart ) |
| 129 |
{ |
| 130 |
// error, but remember that we did an init(). |
| 131 |
m_aPath = "/"; |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ( nEnd == -1 ) |
| 136 |
{ |
| 137 |
// Trailing slash missing. |
| 138 |
nEnd = m_aUri.getLength(); |
| 139 |
m_aUri += "/"; |
| 140 |
} |
| 141 |
|
| 142 |
m_aService = m_aUri.copy( nStart, nEnd - nStart ); |
| 143 |
|
| 144 |
nPos = nEnd; |
| 145 |
} |
| 146 |
|
| 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 |
| 151 |
|
| 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 ); |
| 156 |
|
| 157 |
// Path (includes leading slash) |
| 158 |
m_aPath = m_aUri.copy( nPos ); |
| 159 |
|
| 160 |
// parent URI + name |
| 161 |
sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' ); |
| 162 |
if ( ( nLastSlash != -1 ) && |
| 163 |
( nLastSlash != m_aUri.getLength() - 1 ) ) // root |
| 164 |
{ |
| 165 |
m_aParentUri = m_aUri.copy( 0, nLastSlash ); |
| 166 |
} |
| 167 |
|
| 168 |
// success |
| 169 |
m_bValid = true; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
// error, but remember that we did an init(). |
| 174 |
m_aPath = "/"; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |