Update ooo320-m1
[ooovba.git] / ucb / source / ucp / package / pkguri.cxx
bloba246d9cbd5eca43d08d90bdd62346950febe027a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pkguri.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include "rtl/ustrbuf.hxx"
41 #include "osl/diagnose.h"
43 #include "../inc/urihelper.hxx"
45 #include "pkguri.hxx"
47 using namespace package_ucp;
48 using namespace rtl;
50 //=========================================================================
51 //=========================================================================
53 // PackageUri Implementation.
55 //=========================================================================
56 //=========================================================================
58 static void normalize( OUString& rURL )
60 sal_Int32 nPos = 0;
63 nPos = rURL.indexOf( '%', nPos );
64 if ( nPos != -1 )
66 if ( nPos < ( rURL.getLength() - 2 ) )
68 OUString aTmp = rURL.copy( nPos + 1, 2 );
69 rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
70 nPos++;
74 while ( nPos != -1 );
77 //=========================================================================
78 void PackageUri::init() const
80 // Already inited?
81 if ( m_aUri.getLength() && !m_aPath.getLength() )
83 // Note: Maybe it's a re-init, setUri only resets m_aPath!
84 m_aPackage = m_aParentUri = m_aName = m_aParam = m_aScheme
85 = OUString();
87 // URI must match at least: <sheme>://<non_empty_url_to_file>
88 if ( ( m_aUri.getLength() < PACKAGE_URL_SCHEME_LENGTH + 4 ) )
90 // error, but remember that we did a init().
91 m_aPath = rtl::OUString::createFromAscii( "/" );
92 return;
95 // Scheme must be followed by '://'
96 if ( ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH ]
97 != sal_Unicode( ':' ) )
99 ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH + 1 ]
100 != sal_Unicode( '/' ) )
102 ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH + 2 ]
103 != sal_Unicode( '/' ) ) )
105 // error, but remember that we did a init().
106 m_aPath = rtl::OUString::createFromAscii( "/" );
107 return;
110 rtl::OUString aPureUri;
111 sal_Int32 nParam = m_aUri.indexOf( '?' );
112 if( nParam >= 0 )
114 m_aParam = m_aUri.copy( nParam );
115 aPureUri = m_aUri.copy( 0, nParam );
117 else
118 aPureUri = m_aUri;
120 // Scheme is case insensitive.
121 m_aScheme = aPureUri.copy(
122 0, PACKAGE_URL_SCHEME_LENGTH ).toAsciiLowerCase();
124 if ( m_aScheme.equalsAsciiL(
125 RTL_CONSTASCII_STRINGPARAM( PACKAGE_URL_SCHEME ) )
126 || m_aScheme.equalsAsciiL(
127 RTL_CONSTASCII_STRINGPARAM( PACKAGE_ZIP_URL_SCHEME ) ) )
129 if ( m_aScheme.equalsAsciiL(
130 RTL_CONSTASCII_STRINGPARAM( PACKAGE_ZIP_URL_SCHEME ) ) )
132 m_aParam +=
133 ( m_aParam.getLength()
134 ? ::rtl::OUString::createFromAscii( "&purezip" )
135 : ::rtl::OUString::createFromAscii( "?purezip" ) );
138 aPureUri = aPureUri.replaceAt( 0,
139 m_aScheme.getLength(),
140 m_aScheme );
142 sal_Int32 nStart = PACKAGE_URL_SCHEME_LENGTH + 3;
143 sal_Int32 nEnd = aPureUri.lastIndexOf( '/' );
144 if ( nEnd == PACKAGE_URL_SCHEME_LENGTH + 3 )
146 // Only <scheme>:/// - Empty authority
148 // error, but remember that we did a init().
149 m_aPath = rtl::OUString::createFromAscii( "/" );
150 return;
152 else if ( nEnd == ( aPureUri.getLength() - 1 ) )
154 if ( aPureUri.getStr()[ aPureUri.getLength() - 2 ]
155 == sal_Unicode( '/' ) )
157 // Only <scheme>://// or <scheme>://<something>//
159 // error, but remember that we did a init().
160 m_aPath = rtl::OUString::createFromAscii( "/" );
161 return;
164 // Remove trailing slash.
165 aPureUri = aPureUri.copy( 0, nEnd );
169 nEnd = aPureUri.indexOf( '/', nStart );
170 if ( nEnd == -1 )
172 // root folder.
174 OUString aNormPackage = aPureUri.copy( nStart );
175 normalize( aNormPackage );
177 aPureUri = aPureUri.replaceAt(
178 nStart, aPureUri.getLength() - nStart, aNormPackage );
179 m_aPackage
180 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
181 m_aPath = rtl::OUString::createFromAscii( "/" );
182 m_aUri = m_aUri.replaceAt( 0,
183 ( nParam >= 0 )
184 ? nParam
185 : m_aUri.getLength(), aPureUri );
187 sal_Int32 nLastSlash = m_aPackage.lastIndexOf( '/' );
188 if ( nLastSlash != -1 )
189 m_aName = ::ucb_impl::urihelper::decodeSegment(
190 m_aPackage.copy( nLastSlash + 1 ) );
191 else
192 m_aName
193 = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
195 else
197 m_aPath = aPureUri.copy( nEnd + 1 );
199 // Empty path segments or encoded slashes?
200 if ( m_aPath.indexOf(
201 rtl::OUString::createFromAscii( "//" ) ) != -1
202 || m_aPath.indexOf(
203 rtl::OUString::createFromAscii( "%2F" ) ) != -1
204 || m_aPath.indexOf(
205 rtl::OUString::createFromAscii( "%2f" ) ) != -1 )
207 // error, but remember that we did a init().
208 m_aPath = rtl::OUString::createFromAscii( "/" );
209 return;
212 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
213 normalize( aNormPackage );
215 aPureUri = aPureUri.replaceAt(
216 nStart, nEnd - nStart, aNormPackage );
217 aPureUri = aPureUri.replaceAt(
218 nEnd + 1,
219 aPureUri.getLength() - nEnd - 1,
220 ::ucb_impl::urihelper::encodeURI( m_aPath ) );
222 m_aPackage
223 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
224 m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
225 m_aUri = m_aUri.replaceAt( 0,
226 ( nParam >= 0 )
227 ? nParam
228 : m_aUri.getLength(), aPureUri );
230 sal_Int32 nLastSlash = aPureUri.lastIndexOf( '/' );
231 if ( nLastSlash != -1 )
233 m_aParentUri = aPureUri.copy( 0, nLastSlash );
234 m_aName = ::ucb_impl::urihelper::decodeSegment(
235 aPureUri.copy( nLastSlash + 1 ) );
239 // success
240 m_bValid = true;
242 else
244 // error, but remember that we did a init().
245 m_aPath = rtl::OUString::createFromAscii( "/" );