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