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 <i18nlangtag/languagetag.hxx>
30 #include <boost/algorithm/string.hpp>
32 #include "oox/helper/attributelist.hxx"
34 using namespace ::com::sun::star
;
39 // ------------------------------------------------
40 OOXMLDocPropHandler::OOXMLDocPropHandler( const uno::Reference
< uno::XComponentContext
>& xContext
,
41 const uno::Reference
< document::XDocumentProperties
> xDocProp
)
42 : m_xContext( xContext
)
43 , m_xDocProp( xDocProp
)
49 if ( !xContext
.is() || !xDocProp
.is() )
50 throw uno::RuntimeException();
53 // ------------------------------------------------
54 OOXMLDocPropHandler::~OOXMLDocPropHandler()
58 // ------------------------------------------------
59 void OOXMLDocPropHandler::InitNew()
63 m_aCustomPropertyName
= "";
68 // ------------------------------------------------
69 void OOXMLDocPropHandler::AddCustomProperty( const uno::Any
& aAny
)
71 if ( !m_aCustomPropertyName
.isEmpty() )
73 const uno::Reference
< beans::XPropertyContainer
> xUserProps
=
74 m_xDocProp
->getUserDefinedProperties();
75 if ( !xUserProps
.is() )
76 throw uno::RuntimeException();
80 xUserProps
->addProperty( m_aCustomPropertyName
,
81 beans::PropertyAttribute::REMOVABLE
, aAny
);
83 catch( beans::PropertyExistException
& )
85 // conflicts with core and extended properties are possible
87 catch( uno::Exception
& )
89 OSL_FAIL( "Can not add custom property!" );
94 // ------------------------------------------------
95 util::DateTime
OOXMLDocPropHandler::GetDateTimeFromW3CDTF( const OUString
& aChars
)
97 oslDateTime aOslDTime
= { 0, 0, 0, 0, 0, 0, 0, 0 };
98 const sal_Int32 nLen
= aChars
.getLength();
101 aOslDTime
.Year
= (sal_Int16
)aChars
.copy( 0, 4 ).toInt32();
103 if ( nLen
>= 7 && aChars
[4] == (sal_Unicode
)'-' )
105 aOslDTime
.Month
= (sal_uInt16
)aChars
.copy( 5, 2 ).toInt32();
107 if ( nLen
>= 10 && aChars
[7] == (sal_Unicode
)'-' )
109 aOslDTime
.Day
= (sal_uInt16
)aChars
.copy( 8, 2 ).toInt32();
111 if ( nLen
>= 16 && aChars
[10] == (sal_Unicode
)'T' && aChars
[13] == (sal_Unicode
)':' )
113 aOslDTime
.Hours
= (sal_uInt16
)aChars
.copy( 11, 2 ).toInt32();
114 aOslDTime
.Minutes
= (sal_uInt16
)aChars
.copy( 14, 2 ).toInt32();
116 sal_Int32 nOptTime
= 0;
117 if ( nLen
>= 19 && aChars
[16] == (sal_Unicode
)':' )
119 aOslDTime
.Seconds
= (sal_uInt16
)aChars
.copy( 17, 2 ).toInt32();
121 if ( nLen
>= 20 && aChars
[19] == (sal_Unicode
)'.' )
124 sal_Int32 digitPos
= 20;
125 while (nLen
> digitPos
&& digitPos
< 29)
127 sal_Unicode c
= aChars
[digitPos
];
128 if ( c
< '0' || c
> '9')
130 aOslDTime
.NanoSeconds
*= 10;
131 aOslDTime
.NanoSeconds
+= c
- '0';
136 // read less digits than 9
137 // add correct exponent of 10
138 nOptTime
+= digitPos
- 20;
139 for(; digitPos
<29; ++digitPos
)
141 aOslDTime
.NanoSeconds
*= 10;
146 //skip digits with more precision than we can handle
147 while(nLen
> digitPos
)
149 sal_Unicode c
= aChars
[digitPos
];
150 if ( c
< '0' || c
> '9')
154 nOptTime
+= digitPos
- 20;
159 sal_Int32 nModif
= 0;
160 if ( nLen
>= 16 + nOptTime
+ 6 )
162 if ( ( aChars
[16 + nOptTime
] == (sal_Unicode
)'+' || aChars
[16 + nOptTime
] == (sal_Unicode
)'-' )
163 && aChars
[16 + nOptTime
+ 3] == (sal_Unicode
)':' )
165 nModif
= aChars
.copy( 16 + nOptTime
+ 1, 2 ).toInt32() * 3600;
166 nModif
+= aChars
.copy( 16 + nOptTime
+ 4, 2 ).toInt32() * 60;
167 if ( aChars
[16 + nOptTime
] == (sal_Unicode
)'-' )
174 // convert to UTC time
176 if ( osl_getTimeValueFromDateTime( &aOslDTime
, &aTmp
) )
178 aTmp
.Seconds
-= nModif
;
179 osl_getDateTimeFromTimeValue( &aTmp
, &aOslDTime
);
187 return util::DateTime( aOslDTime
.NanoSeconds
, aOslDTime
.Seconds
,
188 aOslDTime
.Minutes
, aOslDTime
.Hours
,
189 aOslDTime
.Day
, aOslDTime
.Month
, aOslDTime
.Year
, false);
192 // ------------------------------------------------
193 uno::Sequence
< OUString
> OOXMLDocPropHandler::GetKeywordsSet( const OUString
& aChars
)
195 if ( !aChars
.isEmpty() )
197 std::string aUtf8Chars
= OUStringToOString( aChars
, RTL_TEXTENCODING_UTF8
).getStr();
198 std::vector
<std::string
> aUtf8Result
;
199 boost::split( aUtf8Result
, aUtf8Chars
, boost::is_any_of(" ,;:\t"), boost::token_compress_on
);
201 if (!aUtf8Result
.empty())
203 uno::Sequence
< OUString
> aResult( aUtf8Result
.size() );
204 OUString
* pResultValues
= aResult
.getArray();
205 for ( std::vector
< std::string
>::const_iterator i
= aUtf8Result
.begin();
206 i
!= aUtf8Result
.end(); ++i
, ++pResultValues
)
207 *pResultValues
= OUString( i
->c_str(), static_cast< sal_Int32
>( i
->size() ),RTL_TEXTENCODING_UTF8
);
212 return uno::Sequence
< OUString
>();
215 // ------------------------------------------------
216 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString
& aChars
)
218 uno::Sequence
< beans::NamedValue
> aSet
= m_xDocProp
->getDocumentStatistics();
223 case EXTPR_TOKEN( Characters
):
224 aName
= "CharacterCount";
227 case EXTPR_TOKEN( Pages
):
231 case EXTPR_TOKEN( Words
):
235 case EXTPR_TOKEN( Paragraphs
):
236 aName
= "ParagraphCount";
240 OSL_FAIL( "Unexpected statistic!" );
244 if ( !aName
.isEmpty() )
246 sal_Bool bFound
= sal_False
;
247 sal_Int32 nLen
= aSet
.getLength();
248 for ( sal_Int32 nInd
= 0; nInd
< nLen
; nInd
++ )
249 if ( aSet
[nInd
].Name
.equals( aName
) )
251 aSet
[nInd
].Value
= uno::makeAny( aChars
.toInt32() );
258 aSet
.realloc( nLen
+ 1 );
259 aSet
[nLen
].Name
= aName
;
260 aSet
[nLen
].Value
= uno::makeAny( aChars
.toInt32() );
263 m_xDocProp
->setDocumentStatistics( aSet
);
267 // ------------------------------------------------
268 // com.sun.star.xml.sax.XFastDocumentHandler
269 // ------------------------------------------------
270 void SAL_CALL
OOXMLDocPropHandler::startDocument()
271 throw (xml::sax::SAXException
, uno::RuntimeException
)
275 // ------------------------------------------------
276 void SAL_CALL
OOXMLDocPropHandler::endDocument()
277 throw (xml::sax::SAXException
, uno::RuntimeException
)
282 // ------------------------------------------------
283 void SAL_CALL
OOXMLDocPropHandler::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& )
284 throw (xml::sax::SAXException
, uno::RuntimeException
)
289 // com.sun.star.xml.sax.XFastContextHandler
290 // ------------------------------------------------
291 void SAL_CALL
OOXMLDocPropHandler::startFastElement( ::sal_Int32 nElement
, const uno::Reference
< xml::sax::XFastAttributeList
>& xAttribs
)
292 throw (xml::sax::SAXException
, uno::RuntimeException
)
294 if ( !m_nInBlock
&& !m_nState
)
296 if ( nElement
== COREPR_TOKEN( coreProperties
)
297 || nElement
== EXTPR_TOKEN( Properties
)
298 || nElement
== CUSTPR_TOKEN( Properties
) )
304 OSL_FAIL( "Unexpected file format!" );
307 else if ( m_nState
&& m_nInBlock
== 1 ) // that tag should contain the property name
309 // Currently the attributes are ignored for the core properties since the only
310 // known attribute is xsi:type that can only be used with dcterms:created and
311 // dcterms:modified, and this element is allowed currently to have only one value dcterms:W3CDTF
314 if ( xAttribs
.is() && xAttribs
->hasAttribute( XML_name
) )
315 m_aCustomPropertyName
= xAttribs
->getValue( XML_name
);
317 else if ( m_nState
&& m_nInBlock
&& m_nInBlock
== 2 && getNamespace( nElement
) == NMSP_officeDocPropsVT
)
323 OSL_FAIL( "For now unexpected tags are ignored!" );
326 if ( m_nInBlock
== SAL_MAX_INT32
)
327 throw uno::RuntimeException();
332 // ------------------------------------------------
333 void SAL_CALL
OOXMLDocPropHandler::startUnknownElement( const OUString
& aNamespace
, const OUString
& aName
, const uno::Reference
< xml::sax::XFastAttributeList
>& )
334 throw (xml::sax::SAXException
, uno::RuntimeException
)
336 SAL_WARN("oox", "Unknown element " << aNamespace
<< ":" << aName
);
338 if ( m_nInBlock
== SAL_MAX_INT32
)
339 throw uno::RuntimeException();
344 // ------------------------------------------------
345 void SAL_CALL
OOXMLDocPropHandler::endFastElement( ::sal_Int32
)
346 throw (xml::sax::SAXException
, uno::RuntimeException
)
354 else if ( m_nInBlock
== 1 )
357 m_aCustomPropertyName
= "";
359 else if ( m_nInBlock
== 2 )
364 // ------------------------------------------------
365 void SAL_CALL
OOXMLDocPropHandler::endUnknownElement( const OUString
&, const OUString
& )
366 throw (xml::sax::SAXException
, uno::RuntimeException
)
372 // ------------------------------------------------
373 uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
OOXMLDocPropHandler::createFastChildContext( ::sal_Int32
, const uno::Reference
< xml::sax::XFastAttributeList
>& )
374 throw (xml::sax::SAXException
, uno::RuntimeException
)
376 // Should the arguments be parsed?
377 return uno::Reference
< xml::sax::XFastContextHandler
>( static_cast< xml::sax::XFastContextHandler
* >( this ) );
380 // ------------------------------------------------
381 uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
OOXMLDocPropHandler::createUnknownChildContext( const OUString
&, const OUString
&, const uno::Reference
< xml::sax::XFastAttributeList
>& )
382 throw (xml::sax::SAXException
, uno::RuntimeException
)
384 return uno::Reference
< xml::sax::XFastContextHandler
>( static_cast< xml::sax::XFastContextHandler
* >( this ) );
387 // ------------------------------------------------
388 void SAL_CALL
OOXMLDocPropHandler::characters( const OUString
& aChars
)
389 throw (xml::sax::SAXException
, uno::RuntimeException
)
393 if ( (m_nInBlock
== 2) || ((m_nInBlock
== 3) && m_nType
) )
395 if ( m_nState
== COREPR_TOKEN( coreProperties
) )
399 case COREPR_TOKEN( category
):
400 m_aCustomPropertyName
= "category";
401 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
404 case COREPR_TOKEN( contentStatus
):
405 m_aCustomPropertyName
= "contentStatus";
406 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
409 case COREPR_TOKEN( contentType
):
410 m_aCustomPropertyName
= "contentType";
411 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
414 case COREPR_TOKEN( identifier
):
415 m_aCustomPropertyName
= "identifier";
416 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
419 case COREPR_TOKEN( version
):
420 m_aCustomPropertyName
= "version";
421 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
424 case DCT_TOKEN( created
):
425 if ( aChars
.getLength() >= 4 )
426 m_xDocProp
->setCreationDate( GetDateTimeFromW3CDTF( aChars
) );
429 case DC_TOKEN( creator
):
430 m_xDocProp
->setAuthor( aChars
);
433 case DC_TOKEN( description
):
434 m_xDocProp
->setDescription( aChars
);
437 case COREPR_TOKEN( keywords
):
438 m_xDocProp
->setKeywords( GetKeywordsSet( aChars
) );
441 case DC_TOKEN( language
):
442 if ( aChars
.getLength() >= 2 )
443 m_xDocProp
->setLanguage( LanguageTag::convertToLocale( aChars
) );
446 case COREPR_TOKEN( lastModifiedBy
):
447 m_xDocProp
->setModifiedBy( aChars
);
450 case COREPR_TOKEN( lastPrinted
):
451 if ( aChars
.getLength() >= 4 )
452 m_xDocProp
->setPrintDate( GetDateTimeFromW3CDTF( aChars
) );
455 case DCT_TOKEN( modified
):
456 if ( aChars
.getLength() >= 4 )
457 m_xDocProp
->setModificationDate( GetDateTimeFromW3CDTF( aChars
) );
460 case COREPR_TOKEN( revision
):
463 m_xDocProp
->setEditingCycles(
464 static_cast<sal_Int16
>(aChars
.toInt32()) );
466 catch (lang::IllegalArgumentException
&)
472 case DC_TOKEN( subject
):
473 m_xDocProp
->setSubject( m_xDocProp
->getSubject() + aChars
);
476 case DC_TOKEN( title
):
477 m_xDocProp
->setTitle( m_xDocProp
->getTitle() + aChars
);
481 OSL_FAIL( "Unexpected core property!" );
484 else if ( m_nState
== EXTPR_TOKEN( Properties
) )
488 case EXTPR_TOKEN( Application
):
489 m_xDocProp
->setGenerator( aChars
);
492 case EXTPR_TOKEN( Template
):
493 m_xDocProp
->setTemplateName( aChars
);
496 case EXTPR_TOKEN( TotalTime
):
499 m_xDocProp
->setEditingDuration( aChars
.toInt32() );
501 catch (lang::IllegalArgumentException
&)
507 case EXTPR_TOKEN( Characters
):
508 case EXTPR_TOKEN( Pages
):
509 case EXTPR_TOKEN( Words
):
510 case EXTPR_TOKEN( Paragraphs
):
511 UpdateDocStatistic( aChars
);
514 case EXTPR_TOKEN( HyperlinksChanged
):
515 m_aCustomPropertyName
= "HyperlinksChanged";
516 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
519 case EXTPR_TOKEN( LinksUpToDate
):
520 m_aCustomPropertyName
= "LinksUpToDate";
521 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
524 case EXTPR_TOKEN( ScaleCrop
):
525 m_aCustomPropertyName
= "ScaleCrop";
526 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
529 case EXTPR_TOKEN( SharedDoc
):
530 m_aCustomPropertyName
= "ShareDoc";
531 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) ); // the property has boolean type
534 case EXTPR_TOKEN( DocSecurity
):
535 m_aCustomPropertyName
= "DocSecurity";
536 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
539 case EXTPR_TOKEN( HiddenSlides
):
540 m_aCustomPropertyName
= "HiddenSlides";
541 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
544 case EXTPR_TOKEN( MMClips
):
545 m_aCustomPropertyName
= "MMClips";
546 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
549 case EXTPR_TOKEN( Notes
):
550 m_aCustomPropertyName
= "Notes";
551 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
554 case EXTPR_TOKEN( Slides
):
555 m_aCustomPropertyName
= "Slides";
556 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) ); // the property has sal_Int32 type
559 case EXTPR_TOKEN( AppVersion
):
560 m_aCustomPropertyName
= "AppVersion";
561 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
564 case EXTPR_TOKEN( Company
):
565 m_aCustomPropertyName
= "Company";
566 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
569 case EXTPR_TOKEN( HyperlinkBase
):
570 m_aCustomPropertyName
= "HyperlinkBase";
571 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
574 case EXTPR_TOKEN( Manager
):
575 m_aCustomPropertyName
= "Manager";
576 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
579 case EXTPR_TOKEN( PresentationFormat
):
580 m_aCustomPropertyName
= "PresentationFormat";
581 AddCustomProperty( uno::makeAny( aChars
) ); // the property has string type
584 case EXTPR_TOKEN( CharactersWithSpaces
):
585 case EXTPR_TOKEN( Lines
):
586 case EXTPR_TOKEN( DigSig
):
587 case EXTPR_TOKEN( HeadingPairs
):
588 case EXTPR_TOKEN( HLinks
):
589 case EXTPR_TOKEN( TitlesOfParts
):
590 // ignored during the import currently
594 OSL_FAIL( "Unexpected extended property!" );
597 else if ( m_nState
== CUSTPR_TOKEN( Properties
) )
599 if ( m_nBlock
== CUSTPR_TOKEN( property
) )
601 // this is a custom property
604 case VT_TOKEN( bool ):
605 AddCustomProperty( uno::makeAny( aChars
.toBoolean() ) );
608 case VT_TOKEN( bstr
):
609 case VT_TOKEN( lpstr
):
610 case VT_TOKEN( lpwstr
):
611 // the property has string type
612 AddCustomProperty( uno::makeAny( AttributeConversion::decodeXString( aChars
) ) );
615 case VT_TOKEN( date
):
616 case VT_TOKEN( filetime
):
617 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars
) ) );
622 AddCustomProperty( uno::makeAny( (sal_Int16
)aChars
.toInt32() ) );
626 case VT_TOKEN( int ):
627 AddCustomProperty( uno::makeAny( aChars
.toInt32() ) );
631 AddCustomProperty( uno::makeAny( aChars
.toInt64() ) );
635 AddCustomProperty( uno::makeAny( aChars
.toFloat() ) );
639 AddCustomProperty( uno::makeAny( aChars
.toDouble() ) );
643 // all the other types are ignored;
649 OSL_FAIL( "Unexpected tag in custom property!" );
654 catch( uno::RuntimeException
& )
658 catch( xml::sax::SAXException
& )
662 catch( uno::Exception
& e
)
664 throw xml::sax::SAXException(
665 OUString("Error while setting document property!"),
666 uno::Reference
< uno::XInterface
>(),
671 // ------------------------------------------------
672 void SAL_CALL
OOXMLDocPropHandler::ignorableWhitespace( const OUString
& )
673 throw (xml::sax::SAXException
, uno::RuntimeException
)
677 // ------------------------------------------------
678 void SAL_CALL
OOXMLDocPropHandler::processingInstruction( const OUString
&, const OUString
& )
679 throw (xml::sax::SAXException
, uno::RuntimeException
)
683 } // namespace docprop
686 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */