Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "xmlsorti.hxx"
21 : #include "xmlimprt.hxx"
22 : #include "docuno.hxx"
23 : #include "convuno.hxx"
24 : #include "XMLConverter.hxx"
25 : #include "unonames.hxx"
26 : #include "rangeutl.hxx"
27 :
28 : #include <xmloff/xmltkmap.hxx>
29 : #include <xmloff/nmspmap.hxx>
30 : #include <comphelper/extract.hxx>
31 : #include <xmloff/xmltoken.hxx>
32 :
33 : #define SC_USERLIST "UserList"
34 :
35 : using namespace com::sun::star;
36 : using namespace xmloff::token;
37 :
38 : //------------------------------------------------------------------
39 :
40 0 : ScXMLSortContext::ScXMLSortContext( ScXMLImport& rImport,
41 : sal_uInt16 nPrfx,
42 : const ::rtl::OUString& rLName,
43 : const ::com::sun::star::uno::Reference<
44 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
45 : ScXMLDatabaseRangeContext* pTempDatabaseRangeContext) :
46 : SvXMLImportContext( rImport, nPrfx, rLName ),
47 : pDatabaseRangeContext(pTempDatabaseRangeContext),
48 : sCountry(),
49 : sLanguage(),
50 : sAlgorithm(),
51 : nUserListIndex(0),
52 : bCopyOutputData(false),
53 : bBindFormatsToContent(true),
54 : bIsCaseSensitive(false),
55 0 : bEnabledUserList(false)
56 : {
57 0 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
58 0 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetSortAttrTokenMap());
59 0 : for( sal_Int16 i=0; i < nAttrCount; ++i )
60 : {
61 0 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
62 0 : rtl::OUString aLocalName;
63 0 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
64 0 : sAttrName, &aLocalName ));
65 0 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
66 :
67 0 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
68 : {
69 : case XML_TOK_SORT_ATTR_BIND_STYLES_TO_CONTENT :
70 : {
71 0 : bBindFormatsToContent = IsXMLToken(sValue, XML_TRUE);
72 : }
73 0 : break;
74 : case XML_TOK_SORT_ATTR_TARGET_RANGE_ADDRESS :
75 : {
76 0 : ScRange aScRange;
77 0 : sal_Int32 nOffset(0);
78 0 : if (ScRangeStringConverter::GetRangeFromString( aScRange, sValue, GetScImport().GetDocument(), ::formula::FormulaGrammar::CONV_OOO, nOffset ))
79 : {
80 0 : ScUnoConversion::FillApiAddress( aOutputPosition, aScRange.aStart );
81 0 : bCopyOutputData = true;
82 : }
83 : }
84 0 : break;
85 : case XML_TOK_SORT_ATTR_CASE_SENSITIVE :
86 : {
87 0 : bIsCaseSensitive = IsXMLToken(sValue, XML_TRUE);
88 : }
89 0 : break;
90 : case XML_TOK_SORT_ATTR_LANGUAGE :
91 0 : sLanguage = sValue;
92 0 : break;
93 : case XML_TOK_SORT_ATTR_COUNTRY :
94 0 : sCountry = sValue;
95 0 : break;
96 : case XML_TOK_SORT_ATTR_ALGORITHM :
97 0 : sAlgorithm = sValue;
98 0 : break;
99 : }
100 0 : }
101 0 : }
102 :
103 0 : ScXMLSortContext::~ScXMLSortContext()
104 : {
105 0 : }
106 :
107 0 : SvXMLImportContext *ScXMLSortContext::CreateChildContext( sal_uInt16 nPrefix,
108 : const ::rtl::OUString& rLName,
109 : const ::com::sun::star::uno::Reference<
110 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
111 : {
112 0 : SvXMLImportContext *pContext(0);
113 :
114 0 : const SvXMLTokenMap& rTokenMap(GetScImport().GetSortElemTokenMap());
115 0 : switch( rTokenMap.Get( nPrefix, rLName ) )
116 : {
117 : case XML_TOK_SORT_SORT_BY :
118 : {
119 : pContext = new ScXMLSortByContext( GetScImport(), nPrefix,
120 0 : rLName, xAttrList, this);
121 : }
122 0 : break;
123 : }
124 :
125 0 : if( !pContext )
126 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
127 :
128 0 : return pContext;
129 : }
130 :
131 0 : void ScXMLSortContext::EndElement()
132 : {
133 0 : sal_Int32 nLangLength(sLanguage.getLength());
134 0 : sal_Int32 nCountryLength(sCountry.getLength());
135 0 : sal_Int32 nAlgoLength(sAlgorithm.getLength());
136 0 : sal_uInt8 i (0);
137 0 : if (nLangLength || nCountryLength)
138 0 : ++i;
139 0 : if (nAlgoLength)
140 0 : ++i;
141 0 : uno::Sequence <beans::PropertyValue> aSortDescriptor(7 + i);
142 0 : aSortDescriptor[0].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_BINDFMT));
143 0 : aSortDescriptor[0].Value = ::cppu::bool2any(bBindFormatsToContent);
144 0 : aSortDescriptor[1].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_COPYOUT));
145 0 : aSortDescriptor[1].Value = ::cppu::bool2any(bCopyOutputData);
146 0 : aSortDescriptor[2].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_ISCASE));
147 0 : aSortDescriptor[2].Value = ::cppu::bool2any(bIsCaseSensitive);
148 0 : aSortDescriptor[3].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_ISULIST));
149 0 : aSortDescriptor[3].Value = ::cppu::bool2any(bEnabledUserList);
150 0 : aSortDescriptor[4].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_OUTPOS));
151 0 : aSortDescriptor[4].Value <<= aOutputPosition;
152 0 : aSortDescriptor[5].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_UINDEX));
153 0 : aSortDescriptor[5].Value <<= nUserListIndex;
154 0 : aSortDescriptor[6].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_SORTFLD));
155 0 : aSortDescriptor[6].Value <<= aSortFields;
156 0 : if (nLangLength || nCountryLength)
157 : {
158 0 : lang::Locale aLocale;
159 0 : aLocale.Language = sLanguage;
160 0 : aLocale.Country = sCountry;
161 0 : aSortDescriptor[7].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_COLLLOC));
162 0 : aSortDescriptor[7].Value <<= aLocale;
163 : }
164 0 : if (nAlgoLength)
165 : {
166 0 : aSortDescriptor[6 + i].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_COLLALG));
167 0 : aSortDescriptor[6 + i].Value <<= sAlgorithm;
168 : }
169 0 : pDatabaseRangeContext->SetSortSequence(aSortDescriptor);
170 0 : }
171 :
172 0 : void ScXMLSortContext::AddSortField(const rtl::OUString& sFieldNumber, const rtl::OUString& sDataType, const rtl::OUString& sOrder)
173 : {
174 0 : util::SortField aSortField;
175 0 : aSortField.Field = sFieldNumber.toInt32();
176 0 : if (IsXMLToken(sOrder, XML_ASCENDING))
177 0 : aSortField.SortAscending = true;
178 : else
179 0 : aSortField.SortAscending = false;
180 0 : if (sDataType.getLength() > 8)
181 : {
182 0 : rtl::OUString sTemp = sDataType.copy(0, 8);
183 0 : if (sTemp.compareToAscii(SC_USERLIST) == 0)
184 : {
185 0 : bEnabledUserList = true;
186 0 : sTemp = sDataType.copy(8);
187 0 : nUserListIndex = static_cast<sal_Int16>(sTemp.toInt32());
188 : }
189 : else
190 : {
191 0 : if (IsXMLToken(sDataType, XML_AUTOMATIC))
192 0 : aSortField.FieldType = util::SortFieldType_AUTOMATIC;
193 0 : }
194 : }
195 : else
196 : {
197 0 : if (IsXMLToken(sDataType, XML_TEXT))
198 0 : aSortField.FieldType = util::SortFieldType_ALPHANUMERIC;
199 0 : else if (IsXMLToken(sDataType, XML_NUMBER))
200 0 : aSortField.FieldType = util::SortFieldType_NUMERIC;
201 : }
202 0 : aSortFields.realloc(aSortFields.getLength() + 1);
203 0 : aSortFields[aSortFields.getLength() - 1] = aSortField;
204 0 : }
205 :
206 0 : ScXMLSortByContext::ScXMLSortByContext( ScXMLImport& rImport,
207 : sal_uInt16 nPrfx,
208 : const ::rtl::OUString& rLName,
209 : const ::com::sun::star::uno::Reference<
210 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
211 : ScXMLSortContext* pTempSortContext) :
212 : SvXMLImportContext( rImport, nPrfx, rLName ),
213 : pSortContext(pTempSortContext),
214 0 : sDataType(GetXMLToken(XML_AUTOMATIC)),
215 0 : sOrder(GetXMLToken(XML_ASCENDING))
216 : {
217 0 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
218 0 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetSortSortByAttrTokenMap());
219 0 : for( sal_Int16 i=0; i < nAttrCount; ++i )
220 : {
221 0 : const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
222 0 : rtl::OUString aLocalName;
223 0 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
224 0 : sAttrName, &aLocalName ));
225 0 : const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
226 :
227 0 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
228 : {
229 : case XML_TOK_SORT_BY_ATTR_FIELD_NUMBER :
230 : {
231 0 : sFieldNumber = sValue;
232 : }
233 0 : break;
234 : case XML_TOK_SORT_BY_ATTR_DATA_TYPE :
235 : {
236 0 : sDataType = sValue;
237 : }
238 0 : break;
239 : case XML_TOK_SORT_BY_ATTR_ORDER :
240 : {
241 0 : sOrder = sValue;
242 : }
243 0 : break;
244 : }
245 0 : }
246 0 : }
247 :
248 0 : ScXMLSortByContext::~ScXMLSortByContext()
249 : {
250 0 : }
251 :
252 0 : SvXMLImportContext *ScXMLSortByContext::CreateChildContext( sal_uInt16 nPrefix,
253 : const ::rtl::OUString& rLName,
254 : const ::com::sun::star::uno::Reference<
255 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
256 : {
257 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLName );
258 : }
259 :
260 0 : void ScXMLSortByContext::EndElement()
261 : {
262 0 : pSortContext->AddSortField(sFieldNumber, sDataType, sOrder);
263 0 : }
264 :
265 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|