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