1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pkguri.cxx,v $
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 /**************************************************************************
36 **************************************************************************
38 *************************************************************************/
40 #include "rtl/ustrbuf.hxx"
41 #include "osl/diagnose.h"
43 #include "../inc/urihelper.hxx"
47 using namespace package_ucp
;
50 //=========================================================================
51 //=========================================================================
53 // PackageUri Implementation.
55 //=========================================================================
56 //=========================================================================
58 static void normalize( OUString
& rURL
)
63 nPos
= rURL
.indexOf( '%', nPos
);
66 if ( nPos
< ( rURL
.getLength() - 2 ) )
68 OUString aTmp
= rURL
.copy( nPos
+ 1, 2 );
69 rURL
= rURL
.replaceAt( nPos
+ 1, 2, aTmp
.toAsciiUpperCase() );
77 //=========================================================================
78 void PackageUri::init() const
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
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( "/" );
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( "/" );
110 rtl::OUString aPureUri
;
111 sal_Int32 nParam
= m_aUri
.indexOf( '?' );
114 m_aParam
= m_aUri
.copy( nParam
);
115 aPureUri
= m_aUri
.copy( 0, nParam
);
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
) ) )
133 ( m_aParam
.getLength()
134 ? ::rtl::OUString::createFromAscii( "&purezip" )
135 : ::rtl::OUString::createFromAscii( "?purezip" ) );
138 aPureUri
= aPureUri
.replaceAt( 0,
139 m_aScheme
.getLength(),
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( "/" );
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( "/" );
164 // Remove trailing slash.
165 aPureUri
= aPureUri
.copy( 0, nEnd
);
169 nEnd
= aPureUri
.indexOf( '/', nStart
);
174 OUString aNormPackage
= aPureUri
.copy( nStart
);
175 normalize( aNormPackage
);
177 aPureUri
= aPureUri
.replaceAt(
178 nStart
, aPureUri
.getLength() - nStart
, aNormPackage
);
180 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
181 m_aPath
= rtl::OUString::createFromAscii( "/" );
182 m_aUri
= m_aUri
.replaceAt( 0,
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 ) );
193 = ::ucb_impl::urihelper::decodeSegment( m_aPackage
);
197 m_aPath
= aPureUri
.copy( nEnd
+ 1 );
199 // Empty path segments or encoded slashes?
200 if ( m_aPath
.indexOf(
201 rtl::OUString::createFromAscii( "//" ) ) != -1
203 rtl::OUString::createFromAscii( "%2F" ) ) != -1
205 rtl::OUString::createFromAscii( "%2f" ) ) != -1 )
207 // error, but remember that we did a init().
208 m_aPath
= rtl::OUString::createFromAscii( "/" );
212 OUString aNormPackage
= aPureUri
.copy( nStart
, nEnd
- nStart
);
213 normalize( aNormPackage
);
215 aPureUri
= aPureUri
.replaceAt(
216 nStart
, nEnd
- nStart
, aNormPackage
);
217 aPureUri
= aPureUri
.replaceAt(
219 aPureUri
.getLength() - nEnd
- 1,
220 ::ucb_impl::urihelper::encodeURI( m_aPath
) );
223 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
224 m_aPath
= ::ucb_impl::urihelper::decodeSegment( m_aPath
);
225 m_aUri
= m_aUri
.replaceAt( 0,
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 ) );
244 // error, but remember that we did a init().
245 m_aPath
= rtl::OUString::createFromAscii( "/" );