1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLCodeNameProvider.hxx"
21 #include "document.hxx"
23 using namespace com::sun::star
;
26 bool XMLCodeNameProvider::_getCodeName( const uno::Any
& aAny
, OUString
& rCodeName
)
28 uno::Sequence
<beans::PropertyValue
> aProps
;
29 if( !(aAny
>>= aProps
) )
32 OUString
sCodeNameProp("CodeName");
33 sal_Int32 nPropCount
= aProps
.getLength();
34 for( sal_Int32 i
=0; i
<nPropCount
; i
++ )
36 if( aProps
[i
].Name
== sCodeNameProp
)
39 if( aProps
[i
].Value
>>= sCodeName
)
41 rCodeName
= sCodeName
;
51 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument
* pDoc
) :
54 msCodeNameProp( "CodeName" )
58 XMLCodeNameProvider::~XMLCodeNameProvider()
62 sal_Bool SAL_CALL
XMLCodeNameProvider::hasByName( const OUString
& aName
)
63 throw (uno::RuntimeException
, std::exception
)
65 if( aName
== msDocName
)
66 return !mpDoc
->GetCodeName().isEmpty();
68 SCTAB nCount
= mpDoc
->GetTableCount();
69 OUString sSheetName
, sCodeName
;
70 for( SCTAB i
= 0; i
< nCount
; i
++ )
72 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
.equals(aName
) )
74 mpDoc
->GetCodeName( i
, sCodeName
);
75 return !sCodeName
.isEmpty();
82 uno::Any SAL_CALL
XMLCodeNameProvider::getByName( const OUString
& aName
)
83 throw (container::NoSuchElementException
,
84 lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
87 uno::Sequence
<beans::PropertyValue
> aProps(1);
88 aProps
[0].Name
= msCodeNameProp
;
89 if( aName
== msDocName
)
91 OUString
sUCodeName( mpDoc
->GetCodeName() );
92 aProps
[0].Value
<<= sUCodeName
;
97 SCTAB nCount
= mpDoc
->GetTableCount();
98 OUString sSheetName
, sCodeName
;
99 for( SCTAB i
= 0; i
< nCount
; i
++ )
101 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
.equals(aName
) )
103 mpDoc
->GetCodeName( i
, sCodeName
);
104 aProps
[0].Value
<<= sCodeName
;
113 uno::Sequence
< OUString
> SAL_CALL
XMLCodeNameProvider::getElementNames( )
114 throw (uno::RuntimeException
, std::exception
)
116 SCTAB nCount
= mpDoc
->GetTableCount() + 1;
117 uno::Sequence
< OUString
> aNames( nCount
);
118 sal_Int32 nRealCount
= 0;
120 if( !mpDoc
->GetCodeName().isEmpty() )
121 aNames
[nRealCount
++] = msDocName
;
123 OUString sSheetName
, sCodeName
;
124 for( SCTAB i
= 0; i
< nCount
; i
++ )
126 mpDoc
->GetCodeName( i
, sCodeName
);
127 if (!sCodeName
.isEmpty())
129 if( mpDoc
->GetName( i
, sSheetName
) )
130 aNames
[nRealCount
++] = sSheetName
;
134 if( nCount
!= nRealCount
)
135 aNames
.realloc( nRealCount
);
140 uno::Type SAL_CALL
XMLCodeNameProvider::getElementType( )
141 throw (uno::RuntimeException
, std::exception
)
143 return getCppuType(static_cast<uno::Sequence
<beans::PropertyValue
>*>(0));
146 sal_Bool SAL_CALL
XMLCodeNameProvider::hasElements()
147 throw (uno::RuntimeException
, std::exception
)
149 if( !mpDoc
->GetCodeName().isEmpty() )
152 SCTAB nCount
= mpDoc
->GetTableCount();
153 OUString sSheetName
, sCodeName
;
154 for( SCTAB i
= 0; i
< nCount
; i
++ )
156 mpDoc
->GetCodeName( i
, sCodeName
);
157 if (!sCodeName
.isEmpty() && mpDoc
->GetName(i
, sSheetName
))
164 void XMLCodeNameProvider::set( const uno::Reference
< container::XNameAccess
>& xNameAccess
, ScDocument
*pDoc
)
167 OUString
sDocName("*doc*");
169 if( xNameAccess
->hasByName( sDocName
) )
171 aAny
= xNameAccess
->getByName( sDocName
);
172 if( _getCodeName( aAny
, sCodeName
) )
173 pDoc
->SetCodeName( sCodeName
);
176 SCTAB nCount
= pDoc
->GetTableCount();
178 for( SCTAB i
= 0; i
< nCount
; i
++ )
180 if( pDoc
->GetName( i
, sSheetName
) &&
181 xNameAccess
->hasByName( sSheetName
) )
183 aAny
= xNameAccess
->getByName( sSheetName
);
184 if( _getCodeName( aAny
, sCodeName
) )
185 pDoc
->SetCodeName( i
, sCodeName
);
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */