bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / docprop / docprophandler.cxx
blobcf3ce7b5d48eb305e72710d6e8bc928919416389
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/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/Locale.hpp>
26 #include <com/sun/star/xml/sax/SAXException.hpp>
27 #include <cppuhelper/exc_hlp.hxx>
29 #include <o3tl/safeint.hxx>
30 #include <osl/time.h>
31 #include <osl/diagnose.h>
32 #include <sal/log.hxx>
33 #include <i18nlangtag/languagetag.hxx>
35 #include <vector>
36 #include <boost/algorithm/string.hpp>
38 #include <oox/helper/attributelist.hxx>
40 using namespace ::com::sun::star;
42 namespace oox {
43 namespace docprop {
45 OOXMLDocPropHandler::OOXMLDocPropHandler( const uno::Reference< uno::XComponentContext >& xContext,
46 const uno::Reference< document::XDocumentProperties >& rDocProp )
47 : m_xDocProp( rDocProp )
48 , m_nState( 0 )
49 , m_nBlock( 0 )
50 , m_nType( 0 )
51 , m_nInBlock( 0 )
52 , m_CustomStringPropertyState(NONE)
54 if ( !xContext.is() || !rDocProp.is() )
55 throw uno::RuntimeException();
58 OOXMLDocPropHandler::~OOXMLDocPropHandler()
62 void OOXMLDocPropHandler::InitNew()
64 m_nState = 0;
65 m_nBlock = 0;
66 m_aCustomPropertyName.clear();
67 m_nType = 0;
68 m_nInBlock = 0;
69 m_CustomStringPropertyState = NONE;
72 void OOXMLDocPropHandler::AddCustomProperty( const uno::Any& aAny )
74 if ( !m_aCustomPropertyName.isEmpty() )
76 const uno::Reference< beans::XPropertyContainer > xUserProps =
77 m_xDocProp->getUserDefinedProperties();
78 if ( !xUserProps.is() )
79 throw uno::RuntimeException();
81 try
83 xUserProps->addProperty( m_aCustomPropertyName,
84 beans::PropertyAttribute::REMOVABLE, aAny );
86 catch( beans::PropertyExistException& )
88 // conflicts with core and extended properties are possible
90 catch( uno::Exception& )
92 OSL_FAIL( "Can not add custom property!" );
97 util::DateTime OOXMLDocPropHandler::GetDateTimeFromW3CDTF( const OUString& aChars )
99 oslDateTime aOslDTime = { 0, 0, 0, 0, 0, 0, 0, 0 };
100 const sal_Int32 nLen = aChars.getLength();
101 if ( nLen >= 4 )
103 aOslDTime.Year = static_cast<sal_Int16>(aChars.copy( 0, 4 ).toInt32());
105 if ( nLen >= 7 && aChars[4] == '-' )
107 aOslDTime.Month = static_cast<sal_uInt16>(aChars.copy( 5, 2 ).toInt32());
109 if ( nLen >= 10 && aChars[7] == '-' )
111 aOslDTime.Day = static_cast<sal_uInt16>(aChars.copy( 8, 2 ).toInt32());
113 if ( nLen >= 16 && aChars[10] == 'T' && aChars[13] == ':' )
115 aOslDTime.Hours = static_cast<sal_uInt16>(aChars.copy( 11, 2 ).toInt32());
116 aOslDTime.Minutes = static_cast<sal_uInt16>(aChars.copy( 14, 2 ).toInt32());
118 sal_Int32 nOptTime = 0;
119 if ( nLen >= 19 && aChars[16] == ':' )
121 aOslDTime.Seconds = static_cast<sal_uInt16>(aChars.copy( 17, 2 ).toInt32());
122 nOptTime += 3;
123 if ( nLen >= 20 && aChars[19] == '.' )
125 nOptTime += 1;
126 sal_Int32 digitPos = 20;
127 while (nLen > digitPos && digitPos < 29)
129 sal_Unicode c = aChars[digitPos];
130 if ( c < '0' || c > '9')
131 break;
132 aOslDTime.NanoSeconds *= 10;
133 aOslDTime.NanoSeconds += c - '0';
134 ++digitPos;
136 if ( digitPos < 29 )
138 // read less digits than 9
139 // add correct exponent of 10
140 nOptTime += digitPos - 20;
141 for(; digitPos<29; ++digitPos)
143 aOslDTime.NanoSeconds *= 10;
146 else
148 //skip digits with more precision than we can handle
149 while(nLen > digitPos)
151 sal_Unicode c = aChars[digitPos];
152 if ( c < '0' || c > '9')
153 break;
154 ++digitPos;
156 nOptTime += digitPos - 20;
161 sal_Int32 nModif = 0;
162 if ( nLen >= 16 + nOptTime + 6 )
164 if ( ( aChars[16 + nOptTime] == '+' || aChars[16 + nOptTime] == '-' )
165 && aChars[16 + nOptTime + 3] == ':' )
167 nModif = aChars.copy( 16 + nOptTime + 1, 2 ).toInt32() * 3600;
168 nModif += aChars.copy( 16 + nOptTime + 4, 2 ).toInt32() * 60;
169 if ( aChars[16 + nOptTime] == '-' )
170 nModif *= -1;
174 if ( nModif )
176 // convert to UTC time
177 TimeValue aTmp;
178 if ( osl_getTimeValueFromDateTime( &aOslDTime, &aTmp ) )
180 aTmp.Seconds -= nModif;
181 osl_getDateTimeFromTimeValue( &aTmp, &aOslDTime );
189 return util::DateTime( aOslDTime.NanoSeconds, aOslDTime.Seconds,
190 aOslDTime.Minutes, aOslDTime.Hours,
191 aOslDTime.Day, aOslDTime.Month, aOslDTime.Year, false);
194 uno::Sequence< OUString > OOXMLDocPropHandler::GetKeywordsSet( const OUString& aChars )
196 if ( !aChars.isEmpty() )
198 std::string aUtf8Chars = OUStringToOString( aChars, RTL_TEXTENCODING_UTF8 ).getStr();
199 std::vector<std::string> aUtf8Result;
200 boost::split( aUtf8Result, aUtf8Chars, boost::is_any_of(" ,;:\t"), boost::token_compress_on );
202 if (!aUtf8Result.empty())
204 uno::Sequence< OUString > aResult( aUtf8Result.size() );
205 OUString* pResultValues = aResult.getArray();
206 for (auto const& elem : aUtf8Result)
208 *pResultValues = OUString( elem.c_str(), static_cast< sal_Int32 >( elem.size() ),RTL_TEXTENCODING_UTF8 );
209 ++pResultValues;
212 return aResult;
215 return uno::Sequence< OUString >();
218 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString& aChars )
220 uno::Sequence< beans::NamedValue > aSet = m_xDocProp->getDocumentStatistics();
221 OUString aName;
223 switch( m_nBlock )
225 case EXTPR_TOKEN( Characters ):
226 aName = "NonWhitespaceCharacterCount";
227 break;
229 case EXTPR_TOKEN( CharactersWithSpaces ):
230 aName = "CharacterCount";
231 break;
233 case EXTPR_TOKEN( Pages ):
234 aName = "PageCount";
235 break;
237 case EXTPR_TOKEN( Words ):
238 aName = "WordCount";
239 break;
241 case EXTPR_TOKEN( Paragraphs ):
242 aName = "ParagraphCount";
243 break;
245 default:
246 OSL_FAIL( "Unexpected statistic!" );
247 break;
250 if ( !aName.isEmpty() )
252 bool bFound = false;
253 sal_Int32 nLen = aSet.getLength();
254 for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
255 if ( aSet[nInd].Name == aName )
257 aSet[nInd].Value <<= aChars.toInt32();
258 bFound = true;
259 break;
262 if ( !bFound )
264 aSet.realloc( nLen + 1 );
265 aSet[nLen].Name = aName;
266 aSet[nLen].Value <<= aChars.toInt32();
269 m_xDocProp->setDocumentStatistics( aSet );
273 // com.sun.star.xml.sax.XFastDocumentHandler
275 void SAL_CALL OOXMLDocPropHandler::startDocument()
279 void SAL_CALL OOXMLDocPropHandler::endDocument()
281 InitNew();
284 void OOXMLDocPropHandler::processingInstruction( const OUString& /*rTarget*/, const OUString& /*rData*/ )
288 void SAL_CALL OOXMLDocPropHandler::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& )
292 // com.sun.star.xml.sax.XFastContextHandler
294 void SAL_CALL OOXMLDocPropHandler::startFastElement( ::sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttribs )
296 if ( !m_nInBlock && !m_nState )
298 if ( nElement == COREPR_TOKEN( coreProperties )
299 || nElement == EXTPR_TOKEN( Properties )
300 || nElement == CUSTPR_TOKEN( Properties ) )
302 m_nState = nElement;
304 else
306 OSL_FAIL( "Unexpected file format!" );
309 else if ( m_nState && m_nInBlock == 1 ) // that tag should contain the property name
311 // Currently the attributes are ignored for the core properties since the only
312 // known attribute is xsi:type that can only be used with dcterms:created and
313 // dcterms:modified, and this element is allowed currently to have only one value dcterms:W3CDTF
314 m_nBlock = nElement;
316 if ( xAttribs.is() && xAttribs->hasAttribute( XML_name ) )
317 m_aCustomPropertyName = xAttribs->getValue( XML_name );
319 else if ( m_nState && m_nInBlock == 2 && getNamespace( nElement ) == NMSP_officeDocPropsVT )
321 m_nType = nElement;
323 else
325 SAL_WARN("oox", "OOXMLDocPropHandler::startFastElement: unknown element " << getBaseToken(nElement));
328 if ( m_nInBlock == SAL_MAX_INT32 )
329 throw uno::RuntimeException();
331 m_nInBlock++;
334 void SAL_CALL OOXMLDocPropHandler::startUnknownElement( const OUString& aNamespace, const OUString& aName, const uno::Reference< xml::sax::XFastAttributeList >& )
336 SAL_WARN("oox", "Unknown element " << aNamespace << ":" << aName);
338 if ( m_nInBlock == SAL_MAX_INT32 )
339 throw uno::RuntimeException();
341 m_nInBlock++;
344 void SAL_CALL OOXMLDocPropHandler::endFastElement( ::sal_Int32 )
346 if ( m_nInBlock )
348 m_nInBlock--;
350 if ( !m_nInBlock )
351 m_nState = 0;
352 else if ( m_nInBlock == 1 )
354 m_nBlock = 0;
355 m_aCustomPropertyName.clear();
357 else if ( m_nInBlock == 2 )
359 if ( m_nState == CUSTPR_TOKEN(Properties)
360 && m_nBlock == CUSTPR_TOKEN(property))
362 switch (m_nType)
364 case VT_TOKEN(bstr):
365 case VT_TOKEN(lpstr):
366 case VT_TOKEN(lpwstr):
367 if (!m_aCustomPropertyName.isEmpty() &&
368 INSERTED != m_CustomStringPropertyState)
370 // the property has string type, so it is valid
371 // even with an empty value - characters() has
372 // not been called in that case
373 AddCustomProperty(uno::makeAny(OUString()));
375 break;
378 m_CustomStringPropertyState = NONE;
379 m_nType = 0;
384 void SAL_CALL OOXMLDocPropHandler::endUnknownElement( const OUString&, const OUString& )
386 if ( m_nInBlock )
387 m_nInBlock--;
390 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createFastChildContext( ::sal_Int32, const uno::Reference< xml::sax::XFastAttributeList >& )
392 // Should the arguments be parsed?
393 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
396 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createUnknownChildContext( const OUString&, const OUString&, const uno::Reference< xml::sax::XFastAttributeList >& )
398 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
401 void SAL_CALL OOXMLDocPropHandler::characters( const OUString& aChars )
405 if ( (m_nInBlock == 2) || ((m_nInBlock == 3) && m_nType) )
407 if ( m_nState == COREPR_TOKEN( coreProperties ) )
409 switch( m_nBlock )
411 case COREPR_TOKEN( category ):
412 m_aCustomPropertyName = "category";
413 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
414 break;
416 case COREPR_TOKEN( contentStatus ):
417 m_aCustomPropertyName = "contentStatus";
418 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
419 break;
421 case COREPR_TOKEN( contentType ):
422 m_aCustomPropertyName = "contentType";
423 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
424 break;
426 case COREPR_TOKEN( identifier ):
427 m_aCustomPropertyName = "identifier";
428 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
429 break;
431 case COREPR_TOKEN( version ):
432 m_aCustomPropertyName = "version";
433 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
434 break;
436 case DCT_TOKEN( created ):
437 if ( aChars.getLength() >= 4 )
438 m_xDocProp->setCreationDate( GetDateTimeFromW3CDTF( aChars ) );
439 break;
441 case DC_TOKEN( creator ):
442 m_xDocProp->setAuthor( aChars );
443 break;
445 case DC_TOKEN( description ):
446 m_xDocProp->setDescription( aChars );
447 break;
449 case COREPR_TOKEN( keywords ):
450 m_xDocProp->setKeywords( GetKeywordsSet( aChars ) );
451 break;
453 case DC_TOKEN( language ):
454 if ( aChars.getLength() >= 2 )
455 m_xDocProp->setLanguage( LanguageTag::convertToLocale( aChars) );
456 break;
458 case COREPR_TOKEN( lastModifiedBy ):
459 m_xDocProp->setModifiedBy( aChars );
460 break;
462 case COREPR_TOKEN( lastPrinted ):
463 if ( aChars.getLength() >= 4 )
464 m_xDocProp->setPrintDate( GetDateTimeFromW3CDTF( aChars ) );
465 break;
467 case DCT_TOKEN( modified ):
468 if ( aChars.getLength() >= 4 )
469 m_xDocProp->setModificationDate( GetDateTimeFromW3CDTF( aChars ) );
470 break;
472 case COREPR_TOKEN( revision ):
475 m_xDocProp->setEditingCycles(
476 static_cast<sal_Int16>(aChars.toInt32()) );
478 catch (lang::IllegalArgumentException &)
480 // ignore
482 break;
484 case DC_TOKEN( subject ):
485 m_xDocProp->setSubject( m_xDocProp->getSubject() + aChars );
486 break;
488 case DC_TOKEN( title ):
489 m_xDocProp->setTitle( m_xDocProp->getTitle() + aChars );
490 break;
492 default:
493 OSL_FAIL( "Unexpected core property!" );
496 else if ( m_nState == EXTPR_TOKEN( Properties ) )
498 switch( m_nBlock )
500 case EXTPR_TOKEN( Application ):
501 m_xDocProp->setGenerator( aChars );
502 break;
504 case EXTPR_TOKEN( Template ):
505 m_xDocProp->setTemplateName( aChars );
506 break;
508 case EXTPR_TOKEN( TotalTime ):
510 sal_Int32 nDuration;
511 if (!o3tl::checked_multiply<sal_Int32>(aChars.toInt32(), 60, nDuration))
515 // The TotalTime is in mins as per ECMA specification.
516 m_xDocProp->setEditingDuration(nDuration);
518 catch (const lang::IllegalArgumentException&)
520 // ignore
523 break;
525 case EXTPR_TOKEN( Characters ):
526 case EXTPR_TOKEN( CharactersWithSpaces ):
527 case EXTPR_TOKEN( Pages ):
528 case EXTPR_TOKEN( Words ):
529 case EXTPR_TOKEN( Paragraphs ):
530 UpdateDocStatistic( aChars );
531 break;
533 case EXTPR_TOKEN( HyperlinksChanged ):
534 m_aCustomPropertyName = "HyperlinksChanged";
535 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
536 break;
538 case EXTPR_TOKEN( LinksUpToDate ):
539 m_aCustomPropertyName = "LinksUpToDate";
540 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
541 break;
543 case EXTPR_TOKEN( ScaleCrop ):
544 m_aCustomPropertyName = "ScaleCrop";
545 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
546 break;
548 case EXTPR_TOKEN( SharedDoc ):
549 m_aCustomPropertyName = "ShareDoc";
550 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
551 break;
553 case EXTPR_TOKEN( DocSecurity ):
554 m_aCustomPropertyName = "DocSecurity";
555 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
556 break;
558 case EXTPR_TOKEN( HiddenSlides ):
559 m_aCustomPropertyName = "HiddenSlides";
560 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
561 break;
563 case EXTPR_TOKEN( MMClips ):
564 m_aCustomPropertyName = "MMClips";
565 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
566 break;
568 case EXTPR_TOKEN( Notes ):
569 m_aCustomPropertyName = "Notes";
570 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
571 break;
573 case EXTPR_TOKEN( Slides ):
574 m_aCustomPropertyName = "Slides";
575 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
576 break;
578 case EXTPR_TOKEN( AppVersion ):
579 m_aCustomPropertyName = "AppVersion";
580 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
581 break;
583 case EXTPR_TOKEN( Company ):
584 m_aCustomPropertyName = "Company";
585 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
586 break;
588 case EXTPR_TOKEN( HyperlinkBase ):
589 m_aCustomPropertyName = "HyperlinkBase";
590 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
591 break;
593 case EXTPR_TOKEN( Manager ):
594 m_aCustomPropertyName = "Manager";
595 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
596 break;
598 case EXTPR_TOKEN( PresentationFormat ):
599 m_aCustomPropertyName = "PresentationFormat";
600 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
601 break;
603 case EXTPR_TOKEN( Lines ):
604 case EXTPR_TOKEN( DigSig ):
605 case EXTPR_TOKEN( HeadingPairs ):
606 case EXTPR_TOKEN( HLinks ):
607 case EXTPR_TOKEN( TitlesOfParts ):
608 // ignored during the import currently
609 break;
611 default:
612 OSL_FAIL( "Unexpected extended property!" );
615 else if ( m_nState == CUSTPR_TOKEN( Properties ) )
617 if ( m_nBlock == CUSTPR_TOKEN( property ) )
619 // this is a custom property
620 switch( m_nType )
622 case VT_TOKEN( bool ):
623 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) );
624 break;
626 case VT_TOKEN( bstr ):
627 case VT_TOKEN( lpstr ):
628 case VT_TOKEN( lpwstr ):
629 // the property has string type
630 AddCustomProperty( uno::makeAny( AttributeConversion::decodeXString( aChars ) ) );
631 m_CustomStringPropertyState = INSERTED;
632 break;
634 case VT_TOKEN( date ):
635 case VT_TOKEN( filetime ):
636 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars ) ) );
637 break;
639 case VT_TOKEN( i1 ):
640 case VT_TOKEN( i2 ):
641 AddCustomProperty( uno::makeAny( static_cast<sal_Int16>(aChars.toInt32()) ) );
642 break;
644 case VT_TOKEN( i4 ):
645 case VT_TOKEN( int ):
646 AddCustomProperty( uno::makeAny( aChars.toInt32() ) );
647 break;
649 case VT_TOKEN( i8 ):
650 AddCustomProperty( uno::makeAny( aChars.toInt64() ) );
651 break;
653 case VT_TOKEN( r4 ):
654 AddCustomProperty( uno::makeAny( aChars.toFloat() ) );
655 break;
657 case VT_TOKEN( r8 ):
658 AddCustomProperty( uno::makeAny( aChars.toDouble() ) );
659 break;
661 default:
662 // all the other types are ignored;
663 break;
666 else
668 OSL_FAIL( "Unexpected tag in custom property!" );
673 catch( uno::RuntimeException& )
675 throw;
677 catch( xml::sax::SAXException& )
679 throw;
681 catch( uno::Exception& )
683 css::uno::Any anyEx = cppu::getCaughtException();
684 throw xml::sax::SAXException(
685 "Error while setting document property!",
686 uno::Reference< uno::XInterface >(),
687 anyEx );
691 } // namespace docprop
692 } // namespace oox
694 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */