xref: /aoo41x/main/sc/source/filter/xml/xmlcoli.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include "xmlcoli.hxx"
32 #include "xmlimprt.hxx"
33 #include "global.hxx"
34 #include "xmlstyli.hxx"
35 #include "document.hxx"
36 #include "docuno.hxx"
37 #include "olinetab.hxx"
38 #include "sheetdata.hxx"
39 #include "unonames.hxx"
40 
41 #include <xmloff/xmltkmap.hxx>
42 #include <xmloff/nmspmap.hxx>
43 #include <xmloff/xmlnmspe.hxx>
44 #include <xmloff/families.hxx>
45 #include <xmloff/xmltoken.hxx>
46 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
47 #include <com/sun/star/sheet/XSpreadsheet.hpp>
48 #include <com/sun/star/table/XColumnRowRange.hpp>
49 #include <com/sun/star/sheet/XPrintAreas.hpp>
50 
51 using namespace com::sun::star;
52 using namespace xmloff::token;
53 
54 //------------------------------------------------------------------
55 
ScXMLTableColContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)56 ScXMLTableColContext::ScXMLTableColContext( ScXMLImport& rImport,
57 									  sal_uInt16 nPrfx,
58 									  const ::rtl::OUString& rLName,
59 									  const ::com::sun::star::uno::Reference<
60 									  ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) :
61 	SvXMLImportContext( rImport, nPrfx, rLName ),
62 	sVisibility(GetXMLToken(XML_VISIBLE))
63 {
64 	nColCount = 1;
65 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
66 	const SvXMLTokenMap& rAttrTokenMap = GetScImport().GetTableColAttrTokenMap();
67 
68 	for( sal_Int16 i=0; i < nAttrCount; ++i )
69 	{
70 		const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
71 		rtl::OUString aLocalName;
72 		sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
73 											sAttrName, &aLocalName );
74 		const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
75 
76 		switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
77 		{
78 			case XML_TOK_TABLE_COL_ATTR_REPEATED:
79 				{
80 					nColCount = sValue.toInt32();
81 				}
82 				break;
83 			case XML_TOK_TABLE_COL_ATTR_STYLE_NAME:
84 				{
85 					sStyleName = sValue;
86 				}
87 				break;
88 			case XML_TOK_TABLE_COL_ATTR_VISIBILITY:
89 				{
90 					sVisibility = sValue;
91 				}
92 				break;
93 			case XML_TOK_TABLE_COL_ATTR_DEFAULT_CELL_STYLE_NAME:
94 				{
95 					sCellStyleName = sValue;
96 				}
97 				break;
98 		}
99 	}
100 }
101 
~ScXMLTableColContext()102 ScXMLTableColContext::~ScXMLTableColContext()
103 {
104 }
105 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &)106 SvXMLImportContext *ScXMLTableColContext::CreateChildContext( sal_uInt16 nPrefix,
107 											const ::rtl::OUString& rLName,
108 											const ::com::sun::star::uno::Reference<
109                                         ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
110 {
111 	SvXMLImportContext *pContext = 0;
112 /*
113 	const SvXMLTokenMap& rTokenMap = GetScImport().GetTableRowElemTokenMap();
114 	sal_Bool bHeader = sal_False;
115 	switch( rTokenMap.Get( nPrefix, rLName ) )
116 	{
117 	case XML_TOK_TABLE_ROW_CELL:
118 //		if( IsInsertCellPossible() )
119 			pContext = new ScXMLTableRowCellContext( GetScImport(), nPrefix,
120 													  rLName, xAttrList//,
121 													  //this
122 													  );
123 		break;
124 	case XML_TOK_TABLE_ROW_COVERED_CELL:
125 //		if( IsInsertCellPossible() )
126 			pContext = new ScXMLTableRowCellContext( GetScImport(), nPrefix,
127 													  rLName, xAttrList//,
128 													  //this
129 													  );
130 		break;
131 	}*/
132 
133 	if( !pContext )
134 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
135 
136 	return pContext;
137 }
138 
EndElement()139 void ScXMLTableColContext::EndElement()
140 {
141 	ScXMLImport& rXMLImport = GetScImport();
142     sal_Int32 nSheet = rXMLImport.GetTables().GetCurrentSheet();
143 	sal_Int32 nCurrentColumn = rXMLImport.GetTables().GetCurrentColumn();
144 	uno::Reference<sheet::XSpreadsheet> xSheet(rXMLImport.GetTables().GetCurrentXSheet());
145 	if(xSheet.is())
146 	{
147 		sal_Int32 nLastColumn(nCurrentColumn + nColCount - 1);
148 		if (nLastColumn > MAXCOL)
149 			nLastColumn = MAXCOL;
150 		if (nCurrentColumn > MAXCOL)
151 			nCurrentColumn = MAXCOL;
152 		uno::Reference<table::XColumnRowRange> xColumnRowRange (xSheet->getCellRangeByPosition(nCurrentColumn, 0, nLastColumn, 0), uno::UNO_QUERY);
153 		if (xColumnRowRange.is())
154 		{
155 			uno::Reference <beans::XPropertySet> xColumnProperties(xColumnRowRange->getColumns(), uno::UNO_QUERY);
156 			if (xColumnProperties.is())
157 			{
158 				if (sStyleName.getLength())
159 				{
160 					XMLTableStylesContext *pStyles = (XMLTableStylesContext *)rXMLImport.GetAutoStyles();
161                     if ( pStyles )
162                     {
163                         XMLTableStyleContext* pStyle = (XMLTableStyleContext *)pStyles->FindStyleChildContext(
164                             XML_STYLE_FAMILY_TABLE_COLUMN, sStyleName, sal_True);
165                         if (pStyle)
166                         {
167                             pStyle->FillPropertySet(xColumnProperties);
168 
169                             if ( nSheet != pStyle->GetLastSheet() )
170                             {
171                                 ScSheetSaveData* pSheetData = ScModelObj::getImplementation(rXMLImport.GetModel())->GetSheetSaveData();
172                                 pSheetData->AddColumnStyle( sStyleName, ScAddress( (SCCOL)nCurrentColumn, 0, (SCTAB)nSheet ) );
173                                 pStyle->SetLastSheet(nSheet);
174                             }
175                         }
176                     }
177 				}
178 				rtl::OUString sVisible(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_CELLVIS));
179 				sal_Bool bValue(sal_True);
180 				if (!IsXMLToken(sVisibility, XML_VISIBLE))
181 					bValue = sal_False;
182                 xColumnProperties->setPropertyValue(sVisible, uno::makeAny(bValue));
183 			}
184 		}
185 	}
186 
187     // #i57915# ScXMLImport::SetStyleToRange can't handle empty style names.
188     // The default for a column if there is no attribute is the style "Default" (programmatic API name).
189     if ( !sCellStyleName.getLength() )
190         sCellStyleName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Default" ));
191 
192 	GetScImport().GetTables().AddColCount(nColCount);
193 	GetScImport().GetTables().AddColStyle(nColCount, sCellStyleName);
194 }
195 
ScXMLTableColsContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList,const sal_Bool bTempHeader,const sal_Bool bTempGroup)196 ScXMLTableColsContext::ScXMLTableColsContext( ScXMLImport& rImport,
197 									  sal_uInt16 nPrfx,
198 									  const ::rtl::OUString& rLName,
199 									  const ::com::sun::star::uno::Reference<
200 									  ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
201 									  const sal_Bool bTempHeader, const sal_Bool bTempGroup) :
202 	SvXMLImportContext( rImport, nPrfx, rLName ),
203 	nHeaderStartCol(0),
204 	nHeaderEndCol(0),
205 	nGroupStartCol(0),
206 	nGroupEndCol(0),
207 	bHeader(bTempHeader),
208 	bGroup(bTempGroup),
209 	bGroupDisplay(sal_True)
210 {
211 	// don't have any attributes
212 	if (bHeader)
213 		nHeaderStartCol = rImport.GetTables().GetCurrentColumn();
214 	else if (bGroup)
215 	{
216 		nGroupStartCol = rImport.GetTables().GetCurrentColumn();
217 		sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
218 		for( sal_Int16 i=0; i < nAttrCount; ++i )
219 		{
220 			const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
221 			rtl::OUString aLocalName;
222 			sal_uInt16 nPrefix = GetScImport().GetNamespaceMap().GetKeyByAttrName(
223 												sAttrName, &aLocalName );
224 			const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
225 
226             if (nPrefix == XML_NAMESPACE_TABLE && IsXMLToken(aLocalName, XML_DISPLAY))
227 			{
228 				if (IsXMLToken(sValue, XML_FALSE))
229 					bGroupDisplay = sal_False;
230 			}
231 		}
232 	}
233 }
234 
~ScXMLTableColsContext()235 ScXMLTableColsContext::~ScXMLTableColsContext()
236 {
237 }
238 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)239 SvXMLImportContext *ScXMLTableColsContext::CreateChildContext( sal_uInt16 nPrefix,
240 											const ::rtl::OUString& rLName,
241 											const ::com::sun::star::uno::Reference<
242 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
243 {
244 	SvXMLImportContext *pContext = 0;
245 
246 	const SvXMLTokenMap& rTokenMap = GetScImport().GetTableColsElemTokenMap();
247 	switch( rTokenMap.Get( nPrefix, rLName ) )
248 	{
249 	case XML_TOK_TABLE_COLS_COL_GROUP:
250 		pContext = new ScXMLTableColsContext( GetScImport(), nPrefix,
251 												   rLName, xAttrList,
252 												   sal_False, sal_True );
253 		break;
254 	case XML_TOK_TABLE_COLS_HEADER_COLS:
255 		pContext = new ScXMLTableColsContext( GetScImport(), nPrefix,
256 												   rLName, xAttrList,
257 												   sal_True, sal_False );
258 		break;
259 	case XML_TOK_TABLE_COLS_COLS:
260 		pContext = new ScXMLTableColsContext( GetScImport(), nPrefix,
261 												   rLName, xAttrList,
262 												   sal_False, sal_False );
263 		break;
264 	case XML_TOK_TABLE_COLS_COL:
265 			pContext = new ScXMLTableColContext( GetScImport(), nPrefix,
266 													  rLName, xAttrList//,
267 													  //this
268 													  );
269 		break;
270 	}
271 
272 	if( !pContext )
273 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
274 
275 	return pContext;
276 }
277 
EndElement()278 void ScXMLTableColsContext::EndElement()
279 {
280 	ScXMLImport& rXMLImport = GetScImport();
281 	if (bHeader)
282 	{
283 		nHeaderEndCol = rXMLImport.GetTables().GetCurrentColumn();
284 		nHeaderEndCol--;
285 		if (nHeaderStartCol <= nHeaderEndCol)
286 		{
287 			uno::Reference <sheet::XPrintAreas> xPrintAreas (rXMLImport.GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
288 			if (xPrintAreas.is())
289 			{
290 				if (!xPrintAreas->getPrintTitleColumns())
291 				{
292 					xPrintAreas->setPrintTitleColumns(sal_True);
293 					table::CellRangeAddress aColumnHeaderRange;
294 					aColumnHeaderRange.StartColumn = nHeaderStartCol;
295 					aColumnHeaderRange.EndColumn = nHeaderEndCol;
296 					xPrintAreas->setTitleColumns(aColumnHeaderRange);
297 				}
298 				else
299 				{
300 					table::CellRangeAddress aColumnHeaderRange(xPrintAreas->getTitleColumns());
301 					aColumnHeaderRange.EndColumn = nHeaderEndCol;
302 					xPrintAreas->setTitleColumns(aColumnHeaderRange);
303 				}
304 			}
305 		}
306 	}
307 	else if (bGroup)
308 	{
309 		sal_Int32 nSheet = rXMLImport.GetTables().GetCurrentSheet();
310 		nGroupEndCol = rXMLImport.GetTables().GetCurrentColumn();
311 		nGroupEndCol--;
312 		if (nGroupStartCol <= nGroupEndCol)
313 		{
314 			ScDocument* pDoc = GetScImport().GetDocument();
315 			if (pDoc)
316 			{
317 				rXMLImport.LockSolarMutex();
318 				ScOutlineTable* pOutlineTable = pDoc->GetOutlineTable(static_cast<SCTAB>(nSheet), sal_True);
319 				ScOutlineArray* pColArray = pOutlineTable ? pOutlineTable->GetColArray() : NULL;
320 				if (pColArray)
321 				{
322 					sal_Bool bResized;
323 					pColArray->Insert(static_cast<SCCOL>(nGroupStartCol), static_cast<SCCOL>(nGroupEndCol), bResized, !bGroupDisplay, sal_True);
324 				}
325 				rXMLImport.UnlockSolarMutex();
326 			}
327 		}
328 	}
329 }
330