1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "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>
31 #include <osl/diagnose.h>
32 #include <sal/log.hxx>
33 #include <i18nlangtag/languagetag.hxx>
36 #include <boost/algorithm/string.hpp>
38 #include <oox/helper/attributelist.hxx>
40 using namespace ::com::sun::star
;
45 OOXMLDocPropHandler::OOXMLDocPropHandler( const uno::Reference
< uno::XComponentContext
>& xContext
,
46 const uno::Reference
< document::XDocumentProperties
>& rDocProp
)
47 : m_xDocProp( rDocProp
)
52 , m_CustomStringPropertyState(NONE
)
54 if ( !xContext
.is() || !rDocProp
.is() )
55 throw uno::RuntimeException();
58 OOXMLDocPropHandler::~OOXMLDocPropHandler()
62 void OOXMLDocPropHandler::InitNew()
66 m_aCustomPropertyName
.clear();
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();
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();
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());
123 if ( nLen
>= 20 && aChars
[19] == '.' )
126 sal_Int32 digitPos
= 20;
127 while (nLen
> digitPos
&& digitPos
< 29)
129 sal_Unicode c
= aChars
[digitPos
];
130 if ( c
< '0' || c
> '9')
132 aOslDTime
.NanoSeconds
*= 10;
133 aOslDTime
.NanoSeconds
+= c
- '0';
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;
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')
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
] == '-' )
176 // convert to UTC time
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
);
215 return uno::Sequence
< OUString
>();
218 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString
& aChars
)
220 uno::Sequence
< beans::NamedValue
> aSet
= m_xDocProp
->getDocumentStatistics();
225 case EXTPR_TOKEN( Characters
):
226 aName
= "NonWhitespaceCharacterCount";
229 case EXTPR_TOKEN( CharactersWithSpaces
):
230 aName
= "CharacterCount";
233 case EXTPR_TOKEN( Pages
):
237 case EXTPR_TOKEN( Words
):
241 case EXTPR_TOKEN( Paragraphs
):
242 aName
= "ParagraphCount";
246 OSL_FAIL( "Unexpected statistic!" );
250 if ( !aName
.isEmpty() )
253 for ( auto pProp
= aSet
.getConstArray(); nInd
< aSet
.getLength(); ++nInd
)
254 if ( pProp
[nInd
].Name
== aName
)
257 if (nInd
== aSet
.getLength())
258 aSet
.realloc( nInd
+ 1 );
260 beans::NamedValue aProp
;
262 aProp
.Value
<<= aChars
.toInt32();
266 m_xDocProp
->setDocumentStatistics( aSet
);
270 // com.sun.star.xml.sax.XFastDocumentHandler
272 void SAL_CALL
OOXMLDocPropHandler::startDocument()
276 void SAL_CALL
OOXMLDocPropHandler::endDocument()
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
) )
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
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
)
322 SAL_WARN("oox", "OOXMLDocPropHandler::startFastElement: unknown element " << getBaseToken(nElement
));
325 if ( m_nInBlock
== SAL_MAX_INT32
)
326 throw uno::RuntimeException();
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();
341 void SAL_CALL
OOXMLDocPropHandler::endFastElement( ::sal_Int32
)
349 else if ( m_nInBlock
== 1 )
352 m_aCustomPropertyName
.clear();
354 else if ( m_nInBlock
== 2 )
356 if ( m_nState
== CUSTPR_TOKEN(Properties
)
357 && m_nBlock
== CUSTPR_TOKEN(property
))
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()));
375 m_CustomStringPropertyState
= NONE
;
381 void SAL_CALL
OOXMLDocPropHandler::endUnknownElement( const OUString
&, const OUString
& )
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
) )
408 case COREPR_TOKEN( category
):
409 m_aCustomPropertyName
= "category";
410 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
413 case COREPR_TOKEN( contentStatus
):
414 m_aCustomPropertyName
= "contentStatus";
415 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
418 case COREPR_TOKEN( contentType
):
419 m_aCustomPropertyName
= "contentType";
420 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
423 case COREPR_TOKEN( identifier
):
424 m_aCustomPropertyName
= "identifier";
425 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
428 case COREPR_TOKEN( version
):
429 m_aCustomPropertyName
= "version";
430 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
433 case DCT_TOKEN( created
):
434 if ( aChars
.getLength() >= 4 )
435 m_xDocProp
->setCreationDate( GetDateTimeFromW3CDTF( aChars
) );
438 case DC_TOKEN( creator
):
439 m_xDocProp
->setAuthor( aChars
);
442 case DC_TOKEN( description
):
443 m_xDocProp
->setDescription( aChars
);
446 case COREPR_TOKEN( keywords
):
447 m_xDocProp
->setKeywords( GetKeywordsSet( aChars
) );
450 case DC_TOKEN( language
):
451 if ( aChars
.getLength() >= 2 )
452 m_xDocProp
->setLanguage( LanguageTag::convertToLocale( aChars
) );
455 case COREPR_TOKEN( lastModifiedBy
):
456 m_xDocProp
->setModifiedBy( aChars
);
459 case COREPR_TOKEN( lastPrinted
):
460 if ( aChars
.getLength() >= 4 )
461 m_xDocProp
->setPrintDate( GetDateTimeFromW3CDTF( aChars
) );
464 case DCT_TOKEN( modified
):
465 if ( aChars
.getLength() >= 4 )
466 m_xDocProp
->setModificationDate( GetDateTimeFromW3CDTF( aChars
) );
469 case COREPR_TOKEN( revision
):
472 m_xDocProp
->setEditingCycles(
473 static_cast<sal_Int16
>(aChars
.toInt32()) );
475 catch (lang::IllegalArgumentException
&)
481 case DC_TOKEN( subject
):
482 m_xDocProp
->setSubject( m_xDocProp
->getSubject() + aChars
);
485 case DC_TOKEN( title
):
486 m_xDocProp
->setTitle( m_xDocProp
->getTitle() + aChars
);
490 OSL_FAIL( "Unexpected core property!" );
493 else if ( m_nState
== EXTPR_TOKEN( Properties
) )
497 case EXTPR_TOKEN( Application
):
498 m_xDocProp
->setGenerator( aChars
);
501 case EXTPR_TOKEN( Template
):
502 m_xDocProp
->setTemplateName( aChars
);
505 case EXTPR_TOKEN( TotalTime
):
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
&)
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
);
530 case EXTPR_TOKEN( HyperlinksChanged
):
531 m_aCustomPropertyName
= "HyperlinksChanged";
532 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
535 case EXTPR_TOKEN( LinksUpToDate
):
536 m_aCustomPropertyName
= "LinksUpToDate";
537 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
540 case EXTPR_TOKEN( ScaleCrop
):
541 m_aCustomPropertyName
= "ScaleCrop";
542 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
545 case EXTPR_TOKEN( SharedDoc
):
546 m_aCustomPropertyName
= "ShareDoc";
547 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
550 case EXTPR_TOKEN( DocSecurity
):
551 m_aCustomPropertyName
= "DocSecurity";
552 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
555 case EXTPR_TOKEN( HiddenSlides
):
556 m_aCustomPropertyName
= "HiddenSlides";
557 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
560 case EXTPR_TOKEN( MMClips
):
561 m_aCustomPropertyName
= "MMClips";
562 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
565 case EXTPR_TOKEN( Notes
):
566 m_aCustomPropertyName
= "Notes";
567 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
570 case EXTPR_TOKEN( Slides
):
571 m_aCustomPropertyName
= "Slides";
572 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
575 case EXTPR_TOKEN( AppVersion
):
576 m_aCustomPropertyName
= "AppVersion";
577 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
580 case EXTPR_TOKEN( Company
):
581 m_aCustomPropertyName
= "Company";
582 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
585 case EXTPR_TOKEN( HyperlinkBase
):
586 m_aCustomPropertyName
= "HyperlinkBase";
587 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
590 case EXTPR_TOKEN( Manager
):
591 m_aCustomPropertyName
= "Manager";
592 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
595 case EXTPR_TOKEN( PresentationFormat
):
596 m_aCustomPropertyName
= "PresentationFormat";
597 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
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
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
619 case VT_TOKEN( bool ):
620 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) );
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
;
631 case VT_TOKEN( date
):
632 case VT_TOKEN( filetime
):
633 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars
) ) );
638 AddCustomProperty( uno::makeAny( static_cast<sal_Int16
>(aChars
.toInt32()) ) );
642 case VT_TOKEN( int ):
643 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) );
647 AddCustomProperty( uno::makeAny( aChars
.toInt64() ) );
651 AddCustomProperty( uno::makeAny( aChars
.toFloat() ) );
655 AddCustomProperty( uno::makeAny( aChars
.toDouble() ) );
659 // all the other types are ignored;
665 OSL_FAIL( "Unexpected tag in custom property!" );
670 catch( uno::RuntimeException
& )
674 catch( xml::sax::SAXException
& )
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
>(),
688 } // namespace docprop
691 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */