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 0 : void HierarchyUri::init() const
48 : {
49 : // Already inited?
50 0 : if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
51 : {
52 : // Note: Maybe it's a re-init, setUri only resets m_aPath!
53 0 : m_aService = m_aParentUri = m_aName = rtl::OUString();
54 :
55 : // URI must match at least: <sheme>:
56 0 : if ( ( m_aUri.getLength() < HIERARCHY_URL_SCHEME_LENGTH + 1 ) )
57 : {
58 : // error, but remember that we did a init().
59 0 : m_aPath = rtl::OUString("/");
60 : return;
61 : }
62 :
63 : // Scheme is case insensitive.
64 : rtl::OUString aScheme
65 0 : = m_aUri.copy( 0, HIERARCHY_URL_SCHEME_LENGTH ).toAsciiLowerCase();
66 0 : if ( aScheme == HIERARCHY_URL_SCHEME )
67 : {
68 0 : m_aUri = m_aUri.replaceAt( 0, aScheme.getLength(), aScheme );
69 :
70 0 : 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 0 : if ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 1 )
76 : {
77 : // root folder URI without path and service specifier.
78 0 : m_aUri += rtl::OUString( "//" DEFAULT_DATA_SOURCE_SERVICE "/" );
79 0 : m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
80 :
81 0 : nPos = m_aUri.getLength() - 1;
82 : }
83 0 : else if ( ( m_aUri.getLength() == HIERARCHY_URL_SCHEME_LENGTH + 2 )
84 : &&
85 0 : ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 1 ]
86 : == sal_Unicode( '/' ) ) )
87 : {
88 : // root folder URI without service specifier.
89 0 : m_aUri += rtl::OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" );
90 0 : m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
91 :
92 0 : nPos = m_aUri.getLength() - 1;
93 : }
94 0 : else if ( ( m_aUri.getLength() > HIERARCHY_URL_SCHEME_LENGTH + 2 )
95 : &&
96 0 : ( m_aUri.getStr()[ HIERARCHY_URL_SCHEME_LENGTH + 2 ]
97 : != sal_Unicode( '/' ) ) )
98 : {
99 : // other (no root folder) URI without service specifier.
100 : m_aUri = m_aUri.replaceAt(
101 : HIERARCHY_URL_SCHEME_LENGTH + 2,
102 : 0,
103 0 : rtl::OUString( "/" DEFAULT_DATA_SOURCE_SERVICE "/" ) );
104 0 : m_aService = rtl::OUString( DEFAULT_DATA_SOURCE_SERVICE );
105 :
106 : nPos
107 0 : = HIERARCHY_URL_SCHEME_LENGTH + 3 + m_aService.getLength();
108 : }
109 : else
110 : {
111 : // URI with service specifier.
112 0 : sal_Int32 nStart = HIERARCHY_URL_SCHEME_LENGTH + 3;
113 :
114 : // Here: - m_aUri has at least the form "<scheme>://"
115 : // - nStart points to char after <scheme>://
116 :
117 : // Only <scheme>:// ?
118 0 : if ( nStart == m_aUri.getLength() )
119 : {
120 : // error, but remember that we did a init().
121 0 : m_aPath = rtl::OUString("/");
122 : return;
123 : }
124 :
125 : // Empty path segments?
126 0 : if ( m_aUri.indexOf(
127 : rtl::OUString("//"),
128 0 : nStart ) != -1 )
129 : {
130 : // error, but remember that we did a init().
131 0 : m_aPath = rtl::OUString("/");
132 : return;
133 : }
134 :
135 0 : sal_Int32 nEnd = m_aUri.indexOf( '/', nStart );
136 :
137 : // Only <scheme>:/// ?
138 0 : if ( nEnd == nStart )
139 : {
140 : // error, but remember that we did a init().
141 0 : m_aPath = rtl::OUString("/");
142 : return;
143 : }
144 :
145 0 : if ( nEnd == -1 )
146 : {
147 : // Trailing slash missing.
148 0 : nEnd = m_aUri.getLength();
149 0 : m_aUri += rtl::OUString("/");
150 : }
151 :
152 0 : m_aService = m_aUri.copy( nStart, nEnd - nStart );
153 :
154 0 : nPos = nEnd;
155 : }
156 :
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
161 :
162 : // Remove trailing slash, if not a root folder URI.
163 0 : sal_Int32 nEnd = m_aUri.lastIndexOf( '/' );
164 0 : if ( ( nEnd > nPos ) && ( nEnd == ( m_aUri.getLength() - 1 ) ) )
165 0 : m_aUri = m_aUri.copy( 0, nEnd );
166 :
167 : // Path (includes leading slash)
168 0 : m_aPath = m_aUri.copy( nPos );
169 :
170 : // parent URI + name
171 0 : sal_Int32 nLastSlash = m_aUri.lastIndexOf( '/' );
172 0 : if ( ( nLastSlash != -1 ) &&
173 0 : ( nLastSlash != m_aUri.getLength() - 1 ) ) // root
174 : {
175 0 : m_aParentUri = m_aUri.copy( 0, nLastSlash );
176 0 : m_aName = m_aUri.copy( nLastSlash + 1 );
177 : }
178 :
179 : // success
180 0 : m_bValid = true;
181 : }
182 : else
183 : {
184 : // error, but remember that we did a init().
185 0 : m_aPath = rtl::OUString("/");
186 0 : }
187 : }
188 : }
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|