1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "xecontent.hxx"
#include <list>
#include <algorithm>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/sheet/XAreaLinks.hpp>
#include <com/sun/star/sheet/XAreaLink.hpp>
#include <sfx2/objsh.hxx>
#include <tools/urlobj.hxx>
#include <svl/itemset.hxx>
#include <formula/grammar.hxx>
#include "scitems.hxx"
#include <editeng/eeitem.hxx>
#include <editeng/flditem.hxx>
#include "document.hxx"
#include "validat.hxx"
#include "unonames.hxx"
#include "convuno.hxx"
#include "rangenam.hxx"
#include "tokenarray.hxx"
#include "stlpool.hxx"
#include "patattr.hxx"
#include "fapihelper.hxx"
#include "xehelper.hxx"
#include "xestyle.hxx"
#include "xename.hxx"
using namespace ::oox;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::UNO_QUERY;
using ::com::sun::star::beans::XPropertySet;
using ::com::sun::star::container::XIndexAccess;
using ::com::sun::star::frame::XModel;
using ::com::sun::star::table::CellRangeAddress;
using ::com::sun::star::sheet::XAreaLinks;
using ::com::sun::star::sheet::XAreaLink;
using ::rtl::OString;
using ::rtl::OUString;
using ::rtl::OUStringBuffer;
// Shared string table ========================================================
/** A single string entry in the hash table. */
struct XclExpHashEntry
{
const XclExpString* mpString; /// Pointer to the string (no ownership).
sal_uInt32 mnSstIndex; /// The SST index of this string.
inline explicit XclExpHashEntry( const XclExpString* pString = 0, sal_uInt32 nSstIndex = 0 ) :
mpString( pString ), mnSstIndex( nSstIndex ) {}
};
/** Function object for strict weak ordering. */
struct XclExpHashEntrySWO
{
inline bool operator()( const XclExpHashEntry& rLeft, const XclExpHashEntry& rRight ) const
{ return *rLeft.mpString < *rRight.mpString; }
};
// ----------------------------------------------------------------------------
/** Implementation of the SST export.
@descr Stores all passed strings in a hash table and prevents repeated
insertion of equal strings. */
class XclExpSstImpl
{
public:
explicit XclExpSstImpl();
/** Inserts the passed string, if not already inserted, and returns the unique SST index. */
sal_uInt32 Insert( XclExpStringRef xString );
/** Writes the complete SST and EXTSST records. */
void Save( XclExpStream& rStrm );
void SaveXml( XclExpXmlStream& rStrm );
private:
typedef ::std::list< XclExpStringRef > XclExpStringList;
typedef ::std::vector< XclExpHashEntry > XclExpHashVec;
typedef ::std::vector< XclExpHashVec > XclExpHashTab;
XclExpStringList maStringList; /// List of unique strings (in SST ID order).
XclExpHashTab maHashTab; /// Hashed table that manages string pointers.
sal_uInt32 mnTotal; /// Total count of strings (including doubles).
sal_uInt32 mnSize; /// Size of the SST (count of unique strings).
};
// ----------------------------------------------------------------------------
const sal_uInt32 EXC_SST_HASHTABLE_SIZE = 2048;
XclExpSstImpl::XclExpSstImpl() :
maHashTab( EXC_SST_HASHTABLE_SIZE ),
mnTotal( 0 ),
mnSize( 0 )
{
}
sal_uInt32 XclExpSstImpl::Insert( XclExpStringRef xString )
{
OSL_ENSURE( xString.get(), "XclExpSstImpl::Insert - empty pointer not allowed" );
if( !xString.get() )
xString.reset( new XclExpString );
++mnTotal;
sal_uInt32 nSstIndex = 0;
// calculate hash value in range [0,EXC_SST_HASHTABLE_SIZE)
sal_uInt16 nHash = xString->GetHash();
(nHash ^= (nHash / EXC_SST_HASHTABLE_SIZE)) %= EXC_SST_HASHTABLE_SIZE;
XclExpHashVec& rVec = maHashTab[ nHash ];
XclExpHashEntry aEntry( xString.get(), mnSize );
XclExpHashVec::iterator aIt = ::std::lower_bound( rVec.begin(), rVec.end(), aEntry, XclExpHashEntrySWO() );
if( (aIt == rVec.end()) || (*aIt->mpString != *xString) )
{
nSstIndex = mnSize;
maStringList.push_back( xString );
rVec.insert( aIt, aEntry );
++mnSize;
}
else
{
nSstIndex = aIt->mnSstIndex;
}
return nSstIndex;
}
void XclExpSstImpl::Save( XclExpStream& rStrm )
{
if( maStringList.empty() )
return;
SvMemoryStream aExtSst( 8192 );
sal_uInt32 nBucket = mnSize;
while( nBucket > 0x0100 )
nBucket /= 2;
sal_uInt16 nPerBucket = llimit_cast< sal_uInt16 >( nBucket, 8 );
sal_uInt16 nBucketIndex = 0;
// *** write the SST record ***
rStrm.StartRecord( EXC_ID_SST, 8 );
rStrm << mnTotal << mnSize;
for( XclExpStringList::const_iterator aIt = maStringList.begin(), aEnd = maStringList.end(); aIt != aEnd; ++aIt )
{
if( !nBucketIndex )
{
// write bucket info before string to get correct record position
sal_uInt32 nStrmPos = static_cast< sal_uInt32 >( rStrm.GetSvStreamPos() );
sal_uInt16 nRecPos = rStrm.GetRawRecPos() + 4;
aExtSst << nStrmPos // stream position
<< nRecPos // position from start of SST or CONTINUE
<< sal_uInt16( 0 ); // reserved
}
rStrm << **aIt;
if( ++nBucketIndex == nPerBucket )
nBucketIndex = 0;
}
rStrm.EndRecord();
// *** write the EXTSST record ***
rStrm.StartRecord( EXC_ID_EXTSST, 0 );
rStrm << nPerBucket;
rStrm.SetSliceSize( 8 ); // size of one bucket info
aExtSst.Seek( STREAM_SEEK_TO_BEGIN );
rStrm.CopyFromStream( aExtSst );
rStrm.EndRecord();
}
void XclExpSstImpl::SaveXml( XclExpXmlStream& rStrm )
{
if( maStringList.empty() )
return;
sax_fastparser::FSHelperPtr pSst = rStrm.CreateOutputStream(
OUString(RTL_CONSTASCII_USTRINGPARAM( "xl/sharedStrings.xml") ),
OUString(RTL_CONSTASCII_USTRINGPARAM( "sharedStrings.xml" )),
rStrm.GetCurrentStream()->getOutputStream(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" );
rStrm.PushStream( pSst );
pSst->startElement( XML_sst,
XML_xmlns, "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
XML_count, OString::valueOf( (sal_Int32) mnTotal ).getStr(),
XML_uniqueCount, OString::valueOf( (sal_Int32) mnSize ).getStr(),
FSEND );
for( XclExpStringList::const_iterator aIt = maStringList.begin(), aEnd = maStringList.end(); aIt != aEnd; ++aIt )
{
pSst->startElement( XML_si, FSEND );
(*aIt)->WriteXml( rStrm );
pSst->endElement( XML_si );
}
pSst->endElement( XML_sst );
rStrm.PopStream();
}
// ----------------------------------------------------------------------------
XclExpSst::XclExpSst() :
mxImpl( new XclExpSstImpl )
{
}
XclExpSst::~XclExpSst()
{
}
sal_uInt32 XclExpSst::Insert( XclExpStringRef xString )
{
return mxImpl->Insert( xString );
}
void XclExpSst::Save( XclExpStream& rStrm )
{
mxImpl->Save( rStrm );
}
void XclExpSst::SaveXml( XclExpXmlStream& rStrm )
{
mxImpl->SaveXml( rStrm );
}
// Merged cells ===============================================================
XclExpMergedcells::XclExpMergedcells( const XclExpRoot& rRoot ) :
XclExpRoot( rRoot )
{
}
void XclExpMergedcells::AppendRange( const ScRange& rRange, sal_uInt32 nBaseXFId )
{
if( GetBiff() == EXC_BIFF8 )
{
maMergedRanges.Append( rRange );
maBaseXFIds.push_back( nBaseXFId );
}
}
sal_uInt32 XclExpMergedcells::GetBaseXFId( const ScAddress& rPos ) const
{
OSL_ENSURE( maBaseXFIds.size() == maMergedRanges.size(), "XclExpMergedcells::GetBaseXFId - invalid lists" );
ScfUInt32Vec::const_iterator aIt = maBaseXFIds.begin();
ScRangeList& rNCRanges = const_cast< ScRangeList& >( maMergedRanges );
for ( size_t i = 0, nRanges = rNCRanges.size(); i < nRanges; ++i, ++aIt )
{
const ScRange* pScRange = rNCRanges[ i ];
if( pScRange->In( rPos ) )
return *aIt;
}
return EXC_XFID_NOTFOUND;
}
void XclExpMergedcells::Save( XclExpStream& rStrm )
{
if( GetBiff() == EXC_BIFF8 )
{
XclRangeList aXclRanges;
GetAddressConverter().ConvertRangeList( aXclRanges, maMergedRanges, true );
size_t nFirstRange = 0;
size_t nRemainingRanges = aXclRanges.size();
while( nRemainingRanges > 0 )
{
size_t nRangeCount = ::std::min< size_t >( nRemainingRanges, EXC_MERGEDCELLS_MAXCOUNT );
rStrm.StartRecord( EXC_ID_MERGEDCELLS, 2 + 8 * nRangeCount );
aXclRanges.WriteSubList( rStrm, nFirstRange, nRangeCount );
rStrm.EndRecord();
nFirstRange += nRangeCount;
nRemainingRanges -= nRangeCount;
}
}
}
void XclExpMergedcells::SaveXml( XclExpXmlStream& rStrm )
{
size_t nCount = maMergedRanges.size();
if( !nCount )
return;
sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
rWorksheet->startElement( XML_mergeCells,
XML_count, OString::valueOf( (sal_Int32) nCount ).getStr(),
FSEND );
for( size_t i = 0; i < nCount; ++i )
{
if( const ScRange* pRange = maMergedRanges[ i ] )
{
rWorksheet->singleElement( XML_mergeCell,
XML_ref, XclXmlUtils::ToOString( *pRange ).getStr(),
FSEND );
}
}
rWorksheet->endElement( XML_mergeCells );
}
// Hyperlinks =================================================================
XclExpHyperlink::XclExpHyperlink( const XclExpRoot& rRoot, const SvxURLField& rUrlField, const ScAddress& rScPos ) :
XclExpRecord( EXC_ID_HLINK ),
maScPos( rScPos ),
mxVarData( new SvMemoryStream ),
mnFlags( 0 )
{
const String& rUrl = rUrlField.GetURL();
const String& rRepr = rUrlField.GetRepresentation();
INetURLObject aUrlObj( rUrl );
const INetProtocol eProtocol = aUrlObj.GetProtocol();
bool bWithRepr = rRepr.Len() > 0;
XclExpStream aXclStrm( *mxVarData, rRoot ); // using in raw write mode.
// description
if( bWithRepr )
{
XclExpString aDescr( rRepr, EXC_STR_FORCEUNICODE, 255 );
aXclStrm << sal_uInt32( aDescr.Len() + 1 ); // string length + 1 trailing zero word
aDescr.WriteBuffer( aXclStrm ); // NO flags
aXclStrm << sal_uInt16( 0 );
mnFlags |= EXC_HLINK_DESCR;
mxRepr.reset( new String( rRepr ) );
}
// file link or URL
if( eProtocol == INET_PROT_FILE || eProtocol == INET_PROT_SMB )
{
sal_uInt16 nLevel;
bool bRel;
String aFileName( BuildFileName( nLevel, bRel, rUrl, rRoot ) );
if( eProtocol == INET_PROT_SMB )
{
// #n382718# (and #n261623#) Convert smb notation to '\\'
aFileName = aUrlObj.GetMainURL( INetURLObject::NO_DECODE );
aFileName = String( aFileName.GetBuffer() + 4 ); // skip the 'smb:' part
aFileName.SearchAndReplaceAll( '/', '\\' );
}
if( !bRel )
mnFlags |= EXC_HLINK_ABS;
mnFlags |= EXC_HLINK_BODY;
rtl::OString aAsciiLink(rtl::OUStringToOString(aFileName,
rRoot.GetTextEncoding()));
XclExpString aLink( aFileName, EXC_STR_FORCEUNICODE, 255 );
aXclStrm << XclTools::maGuidFileMoniker
<< nLevel
<< sal_uInt32( aAsciiLink.getLength() + 1 ); // string length + 1 trailing zero byte
aXclStrm.Write( aAsciiLink.getStr(), aAsciiLink.getLength() );
aXclStrm << sal_uInt8( 0 )
<< sal_uInt32( 0xDEADFFFF );
aXclStrm.WriteZeroBytes( 20 );
aXclStrm << sal_uInt32( aLink.GetBufferSize() + 6 )
<< sal_uInt32( aLink.GetBufferSize() ) // byte count, not string length
<< sal_uInt16( 0x0003 );
aLink.WriteBuffer( aXclStrm ); // NO flags
if( !mxRepr.get() )
mxRepr.reset( new String( aFileName ) );
msTarget = XclXmlUtils::ToOUString( aLink );
}
else if( eProtocol != INET_PROT_NOT_VALID )
{
XclExpString aUrl( aUrlObj.GetURLNoMark(), EXC_STR_FORCEUNICODE, 255 );
aXclStrm << XclTools::maGuidUrlMoniker
<< sal_uInt32( aUrl.GetBufferSize() + 2 ); // byte count + 1 trailing zero word
aUrl.WriteBuffer( aXclStrm ); // NO flags
aXclStrm << sal_uInt16( 0 );
mnFlags |= EXC_HLINK_BODY | EXC_HLINK_ABS;
if( !mxRepr.get() )
mxRepr.reset( new String( rUrl ) );
msTarget = XclXmlUtils::ToOUString( aUrl );
}
else if( rUrl.GetChar( 0 ) == '#' ) // hack for #89066#
{
String aTextMark( rUrl.Copy( 1 ) );
xub_StrLen nSepPos = aTextMark.SearchAndReplace( '.', '!' );
String aSheetName( aTextMark.Copy(0, nSepPos));
if ( aSheetName.Search(' ') != STRING_NOTFOUND && aSheetName.GetChar(0) != '\'')
{
aTextMark.Insert('\'', nSepPos);
aTextMark.Insert('\'', 0);
}
mxTextMark.reset( new XclExpString( aTextMark, EXC_STR_FORCEUNICODE, 255 ) );
}
// text mark
if( !mxTextMark.get() && aUrlObj.HasMark() )
mxTextMark.reset( new XclExpString( aUrlObj.GetMark(), EXC_STR_FORCEUNICODE, 255 ) );
if( mxTextMark.get() )
{
aXclStrm << sal_uInt32( mxTextMark->Len() + 1 ); // string length + 1 trailing zero word
mxTextMark->WriteBuffer( aXclStrm ); // NO flags
aXclStrm << sal_uInt16( 0 );
mnFlags |= EXC_HLINK_MARK;
}
SetRecSize( 32 + mxVarData->Tell() );
}
XclExpHyperlink::~XclExpHyperlink()
{
}
String XclExpHyperlink::BuildFileName(
sal_uInt16& rnLevel, bool& rbRel, const String& rUrl, const XclExpRoot& rRoot ) const
{
String aDosName( INetURLObject( rUrl ).getFSysPath( INetURLObject::FSYS_DOS ) );
rnLevel = 0;
rbRel = rRoot.IsRelUrl();
if( rbRel )
{
// try to convert to relative file name
String aTmpName( aDosName );
aDosName = INetURLObject::GetRelURL( rRoot.GetBasePath(), rUrl,
INetURLObject::WAS_ENCODED, INetURLObject::DECODE_WITH_CHARSET );
if( aDosName.SearchAscii( INET_FILE_SCHEME ) == 0 )
{
// not converted to rel -> back to old, return absolute flag
aDosName = aTmpName;
rbRel = false;
}
else if( aDosName.SearchAscii( "./" ) == 0 )
{
aDosName.Erase( 0, 2 );
}
else
{
while( aDosName.SearchAndReplaceAscii( "../", EMPTY_STRING ) == 0 )
++rnLevel;
}
}
return aDosName;
}
void XclExpHyperlink::WriteBody( XclExpStream& rStrm )
{
sal_uInt16 nXclCol = static_cast< sal_uInt16 >( maScPos.Col() );
sal_uInt16 nXclRow = static_cast< sal_uInt16 >( maScPos.Row() );
rStrm << nXclRow << nXclRow << nXclCol << nXclCol;
WriteEmbeddedData( rStrm );
}
void XclExpHyperlink::WriteEmbeddedData( XclExpStream& rStrm )
{
rStrm << XclTools::maGuidStdLink
<< sal_uInt32( 2 )
<< mnFlags;
mxVarData->Seek( STREAM_SEEK_TO_BEGIN );
rStrm.CopyFromStream( *mxVarData );
}
void XclExpHyperlink::SaveXml( XclExpXmlStream& rStrm )
{
OUString sId = msTarget.getLength() ? rStrm.addRelation( rStrm.GetCurrentStream()->getOutputStream(),
XclXmlUtils::ToOUString( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" ),
msTarget, true ) : OUString();
rStrm.GetCurrentStream()->singleElement( XML_hyperlink,
XML_ref, XclXmlUtils::ToOString( maScPos ).getStr(),
FSNS( XML_r, XML_id ), sId.getLength()
? XclXmlUtils::ToOString( sId ).getStr()
: NULL,
XML_location, mxTextMark.get() != NULL
? XclXmlUtils::ToOString( *mxTextMark ).getStr()
: NULL,
// OOXTODO: XML_tooltip, from record HLinkTooltip 800h wzTooltip
XML_display, XclXmlUtils::ToOString( *mxRepr ).getStr(),
FSEND );
}
// Label ranges ===============================================================
XclExpLabelranges::XclExpLabelranges( const XclExpRoot& rRoot ) :
XclExpRoot( rRoot )
{
SCTAB nScTab = GetCurrScTab();
// row label ranges
FillRangeList( maRowRanges, rRoot.GetDoc().GetRowNameRangesRef(), nScTab );
// row labels only over 1 column (restriction of Excel97/2000/XP)
for ( size_t i = 0, nRanges = maRowRanges.size(); i < nRanges; ++i )
{
ScRange* pScRange = maRowRanges[ i ];
if( pScRange->aStart.Col() != pScRange->aEnd.Col() )
pScRange->aEnd.SetCol( pScRange->aStart.Col() );
}
// col label ranges
FillRangeList( maColRanges, rRoot.GetDoc().GetColNameRangesRef(), nScTab );
}
void XclExpLabelranges::FillRangeList( ScRangeList& rScRanges,
ScRangePairListRef xLabelRangesRef, SCTAB nScTab )
{
for ( size_t i = 0, nPairs = xLabelRangesRef->size(); i < nPairs; ++i )
{
ScRangePair* pRangePair = (*xLabelRangesRef)[i];
const ScRange& rScRange = pRangePair->GetRange( 0 );
if( rScRange.aStart.Tab() == nScTab )
rScRanges.Append( rScRange );
}
}
void XclExpLabelranges::Save( XclExpStream& rStrm )
{
XclExpAddressConverter& rAddrConv = GetAddressConverter();
XclRangeList aRowXclRanges, aColXclRanges;
rAddrConv.ConvertRangeList( aRowXclRanges, maRowRanges, false );
rAddrConv.ConvertRangeList( aColXclRanges, maColRanges, false );
if( !aRowXclRanges.empty() || !aColXclRanges.empty() )
{
rStrm.StartRecord( EXC_ID_LABELRANGES, 4 + 8 * (aRowXclRanges.size() + aColXclRanges.size()) );
rStrm << aRowXclRanges << aColXclRanges;
rStrm.EndRecord();
}
}
// Conditional formatting ====================================================
/** Represents a CF record that contains one condition of a conditional format. */
class XclExpCFImpl : protected XclExpRoot
{
public:
explicit XclExpCFImpl( const XclExpRoot& rRoot, const ScCondFormatEntry& rFormatEntry );
/** Writes the body of the CF record. */
void WriteBody( XclExpStream& rStrm );
private:
const ScCondFormatEntry& mrFormatEntry; /// Calc conditional format entry.
XclFontData maFontData; /// Font formatting attributes.
XclExpCellBorder maBorder; /// Border formatting attributes.
XclExpCellArea maArea; /// Pattern formatting attributes.
XclTokenArrayRef mxTokArr1; /// Formula for first condition.
XclTokenArrayRef mxTokArr2; /// Formula for second condition.
sal_uInt32 mnFontColorId; /// Font color ID.
sal_uInt8 mnType; /// Type of the condition (cell/formula).
sal_uInt8 mnOperator; /// Comparison operator for cell type.
bool mbFontUsed; /// true = Any font attribute used.
bool mbHeightUsed; /// true = Font height used.
bool mbWeightUsed; /// true = Font weight used.
bool mbColorUsed; /// true = Font color used.
bool mbUnderlUsed; /// true = Font underline type used.
bool mbItalicUsed; /// true = Font posture used.
bool mbStrikeUsed; /// true = Font strikeout used.
bool mbBorderUsed; /// true = Border attribute used.
bool mbPattUsed; /// true = Pattern attribute used.
};
// ----------------------------------------------------------------------------
XclExpCFImpl::XclExpCFImpl( const XclExpRoot& rRoot, const ScCondFormatEntry& rFormatEntry ) :
XclExpRoot( rRoot ),
mrFormatEntry( rFormatEntry ),
mnFontColorId( 0 ),
mnType( EXC_CF_TYPE_CELL ),
mnOperator( EXC_CF_CMP_NONE ),
mbFontUsed( false ),
mbHeightUsed( false ),
mbWeightUsed( false ),
mbColorUsed( false ),
mbUnderlUsed( false ),
mbItalicUsed( false ),
mbStrikeUsed( false ),
mbBorderUsed( false ),
mbPattUsed( false )
{
/* Get formatting attributes here, and not in WriteBody(). This is needed to
correctly insert all colors into the palette. */
if( SfxStyleSheetBase* pStyleSheet = GetDoc().GetStyleSheetPool()->Find( mrFormatEntry.GetStyle(), SFX_STYLE_FAMILY_PARA ) )
{
const SfxItemSet& rItemSet = pStyleSheet->GetItemSet();
// font
mbHeightUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_HEIGHT, true );
mbWeightUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_WEIGHT, true );
mbColorUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_COLOR, true );
mbUnderlUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_UNDERLINE, true );
mbItalicUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_POSTURE, true );
mbStrikeUsed = ScfTools::CheckItem( rItemSet, ATTR_FONT_CROSSEDOUT, true );
mbFontUsed = mbHeightUsed || mbWeightUsed || mbColorUsed || mbUnderlUsed || mbItalicUsed || mbStrikeUsed;
if( mbFontUsed )
{
Font aFont;
ScPatternAttr::GetFont( aFont, rItemSet, SC_AUTOCOL_RAW );
maFontData.FillFromVclFont( aFont );
mnFontColorId = GetPalette().InsertColor( maFontData.maColor, EXC_COLOR_CELLTEXT );
}
// border
mbBorderUsed = ScfTools::CheckItem( rItemSet, ATTR_BORDER, true );
if( mbBorderUsed )
maBorder.FillFromItemSet( rItemSet, GetPalette(), GetBiff() );
// pattern
mbPattUsed = ScfTools::CheckItem( rItemSet, ATTR_BACKGROUND, true );
if( mbPattUsed )
maArea.FillFromItemSet( rItemSet, GetPalette(), GetBiff() );
}
// *** mode and comparison operator ***
bool bFmla2 = false;
switch( rFormatEntry.GetOperation() )
{
case SC_COND_NONE: mnType = EXC_CF_TYPE_NONE; break;
case SC_COND_BETWEEN: mnOperator = EXC_CF_CMP_BETWEEN; bFmla2 = true; break;
case SC_COND_NOTBETWEEN: mnOperator = EXC_CF_CMP_NOT_BETWEEN; bFmla2 = true; break;
case SC_COND_EQUAL: mnOperator = EXC_CF_CMP_EQUAL; break;
case SC_COND_NOTEQUAL: mnOperator = EXC_CF_CMP_NOT_EQUAL; break;
case SC_COND_GREATER: mnOperator = EXC_CF_CMP_GREATER; break;
case SC_COND_LESS: mnOperator = EXC_CF_CMP_LESS; break;
case SC_COND_EQGREATER: mnOperator = EXC_CF_CMP_GREATER_EQUAL; break;
case SC_COND_EQLESS: mnOperator = EXC_CF_CMP_LESS_EQUAL; break;
case SC_COND_DIRECT: mnType = EXC_CF_TYPE_FMLA; break;
default: mnType = EXC_CF_TYPE_NONE;
OSL_FAIL( "XclExpCF::WriteBody - unknown condition type" );
}
// *** formulas ***
XclExpFormulaCompiler& rFmlaComp = GetFormulaCompiler();
SAL_WNODEPRECATED_DECLARATIONS_PUSH
::std::auto_ptr< ScTokenArray > xScTokArr( mrFormatEntry.CreateTokenArry( 0 ) );
SAL_WNODEPRECATED_DECLARATIONS_POP
mxTokArr1 = rFmlaComp.CreateFormula( EXC_FMLATYPE_CONDFMT, *xScTokArr );
if( bFmla2 )
{
xScTokArr.reset( mrFormatEntry.CreateTokenArry( 1 ) );
mxTokArr2 = rFmlaComp.CreateFormula( EXC_FMLATYPE_CONDFMT, *xScTokArr );
}
}
void XclExpCFImpl::WriteBody( XclExpStream& rStrm )
{
// *** mode and comparison operator ***
rStrm << mnType << mnOperator;
// *** formula sizes ***
sal_uInt16 nFmlaSize1 = mxTokArr1.get() ? mxTokArr1->GetSize() : 0;
sal_uInt16 nFmlaSize2 = mxTokArr2.get() ? mxTokArr2->GetSize() : 0;
rStrm << nFmlaSize1 << nFmlaSize2;
// *** formatting blocks ***
if( mbFontUsed || mbBorderUsed || mbPattUsed )
{
sal_uInt32 nFlags = EXC_CF_ALLDEFAULT;
::set_flag( nFlags, EXC_CF_BLOCK_FONT, mbFontUsed );
::set_flag( nFlags, EXC_CF_BLOCK_BORDER, mbBorderUsed );
::set_flag( nFlags, EXC_CF_BLOCK_AREA, mbPattUsed );
// attributes used -> set flags to 0.
::set_flag( nFlags, EXC_CF_BORDER_ALL, !mbBorderUsed );
::set_flag( nFlags, EXC_CF_AREA_ALL, !mbPattUsed );
rStrm << nFlags << sal_uInt16( 0 );
if( mbFontUsed )
{
// font height, 0xFFFFFFFF indicates unused
sal_uInt32 nHeight = mbHeightUsed ? maFontData.mnHeight : 0xFFFFFFFF;
// font style: italic and strikeout
sal_uInt32 nStyle = 0;
::set_flag( nStyle, EXC_CF_FONT_STYLE, maFontData.mbItalic );
::set_flag( nStyle, EXC_CF_FONT_STRIKEOUT, maFontData.mbStrikeout );
// font color, 0xFFFFFFFF indicates unused
sal_uInt32 nColor = mbColorUsed ? GetPalette().GetColorIndex( mnFontColorId ) : 0xFFFFFFFF;
// font used flags for italic, weight, and strikeout -> 0 = used, 1 = default
sal_uInt32 nFontFlags1 = EXC_CF_FONT_ALLDEFAULT;
::set_flag( nFontFlags1, EXC_CF_FONT_STYLE, !(mbItalicUsed || mbWeightUsed) );
::set_flag( nFontFlags1, EXC_CF_FONT_STRIKEOUT, !mbStrikeUsed );
// font used flag for underline -> 0 = used, 1 = default
sal_uInt32 nFontFlags3 = mbUnderlUsed ? 0 : EXC_CF_FONT_UNDERL;
rStrm.WriteZeroBytesToRecord( 64 );
rStrm << nHeight
<< nStyle
<< maFontData.mnWeight
<< EXC_FONTESC_NONE
<< maFontData.mnUnderline;
rStrm.WriteZeroBytesToRecord( 3 );
rStrm << nColor
<< sal_uInt32( 0 )
<< nFontFlags1
<< EXC_CF_FONT_ESCAPEM // escapement never used -> set the flag
<< nFontFlags3;
rStrm.WriteZeroBytesToRecord( 16 );
rStrm << sal_uInt16( 1 ); // must be 1
}
if( mbBorderUsed )
{
sal_uInt16 nLineStyle = 0;
sal_uInt32 nLineColor = 0;
maBorder.SetFinalColors( GetPalette() );
maBorder.FillToCF8( nLineStyle, nLineColor );
rStrm << nLineStyle << nLineColor << sal_uInt16( 0 );
}
if( mbPattUsed )
{
sal_uInt16 nPattern = 0, nColor = 0;
maArea.SetFinalColors( GetPalette() );
maArea.FillToCF8( nPattern, nColor );
rStrm << nPattern << nColor;
}
}
else
{
// no data blocks at all
rStrm << sal_uInt32( 0 ) << sal_uInt16( 0 );
}
// *** formulas ***
if( mxTokArr1.get() )
mxTokArr1->WriteArray( rStrm );
if( mxTokArr2.get() )
mxTokArr2->WriteArray( rStrm );
}
// ----------------------------------------------------------------------------
XclExpCF::XclExpCF( const XclExpRoot& rRoot, const ScCondFormatEntry& rFormatEntry ) :
XclExpRecord( EXC_ID_CF ),
XclExpRoot( rRoot ),
mxImpl( new XclExpCFImpl( rRoot, rFormatEntry ) )
{
}
XclExpCF::~XclExpCF()
{
}
void XclExpCF::WriteBody( XclExpStream& rStrm )
{
mxImpl->WriteBody( rStrm );
}
// ----------------------------------------------------------------------------
XclExpCondfmt::XclExpCondfmt( const XclExpRoot& rRoot, const ScConditionalFormat& rCondFormat ) :
XclExpRecord( EXC_ID_CONDFMT ),
XclExpRoot( rRoot )
{
ScRangeList aScRanges;
GetDoc().FindConditionalFormat( rCondFormat.GetKey(), aScRanges, GetCurrScTab() );
GetAddressConverter().ConvertRangeList( maXclRanges, aScRanges, true );
if( !maXclRanges.empty() )
{
for( sal_uInt16 nIndex = 0, nCount = rCondFormat.Count(); nIndex < nCount; ++nIndex )
if( const ScCondFormatEntry* pEntry = rCondFormat.GetEntry( nIndex ) )
maCFList.AppendNewRecord( new XclExpCF( GetRoot(), *pEntry ) );
aScRanges.Format( msSeqRef, SCA_VALID, NULL, formula::FormulaGrammar::CONV_XL_A1 );
}
}
XclExpCondfmt::~XclExpCondfmt()
{
}
bool XclExpCondfmt::IsValid() const
{
return !maCFList.IsEmpty() && !maXclRanges.empty();
}
void XclExpCondfmt::Save( XclExpStream& rStrm )
{
if( IsValid() )
{
XclExpRecord::Save( rStrm );
maCFList.Save( rStrm );
}
}
void XclExpCondfmt::WriteBody( XclExpStream& rStrm )
{
OSL_ENSURE( !maCFList.IsEmpty(), "XclExpCondfmt::WriteBody - no CF records to write" );
OSL_ENSURE( !maXclRanges.empty(), "XclExpCondfmt::WriteBody - no cell ranges found" );
rStrm << static_cast< sal_uInt16 >( maCFList.GetSize() )
<< sal_uInt16( 1 )
<< maXclRanges.GetEnclosingRange()
<< maXclRanges;
}
void XclExpCondfmt::SaveXml( XclExpXmlStream& rStrm )
{
if( !IsValid() )
return;
sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
rWorksheet->startElement( XML_conditionalFormatting,
XML_sqref, XclXmlUtils::ToOString( msSeqRef ).getStr(),
// OOXTODO: XML_pivot,
FSEND );
maCFList.SaveXml( rStrm );
// OOXTODO: XML_extLst
rWorksheet->endElement( XML_conditionalFormatting );
}
// ----------------------------------------------------------------------------
XclExpCondFormatBuffer::XclExpCondFormatBuffer( const XclExpRoot& rRoot ) :
XclExpRoot( rRoot )
{
if( const ScConditionalFormatList* pCondFmtList = GetDoc().GetCondFormList() )
{
if( const ScConditionalFormatPtr* ppCondFmt = pCondFmtList->GetData() )
{
const ScConditionalFormatPtr* ppCondEnd = ppCondFmt + pCondFmtList->Count();
for( ; ppCondFmt < ppCondEnd; ++ppCondFmt )
{
if( *ppCondFmt )
{
XclExpCondfmtList::RecordRefType xCondfmtRec( new XclExpCondfmt( GetRoot(), **ppCondFmt ) );
if( xCondfmtRec->IsValid() )
maCondfmtList.AppendRecord( xCondfmtRec );
}
}
}
}
}
void XclExpCondFormatBuffer::Save( XclExpStream& rStrm )
{
maCondfmtList.Save( rStrm );
}
void XclExpCondFormatBuffer::SaveXml( XclExpXmlStream& rStrm )
{
maCondfmtList.SaveXml( rStrm );
}
// Validation =================================================================
namespace {
/** Writes a formula for the DV record. */
void lclWriteDvFormula( XclExpStream& rStrm, const XclTokenArray* pXclTokArr )
{
sal_uInt16 nFmlaSize = pXclTokArr ? pXclTokArr->GetSize() : 0;
rStrm << nFmlaSize << sal_uInt16( 0 );
if( pXclTokArr )
pXclTokArr->WriteArray( rStrm );
}
/** Writes a formula for the DV record, based on a single string. */
void lclWriteDvFormula( XclExpStream& rStrm, const XclExpString& rString )
{
// fake a formula with a single tStr token
rStrm << static_cast< sal_uInt16 >( rString.GetSize() + 1 )
<< sal_uInt16( 0 )
<< EXC_TOKID_STR
<< rString;
}
const char* lcl_GetValidationType( sal_uInt32 nFlags )
{
switch( nFlags & EXC_DV_MODE_MASK )
{
case EXC_DV_MODE_ANY: return "none";
case EXC_DV_MODE_WHOLE: return "whole";
case EXC_DV_MODE_DECIMAL: return "decimal";
case EXC_DV_MODE_LIST: return "list";
case EXC_DV_MODE_DATE: return "date";
case EXC_DV_MODE_TIME: return "time";
case EXC_DV_MODE_TEXTLEN: return "textLength";
case EXC_DV_MODE_CUSTOM: return "custom";
}
return NULL;
}
const char* lcl_GetOperatorType( sal_uInt32 nFlags )
{
switch( nFlags & EXC_DV_COND_MASK )
{
case EXC_DV_COND_BETWEEN: return "between";
case EXC_DV_COND_NOTBETWEEN: return "notBetween";
case EXC_DV_COND_EQUAL: return "equal";
case EXC_DV_COND_NOTEQUAL: return "notEqual";
case EXC_DV_COND_GREATER: return "greaterThan";
case EXC_DV_COND_LESS: return "lessThan";
case EXC_DV_COND_EQGREATER: return "greaterThanOrEqual";
case EXC_DV_COND_EQLESS: return "lessThanOrEqual";
}
return NULL;
}
} // namespace
// ----------------------------------------------------------------------------
XclExpDV::XclExpDV( const XclExpRoot& rRoot, sal_uLong nScHandle ) :
XclExpRecord( EXC_ID_DV ),
XclExpRoot( rRoot ),
mnFlags( 0 ),
mnScHandle( nScHandle )
{
if( const ScValidationData* pValData = GetDoc().GetValidationEntry( mnScHandle ) )
{
// prompt box - empty string represented by single NUL character
String aTitle, aText;
bool bShowPrompt = (pValData->GetInput( aTitle, aText ) == sal_True);
if( aTitle.Len() )
maPromptTitle.Assign( aTitle );
else
maPromptTitle.Assign( '\0' );
if( aText.Len() )
maPromptText.Assign( aText );
else
maPromptText.Assign( '\0' );
// error box - empty string represented by single NUL character
ScValidErrorStyle eScErrorStyle;
bool bShowError = (pValData->GetErrMsg( aTitle, aText, eScErrorStyle ) == sal_True);
if( aTitle.Len() )
maErrorTitle.Assign( aTitle );
else
maErrorTitle.Assign( '\0' );
if( aText.Len() )
maErrorText.Assign( aText );
else
maErrorText.Assign( '\0' );
// flags
switch( pValData->GetDataMode() )
{
case SC_VALID_ANY: mnFlags |= EXC_DV_MODE_ANY; break;
case SC_VALID_WHOLE: mnFlags |= EXC_DV_MODE_WHOLE; break;
case SC_VALID_DECIMAL: mnFlags |= EXC_DV_MODE_DECIMAL; break;
case SC_VALID_LIST: mnFlags |= EXC_DV_MODE_LIST; break;
case SC_VALID_DATE: mnFlags |= EXC_DV_MODE_DATE; break;
case SC_VALID_TIME: mnFlags |= EXC_DV_MODE_TIME; break;
case SC_VALID_TEXTLEN: mnFlags |= EXC_DV_MODE_TEXTLEN; break;
case SC_VALID_CUSTOM: mnFlags |= EXC_DV_MODE_CUSTOM; break;
default: OSL_FAIL( "XclExpDV::XclExpDV - unknown mode" );
}
switch( pValData->GetOperation() )
{
case SC_COND_NONE:
case SC_COND_EQUAL: mnFlags |= EXC_DV_COND_EQUAL; break;
case SC_COND_LESS: mnFlags |= EXC_DV_COND_LESS; break;
case SC_COND_GREATER: mnFlags |= EXC_DV_COND_GREATER; break;
case SC_COND_EQLESS: mnFlags |= EXC_DV_COND_EQLESS; break;
case SC_COND_EQGREATER: mnFlags |= EXC_DV_COND_EQGREATER; break;
case SC_COND_NOTEQUAL: mnFlags |= EXC_DV_COND_NOTEQUAL; break;
case SC_COND_BETWEEN: mnFlags |= EXC_DV_COND_BETWEEN; break;
case SC_COND_NOTBETWEEN: mnFlags |= EXC_DV_COND_NOTBETWEEN; break;
default: OSL_FAIL( "XclExpDV::XclExpDV - unknown condition" );
}
switch( eScErrorStyle )
{
case SC_VALERR_STOP: mnFlags |= EXC_DV_ERROR_STOP; break;
case SC_VALERR_WARNING: mnFlags |= EXC_DV_ERROR_WARNING; break;
case SC_VALERR_INFO: mnFlags |= EXC_DV_ERROR_INFO; break;
case SC_VALERR_MACRO:
// set INFO for validity with macro call, delete title
mnFlags |= EXC_DV_ERROR_INFO;
maErrorTitle.Assign( '\0' ); // contains macro name
break;
default: OSL_FAIL( "XclExpDV::XclExpDV - unknown error style" );
}
::set_flag( mnFlags, EXC_DV_IGNOREBLANK, pValData->IsIgnoreBlank() );
::set_flag( mnFlags, EXC_DV_SUPPRESSDROPDOWN, pValData->GetListType() == ValidListType::INVISIBLE );
::set_flag( mnFlags, EXC_DV_SHOWPROMPT, bShowPrompt );
::set_flag( mnFlags, EXC_DV_SHOWERROR, bShowError );
// formulas
XclExpFormulaCompiler& rFmlaComp = GetFormulaCompiler();
SAL_WNODEPRECATED_DECLARATIONS_PUSH
::std::auto_ptr< ScTokenArray > xScTokArr;
SAL_WNODEPRECATED_DECLARATIONS_POP
// first formula
xScTokArr.reset( pValData->CreateTokenArry( 0 ) );
if( xScTokArr.get() )
{
if( pValData->GetDataMode() == SC_VALID_LIST )
{
String aString;
if( XclTokenArrayHelper::GetStringList( aString, *xScTokArr, '\n' ) )
{
OUStringBuffer sFormulaBuf;
sFormulaBuf.append( (sal_Unicode) '"' );
/* Formula is a list of string tokens -> build the Excel string.
Data validity is BIFF8 only (important for the XclExpString object).
Excel uses the NUL character as string list separator. */
mxString1.reset( new XclExpString( EXC_STR_8BITLENGTH ) );
xub_StrLen nTokenCnt = aString.GetTokenCount( '\n' );
xub_StrLen nStringIx = 0;
for( xub_StrLen nToken = 0; nToken < nTokenCnt; ++nToken )
{
String aToken( aString.GetToken( 0, '\n', nStringIx ) );
if( nToken > 0 )
{
mxString1->Append( '\0' );
sFormulaBuf.append( (sal_Unicode) ',' );
}
mxString1->Append( aToken );
sFormulaBuf.append( XclXmlUtils::ToOUString( aToken ) );
}
::set_flag( mnFlags, EXC_DV_STRINGLIST );
sFormulaBuf.append( (sal_Unicode) '"' );
msFormula1 = sFormulaBuf.makeStringAndClear();
}
else
{
/* All other formulas in validation are stored like conditional
formatting formulas (with tRefN/tAreaN tokens as value or
array class). But NOT the cell references and defined names
in list validation - they are stored as reference class
tokens... Example:
1) Cell must be equal to A1 -> formula is =A1 -> writes tRefNV token
2) List is taken from A1 -> formula is =A1 -> writes tRefNR token
Formula compiler supports this by offering two different functions
CreateDataValFormula() and CreateListValFormula(). */
mxTokArr1 = rFmlaComp.CreateFormula( EXC_FMLATYPE_LISTVAL, *xScTokArr );
msFormula1 = XclXmlUtils::ToOUString( GetDoc(), pValData->GetSrcPos(), xScTokArr.get() );
}
}
else
{
// no list validation -> convert the formula
mxTokArr1 = rFmlaComp.CreateFormula( EXC_FMLATYPE_DATAVAL, *xScTokArr );
msFormula1 = XclXmlUtils::ToOUString( GetDoc(), pValData->GetSrcPos(), xScTokArr.get() );
}
}
// second formula
xScTokArr.reset( pValData->CreateTokenArry( 1 ) );
if( xScTokArr.get() )
{
mxTokArr2 = rFmlaComp.CreateFormula( EXC_FMLATYPE_DATAVAL, *xScTokArr );
msFormula2 = XclXmlUtils::ToOUString( GetDoc(), pValData->GetSrcPos(), xScTokArr.get() );
}
}
else
{
OSL_FAIL( "XclExpDV::XclExpDV - missing core data" );
mnScHandle = ULONG_MAX;
}
}
XclExpDV::~XclExpDV()
{
}
void XclExpDV::InsertCellRange( const ScRange& rRange )
{
maScRanges.Join( rRange );
}
bool XclExpDV::Finalize()
{
GetAddressConverter().ConvertRangeList( maXclRanges, maScRanges, true );
return (mnScHandle != ULONG_MAX) && !maXclRanges.empty();
}
void XclExpDV::WriteBody( XclExpStream& rStrm )
{
// flags and strings
rStrm << mnFlags << maPromptTitle << maErrorTitle << maPromptText << maErrorText;
// condition formulas
if( mxString1.get() )
lclWriteDvFormula( rStrm, *mxString1 );
else
lclWriteDvFormula( rStrm, mxTokArr1.get() );
lclWriteDvFormula( rStrm, mxTokArr2.get() );
// cell ranges
rStrm << maXclRanges;
}
void XclExpDV::SaveXml( XclExpXmlStream& rStrm )
{
sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
rWorksheet->startElement( XML_dataValidation,
XML_allowBlank, XclXmlUtils::ToPsz( ::get_flag( mnFlags, EXC_DV_IGNOREBLANK ) ),
XML_error, XESTRING_TO_PSZ( maErrorText ),
// OOXTODO: XML_errorStyle,
XML_errorTitle, XESTRING_TO_PSZ( maErrorTitle ),
// OOXTODO: XML_imeMode,
XML_operator, lcl_GetOperatorType( mnFlags ),
XML_prompt, XESTRING_TO_PSZ( maPromptText ),
XML_promptTitle, XESTRING_TO_PSZ( maPromptTitle ),
// showDropDown should have been showNoDropDown - check oox/xlsx import for details
XML_showDropDown, XclXmlUtils::ToPsz( ::get_flag( mnFlags, EXC_DV_SUPPRESSDROPDOWN ) ),
XML_showErrorMessage, XclXmlUtils::ToPsz( ::get_flag( mnFlags, EXC_DV_SHOWERROR ) ),
XML_showInputMessage, XclXmlUtils::ToPsz( ::get_flag( mnFlags, EXC_DV_SHOWPROMPT ) ),
XML_sqref, XclXmlUtils::ToOString( maScRanges ).getStr(),
XML_type, lcl_GetValidationType( mnFlags ),
FSEND );
if( msFormula1.getLength() )
{
rWorksheet->startElement( XML_formula1, FSEND );
rWorksheet->writeEscaped( msFormula1 );
rWorksheet->endElement( XML_formula1 );
}
if( msFormula2.getLength() )
{
rWorksheet->startElement( XML_formula2, FSEND );
rWorksheet->writeEscaped( msFormula2 );
rWorksheet->endElement( XML_formula2 );
}
rWorksheet->endElement( XML_dataValidation );
}
// ----------------------------------------------------------------------------
XclExpDval::XclExpDval( const XclExpRoot& rRoot ) :
XclExpRecord( EXC_ID_DVAL, 18 ),
XclExpRoot( rRoot )
{
}
XclExpDval::~XclExpDval()
{
}
void XclExpDval::InsertCellRange( const ScRange& rRange, sal_uLong nScHandle )
{
if( GetBiff() == EXC_BIFF8 )
{
XclExpDV& rDVRec = SearchOrCreateDv( nScHandle );
rDVRec.InsertCellRange( rRange );
}
}
void XclExpDval::Save( XclExpStream& rStrm )
{
// check all records
size_t nPos = maDVList.GetSize();
while( nPos )
{
--nPos; // backwards to keep nPos valid
XclExpDVRef xDVRec = maDVList.GetRecord( nPos );
if( !xDVRec->Finalize() )
maDVList.RemoveRecord( nPos );
}
// write the DVAL and the DV's
if( !maDVList.IsEmpty() )
{
XclExpRecord::Save( rStrm );
maDVList.Save( rStrm );
}
}
void XclExpDval::SaveXml( XclExpXmlStream& rStrm )
{
if( maDVList.IsEmpty() )
return;
sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
rWorksheet->startElement( XML_dataValidations,
XML_count, OString::valueOf( (sal_Int32) maDVList.GetSize() ).getStr(),
// OOXTODO: XML_disablePrompts,
// OOXTODO: XML_xWindow,
// OOXTODO: XML_yWindow,
FSEND );
maDVList.SaveXml( rStrm );
rWorksheet->endElement( XML_dataValidations );
}
XclExpDV& XclExpDval::SearchOrCreateDv( sal_uLong nScHandle )
{
// test last found record
if( mxLastFoundDV.get() && (mxLastFoundDV->GetScHandle() == nScHandle) )
return *mxLastFoundDV;
// binary search
size_t nCurrPos = 0;
if( !maDVList.IsEmpty() )
{
size_t nFirstPos = 0;
size_t nLastPos = maDVList.GetSize() - 1;
bool bLoop = true;
sal_uLong nCurrScHandle = ::std::numeric_limits< sal_uLong >::max();
while( (nFirstPos <= nLastPos) && bLoop )
{
nCurrPos = (nFirstPos + nLastPos) / 2;
mxLastFoundDV = maDVList.GetRecord( nCurrPos );
nCurrScHandle = mxLastFoundDV->GetScHandle();
if( nCurrScHandle == nScHandle )
bLoop = false;
else if( nCurrScHandle < nScHandle )
nFirstPos = nCurrPos + 1;
else if( nCurrPos )
nLastPos = nCurrPos - 1;
else // special case for nLastPos = -1
bLoop = false;
}
if( nCurrScHandle == nScHandle )
return *mxLastFoundDV;
else if( nCurrScHandle < nScHandle )
++nCurrPos;
}
// create new DV record
mxLastFoundDV.reset( new XclExpDV( *this, nScHandle ) );
maDVList.InsertRecord( mxLastFoundDV, nCurrPos );
return *mxLastFoundDV;
}
void XclExpDval::WriteBody( XclExpStream& rStrm )
{
rStrm.WriteZeroBytes( 10 );
rStrm << EXC_DVAL_NOOBJ << static_cast< sal_uInt32 >( maDVList.GetSize() );
}
// Web Queries ================================================================
XclExpWebQuery::XclExpWebQuery(
const String& rRangeName,
const String& rUrl,
const String& rSource,
sal_Int32 nRefrSecs ) :
maDestRange( rRangeName ),
maUrl( rUrl ),
// refresh delay time: seconds -> minutes
mnRefresh( ulimit_cast< sal_Int16 >( (nRefrSecs + 59L) / 60L ) ),
mbEntireDoc( false )
{
// comma separated list of HTML table names or indexes
xub_StrLen nTokenCnt = rSource.GetTokenCount( ';' );
String aNewTables, aAppendTable;
xub_StrLen nStringIx = 0;
bool bExitLoop = false;
for( xub_StrLen nToken = 0; (nToken < nTokenCnt) && !bExitLoop; ++nToken )
{
String aToken( rSource.GetToken( 0, ';', nStringIx ) );
mbEntireDoc = ScfTools::IsHTMLDocName( aToken );
bExitLoop = mbEntireDoc || ScfTools::IsHTMLTablesName( aToken );
if( !bExitLoop && ScfTools::GetHTMLNameFromName( aToken, aAppendTable ) )
ScGlobal::AddToken( aNewTables, aAppendTable, ',' );
}
if( !bExitLoop ) // neither HTML_all nor HTML_tables found
{
if( aNewTables.Len() )
mxQryTables.reset( new XclExpString( aNewTables ) );
else
mbEntireDoc = true;
}
}
XclExpWebQuery::~XclExpWebQuery()
{
}
void XclExpWebQuery::Save( XclExpStream& rStrm )
{
OSL_ENSURE( !mbEntireDoc || !mxQryTables.get(), "XclExpWebQuery::Save - illegal mode" );
sal_uInt16 nFlags;
// QSI record
rStrm.StartRecord( EXC_ID_QSI, 10 + maDestRange.GetSize() );
rStrm << EXC_QSI_DEFAULTFLAGS
<< sal_uInt16( 0x0010 )
<< sal_uInt16( 0x0012 )
<< sal_uInt32( 0x00000000 )
<< maDestRange;
rStrm.EndRecord();
// PARAMQRY record
nFlags = 0;
::insert_value( nFlags, EXC_PQRYTYPE_WEBQUERY, 0, 3 );
::set_flag( nFlags, EXC_PQRY_WEBQUERY );
::set_flag( nFlags, EXC_PQRY_TABLES, !mbEntireDoc );
rStrm.StartRecord( EXC_ID_PQRY, 12 );
rStrm << nFlags
<< sal_uInt16( 0x0000 )
<< sal_uInt16( 0x0001 );
rStrm.WriteZeroBytes( 6 );
rStrm.EndRecord();
// WQSTRING record
rStrm.StartRecord( EXC_ID_WQSTRING, maUrl.GetSize() );
rStrm << maUrl;
rStrm.EndRecord();
// unknown record 0x0802
rStrm.StartRecord( EXC_ID_0802, 16 + maDestRange.GetSize() );
rStrm << EXC_ID_0802; // repeated record id ?!?
rStrm.WriteZeroBytes( 6 );
rStrm << sal_uInt16( 0x0003 )
<< sal_uInt32( 0x00000000 )
<< sal_uInt16( 0x0010 )
<< maDestRange;
rStrm.EndRecord();
// WEBQRYSETTINGS record
nFlags = mxQryTables.get() ? EXC_WQSETT_SPECTABLES : EXC_WQSETT_ALL;
rStrm.StartRecord( EXC_ID_WQSETT, 28 );
rStrm << EXC_ID_WQSETT // repeated record id ?!?
<< sal_uInt16( 0x0000 )
<< sal_uInt16( 0x0004 )
<< sal_uInt16( 0x0000 )
<< EXC_WQSETT_DEFAULTFLAGS
<< nFlags;
rStrm.WriteZeroBytes( 10 );
rStrm << mnRefresh // refresh delay in minutes
<< EXC_WQSETT_FORMATFULL
<< sal_uInt16( 0x0000 );
rStrm.EndRecord();
// WEBQRYTABLES record
if( mxQryTables.get() )
{
rStrm.StartRecord( EXC_ID_WQTABLES, 4 + mxQryTables->GetSize() );
rStrm << EXC_ID_WQTABLES // repeated record id ?!?
<< sal_uInt16( 0x0000 )
<< *mxQryTables; // comma separated list of source tables
rStrm.EndRecord();
}
}
// ----------------------------------------------------------------------------
XclExpWebQueryBuffer::XclExpWebQueryBuffer( const XclExpRoot& rRoot )
{
SCTAB nScTab = rRoot.GetCurrScTab();
SfxObjectShell* pShell = rRoot.GetDocShell();
if( !pShell ) return;
ScfPropertySet aModelProp( pShell->GetModel() );
if( !aModelProp.Is() ) return;
Reference< XAreaLinks > xAreaLinks;
aModelProp.GetProperty( xAreaLinks, CREATE_OUSTRING( SC_UNO_AREALINKS ) );
Reference< XIndexAccess > xLinksIA( xAreaLinks, UNO_QUERY );
if( !xLinksIA.is() ) return;
for( sal_Int32 nIndex = 0, nCount = xLinksIA->getCount(); nIndex < nCount; ++nIndex )
{
Reference< XAreaLink > xAreaLink( xLinksIA->getByIndex( nIndex ), UNO_QUERY );
if( xAreaLink.is() )
{
CellRangeAddress aDestRange( xAreaLink->getDestArea() );
if( static_cast< SCTAB >( aDestRange.Sheet ) == nScTab )
{
ScfPropertySet aLinkProp( xAreaLink );
OUString aFilter;
if( aLinkProp.GetProperty( aFilter, CREATE_OUSTRING( SC_UNONAME_FILTER ) ) &&
(aFilter == CREATE_OUSTRING( EXC_WEBQRY_FILTER )) )
{
// get properties
OUString /*aFilterOpt,*/ aUrl;
sal_Int32 nRefresh = 0;
// aLinkProp.GetProperty( aFilterOpt, CREATE_OUSTRING( SC_UNONAME_FILTOPT ) );
aLinkProp.GetProperty( aUrl, CREATE_OUSTRING( SC_UNONAME_LINKURL ) );
aLinkProp.GetProperty( nRefresh, CREATE_OUSTRING( SC_UNONAME_REFDELAY ) );
String aAbsDoc( ScGlobal::GetAbsDocName( aUrl, pShell ) );
INetURLObject aUrlObj( aAbsDoc );
String aWebQueryUrl( aUrlObj.getFSysPath( INetURLObject::FSYS_DOS ) );
if( !aWebQueryUrl.Len() )
aWebQueryUrl = aAbsDoc;
// find range or create a new range
String aRangeName;
ScRange aScDestRange;
ScUnoConversion::FillScRange( aScDestRange, aDestRange );
if( const ScRangeData* pRangeData = rRoot.GetNamedRanges().findByRange( aScDestRange ) )
{
aRangeName = pRangeData->GetName();
}
else
{
XclExpFormulaCompiler& rFmlaComp = rRoot.GetFormulaCompiler();
XclExpNameManager& rNameMgr = rRoot.GetNameManager();
// create a new unique defined name containing the range
XclTokenArrayRef xTokArr = rFmlaComp.CreateFormula( EXC_FMLATYPE_WQUERY, aScDestRange );
sal_uInt16 nNameIdx = rNameMgr.InsertUniqueName( aUrlObj.getBase(), xTokArr, nScTab );
aRangeName = rNameMgr.GetOrigName( nNameIdx );
}
// create and store the web query record
if( aRangeName.Len() )
AppendNewRecord( new XclExpWebQuery(
aRangeName, aWebQueryUrl, xAreaLink->getSourceArea(), nRefresh ) );
}
}
}
}
}
// ============================================================================
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|