Line data Source code
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 "rtl/ustrbuf.hxx"
28 : #include "osl/diagnose.h"
29 :
30 : #include "hierarchyuri.hxx"
31 :
32 : using namespace hierarchy_ucp;
33 :
34 :
35 :
36 : #define DEFAULT_DATA_SOURCE_SERVICE \
37 : "com.sun.star.ucb.DefaultHierarchyDataSource"
38 :
39 :
40 :
41 :
42 : // HierarchyUri Implementation.
43 :
44 :
45 :
46 :
47 574 : void HierarchyUri::init() const
48 : {
49 : // Already inited?
50 574 : if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
51 : {
52 : // Note: Maybe it's a re-init, setUri only resets m_aPath!
53 378 : m_aService = m_aParentUri = m_aName = "";
54 :
55 : // URI must match at least: <sheme>:
56 378 : if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
57 : {
58 : // error, but remember that we did a init().
59 0 : m_aPath = "/";
60 0 : return;
61 : }
62 :
63 : // Scheme is case insensitive.
64 : OUString aScheme
65 378 : = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
66 378 : if ( aScheme == HIERARCHY_URL_SCHEME )
67 : {
68 378 : m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
69 :
70 378 : sal_Int32 nPos = 0;
71 :
72 : // If the URI has no service specifier, insert default service.
73 : // This is for backward compatibility and for convenience.
74 :
75 378 : if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
76 : {
77 : // root folder URI without path and service specifier.
78 0 : m_aUri += "//" DEFAULT_DATA_SOURCE_SERVICE "/";
79 0 : m_aService = DEFAULT_DATA_SOURCE_SERVICE ;
80 :
81 0 : nPos = m_aUri.getLength() - 1;
82 : }
83 756 : else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
84 382 : &&
85 4 : ( m_aUri[ HIERARCHY_URL_SCHEME_LENGTH + 1 ] == '/' ) )
86 : {
87 : // root folder URI without service specifier.
88 4 : m_aUri += "/" DEFAULT_DATA_SOURCE_SERVICE "/";
89 4 : m_aService = DEFAULT_DATA_SOURCE_SERVICE;
90 :
91 4 : nPos = m_aUri.getLength() - 1;
92 : }
93 748 : else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
94 748 : &&
95 374 : ( m_aUri[ HIERARCHY_URL_SCHEME_LENGTH + 2 ] != '/' ) )
96 : {
97 : // other (no root folder) URI without service specifier.
98 100 : m_aUri = m_aUri.replaceAt(
99 : HIERARCHY_URL_SCHEME_LENGTH + 2,
100 : 0,
101 50 : OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
102 50 : m_aService = DEFAULT_DATA_SOURCE_SERVICE;
103 :
104 : nPos
105 50 : = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
106 : }
107 : else
108 : {
109 : // URI with service specifier.
110 324 : sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
111 :
112 : // Here: - m_aUri has at least the form "<scheme>://"
113 : // - nStart points to char after <scheme>:
114 :
115 : // Only <scheme>:// ?
116 324 : if ( nStart == m_aUri.getLength() )
117 : {
118 : // error, but remember that we did a init().
119 0 : m_aPath = "/";
120 0 : return;
121 : }
122 :
123 : // Empty path segments?
124 648 : if ( m_aUri.indexOf(
125 : OUString("//"),
126 648 : nStart ) != -1 )
127 : {
128 : // error, but remember that we did a init().
129 0 : m_aPath = "/";
130 0 : return;
131 : }
132 :
133 324 : sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
134 :
135 : // Only <scheme>:/// ?
136 324 : if ( nEnd == nStart )
137 : {
138 : // error, but remember that we did a init().
139 0 : m_aPath = "/";
140 0 : return;
141 : }
142 :
143 324 : if ( nEnd == -1 )
144 : {
145 : // Trailing slash missing.
146 0 : nEnd = m_aUri.getLength();
147 0 : m_aUri += "/";
148 : }
149 :
150 324 : m_aService = m_aUri.copy( nStart, nEnd - nStart );
151 :
152 324 : nPos = nEnd;
153 : }
154 :
155 : // Here: - m_aUri has at least the form "<scheme>://<service>/"
156 : // - m_aService was set
157 : // - m_aPath, m_aParentPath, m_aName not yet set
158 : // - nPos points to slash after service specifier
159 :
160 : // Remove trailing slash, if not a root folder URI.
161 378 : sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
162 378 : if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
163 0 : m_aUri = m_aUri.copy( 0, nEnd );
164 :
165 : // Path (includes leading slash)
166 378 : m_aPath = m_aUri.copy( nPos );
167 :
168 : // parent URI + name
169 378 : sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
170 756 : if ( ( nLastSlash != -1 ) &&
171 378 : ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
172 : {
173 364 : m_aParentUri = m_aUri.copy( 0, nLastSlash );
174 364 : m_aName = m_aUri.copy( nLastSlash + 1 );
175 : }
176 :
177 : // success
178 378 : m_bValid = true;
179 : }
180 : else
181 : {
182 : // error, but remember that we did a init().
183 0 : m_aPath = "/";
184 378 : }
185 : }
186 : }
187 :
188 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|