bump product version to 4.2.0.1
[LibreOffice.git] / oox / source / docprop / docprophandler.cxx
blob39082a2d9c667d1bb36d4e78e8c42fadaee054fe
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/Locale.hpp>
26 #include <osl/time.h>
27 #include <i18nlangtag/languagetag.hxx>
29 #include <vector>
30 #include <boost/algorithm/string.hpp>
32 #include "oox/helper/attributelist.hxx"
34 using namespace ::com::sun::star;
36 namespace oox {
37 namespace docprop {
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 )
44 , m_nState( 0 )
45 , m_nBlock( 0 )
46 , m_nType( 0 )
47 , m_nInBlock( 0 )
49 if ( !xContext.is() || !xDocProp.is() )
50 throw uno::RuntimeException();
53 // ------------------------------------------------
54 OOXMLDocPropHandler::~OOXMLDocPropHandler()
58 // ------------------------------------------------
59 void OOXMLDocPropHandler::InitNew()
61 m_nState = 0;
62 m_nBlock = 0;
63 m_aCustomPropertyName = "";
64 m_nType = 0;
65 m_nInBlock = 0;
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();
78 try
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();
99 if ( nLen >= 4 )
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();
120 nOptTime += 3;
121 if ( nLen >= 20 && aChars[19] == (sal_Unicode)'.' )
123 nOptTime += 1;
124 sal_Int32 digitPos = 20;
125 while (nLen > digitPos && digitPos < 29)
127 sal_Unicode c = aChars[digitPos];
128 if ( c < '0' || c > '9')
129 break;
130 aOslDTime.NanoSeconds *= 10;
131 aOslDTime.NanoSeconds += c - '0';
132 ++digitPos;
134 if ( digitPos < 29 )
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;
144 else
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')
151 break;
152 ++digitPos;
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)'-' )
168 nModif *= -1;
172 if ( nModif )
174 // convert to UTC time
175 TimeValue aTmp;
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 );
209 return aResult;
212 return uno::Sequence< OUString >();
215 // ------------------------------------------------
216 void OOXMLDocPropHandler::UpdateDocStatistic( const OUString& aChars )
218 uno::Sequence< beans::NamedValue > aSet = m_xDocProp->getDocumentStatistics();
219 OUString aName;
221 switch( m_nBlock )
223 case EXTPR_TOKEN( Characters ):
224 aName = "CharacterCount";
225 break;
227 case EXTPR_TOKEN( Pages ):
228 aName = "PageCount";
229 break;
231 case EXTPR_TOKEN( Words ):
232 aName = "WordCount";
233 break;
235 case EXTPR_TOKEN( Paragraphs ):
236 aName = "ParagraphCount";
237 break;
239 default:
240 OSL_FAIL( "Unexpected statistic!" );
241 break;
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() );
252 bFound = sal_True;
253 break;
256 if ( !bFound )
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)
279 InitNew();
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 ) )
300 m_nState = nElement;
302 else
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
312 m_nBlock = nElement;
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 )
319 m_nType = nElement;
321 else
323 OSL_FAIL( "For now unexpected tags are ignored!" );
326 if ( m_nInBlock == SAL_MAX_INT32 )
327 throw uno::RuntimeException();
329 m_nInBlock++;
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();
341 m_nInBlock++;
344 // ------------------------------------------------
345 void SAL_CALL OOXMLDocPropHandler::endFastElement( ::sal_Int32 )
346 throw (xml::sax::SAXException, uno::RuntimeException)
348 if ( m_nInBlock )
350 m_nInBlock--;
352 if ( !m_nInBlock )
353 m_nState = 0;
354 else if ( m_nInBlock == 1 )
356 m_nBlock = 0;
357 m_aCustomPropertyName = "";
359 else if ( m_nInBlock == 2 )
360 m_nType = 0;
364 // ------------------------------------------------
365 void SAL_CALL OOXMLDocPropHandler::endUnknownElement( const OUString&, const OUString& )
366 throw (xml::sax::SAXException, uno::RuntimeException)
368 if ( m_nInBlock )
369 m_nInBlock--;
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 ) )
397 switch( m_nBlock )
399 case COREPR_TOKEN( category ):
400 m_aCustomPropertyName = "category";
401 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
402 break;
404 case COREPR_TOKEN( contentStatus ):
405 m_aCustomPropertyName = "contentStatus";
406 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
407 break;
409 case COREPR_TOKEN( contentType ):
410 m_aCustomPropertyName = "contentType";
411 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
412 break;
414 case COREPR_TOKEN( identifier ):
415 m_aCustomPropertyName = "identifier";
416 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
417 break;
419 case COREPR_TOKEN( version ):
420 m_aCustomPropertyName = "version";
421 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
422 break;
424 case DCT_TOKEN( created ):
425 if ( aChars.getLength() >= 4 )
426 m_xDocProp->setCreationDate( GetDateTimeFromW3CDTF( aChars ) );
427 break;
429 case DC_TOKEN( creator ):
430 m_xDocProp->setAuthor( aChars );
431 break;
433 case DC_TOKEN( description ):
434 m_xDocProp->setDescription( aChars );
435 break;
437 case COREPR_TOKEN( keywords ):
438 m_xDocProp->setKeywords( GetKeywordsSet( aChars ) );
439 break;
441 case DC_TOKEN( language ):
442 if ( aChars.getLength() >= 2 )
443 m_xDocProp->setLanguage( LanguageTag::convertToLocale( aChars) );
444 break;
446 case COREPR_TOKEN( lastModifiedBy ):
447 m_xDocProp->setModifiedBy( aChars );
448 break;
450 case COREPR_TOKEN( lastPrinted ):
451 if ( aChars.getLength() >= 4 )
452 m_xDocProp->setPrintDate( GetDateTimeFromW3CDTF( aChars ) );
453 break;
455 case DCT_TOKEN( modified ):
456 if ( aChars.getLength() >= 4 )
457 m_xDocProp->setModificationDate( GetDateTimeFromW3CDTF( aChars ) );
458 break;
460 case COREPR_TOKEN( revision ):
463 m_xDocProp->setEditingCycles(
464 static_cast<sal_Int16>(aChars.toInt32()) );
466 catch (lang::IllegalArgumentException &)
468 // ignore
470 break;
472 case DC_TOKEN( subject ):
473 m_xDocProp->setSubject( m_xDocProp->getSubject() + aChars );
474 break;
476 case DC_TOKEN( title ):
477 m_xDocProp->setTitle( m_xDocProp->getTitle() + aChars );
478 break;
480 default:
481 OSL_FAIL( "Unexpected core property!" );
484 else if ( m_nState == EXTPR_TOKEN( Properties ) )
486 switch( m_nBlock )
488 case EXTPR_TOKEN( Application ):
489 m_xDocProp->setGenerator( aChars );
490 break;
492 case EXTPR_TOKEN( Template ):
493 m_xDocProp->setTemplateName( aChars );
494 break;
496 case EXTPR_TOKEN( TotalTime ):
499 m_xDocProp->setEditingDuration( aChars.toInt32() );
501 catch (lang::IllegalArgumentException &)
503 // ignore
505 break;
507 case EXTPR_TOKEN( Characters ):
508 case EXTPR_TOKEN( Pages ):
509 case EXTPR_TOKEN( Words ):
510 case EXTPR_TOKEN( Paragraphs ):
511 UpdateDocStatistic( aChars );
512 break;
514 case EXTPR_TOKEN( HyperlinksChanged ):
515 m_aCustomPropertyName = "HyperlinksChanged";
516 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
517 break;
519 case EXTPR_TOKEN( LinksUpToDate ):
520 m_aCustomPropertyName = "LinksUpToDate";
521 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
522 break;
524 case EXTPR_TOKEN( ScaleCrop ):
525 m_aCustomPropertyName = "ScaleCrop";
526 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
527 break;
529 case EXTPR_TOKEN( SharedDoc ):
530 m_aCustomPropertyName = "ShareDoc";
531 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) ); // the property has boolean type
532 break;
534 case EXTPR_TOKEN( DocSecurity ):
535 m_aCustomPropertyName = "DocSecurity";
536 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
537 break;
539 case EXTPR_TOKEN( HiddenSlides ):
540 m_aCustomPropertyName = "HiddenSlides";
541 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
542 break;
544 case EXTPR_TOKEN( MMClips ):
545 m_aCustomPropertyName = "MMClips";
546 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
547 break;
549 case EXTPR_TOKEN( Notes ):
550 m_aCustomPropertyName = "Notes";
551 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
552 break;
554 case EXTPR_TOKEN( Slides ):
555 m_aCustomPropertyName = "Slides";
556 AddCustomProperty( uno::makeAny( aChars.toInt32() ) ); // the property has sal_Int32 type
557 break;
559 case EXTPR_TOKEN( AppVersion ):
560 m_aCustomPropertyName = "AppVersion";
561 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
562 break;
564 case EXTPR_TOKEN( Company ):
565 m_aCustomPropertyName = "Company";
566 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
567 break;
569 case EXTPR_TOKEN( HyperlinkBase ):
570 m_aCustomPropertyName = "HyperlinkBase";
571 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
572 break;
574 case EXTPR_TOKEN( Manager ):
575 m_aCustomPropertyName = "Manager";
576 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
577 break;
579 case EXTPR_TOKEN( PresentationFormat ):
580 m_aCustomPropertyName = "PresentationFormat";
581 AddCustomProperty( uno::makeAny( aChars ) ); // the property has string type
582 break;
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
591 break;
593 default:
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
602 switch( m_nType )
604 case VT_TOKEN( bool ):
605 AddCustomProperty( uno::makeAny( aChars.toBoolean() ) );
606 break;
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 ) ) );
613 break;
615 case VT_TOKEN( date ):
616 case VT_TOKEN( filetime ):
617 AddCustomProperty( uno::makeAny( GetDateTimeFromW3CDTF( aChars ) ) );
618 break;
620 case VT_TOKEN( i1 ):
621 case VT_TOKEN( i2 ):
622 AddCustomProperty( uno::makeAny( (sal_Int16)aChars.toInt32() ) );
623 break;
625 case VT_TOKEN( i4 ):
626 case VT_TOKEN( int ):
627 AddCustomProperty( uno::makeAny( aChars.toInt32() ) );
628 break;
630 case VT_TOKEN( i8 ):
631 AddCustomProperty( uno::makeAny( aChars.toInt64() ) );
632 break;
634 case VT_TOKEN( r4 ):
635 AddCustomProperty( uno::makeAny( aChars.toFloat() ) );
636 break;
638 case VT_TOKEN( r8 ):
639 AddCustomProperty( uno::makeAny( aChars.toDouble() ) );
640 break;
642 default:
643 // all the other types are ignored;
644 break;
647 else
649 OSL_FAIL( "Unexpected tag in custom property!" );
654 catch( uno::RuntimeException& )
656 throw;
658 catch( xml::sax::SAXException& )
660 throw;
662 catch( uno::Exception& e )
664 throw xml::sax::SAXException(
665 OUString("Error while setting document property!"),
666 uno::Reference< uno::XInterface >(),
667 uno::makeAny( e ) );
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
684 } // namespace oox
686 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */