Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / docprop / docprophandler.cxx
blob2582707745950f74bffe66d660a6360d594768fb
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 sal_Int32 nInd = 0;
253 for ( auto pProp = aSet.getConstArray(); nInd < aSet.getLength(); ++nInd )
254 if ( pProp[nInd].Name == aName )
255 break;
257 if (nInd == aSet.getLength())
258 aSet.realloc( nInd + 1 );
260 beans::NamedValue aProp;
261 aProp.Name = aName;
262 aProp.Value <<= aChars.toInt32();
264 aSet[nInd] = aProp;
266 m_xDocProp->setDocumentStatistics( aSet );
270 // com.sun.star.xml.sax.XFastDocumentHandler
272 void SAL_CALL OOXMLDocPropHandler::startDocument()
276 void SAL_CALL OOXMLDocPropHandler::endDocument()
278 InitNew();
281 void OOXMLDocPropHandler::processingInstruction( const OUString& /*rTarget*/, const OUString& /*rData*/ )
285 void SAL_CALL OOXMLDocPropHandler::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& )
289 // com.sun.star.xml.sax.XFastContextHandler
291 void SAL_CALL OOXMLDocPropHandler::startFastElement( ::sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttribs )
293 if ( !m_nInBlock && !m_nState )
295 if ( nElement == COREPR_TOKEN( coreProperties )
296 || nElement == EXTPR_TOKEN( Properties )
297 || nElement == CUSTPR_TOKEN( Properties ) )
299 m_nState = nElement;
301 else
303 OSL_FAIL( "Unexpected file format!" );
306 else if ( m_nState && m_nInBlock == 1 ) // that tag should contain the property name
308 // Currently the attributes are ignored for the core properties since the only
309 // known attribute is xsi:type that can only be used with dcterms:created and
310 // dcterms:modified, and this element is allowed currently to have only one value dcterms:W3CDTF
311 m_nBlock = nElement;
313 if ( xAttribs.is() && xAttribs->hasAttribute( XML_name ) )
314 m_aCustomPropertyName = xAttribs->getValue( XML_name );
316 else if ( m_nState && m_nInBlock == 2 && getNamespace( nElement ) == NMSP_officeDocPropsVT )
318 m_nType = nElement;
320 else
322 SAL_WARN("oox", "OOXMLDocPropHandler::startFastElement: unknown element " << getBaseToken(nElement));
325 if ( m_nInBlock == SAL_MAX_INT32 )
326 throw uno::RuntimeException();
328 m_nInBlock++;
331 void SAL_CALL OOXMLDocPropHandler::startUnknownElement( const OUString& aNamespace, const OUString& aName, const uno::Reference< xml::sax::XFastAttributeList >& )
333 SAL_WARN("oox", "Unknown element " << aNamespace << ":" << aName);
335 if ( m_nInBlock == SAL_MAX_INT32 )
336 throw uno::RuntimeException();
338 m_nInBlock++;
341 void SAL_CALL OOXMLDocPropHandler::endFastElement( ::sal_Int32 )
343 if ( m_nInBlock )
345 m_nInBlock--;
347 if ( !m_nInBlock )
348 m_nState = 0;
349 else if ( m_nInBlock == 1 )
351 m_nBlock = 0;
352 m_aCustomPropertyName.clear();
354 else if ( m_nInBlock == 2 )
356 if ( m_nState == CUSTPR_TOKEN(Properties)
357 && m_nBlock == CUSTPR_TOKEN(property))
359 switch (m_nType)
361 case VT_TOKEN(bstr):
362 case VT_TOKEN(lpstr):
363 case VT_TOKEN(lpwstr):
364 if (!m_aCustomPropertyName.isEmpty() &&
365 INSERTED != m_CustomStringPropertyState)
367 // the property has string type, so it is valid
368 // even with an empty value - characters() has
369 // not been called in that case
370 AddCustomProperty(uno::makeAny(OUString()));
372 break;
375 m_CustomStringPropertyState = NONE;
376 m_nType = 0;
381 void SAL_CALL OOXMLDocPropHandler::endUnknownElement( const OUString&, const OUString& )
383 if ( m_nInBlock )
384 m_nInBlock--;
387 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createFastChildContext( ::sal_Int32, const uno::Reference< xml::sax::XFastAttributeList >& )
389 // Should the arguments be parsed?
390 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
393 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL OOXMLDocPropHandler::createUnknownChildContext( const OUString&, const OUString&, const uno::Reference< xml::sax::XFastAttributeList >& )
395 return uno::Reference< xml::sax::XFastContextHandler >( static_cast< xml::sax::XFastContextHandler* >( this ) );
398 void SAL_CALL OOXMLDocPropHandler::characters( const OUString& aChars )
402 if ( (m_nInBlock == 2) || ((m_nInBlock == 3) && m_nType) )
404 if ( m_nState == COREPR_TOKEN( coreProperties ) )
406 switch( m_nBlock )
408 case COREPR_TOKEN( category ):
409 m_aCustomPropertyName = "category";
410 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
411 break;
413 case COREPR_TOKEN( contentStatus ):
414 m_aCustomPropertyName = "contentStatus";
415 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
416 break;
418 case COREPR_TOKEN( contentType ):
419 m_aCustomPropertyName = "contentType";
420 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
421 break;
423 case COREPR_TOKEN( identifier ):
424 m_aCustomPropertyName = "identifier";
425 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
426 break;
428 case COREPR_TOKEN( version ):
429 m_aCustomPropertyName = "version";
430 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
431 break;
433 case DCT_TOKEN( created ):
434 if ( aChars.getLength() >= 4 )
435 m_xDocProp->setCreationDate( GetDateTimeFromW3CDTF( aChars ) );
436 break;
438 case DC_TOKEN( creator ):
439 m_xDocProp->setAuthor( aChars );
440 break;
442 case DC_TOKEN( description ):
443 m_xDocProp->setDescription( aChars );
444 break;
446 case COREPR_TOKEN( keywords ):
447 m_xDocProp->setKeywords( GetKeywordsSet( aChars ) );
448 break;
450 case DC_TOKEN( language ):
451 if ( aChars.getLength() >= 2 )
452 m_xDocProp->setLanguage( LanguageTag::convertToLocale( aChars) );
453 break;
455 case COREPR_TOKEN( lastModifiedBy ):
456 m_xDocProp->setModifiedBy( aChars );
457 break;
459 case COREPR_TOKEN( lastPrinted ):
460 if ( aChars.getLength() >= 4 )
461 m_xDocProp->setPrintDate( GetDateTimeFromW3CDTF( aChars ) );
462 break;
464 case DCT_TOKEN( modified ):
465 if ( aChars.getLength() >= 4 )
466 m_xDocProp->setModificationDate( GetDateTimeFromW3CDTF( aChars ) );
467 break;
469 case COREPR_TOKEN( revision ):
472 m_xDocProp->setEditingCycles(
473 static_cast<sal_Int16>(aChars.toInt32()) );
475 catch (lang::IllegalArgumentException &)
477 // ignore
479 break;
481 case DC_TOKEN( subject ):
482 m_xDocProp->setSubject( m_xDocProp->getSubject() + aChars );
483 break;
485 case DC_TOKEN( title ):
486 m_xDocProp->setTitle( m_xDocProp->getTitle() + aChars );
487 break;
489 default:
490 OSL_FAIL( "Unexpected core property!" );
493 else if ( m_nState == EXTPR_TOKEN( Properties ) )
495 switch( m_nBlock )
497 case EXTPR_TOKEN( Application ):
498 m_xDocProp->setGenerator( aChars );
499 break;
501 case EXTPR_TOKEN( Template ):
502 m_xDocProp->setTemplateName( aChars );
503 break;
505 case EXTPR_TOKEN( TotalTime ):
507 sal_Int32 nDuration;
508 if (!o3tl::checked_multiply<sal_Int32>(aChars.toInt32(), 60, nDuration))
512 // The TotalTime is in mins as per ECMA specification.
513 m_xDocProp->setEditingDuration(nDuration);
515 catch (const lang::IllegalArgumentException&)
517 // ignore
520 break;
522 case EXTPR_TOKEN( Characters ):
523 case EXTPR_TOKEN( CharactersWithSpaces ):
524 case EXTPR_TOKEN( Pages ):
525 case EXTPR_TOKEN( Words ):
526 case EXTPR_TOKEN( Paragraphs ):
527 UpdateDocStatistic( aChars );
528 break;
530 case EXTPR_TOKEN( HyperlinksChanged ):
531 m_aCustomPropertyName = "HyperlinksChanged";
532 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
533 break;
535 case EXTPR_TOKEN( LinksUpToDate ):
536 m_aCustomPropertyName = "LinksUpToDate";
537 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
538 break;
540 case EXTPR_TOKEN( ScaleCrop ):
541 m_aCustomPropertyName = "ScaleCrop";
542 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
543 break;
545 case EXTPR_TOKEN( SharedDoc ):
546 m_aCustomPropertyName = "ShareDoc";
547 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
548 break;
550 case EXTPR_TOKEN( DocSecurity ):
551 m_aCustomPropertyName = "DocSecurity";
552 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
553 break;
555 case EXTPR_TOKEN( HiddenSlides ):
556 m_aCustomPropertyName = "HiddenSlides";
557 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
558 break;
560 case EXTPR_TOKEN( MMClips ):
561 m_aCustomPropertyName = "MMClips";
562 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
563 break;
565 case EXTPR_TOKEN( Notes ):
566 m_aCustomPropertyName = "Notes";
567 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
568 break;
570 case EXTPR_TOKEN( Slides ):
571 m_aCustomPropertyName = "Slides";
572 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
573 break;
575 case EXTPR_TOKEN( AppVersion ):
576 m_aCustomPropertyName = "AppVersion";
577 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
578 break;
580 case EXTPR_TOKEN( Company ):
581 m_aCustomPropertyName = "Company";
582 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
583 break;
585 case EXTPR_TOKEN( HyperlinkBase ):
586 m_aCustomPropertyName = "HyperlinkBase";
587 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
588 break;
590 case EXTPR_TOKEN( Manager ):
591 m_aCustomPropertyName = "Manager";
592 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
593 break;
595 case EXTPR_TOKEN( PresentationFormat ):
596 m_aCustomPropertyName = "PresentationFormat";
597 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
598 break;
600 case EXTPR_TOKEN( Lines ):
601 case EXTPR_TOKEN( DigSig ):
602 case EXTPR_TOKEN( HeadingPairs ):
603 case EXTPR_TOKEN( HLinks ):
604 case EXTPR_TOKEN( TitlesOfParts ):
605 // ignored during the import currently
606 break;
608 default:
609 OSL_FAIL( "Unexpected extended property!" );
612 else if ( m_nState == CUSTPR_TOKEN( Properties ) )
614 if ( m_nBlock == CUSTPR_TOKEN( property ) )
616 // this is a custom property
617 switch( m_nType )
619 case VT_TOKEN( bool ):
620 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) );
621 break;
623 case VT_TOKEN( bstr ):
624 case VT_TOKEN( lpstr ):
625 case VT_TOKEN( lpwstr ):
626 // the property has string type
627 AddCustomProperty( uno::makeAny( AttributeConversion::decodeXString( aChars ) ) );
628 m_CustomStringPropertyState = INSERTED;
629 break;
631 case VT_TOKEN( date ):
632 case VT_TOKEN( filetime ):
633 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars ) ) );
634 break;
636 case VT_TOKEN( i1 ):
637 case VT_TOKEN( i2 ):
638 AddCustomProperty( uno::makeAny( static_cast<sal_Int16>(aChars.toInt32()) ) );
639 break;
641 case VT_TOKEN( i4 ):
642 case VT_TOKEN( int ):
643 AddCustomProperty( uno::makeAny( aChars.toInt32() ) );
644 break;
646 case VT_TOKEN( i8 ):
647 AddCustomProperty( uno::makeAny( aChars.toInt64() ) );
648 break;
650 case VT_TOKEN( r4 ):
651 AddCustomProperty( uno::makeAny( aChars.toFloat() ) );
652 break;
654 case VT_TOKEN( r8 ):
655 AddCustomProperty( uno::makeAny( aChars.toDouble() ) );
656 break;
658 default:
659 // all the other types are ignored;
660 break;
663 else
665 OSL_FAIL( "Unexpected tag in custom property!" );
670 catch( uno::RuntimeException& )
672 throw;
674 catch( xml::sax::SAXException& )
676 throw;
678 catch( uno::Exception& )
680 css::uno::Any anyEx = cppu::getCaughtException();
681 throw xml::sax::SAXException(
682 "Error while setting document property!",
683 uno::Reference< uno::XInterface >(),
684 anyEx );
688 } // namespace docprop
689 } // namespace oox
691 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */