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/Locale.hpp>
27 #include <osl/diagnose.h>
28 #include <i18nlangtag/languagetag.hxx>
31 #include <boost/algorithm/string.hpp>
33 #include "oox/helper/attributelist.hxx"
35 using namespace ::com::sun::star
;
40 OOXMLDocPropHandler::OOXMLDocPropHandler( const uno::Reference
< uno::XComponentContext
>& xContext
,
41 const uno::Reference
< document::XDocumentProperties
>& rDocProp
)
42 : m_xContext( xContext
)
43 , m_xDocProp( rDocProp
)
49 if ( !xContext
.is() || !rDocProp
.is() )
50 throw uno::RuntimeException();
53 OOXMLDocPropHandler::~OOXMLDocPropHandler()
57 void OOXMLDocPropHandler::InitNew()
61 m_aCustomPropertyName
.clear();
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();
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();
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();
117 if ( nLen
>= 20 && aChars
[19] == (sal_Unicode
)'.' )
120 sal_Int32 digitPos
= 20;
121 while (nLen
> digitPos
&& digitPos
< 29)
123 sal_Unicode c
= aChars
[digitPos
];
124 if ( c
< '0' || c
> '9')
126 aOslDTime
.NanoSeconds
*= 10;
127 aOslDTime
.NanoSeconds
+= c
- '0';
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;
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')
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
)'-' )
170 // convert to UTC time
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
);
207 return uno::Sequence
< OUString
>();
210 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString
& aChars
)
212 uno::Sequence
< beans::NamedValue
> aSet
= m_xDocProp
->getDocumentStatistics();
217 case EXTPR_TOKEN( Characters
):
218 aName
= "CharacterCount";
221 case EXTPR_TOKEN( Pages
):
225 case EXTPR_TOKEN( Words
):
229 case EXTPR_TOKEN( Paragraphs
):
230 aName
= "ParagraphCount";
234 OSL_FAIL( "Unexpected statistic!" );
238 if ( !aName
.isEmpty() )
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() );
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
)
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
) )
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
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
)
313 OSL_FAIL( "For now unexpected tags are ignored!" );
316 if ( m_nInBlock
== SAL_MAX_INT32
)
317 throw uno::RuntimeException();
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();
333 void SAL_CALL
OOXMLDocPropHandler::endFastElement( ::sal_Int32
)
334 throw (xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
342 else if ( m_nInBlock
== 1 )
345 m_aCustomPropertyName
.clear();
347 else if ( m_nInBlock
== 2 )
352 void SAL_CALL
OOXMLDocPropHandler::endUnknownElement( const OUString
&, const OUString
& )
353 throw (xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
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
) )
383 case COREPR_TOKEN( category
):
384 m_aCustomPropertyName
= "category";
385 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
388 case COREPR_TOKEN( contentStatus
):
389 m_aCustomPropertyName
= "contentStatus";
390 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
393 case COREPR_TOKEN( contentType
):
394 m_aCustomPropertyName
= "contentType";
395 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
398 case COREPR_TOKEN( identifier
):
399 m_aCustomPropertyName
= "identifier";
400 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
403 case COREPR_TOKEN( version
):
404 m_aCustomPropertyName
= "version";
405 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
408 case DCT_TOKEN( created
):
409 if ( aChars
.getLength() >= 4 )
410 m_xDocProp
->setCreationDate( GetDateTimeFromW3CDTF( aChars
) );
413 case DC_TOKEN( creator
):
414 m_xDocProp
->setAuthor( aChars
);
417 case DC_TOKEN( description
):
418 m_xDocProp
->setDescription( aChars
);
421 case COREPR_TOKEN( keywords
):
422 m_xDocProp
->setKeywords( GetKeywordsSet( aChars
) );
425 case DC_TOKEN( language
):
426 if ( aChars
.getLength() >= 2 )
427 m_xDocProp
->setLanguage( LanguageTag::convertToLocale( aChars
) );
430 case COREPR_TOKEN( lastModifiedBy
):
431 m_xDocProp
->setModifiedBy( aChars
);
434 case COREPR_TOKEN( lastPrinted
):
435 if ( aChars
.getLength() >= 4 )
436 m_xDocProp
->setPrintDate( GetDateTimeFromW3CDTF( aChars
) );
439 case DCT_TOKEN( modified
):
440 if ( aChars
.getLength() >= 4 )
441 m_xDocProp
->setModificationDate( GetDateTimeFromW3CDTF( aChars
) );
444 case COREPR_TOKEN( revision
):
447 m_xDocProp
->setEditingCycles(
448 static_cast<sal_Int16
>(aChars
.toInt32()) );
450 catch (lang::IllegalArgumentException
&)
456 case DC_TOKEN( subject
):
457 m_xDocProp
->setSubject( m_xDocProp
->getSubject() + aChars
);
460 case DC_TOKEN( title
):
461 m_xDocProp
->setTitle( m_xDocProp
->getTitle() + aChars
);
465 OSL_FAIL( "Unexpected core property!" );
468 else if ( m_nState
== EXTPR_TOKEN( Properties
) )
472 case EXTPR_TOKEN( Application
):
473 m_xDocProp
->setGenerator( aChars
);
476 case EXTPR_TOKEN( Template
):
477 m_xDocProp
->setTemplateName( aChars
);
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
&)
492 case EXTPR_TOKEN( Characters
):
493 case EXTPR_TOKEN( Pages
):
494 case EXTPR_TOKEN( Words
):
495 case EXTPR_TOKEN( Paragraphs
):
496 UpdateDocStatistic( aChars
);
499 case EXTPR_TOKEN( HyperlinksChanged
):
500 m_aCustomPropertyName
= "HyperlinksChanged";
501 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
504 case EXTPR_TOKEN( LinksUpToDate
):
505 m_aCustomPropertyName
= "LinksUpToDate";
506 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
509 case EXTPR_TOKEN( ScaleCrop
):
510 m_aCustomPropertyName
= "ScaleCrop";
511 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
514 case EXTPR_TOKEN( SharedDoc
):
515 m_aCustomPropertyName
= "ShareDoc";
516 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
519 case EXTPR_TOKEN( DocSecurity
):
520 m_aCustomPropertyName
= "DocSecurity";
521 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
524 case EXTPR_TOKEN( HiddenSlides
):
525 m_aCustomPropertyName
= "HiddenSlides";
526 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
529 case EXTPR_TOKEN( MMClips
):
530 m_aCustomPropertyName
= "MMClips";
531 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
534 case EXTPR_TOKEN( Notes
):
535 m_aCustomPropertyName
= "Notes";
536 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
539 case EXTPR_TOKEN( Slides
):
540 m_aCustomPropertyName
= "Slides";
541 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
544 case EXTPR_TOKEN( AppVersion
):
545 m_aCustomPropertyName
= "AppVersion";
546 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
549 case EXTPR_TOKEN( Company
):
550 m_aCustomPropertyName
= "Company";
551 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
554 case EXTPR_TOKEN( HyperlinkBase
):
555 m_aCustomPropertyName
= "HyperlinkBase";
556 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
559 case EXTPR_TOKEN( Manager
):
560 m_aCustomPropertyName
= "Manager";
561 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
564 case EXTPR_TOKEN( PresentationFormat
):
565 m_aCustomPropertyName
= "PresentationFormat";
566 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
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
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
589 case VT_TOKEN( bool ):
590 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) );
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
) ) );
600 case VT_TOKEN( date
):
601 case VT_TOKEN( filetime
):
602 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars
) ) );
607 AddCustomProperty( uno::makeAny( (sal_Int16
)aChars
.toInt32() ) );
611 case VT_TOKEN( int ):
612 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) );
616 AddCustomProperty( uno::makeAny( aChars
.toInt64() ) );
620 AddCustomProperty( uno::makeAny( aChars
.toFloat() ) );
624 AddCustomProperty( uno::makeAny( aChars
.toDouble() ) );
628 // all the other types are ignored;
634 OSL_FAIL( "Unexpected tag in custom property!" );
639 catch( uno::RuntimeException
& )
643 catch( xml::sax::SAXException
& )
647 catch( uno::Exception
& e
)
649 throw xml::sax::SAXException(
650 OUString("Error while setting document property!"),
651 uno::Reference
< uno::XInterface
>(),
656 } // namespace docprop
659 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */