LCOV - code coverage report
Current view: top level - ucb/source/ucp/hierarchy - hierarchyuri.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 37 58 63.8 %
Date: 2015-06-13 12:38:46 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11