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