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 <xmloff/xmltoken.hxx>
31 :
32 : using namespace com::sun::star;
33 : using namespace xmloff::token;
34 :
35 1 : ScXMLSortContext::ScXMLSortContext( ScXMLImport& rImport,
36 : sal_uInt16 nPrfx,
37 : const OUString& rLName,
38 : const ::com::sun::star::uno::Reference<
39 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
40 : ScXMLDatabaseRangeContext* pTempDatabaseRangeContext) :
41 : SvXMLImportContext( rImport, nPrfx, rLName ),
42 : pDatabaseRangeContext(pTempDatabaseRangeContext),
43 : sAlgorithm(),
44 : nUserListIndex(0),
45 : bCopyOutputData(false),
46 : bBindFormatsToContent(true),
47 : bIsCaseSensitive(false),
48 1 : bEnabledUserList(false)
49 : {
50 1 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
51 1 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetSortAttrTokenMap());
52 1 : for( sal_Int16 i=0; i < nAttrCount; ++i )
53 : {
54 0 : const OUString& sAttrName(xAttrList->getNameByIndex( i ));
55 0 : OUString aLocalName;
56 0 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
57 0 : sAttrName, &aLocalName ));
58 0 : const OUString& sValue(xAttrList->getValueByIndex( i ));
59 :
60 0 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
61 : {
62 : case XML_TOK_SORT_ATTR_BIND_STYLES_TO_CONTENT :
63 : {
64 0 : bBindFormatsToContent = IsXMLToken(sValue, XML_TRUE);
65 : }
66 0 : break;
67 : case XML_TOK_SORT_ATTR_TARGET_RANGE_ADDRESS :
68 : {
69 0 : ScRange aScRange;
70 0 : sal_Int32 nOffset(0);
71 0 : if (ScRangeStringConverter::GetRangeFromString( aScRange, sValue, GetScImport().GetDocument(), ::formula::FormulaGrammar::CONV_OOO, nOffset ))
72 : {
73 0 : ScUnoConversion::FillApiAddress( aOutputPosition, aScRange.aStart );
74 0 : bCopyOutputData = true;
75 : }
76 : }
77 0 : break;
78 : case XML_TOK_SORT_ATTR_CASE_SENSITIVE :
79 : {
80 0 : bIsCaseSensitive = IsXMLToken(sValue, XML_TRUE);
81 : }
82 0 : break;
83 : case XML_TOK_SORT_ATTR_RFC_LANGUAGE_TAG :
84 0 : maLanguageTagODF.maRfcLanguageTag = sValue;
85 0 : break;
86 : case XML_TOK_SORT_ATTR_LANGUAGE :
87 0 : maLanguageTagODF.maLanguage = sValue;
88 0 : break;
89 : case XML_TOK_SORT_ATTR_SCRIPT :
90 0 : maLanguageTagODF.maScript = sValue;
91 0 : break;
92 : case XML_TOK_SORT_ATTR_COUNTRY :
93 0 : maLanguageTagODF.maCountry = sValue;
94 0 : break;
95 : case XML_TOK_SORT_ATTR_ALGORITHM :
96 0 : sAlgorithm = sValue;
97 0 : break;
98 : }
99 0 : }
100 1 : }
101 :
102 2 : ScXMLSortContext::~ScXMLSortContext()
103 : {
104 2 : }
105 :
106 1 : SvXMLImportContext *ScXMLSortContext::CreateChildContext( sal_uInt16 nPrefix,
107 : const OUString& rLName,
108 : const ::com::sun::star::uno::Reference<
109 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
110 : {
111 1 : SvXMLImportContext *pContext(0);
112 :
113 1 : const SvXMLTokenMap& rTokenMap(GetScImport().GetSortElemTokenMap());
114 1 : switch( rTokenMap.Get( nPrefix, rLName ) )
115 : {
116 : case XML_TOK_SORT_SORT_BY :
117 : {
118 : pContext = new ScXMLSortByContext( GetScImport(), nPrefix,
119 1 : rLName, xAttrList, this);
120 : }
121 1 : break;
122 : }
123 :
124 1 : if( !pContext )
125 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
126 :
127 1 : return pContext;
128 : }
129 :
130 1 : void ScXMLSortContext::EndElement()
131 : {
132 1 : sal_Int32 nAlgoLength(sAlgorithm.getLength());
133 1 : sal_uInt8 i (0);
134 1 : if (!maLanguageTagODF.isEmpty())
135 0 : ++i;
136 1 : if (nAlgoLength)
137 0 : ++i;
138 1 : uno::Sequence <beans::PropertyValue> aSortDescriptor(7 + i);
139 1 : aSortDescriptor[0].Name = SC_UNONAME_BINDFMT;
140 1 : aSortDescriptor[0].Value = css::uno::makeAny(bBindFormatsToContent);
141 1 : aSortDescriptor[1].Name = SC_UNONAME_COPYOUT;
142 1 : aSortDescriptor[1].Value = css::uno::makeAny(bCopyOutputData);
143 1 : aSortDescriptor[2].Name = SC_UNONAME_ISCASE;
144 1 : aSortDescriptor[2].Value = css::uno::makeAny(bIsCaseSensitive);
145 1 : aSortDescriptor[3].Name = SC_UNONAME_ISULIST;
146 1 : aSortDescriptor[3].Value = css::uno::makeAny(bEnabledUserList);
147 1 : aSortDescriptor[4].Name = SC_UNONAME_OUTPOS;
148 1 : aSortDescriptor[4].Value <<= aOutputPosition;
149 1 : aSortDescriptor[5].Name = SC_UNONAME_UINDEX;
150 1 : aSortDescriptor[5].Value <<= nUserListIndex;
151 1 : aSortDescriptor[6].Name = SC_UNONAME_SORTFLD;
152 1 : aSortDescriptor[6].Value <<= aSortFields;
153 1 : if (!maLanguageTagODF.isEmpty())
154 : {
155 0 : aSortDescriptor[7].Name = SC_UNONAME_COLLLOC;
156 0 : aSortDescriptor[7].Value <<= maLanguageTagODF.getLanguageTag().getLocale( false);
157 : }
158 1 : if (nAlgoLength)
159 : {
160 0 : aSortDescriptor[6 + i].Name = SC_UNONAME_COLLALG;
161 0 : aSortDescriptor[6 + i].Value <<= sAlgorithm;
162 : }
163 1 : pDatabaseRangeContext->SetSortSequence(aSortDescriptor);
164 1 : }
165 :
166 1 : void ScXMLSortContext::AddSortField(const OUString& sFieldNumber, const OUString& sDataType, const OUString& sOrder)
167 : {
168 1 : util::SortField aSortField;
169 1 : aSortField.Field = sFieldNumber.toInt32();
170 1 : if (IsXMLToken(sOrder, XML_ASCENDING))
171 1 : aSortField.SortAscending = true;
172 : else
173 0 : aSortField.SortAscending = false;
174 1 : if (sDataType.getLength() > 8)
175 : {
176 1 : OUString sTemp = sDataType.copy(0, 8);
177 1 : if (sTemp == "UserList")
178 : {
179 0 : bEnabledUserList = true;
180 0 : sTemp = sDataType.copy(8);
181 0 : nUserListIndex = static_cast<sal_Int16>(sTemp.toInt32());
182 : }
183 : else
184 : {
185 1 : if (IsXMLToken(sDataType, XML_AUTOMATIC))
186 1 : aSortField.FieldType = util::SortFieldType_AUTOMATIC;
187 1 : }
188 : }
189 : else
190 : {
191 0 : if (IsXMLToken(sDataType, XML_TEXT))
192 0 : aSortField.FieldType = util::SortFieldType_ALPHANUMERIC;
193 0 : else if (IsXMLToken(sDataType, XML_NUMBER))
194 0 : aSortField.FieldType = util::SortFieldType_NUMERIC;
195 : }
196 1 : aSortFields.realloc(aSortFields.getLength() + 1);
197 1 : aSortFields[aSortFields.getLength() - 1] = aSortField;
198 1 : }
199 :
200 1 : ScXMLSortByContext::ScXMLSortByContext( ScXMLImport& rImport,
201 : sal_uInt16 nPrfx,
202 : const OUString& rLName,
203 : const ::com::sun::star::uno::Reference<
204 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
205 : ScXMLSortContext* pTempSortContext) :
206 : SvXMLImportContext( rImport, nPrfx, rLName ),
207 : pSortContext(pTempSortContext),
208 1 : sDataType(GetXMLToken(XML_AUTOMATIC)),
209 2 : sOrder(GetXMLToken(XML_ASCENDING))
210 : {
211 1 : sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
212 1 : const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetSortSortByAttrTokenMap());
213 3 : for( sal_Int16 i=0; i < nAttrCount; ++i )
214 : {
215 2 : const OUString& sAttrName(xAttrList->getNameByIndex( i ));
216 4 : OUString aLocalName;
217 2 : sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
218 2 : sAttrName, &aLocalName ));
219 4 : const OUString& sValue(xAttrList->getValueByIndex( i ));
220 :
221 2 : switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
222 : {
223 : case XML_TOK_SORT_BY_ATTR_FIELD_NUMBER :
224 : {
225 1 : sFieldNumber = sValue;
226 : }
227 1 : break;
228 : case XML_TOK_SORT_BY_ATTR_DATA_TYPE :
229 : {
230 1 : sDataType = sValue;
231 : }
232 1 : break;
233 : case XML_TOK_SORT_BY_ATTR_ORDER :
234 : {
235 0 : sOrder = sValue;
236 : }
237 0 : break;
238 : }
239 2 : }
240 1 : }
241 :
242 2 : ScXMLSortByContext::~ScXMLSortByContext()
243 : {
244 2 : }
245 :
246 0 : SvXMLImportContext *ScXMLSortByContext::CreateChildContext( sal_uInt16 nPrefix,
247 : const OUString& rLName,
248 : const ::com::sun::star::uno::Reference<
249 : ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
250 : {
251 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLName );
252 : }
253 :
254 1 : void ScXMLSortByContext::EndElement()
255 : {
256 1 : pSortContext->AddSortField(sFieldNumber, sDataType, sOrder);
257 157 : }
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|