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 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();
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()
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
) )
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
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
)
325 SAL_WARN("oox", "OOXMLDocPropHandler::startFastElement: unknown element " << getBaseToken(nElement
));
328 if ( m_nInBlock
== SAL_MAX_INT32
)
329 throw uno::RuntimeException();
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();
344 void SAL_CALL
OOXMLDocPropHandler::endFastElement( ::sal_Int32
)
352 else if ( m_nInBlock
== 1 )
355 m_aCustomPropertyName
.clear();
357 else if ( m_nInBlock
== 2 )
359 if ( m_nState
== CUSTPR_TOKEN(Properties
)
360 && m_nBlock
== CUSTPR_TOKEN(property
))
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()));
378 m_CustomStringPropertyState
= NONE
;
384 void SAL_CALL
OOXMLDocPropHandler::endUnknownElement( const OUString
&, const OUString
& )
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
) )
411 case COREPR_TOKEN( category
):
412 m_aCustomPropertyName
= "category";
413 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
416 case COREPR_TOKEN( contentStatus
):
417 m_aCustomPropertyName
= "contentStatus";
418 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
421 case COREPR_TOKEN( contentType
):
422 m_aCustomPropertyName
= "contentType";
423 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
426 case COREPR_TOKEN( identifier
):
427 m_aCustomPropertyName
= "identifier";
428 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
431 case COREPR_TOKEN( version
):
432 m_aCustomPropertyName
= "version";
433 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
436 case DCT_TOKEN( created
):
437 if ( aChars
.getLength() >= 4 )
438 m_xDocProp
->setCreationDate( GetDateTimeFromW3CDTF( aChars
) );
441 case DC_TOKEN( creator
):
442 m_xDocProp
->setAuthor( aChars
);
445 case DC_TOKEN( description
):
446 m_xDocProp
->setDescription( aChars
);
449 case COREPR_TOKEN( keywords
):
450 m_xDocProp
->setKeywords( GetKeywordsSet( aChars
) );
453 case DC_TOKEN( language
):
454 if ( aChars
.getLength() >= 2 )
455 m_xDocProp
->setLanguage( LanguageTag::convertToLocale( aChars
) );
458 case COREPR_TOKEN( lastModifiedBy
):
459 m_xDocProp
->setModifiedBy( aChars
);
462 case COREPR_TOKEN( lastPrinted
):
463 if ( aChars
.getLength() >= 4 )
464 m_xDocProp
->setPrintDate( GetDateTimeFromW3CDTF( aChars
) );
467 case DCT_TOKEN( modified
):
468 if ( aChars
.getLength() >= 4 )
469 m_xDocProp
->setModificationDate( GetDateTimeFromW3CDTF( aChars
) );
472 case COREPR_TOKEN( revision
):
475 m_xDocProp
->setEditingCycles(
476 static_cast<sal_Int16
>(aChars
.toInt32()) );
478 catch (lang::IllegalArgumentException
&)
484 case DC_TOKEN( subject
):
485 m_xDocProp
->setSubject( m_xDocProp
->getSubject() + aChars
);
488 case DC_TOKEN( title
):
489 m_xDocProp
->setTitle( m_xDocProp
->getTitle() + aChars
);
493 OSL_FAIL( "Unexpected core property!" );
496 else if ( m_nState
== EXTPR_TOKEN( Properties
) )
500 case EXTPR_TOKEN( Application
):
501 m_xDocProp
->setGenerator( aChars
);
504 case EXTPR_TOKEN( Template
):
505 m_xDocProp
->setTemplateName( aChars
);
508 case EXTPR_TOKEN( TotalTime
):
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
&)
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
);
533 case EXTPR_TOKEN( HyperlinksChanged
):
534 m_aCustomPropertyName
= "HyperlinksChanged";
535 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
538 case EXTPR_TOKEN( LinksUpToDate
):
539 m_aCustomPropertyName
= "LinksUpToDate";
540 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
543 case EXTPR_TOKEN( ScaleCrop
):
544 m_aCustomPropertyName
= "ScaleCrop";
545 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
548 case EXTPR_TOKEN( SharedDoc
):
549 m_aCustomPropertyName
= "ShareDoc";
550 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
553 case EXTPR_TOKEN( DocSecurity
):
554 m_aCustomPropertyName
= "DocSecurity";
555 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
558 case EXTPR_TOKEN( HiddenSlides
):
559 m_aCustomPropertyName
= "HiddenSlides";
560 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
563 case EXTPR_TOKEN( MMClips
):
564 m_aCustomPropertyName
= "MMClips";
565 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
568 case EXTPR_TOKEN( Notes
):
569 m_aCustomPropertyName
= "Notes";
570 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
573 case EXTPR_TOKEN( Slides
):
574 m_aCustomPropertyName
= "Slides";
575 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
578 case EXTPR_TOKEN( AppVersion
):
579 m_aCustomPropertyName
= "AppVersion";
580 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
583 case EXTPR_TOKEN( Company
):
584 m_aCustomPropertyName
= "Company";
585 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
588 case EXTPR_TOKEN( HyperlinkBase
):
589 m_aCustomPropertyName
= "HyperlinkBase";
590 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
593 case EXTPR_TOKEN( Manager
):
594 m_aCustomPropertyName
= "Manager";
595 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
598 case EXTPR_TOKEN( PresentationFormat
):
599 m_aCustomPropertyName
= "PresentationFormat";
600 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
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
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
622 case VT_TOKEN( bool ):
623 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) );
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
;
634 case VT_TOKEN( date
):
635 case VT_TOKEN( filetime
):
636 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars
) ) );
641 AddCustomProperty( uno::makeAny( static_cast<sal_Int16
>(aChars
.toInt32()) ) );
645 case VT_TOKEN( int ):
646 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) );
650 AddCustomProperty( uno::makeAny( aChars
.toInt64() ) );
654 AddCustomProperty( uno::makeAny( aChars
.toFloat() ) );
658 AddCustomProperty( uno::makeAny( aChars
.toDouble() ) );
662 // all the other types are ignored;
668 OSL_FAIL( "Unexpected tag in custom property!" );
673 catch( uno::RuntimeException
& )
677 catch( xml::sax::SAXException
& )
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
>(),
691 } // namespace docprop
694 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */