1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // prevent internal compiler error with MSVC6SP3
28cdf0e10cSrcweir #include <utility>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <chaptercollator.hxx>
31cdf0e10cSrcweir #include <com/sun/star/i18n/KCharacterType.hpp>
32cdf0e10cSrcweir #include <com/sun/star/i18n/ParseResult.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star::lang;
35cdf0e10cSrcweir using namespace ::com::sun::star::uno;
36cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
37cdf0e10cSrcweir using namespace ::rtl;
38cdf0e10cSrcweir 
ChapterCollator(const Reference<XMultiServiceFactory> & rxMSF)39cdf0e10cSrcweir ChapterCollator::ChapterCollator( const Reference < XMultiServiceFactory >& rxMSF ) : CollatorImpl(rxMSF)
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     if ( rxMSF.is()) {
42cdf0e10cSrcweir         Reference < XInterface > xI =
43cdf0e10cSrcweir 		rxMSF->createInstance( OUString::createFromAscii("com.sun.star.i18n.CharacterClassification"));
44cdf0e10cSrcweir         if ( xI.is() )
45cdf0e10cSrcweir             xI->queryInterface(::getCppuType((const Reference< XCharacterClassification>*)0)) >>= cclass;
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~ChapterCollator()49cdf0e10cSrcweir ChapterCollator::~ChapterCollator()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir sal_Int32 SAL_CALL
compareString(const OUString & s1,const OUString & s2)54cdf0e10cSrcweir ChapterCollator::compareString( const OUString& s1, const OUString& s2) throw(RuntimeException)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     return compareSubstring(s1, 0, s1.getLength(),  s2, 0, s2.getLength());
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #define DIGIT KCharacterType::DIGIT
60cdf0e10cSrcweir 
61cdf0e10cSrcweir sal_Int32 SAL_CALL
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)62cdf0e10cSrcweir ChapterCollator::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
63cdf0e10cSrcweir 	const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	if( len1 <= 1 || len2 <= 1 || ! cclass.is() )
66cdf0e10cSrcweir 	    return CollatorImpl::compareSubstring( str1, off1,  len1, str2, off2, len2 );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	sal_Int32 i1, i2;
69cdf0e10cSrcweir 	for (i1 = len1; i1 && (cclass->getCharacterType(str1, off1+i1-1, nLocale) & DIGIT); i1--) ;
70cdf0e10cSrcweir 	for (i2 = len2; i2 && (cclass->getCharacterType(str2, off2+i2-1, nLocale) & DIGIT); i2--) ;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	sal_Int32 ans = CollatorImpl::compareSubstring(str1, off1, i1, str2, off2, i2);
73cdf0e10cSrcweir 	if( ans != 0 )
74cdf0e10cSrcweir 	    return ans;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	const OUString &aAddAllowed = OUString::createFromAscii("?");
77cdf0e10cSrcweir 	ParseResult res1, res2;
78cdf0e10cSrcweir 	// Bug #100323#, since parseAnyToken does not take length as parameter, we have to copy
79cdf0e10cSrcweir 	// it to a temp. string.
80cdf0e10cSrcweir 	OUString s1 = str1.copy(off1+i1, len1-i1), s2 = str2.copy(off2+i2, len2-i2);
81cdf0e10cSrcweir 	res1 = cclass->parseAnyToken( s1, 0, nLocale, DIGIT, aAddAllowed, DIGIT, aAddAllowed );
82cdf0e10cSrcweir 	res2 = cclass->parseAnyToken( s2, 0, nLocale, DIGIT, aAddAllowed, DIGIT, aAddAllowed );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	return res1.Value == res2.Value ? 0 : res1.Value > res2.Value ? 1 : -1;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir const sal_Char *cChapCollator = "com.sun.star.i18n.ChapterCollator";
88cdf0e10cSrcweir 
89cdf0e10cSrcweir OUString SAL_CALL
getImplementationName()90cdf0e10cSrcweir ChapterCollator::getImplementationName() throw( RuntimeException )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	return OUString::createFromAscii(cChapCollator);
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const rtl::OUString & rServiceName)96cdf0e10cSrcweir ChapterCollator::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     return !rServiceName.compareToAscii(cChapCollator);
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getSupportedServiceNames()102cdf0e10cSrcweir ChapterCollator::getSupportedServiceNames() throw( RuntimeException )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     Sequence< OUString > aRet(1);
105cdf0e10cSrcweir     aRet[0] = OUString::createFromAscii(cChapCollator);
106cdf0e10cSrcweir     return aRet;
107cdf0e10cSrcweir }
108