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 #include "XMLTableSourceContext.hxx"
31 #include "xmlimprt.hxx"
32 #include "document.hxx"
33 #include "xmlsubti.hxx"
34 #include "tablink.hxx"
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlnmspe.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #include <com/sun/star/sheet/XSheetLinkable.hpp>
40 
41 using namespace com::sun::star;
42 using namespace xmloff::token;
43 
44 //------------------------------------------------------------------
45 
ScXMLTableSourceContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)46 ScXMLTableSourceContext::ScXMLTableSourceContext( ScXMLImport& rImport,
47 									  sal_uInt16 nPrfx,
48 									  const ::rtl::OUString& rLName,
49 									  const ::com::sun::star::uno::Reference<
50 									  ::com::sun::star::xml::sax::XAttributeList>& xAttrList) :
51 	SvXMLImportContext( rImport, nPrfx, rLName ),
52 	sLink(),
53 	sTableName(),
54 	sFilterName(),
55 	sFilterOptions(),
56 	nRefresh(0),
57 	nMode(sheet::SheetLinkMode_NORMAL)
58 {
59 	sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
60 	for( sal_Int16 i=0; i < nAttrCount; ++i )
61 	{
62 		const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
63 		rtl::OUString aLocalName;
64 		sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
65 											sAttrName, &aLocalName ));
66 		const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
67 		if(nPrefix == XML_NAMESPACE_XLINK)
68 		{
69 			if (IsXMLToken(aLocalName, XML_HREF))
70 				sLink = GetScImport().GetAbsoluteReference(sValue);
71 		}
72 		else if (nPrefix == XML_NAMESPACE_TABLE)
73 		{
74 			if (IsXMLToken(aLocalName, XML_TABLE_NAME))
75 				sTableName = sValue;
76 			else if (IsXMLToken(aLocalName, XML_FILTER_NAME))
77 				sFilterName = sValue;
78 			else if (IsXMLToken(aLocalName, XML_FILTER_OPTIONS))
79 				sFilterOptions = sValue;
80 			else if (IsXMLToken(aLocalName, XML_MODE))
81 			{
82 				if (IsXMLToken(sValue, XML_COPY_RESULTS_ONLY))
83 					nMode = sheet::SheetLinkMode_VALUE;
84 			}
85 			else if (IsXMLToken(aLocalName, XML_REFRESH_DELAY))
86 			{
87 				double fTime;
88 				if( SvXMLUnitConverter::convertTime( fTime, sValue ) )
89 					nRefresh = Max( (sal_Int32)(fTime * 86400.0), (sal_Int32)0 );
90 			}
91 		}
92 	}
93 }
94 
~ScXMLTableSourceContext()95 ScXMLTableSourceContext::~ScXMLTableSourceContext()
96 {
97 }
98 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &)99 SvXMLImportContext *ScXMLTableSourceContext::CreateChildContext( sal_uInt16 nPrefix,
100 											const ::rtl::OUString& rLName,
101 											const ::com::sun::star::uno::Reference<
102                                         ::com::sun::star::xml::sax::XAttributeList>& /* xAttrList */ )
103 {
104 	return new SvXMLImportContext( GetImport(), nPrefix, rLName );
105 }
106 
EndElement()107 void ScXMLTableSourceContext::EndElement()
108 {
109 	if (sLink.getLength())
110 	{
111 		uno::Reference <sheet::XSheetLinkable> xLinkable (GetScImport().GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
112 		ScDocument* pDoc(GetScImport().GetDocument());
113 		if (xLinkable.is() && pDoc)
114 		{
115 			GetScImport().LockSolarMutex();
116 			if (pDoc->RenameTab( static_cast<SCTAB>(GetScImport().GetTables().GetCurrentSheet()),
117 				GetScImport().GetTables().GetCurrentSheetName(), sal_False, sal_True))
118 			{
119 		 		String aFileString(sLink);
120 				String aFilterString(sFilterName);
121 				String aOptString(sFilterOptions);
122 				String aSheetString(sTableName);
123 
124 				aFileString = ScGlobal::GetAbsDocName( aFileString, pDoc->GetDocumentShell() );
125 				if ( !aFilterString.Len() )
126                     ScDocumentLoader::GetFilterName( aFileString, aFilterString, aOptString, sal_False, sal_False );
127 
128 				sal_uInt8 nLinkMode = SC_LINK_NONE;
129 				if ( nMode == sheet::SheetLinkMode_NORMAL )
130 					nLinkMode = SC_LINK_NORMAL;
131 				else if ( nMode == sheet::SheetLinkMode_VALUE )
132 					nLinkMode = SC_LINK_VALUE;
133 
134 				pDoc->SetLink( static_cast<SCTAB>(GetScImport().GetTables().GetCurrentSheet()),
135 					nLinkMode, aFileString, aFilterString, aOptString,
136 					aSheetString, nRefresh );
137 			}
138 			GetScImport().UnlockSolarMutex();
139 		}
140 	}
141 }
142 
143