1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // prevent internal compiler error with MSVC6SP3
32*cdf0e10cSrcweir #include <utility>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <chaptercollator.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/i18n/KCharacterType.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/i18n/ParseResult.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
39*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40*cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
41*cdf0e10cSrcweir using namespace ::rtl;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir ChapterCollator::ChapterCollator( const Reference < XMultiServiceFactory >& rxMSF ) : CollatorImpl(rxMSF)
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     if ( rxMSF.is()) {
46*cdf0e10cSrcweir         Reference < XInterface > xI =
47*cdf0e10cSrcweir 		rxMSF->createInstance( OUString::createFromAscii("com.sun.star.i18n.CharacterClassification"));
48*cdf0e10cSrcweir         if ( xI.is() )
49*cdf0e10cSrcweir             xI->queryInterface(::getCppuType((const Reference< XCharacterClassification>*)0)) >>= cclass;
50*cdf0e10cSrcweir     }
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir ChapterCollator::~ChapterCollator()
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir sal_Int32 SAL_CALL
58*cdf0e10cSrcweir ChapterCollator::compareString( const OUString& s1, const OUString& s2) throw(RuntimeException)
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     return compareSubstring(s1, 0, s1.getLength(),  s2, 0, s2.getLength());
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir #define DIGIT KCharacterType::DIGIT
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir sal_Int32 SAL_CALL
66*cdf0e10cSrcweir ChapterCollator::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
67*cdf0e10cSrcweir 	const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	if( len1 <= 1 || len2 <= 1 || ! cclass.is() )
70*cdf0e10cSrcweir 	    return CollatorImpl::compareSubstring( str1, off1,  len1, str2, off2, len2 );
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	sal_Int32 i1, i2;
73*cdf0e10cSrcweir 	for (i1 = len1; i1 && (cclass->getCharacterType(str1, off1+i1-1, nLocale) & DIGIT); i1--) ;
74*cdf0e10cSrcweir 	for (i2 = len2; i2 && (cclass->getCharacterType(str2, off2+i2-1, nLocale) & DIGIT); i2--) ;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	sal_Int32 ans = CollatorImpl::compareSubstring(str1, off1, i1, str2, off2, i2);
77*cdf0e10cSrcweir 	if( ans != 0 )
78*cdf0e10cSrcweir 	    return ans;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	const OUString &aAddAllowed = OUString::createFromAscii("?");
81*cdf0e10cSrcweir 	ParseResult res1, res2;
82*cdf0e10cSrcweir 	// Bug #100323#, since parseAnyToken does not take length as parameter, we have to copy
83*cdf0e10cSrcweir 	// it to a temp. string.
84*cdf0e10cSrcweir 	OUString s1 = str1.copy(off1+i1, len1-i1), s2 = str2.copy(off2+i2, len2-i2);
85*cdf0e10cSrcweir 	res1 = cclass->parseAnyToken( s1, 0, nLocale, DIGIT, aAddAllowed, DIGIT, aAddAllowed );
86*cdf0e10cSrcweir 	res2 = cclass->parseAnyToken( s2, 0, nLocale, DIGIT, aAddAllowed, DIGIT, aAddAllowed );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	return res1.Value == res2.Value ? 0 : res1.Value > res2.Value ? 1 : -1;
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir const sal_Char *cChapCollator = "com.sun.star.i18n.ChapterCollator";
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir OUString SAL_CALL
94*cdf0e10cSrcweir ChapterCollator::getImplementationName() throw( RuntimeException )
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	return OUString::createFromAscii(cChapCollator);
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir sal_Bool SAL_CALL
100*cdf0e10cSrcweir ChapterCollator::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     return !rServiceName.compareToAscii(cChapCollator);
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir Sequence< OUString > SAL_CALL
106*cdf0e10cSrcweir ChapterCollator::getSupportedServiceNames() throw( RuntimeException )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     Sequence< OUString > aRet(1);
109*cdf0e10cSrcweir     aRet[0] = OUString::createFromAscii(cChapCollator);
110*cdf0e10cSrcweir     return aRet;
111*cdf0e10cSrcweir }
112