fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / oox / source / docprop / docprophandler.cxx
blob1a5000b03906cc5f74a19dea7c7da43c3ee8f601
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "docprophandler.hxx"
22 #include <com/sun/star/beans/PropertyAttribute.hpp>
23 #include <com/sun/star/beans/PropertyExistException.hpp>
24 #include <com/sun/star/lang/Locale.hpp>
26 #include <osl/time.h>
27 #include <osl/diagnose.h>
28 #include <i18nlangtag/languagetag.hxx>
30 #include <vector>
31 #include <boost/algorithm/string.hpp>
33 #include "oox/helper/attributelist.hxx"
35 using namespace ::com::sun::star;
37 namespace oox {
38 namespace docprop {
40 OOXMLDocPropHandler::OOXMLDocPropHandler( const uno::Reference< uno::XComponentContext >& xContext,
41 const uno::Reference< document::XDocumentProperties >& rDocProp )
42 : m_xContext( xContext )
43 , m_xDocProp( rDocProp )
44 , m_nState( 0 )
45 , m_nBlock( 0 )
46 , m_nType( 0 )
47 , m_nInBlock( 0 )
49 if ( !xContext.is() || !rDocProp.is() )
50 throw uno::RuntimeException();
53 OOXMLDocPropHandler::~OOXMLDocPropHandler()
57 void OOXMLDocPropHandler::InitNew()
59 m_nState = 0;
60 m_nBlock = 0;
61 m_aCustomPropertyName.clear();
62 m_nType = 0;
63 m_nInBlock = 0;
66 void OOXMLDocPropHandler::AddCustomProperty( const uno::Any& aAny )
68 if ( !m_aCustomPropertyName.isEmpty() )
70 const uno::Reference< beans::XPropertyContainer > xUserProps =
71 m_xDocProp->getUserDefinedProperties();
72 if ( !xUserProps.is() )
73 throw uno::RuntimeException();
75 try
77 xUserProps->addProperty( m_aCustomPropertyName,
78 beans::PropertyAttribute::REMOVABLE, aAny );
80 catch( beans::PropertyExistException& )
82 // conflicts with core and extended properties are possible
84 catch( uno::Exception& )
86 OSL_FAIL( "Can not add custom property!" );
91 util::DateTime OOXMLDocPropHandler::GetDateTimeFromW3CDTF( const OUString& aChars )
93 oslDateTime aOslDTime = { 0, 0, 0, 0, 0, 0, 0, 0 };
94 const sal_Int32 nLen = aChars.getLength();
95 if ( nLen >= 4 )
97 aOslDTime.Year = (sal_Int16)aChars.copy( 0, 4 ).toInt32();
99 if ( nLen >= 7 && aChars[4] == (sal_Unicode)'-' )
101 aOslDTime.Month = (sal_uInt16)aChars.copy( 5, 2 ).toInt32();
103 if ( nLen >= 10 && aChars[7] == (sal_Unicode)'-' )
105 aOslDTime.Day = (sal_uInt16)aChars.copy( 8, 2 ).toInt32();
107 if ( nLen >= 16 && aChars[10] == (sal_Unicode)'T' && aChars[13] == (sal_Unicode)':' )
109 aOslDTime.Hours = (sal_uInt16)aChars.copy( 11, 2 ).toInt32();
110 aOslDTime.Minutes = (sal_uInt16)aChars.copy( 14, 2 ).toInt32();
112 sal_Int32 nOptTime = 0;
113 if ( nLen >= 19 && aChars[16] == (sal_Unicode)':' )
115 aOslDTime.Seconds = (sal_uInt16)aChars.copy( 17, 2 ).toInt32();
116 nOptTime += 3;
117 if ( nLen >= 20 && aChars[19] == (sal_Unicode)'.' )
119 nOptTime += 1;
120 sal_Int32 digitPos = 20;
121 while (nLen > digitPos && digitPos < 29)
123 sal_Unicode c = aChars[digitPos];
124 if ( c < '0' || c > '9')
125 break;
126 aOslDTime.NanoSeconds *= 10;
127 aOslDTime.NanoSeconds += c - '0';
128 ++digitPos;
130 if ( digitPos < 29 )
132 // read less digits than 9
133 // add correct exponent of 10
134 nOptTime += digitPos - 20;
135 for(; digitPos<29; ++digitPos)
137 aOslDTime.NanoSeconds *= 10;
140 else
142 //skip digits with more precision than we can handle
143 while(nLen > digitPos)
145 sal_Unicode c = aChars[digitPos];
146 if ( c < '0' || c > '9')
147 break;
148 ++digitPos;
150 nOptTime += digitPos - 20;
155 sal_Int32 nModif = 0;
156 if ( nLen >= 16 + nOptTime + 6 )
158 if ( ( aChars[16 + nOptTime] == (sal_Unicode)'+' || aChars[16 + nOptTime] == (sal_Unicode)'-' )
159 && aChars[16 + nOptTime + 3] == (sal_Unicode)':' )
161 nModif = aChars.copy( 16 + nOptTime + 1, 2 ).toInt32() * 3600;
162 nModif += aChars.copy( 16 + nOptTime + 4, 2 ).toInt32() * 60;
163 if ( aChars[16 + nOptTime] == (sal_Unicode)'-' )
164 nModif *= -1;
168 if ( nModif )
170 // convert to UTC time
171 TimeValue aTmp;
172 if ( osl_getTimeValueFromDateTime( &aOslDTime, &aTmp ) )
174 aTmp.Seconds -= nModif;
175 osl_getDateTimeFromTimeValue( &aTmp, &aOslDTime );
183 return util::DateTime( aOslDTime.NanoSeconds, aOslDTime.Seconds,
184 aOslDTime.Minutes, aOslDTime.Hours,
185 aOslDTime.Day, aOslDTime.Month, aOslDTime.Year, false);
188 uno::Sequence< OUString > OOXMLDocPropHandler::GetKeywordsSet( const OUString& aChars )
190 if ( !aChars.isEmpty() )
192 std::string aUtf8Chars = OUStringToOString( aChars, RTL_TEXTENCODING_UTF8 ).getStr();
193 std::vector<std::string> aUtf8Result;
194 boost::split( aUtf8Result, aUtf8Chars, boost::is_any_of(" ,;:\t"), boost::token_compress_on );
196 if (!aUtf8Result.empty())
198 uno::Sequence< OUString > aResult( aUtf8Result.size() );
199 OUString* pResultValues = aResult.getArray();
200 for ( std::vector< std::string >::const_iterator i = aUtf8Result.begin();
201 i != aUtf8Result.end(); ++i, ++pResultValues )
202 *pResultValues = OUString( i->c_str(), static_cast< sal_Int32 >( i->size() ),RTL_TEXTENCODING_UTF8 );
204 return aResult;
207 return uno::Sequence< OUString >();
210 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString& aChars )
212 uno::Sequence< beans::NamedValue > aSet = m_xDocProp->getDocumentStatistics();
213 OUString aName;
215 switch( m_nBlock )
217 case EXTPR_TOKEN( Characters ):
218 aName = "CharacterCount";
219 break;
221 case EXTPR_TOKEN( Pages ):
222 aName = "PageCount";
223 break;
225 case EXTPR_TOKEN( Words ):
226 aName = "WordCount";
227 break;
229 case EXTPR_TOKEN( Paragraphs ):
230 aName = "ParagraphCount";
231 break;
233 default:
234 OSL_FAIL( "Unexpected statistic!" );
235 break;
238 if ( !aName.isEmpty() )
240 bool bFound = false;
241 sal_Int32 nLen = aSet.getLength();
242 for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
243 if ( aSet[nInd].Name.equals( aName ) )
245 aSet[nInd].Value = uno::makeAny( aChars.toInt32() );
246 bFound = true;
247 break;
250 if ( !bFound )
252 aSet.realloc( nLen + 1 );
253 aSet[nLen].Name = aName;
254 aSet[nLen].Value = uno::makeAny( aChars.toInt32() );
257 m_xDocProp->setDocumentStatistics( aSet );
261 // com.sun.star.xml.sax.XFastDocumentHandler
263 void SAL_CALL OOXMLDocPropHandler::startDocument()
264 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
268 void SAL_CALL OOXMLDocPropHandler::endDocument()
269 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
271 InitNew();
274 void SAL_CALL OOXMLDocPropHandler::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& )
275 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
279 // com.sun.star.xml.sax.XFastContextHandler
281 void SAL_CALL OOXMLDocPropHandler::startFastElement( ::sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttribs )
282 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
284 if ( !m_nInBlock && !m_nState )
286 if ( nElement == COREPR_TOKEN( coreProperties )
287 || nElement == EXTPR_TOKEN( Properties )
288 || nElement == CUSTPR_TOKEN( Properties ) )
290 m_nState = nElement;
292 else
294 OSL_FAIL( "Unexpected file format!" );
297 else if ( m_nState && m_nInBlock == 1 ) // that tag should contain the property name
299 // Currently the attributes are ignored for the core properties since the only
300 // known attribute is xsi:type that can only be used with dcterms:created and
301 // dcterms:modified, and this element is allowed currently to have only one value dcterms:W3CDTF
302 m_nBlock = nElement;
304 if ( xAttribs.is() && xAttribs->hasAttribute( XML_name ) )
305 m_aCustomPropertyName = xAttribs->getValue( XML_name );
307 else if ( m_nState && m_nInBlock && m_nInBlock == 2 && getNamespace( nElement ) == NMSP_officeDocPropsVT )
309 m_nType = nElement;
311 else
313 OSL_FAIL( "For now unexpected tags are ignored!" );
316 if ( m_nInBlock == SAL_MAX_INT32 )
317 throw uno::RuntimeException();
319 m_nInBlock++;
322 void SAL_CALL OOXMLDocPropHandler::startUnknownElement( const OUString& aNamespace, const OUString& aName, const uno::Reference< xml::sax::XFastAttributeList >& )
323 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
325 SAL_WARN("oox", "Unknown element " << aNamespace << ":" << aName);
327 if ( m_nInBlock == SAL_MAX_INT32 )
328 throw uno::RuntimeException();
330 m_nInBlock++;
333 void SAL_CALL OOXMLDocPropHandler::endFastElement( ::sal_Int32 )
334 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
336 if ( m_nInBlock )
338 m_nInBlock--;
340 if ( !m_nInBlock )
341 m_nState = 0;
342 else if ( m_nInBlock == 1 )
344 m_nBlock = 0;
345 m_aCustomPropertyName.clear();
347 else if ( m_nInBlock == 2 )
348 m_nType = 0;
352 void SAL_CALL OOXMLDocPropHandler::endUnknownElement( const OUString&, const OUString& )
353 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
355 if ( m_nInBlock )
356 m_nInBlock--;
359 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createFastChildContext( ::sal_Int32, const uno::Reference< xml::sax::XFastAttributeList >& )
360 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
362 // Should the arguments be parsed?
363 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
366 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createUnknownChildContext( const OUString&, const OUString&, const uno::Reference< xml::sax::XFastAttributeList >& )
367 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
369 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
372 void SAL_CALL OOXMLDocPropHandler::characters( const OUString& aChars )
373 throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
377 if ( (m_nInBlock == 2) || ((m_nInBlock == 3) && m_nType) )
379 if ( m_nState == COREPR_TOKEN( coreProperties ) )
381 switch( m_nBlock )
383 case COREPR_TOKEN( category ):
384 m_aCustomPropertyName = "category";
385 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
386 break;
388 case COREPR_TOKEN( contentStatus ):
389 m_aCustomPropertyName = "contentStatus";
390 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
391 break;
393 case COREPR_TOKEN( contentType ):
394 m_aCustomPropertyName = "contentType";
395 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
396 break;
398 case COREPR_TOKEN( identifier ):
399 m_aCustomPropertyName = "identifier";
400 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
401 break;
403 case COREPR_TOKEN( version ):
404 m_aCustomPropertyName = "version";
405 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
406 break;
408 case DCT_TOKEN( created ):
409 if ( aChars.getLength() >= 4 )
410 m_xDocProp->setCreationDate( GetDateTimeFromW3CDTF( aChars ) );
411 break;
413 case DC_TOKEN( creator ):
414 m_xDocProp->setAuthor( aChars );
415 break;
417 case DC_TOKEN( description ):
418 m_xDocProp->setDescription( aChars );
419 break;
421 case COREPR_TOKEN( keywords ):
422 m_xDocProp->setKeywords( GetKeywordsSet( aChars ) );
423 break;
425 case DC_TOKEN( language ):
426 if ( aChars.getLength() >= 2 )
427 m_xDocProp->setLanguage( LanguageTag::convertToLocale( aChars) );
428 break;
430 case COREPR_TOKEN( lastModifiedBy ):
431 m_xDocProp->setModifiedBy( aChars );
432 break;
434 case COREPR_TOKEN( lastPrinted ):
435 if ( aChars.getLength() >= 4 )
436 m_xDocProp->setPrintDate( GetDateTimeFromW3CDTF( aChars ) );
437 break;
439 case DCT_TOKEN( modified ):
440 if ( aChars.getLength() >= 4 )
441 m_xDocProp->setModificationDate( GetDateTimeFromW3CDTF( aChars ) );
442 break;
444 case COREPR_TOKEN( revision ):
447 m_xDocProp->setEditingCycles(
448 static_cast<sal_Int16>(aChars.toInt32()) );
450 catch (lang::IllegalArgumentException &)
452 // ignore
454 break;
456 case DC_TOKEN( subject ):
457 m_xDocProp->setSubject( m_xDocProp->getSubject() + aChars );
458 break;
460 case DC_TOKEN( title ):
461 m_xDocProp->setTitle( m_xDocProp->getTitle() + aChars );
462 break;
464 default:
465 OSL_FAIL( "Unexpected core property!" );
468 else if ( m_nState == EXTPR_TOKEN( Properties ) )
470 switch( m_nBlock )
472 case EXTPR_TOKEN( Application ):
473 m_xDocProp->setGenerator( aChars );
474 break;
476 case EXTPR_TOKEN( Template ):
477 m_xDocProp->setTemplateName( aChars );
478 break;
480 case EXTPR_TOKEN( TotalTime ):
483 // The TotalTime is in mins as per ECMA specification.
484 m_xDocProp->setEditingDuration( aChars.toInt32() * 60 );
486 catch (lang::IllegalArgumentException &)
488 // ignore
490 break;
492 case EXTPR_TOKEN( Characters ):
493 case EXTPR_TOKEN( Pages ):
494 case EXTPR_TOKEN( Words ):
495 case EXTPR_TOKEN( Paragraphs ):
496 UpdateDocStatistic( aChars );
497 break;
499 case EXTPR_TOKEN( HyperlinksChanged ):
500 m_aCustomPropertyName = "HyperlinksChanged";
501 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
502 break;
504 case EXTPR_TOKEN( LinksUpToDate ):
505 m_aCustomPropertyName = "LinksUpToDate";
506 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
507 break;
509 case EXTPR_TOKEN( ScaleCrop ):
510 m_aCustomPropertyName = "ScaleCrop";
511 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
512 break;
514 case EXTPR_TOKEN( SharedDoc ):
515 m_aCustomPropertyName = "ShareDoc";
516 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
517 break;
519 case EXTPR_TOKEN( DocSecurity ):
520 m_aCustomPropertyName = "DocSecurity";
521 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
522 break;
524 case EXTPR_TOKEN( HiddenSlides ):
525 m_aCustomPropertyName = "HiddenSlides";
526 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
527 break;
529 case EXTPR_TOKEN( MMClips ):
530 m_aCustomPropertyName = "MMClips";
531 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
532 break;
534 case EXTPR_TOKEN( Notes ):
535 m_aCustomPropertyName = "Notes";
536 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
537 break;
539 case EXTPR_TOKEN( Slides ):
540 m_aCustomPropertyName = "Slides";
541 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
542 break;
544 case EXTPR_TOKEN( AppVersion ):
545 m_aCustomPropertyName = "AppVersion";
546 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
547 break;
549 case EXTPR_TOKEN( Company ):
550 m_aCustomPropertyName = "Company";
551 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
552 break;
554 case EXTPR_TOKEN( HyperlinkBase ):
555 m_aCustomPropertyName = "HyperlinkBase";
556 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
557 break;
559 case EXTPR_TOKEN( Manager ):
560 m_aCustomPropertyName = "Manager";
561 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
562 break;
564 case EXTPR_TOKEN( PresentationFormat ):
565 m_aCustomPropertyName = "PresentationFormat";
566 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
567 break;
569 case EXTPR_TOKEN( CharactersWithSpaces ):
570 case EXTPR_TOKEN( Lines ):
571 case EXTPR_TOKEN( DigSig ):
572 case EXTPR_TOKEN( HeadingPairs ):
573 case EXTPR_TOKEN( HLinks ):
574 case EXTPR_TOKEN( TitlesOfParts ):
575 // ignored during the import currently
576 break;
578 default:
579 OSL_FAIL( "Unexpected extended property!" );
582 else if ( m_nState == CUSTPR_TOKEN( Properties ) )
584 if ( m_nBlock == CUSTPR_TOKEN( property ) )
586 // this is a custom property
587 switch( m_nType )
589 case VT_TOKEN( bool ):
590 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) );
591 break;
593 case VT_TOKEN( bstr ):
594 case VT_TOKEN( lpstr ):
595 case VT_TOKEN( lpwstr ):
596 // the property has string type
597 AddCustomProperty( uno::makeAny( AttributeConversion::decodeXString( aChars ) ) );
598 break;
600 case VT_TOKEN( date ):
601 case VT_TOKEN( filetime ):
602 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars ) ) );
603 break;
605 case VT_TOKEN( i1 ):
606 case VT_TOKEN( i2 ):
607 AddCustomProperty( uno::makeAny( (sal_Int16)aChars.toInt32() ) );
608 break;
610 case VT_TOKEN( i4 ):
611 case VT_TOKEN( int ):
612 AddCustomProperty( uno::makeAny( aChars.toInt32() ) );
613 break;
615 case VT_TOKEN( i8 ):
616 AddCustomProperty( uno::makeAny( aChars.toInt64() ) );
617 break;
619 case VT_TOKEN( r4 ):
620 AddCustomProperty( uno::makeAny( aChars.toFloat() ) );
621 break;
623 case VT_TOKEN( r8 ):
624 AddCustomProperty( uno::makeAny( aChars.toDouble() ) );
625 break;
627 default:
628 // all the other types are ignored;
629 break;
632 else
634 OSL_FAIL( "Unexpected tag in custom property!" );
639 catch( uno::RuntimeException& )
641 throw;
643 catch( xml::sax::SAXException& )
645 throw;
647 catch( uno::Exception& e )
649 throw xml::sax::SAXException(
650 OUString("Error while setting document property!"),
651 uno::Reference< uno::XInterface >(),
652 uno::makeAny( e ) );
656 } // namespace docprop
657 } // namespace oox
659 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */