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 // INCLUDE ---------------------------------------------------------------
28 
29 #include "XMLCodeNameProvider.hxx"
30 #include "document.hxx"
31 
32 using namespace rtl;
33 using namespace com::sun::star;
34 
_getCodeName(const uno::Any & aAny,String & rCodeName)35 sal_Bool XMLCodeNameProvider::_getCodeName( const uno::Any& aAny, String& rCodeName )
36 {
37 	uno::Sequence<beans::PropertyValue> aProps;
38 	if( !(aAny >>= aProps) )
39 		return sal_False;
40 
41 	OUString sCodeNameProp( RTL_CONSTASCII_USTRINGPARAM("CodeName") );
42 	sal_Int32 nPropCount = aProps.getLength();
43 	for( sal_Int32 i=0; i<nPropCount; i++ )
44 	{
45 		if( aProps[i].Name == sCodeNameProp )
46 		{
47 			OUString sCodeName;
48 			if( aProps[i].Value >>= sCodeName )
49 			{
50 				rCodeName = sCodeName;
51 				return sal_True;
52 			}
53 		}
54 	}
55 
56 	return sal_False;
57 }
58 
59 
XMLCodeNameProvider(ScDocument * pDoc)60 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument* pDoc ) :
61 	mpDoc( pDoc ),
62 	msDocName( RTL_CONSTASCII_USTRINGPARAM("*doc*") ),
63 	msCodeNameProp( RTL_CONSTASCII_USTRINGPARAM("CodeName") )
64 {
65 }
66 
~XMLCodeNameProvider()67 XMLCodeNameProvider::~XMLCodeNameProvider()
68 {
69 }
70 
hasByName(const OUString & aName)71 ::sal_Bool SAL_CALL XMLCodeNameProvider::hasByName( const OUString& aName )
72 	throw (uno::RuntimeException )
73 {
74 	if( aName == msDocName )
75 		return mpDoc->GetCodeName().Len() > 0;
76 
77 	SCTAB nCount = mpDoc->GetTableCount();
78 	String sName( aName );
79 	String sSheetName, sCodeName;
80 	for( SCTAB i = 0; i < nCount; i++ )
81 	{
82 		if( mpDoc->GetName( i, sSheetName ) && sSheetName == sName )
83 		{
84 			mpDoc->GetCodeName( i, sCodeName );
85 			return sCodeName.Len() > 0;
86 		}
87 	}
88 
89 	return sal_False;
90 }
91 
getByName(const OUString & aName)92 uno::Any SAL_CALL XMLCodeNameProvider::getByName( const OUString& aName )
93 	throw (container::NoSuchElementException,
94 		   lang::WrappedTargetException, uno::RuntimeException)
95 {
96 	uno::Any aRet;
97 	uno::Sequence<beans::PropertyValue> aProps(1);
98 	aProps[0].Name = msCodeNameProp;
99 	if( aName == msDocName )
100 	{
101 		OUString sUCodeName( mpDoc->GetCodeName() );
102 		aProps[0].Value <<= sUCodeName;
103 		aRet <<= aProps;
104 		return aRet;
105 	}
106 
107 	SCTAB nCount = mpDoc->GetTableCount();
108 	String sName( aName );
109 	String sSheetName, sCodeName;
110 	for( SCTAB i = 0; i < nCount; i++ )
111 	{
112 		if( mpDoc->GetName( i, sSheetName ) && sSheetName == sName )
113 		{
114 			mpDoc->GetCodeName( i, sCodeName );
115 			OUString sUCodeName( sCodeName );
116 			aProps[0].Value <<= sUCodeName;
117 			aRet <<= aProps;
118 			return aRet;
119 		}
120 	}
121 
122 	return aRet;
123 }
124 
getElementNames()125 uno::Sequence< OUString > SAL_CALL XMLCodeNameProvider::getElementNames(  )
126 	throw (uno::RuntimeException)
127 {
128 	SCTAB nCount = mpDoc->GetTableCount() + 1;
129 	uno::Sequence< rtl::OUString > aNames( nCount );
130 	sal_Int32 nRealCount = 0;
131 
132 	if( mpDoc->GetCodeName().Len() )
133 		aNames[nRealCount++] = msDocName;
134 
135 	String sSheetName, sCodeName;
136 	for( SCTAB i = 0; i < nCount; i++ )
137 	{
138 		mpDoc->GetCodeName( i, sCodeName );
139 		if( sCodeName.Len() > 0 )
140 		{
141 			if( mpDoc->GetName( i, sSheetName ) )
142 				aNames[nRealCount++] = sSheetName;
143 		}
144 	}
145 
146 	if( nCount != nRealCount )
147 		aNames.realloc( nRealCount );
148 
149 	return aNames;
150 }
151 
getElementType()152 uno::Type SAL_CALL XMLCodeNameProvider::getElementType(  )
153 	throw (uno::RuntimeException)
154 {
155 	return getCppuType( static_cast< uno::Sequence< beans::PropertyValue >* >( 0 ) );
156 }
157 
hasElements()158 ::sal_Bool SAL_CALL XMLCodeNameProvider::hasElements()
159 	throw (uno::RuntimeException )
160 {
161 	if( mpDoc->GetCodeName().Len() > 0 )
162 		return sal_True;
163 
164 	SCTAB nCount = mpDoc->GetTableCount();
165 	String sSheetName, sCodeName;
166 	for( SCTAB i = 0; i < nCount; i++ )
167 	{
168 		mpDoc->GetCodeName( i, sCodeName );
169 		if( sCodeName.Len() > 0 && mpDoc->GetName( i, sSheetName ) )
170 			return sal_True;
171 	}
172 
173 	return sal_False;
174 }
175 
set(const uno::Reference<container::XNameAccess> & xNameAccess,ScDocument * pDoc)176 void XMLCodeNameProvider::set( const uno::Reference< container::XNameAccess>& xNameAccess, ScDocument *pDoc )
177 {
178 	uno::Any aAny;
179 	OUString sDocName( RTL_CONSTASCII_USTRINGPARAM("*doc*") );
180 	String sCodeName;
181 	if( xNameAccess->hasByName( sDocName ) )
182 	{
183 		aAny = xNameAccess->getByName( sDocName );
184 		if( _getCodeName( aAny, sCodeName ) )
185 			pDoc->SetCodeName( sCodeName );
186 	}
187 
188 	SCTAB nCount = pDoc->GetTableCount();
189 	String sSheetName;
190 	for( SCTAB i = 0; i < nCount; i++ )
191 	{
192 		if( pDoc->GetName( i, sSheetName ) &&
193 			xNameAccess->hasByName( sSheetName ) )
194 		{
195 			aAny = xNameAccess->getByName( sSheetName );
196 			if( _getCodeName( aAny, sCodeName ) )
197 				pDoc->SetCodeName( i, sCodeName );
198 		}
199 	}
200 }
201