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