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 "ooxmldocpropimport.hxx"
21 #include "services.hxx"
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
26 #include <com/sun/star/embed/XRelationshipAccess.hpp>
27 #include <com/sun/star/embed/XStorage.hpp>
28 #include "oox/core/fastparser.hxx"
29 #include "oox/core/relations.hxx"
30 #include "oox/helper/containerhelper.hxx"
31 #include "oox/helper/helper.hxx"
32 #include "docprophandler.hxx"
34 #include <cppuhelper/supportsservice.hxx>
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::document
;
41 using namespace ::com::sun::star::embed
;
42 using namespace ::com::sun::star::io
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::xml::sax
;
47 OUString SAL_CALL
DocumentPropertiesImport_getImplementationName()
49 return OUString( "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter" );
52 Sequence
< OUString
> SAL_CALL
DocumentPropertiesImport_getSupportedServiceNames()
54 Sequence
< OUString
> aServices( 1 );
55 aServices
[ 0 ] = "com.sun.star.document.OOXMLDocumentPropertiesImporter";
59 Reference
< XInterface
> SAL_CALL
DocumentPropertiesImport_createInstance( const Reference
< XComponentContext
>& rxContext
) throw(Exception
)
61 return static_cast< ::cppu::OWeakObject
* >( new DocumentPropertiesImport( rxContext
) );
66 Sequence
< InputSource
> lclGetRelatedStreams( const Reference
< XStorage
>& rxStorage
, const OUString
& rStreamType
) throw (RuntimeException
, css::io::IOException
)
68 Reference
< XRelationshipAccess
> xRelation( rxStorage
, UNO_QUERY_THROW
);
69 Reference
< XHierarchicalStorageAccess
> xHierarchy( rxStorage
, UNO_QUERY_THROW
);
71 Sequence
< Sequence
< StringPair
> > aPropsInfo
= xRelation
->getRelationshipsByType( rStreamType
);
73 ::std::vector
< InputSource
> aResult
;
75 for( sal_Int32 nIndex
= 0, nLength
= aPropsInfo
.getLength(); nIndex
< nLength
; ++nIndex
)
77 const Sequence
< StringPair
>& rEntries
= aPropsInfo
[ nIndex
];
78 for( sal_Int32 nEntryIndex
= 0, nEntryLength
= rEntries
.getLength(); nEntryIndex
< nEntryLength
; ++nEntryIndex
)
80 const StringPair
& rEntry
= rEntries
[ nEntryIndex
];
81 if ( rEntry
.First
== "Target" )
83 // The stream path is always a relative one, ignore the leading "/" if it's there.
84 OUString aStreamPath
= rEntry
.Second
;
85 if (aStreamPath
.startsWith("/"))
86 aStreamPath
= aStreamPath
.copy(1);
88 Reference
< XExtendedStorageStream
> xExtStream(
89 xHierarchy
->openStreamElementByHierarchicalName( aStreamPath
, ElementModes::READ
), UNO_QUERY_THROW
);
90 Reference
< XInputStream
> xInStream
= xExtStream
->getInputStream();
93 aResult
.resize( aResult
.size() + 1 );
94 aResult
.back().sSystemId
= rEntry
.Second
;
95 aResult
.back().aInputStream
= xExtStream
->getInputStream();
102 return ContainerHelper::vectorToSequence( aResult
);
107 DocumentPropertiesImport::DocumentPropertiesImport( const Reference
< XComponentContext
>& rxContext
) :
108 mxContext( rxContext
)
113 OUString SAL_CALL
DocumentPropertiesImport::getImplementationName() throw (RuntimeException
, std::exception
)
115 return DocumentPropertiesImport_getImplementationName();
118 sal_Bool SAL_CALL
DocumentPropertiesImport::supportsService( const OUString
& rServiceName
) throw (RuntimeException
, std::exception
)
120 return cppu::supportsService(this, rServiceName
);
123 Sequence
< OUString
> SAL_CALL
DocumentPropertiesImport::getSupportedServiceNames() throw (RuntimeException
, std::exception
)
125 return DocumentPropertiesImport_getSupportedServiceNames();
128 // XOOXMLDocumentPropertiesImporter
129 void SAL_CALL
DocumentPropertiesImport::importProperties(
130 const Reference
< XStorage
>& rxSource
, const Reference
< XDocumentProperties
>& rxDocumentProperties
)
131 throw (RuntimeException
, IllegalArgumentException
, SAXException
, Exception
, std::exception
)
133 if( !mxContext
.is() )
134 throw RuntimeException();
136 if( !rxSource
.is() || !rxDocumentProperties
.is() )
137 throw IllegalArgumentException();
139 Sequence
< InputSource
> aCoreStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
141 if( !aCoreStreams
.hasElements() )
142 aCoreStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "metadata/core-properties" ) );
143 // MS Office seems to have a bug, so we have to do similar handling
144 if( !aCoreStreams
.hasElements() )
145 aCoreStreams
= lclGetRelatedStreams( rxSource
, CREATE_PACKAGE_RELATION_TYPE( "metadata/core-properties" ) );
147 Sequence
< InputSource
> aExtStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
149 if( !aExtStreams
.hasElements() )
150 aExtStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "extended-properties" ) );
151 Sequence
< InputSource
> aCustomStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
153 if( !aCustomStreams
.hasElements() )
154 aCustomStreams
= lclGetRelatedStreams( rxSource
, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "custom-properties" ) );
156 if( aCoreStreams
.hasElements() || aExtStreams
.hasElements() || aCustomStreams
.hasElements() )
158 if( aCoreStreams
.getLength() > 1 )
159 throw IOException( "Unexpected core properties stream!" );
161 ::oox::core::FastParser
aParser( mxContext
);
162 aParser
.registerNamespace( NMSP_packageMetaCorePr
);
163 aParser
.registerNamespace( NMSP_dc
);
164 aParser
.registerNamespace( NMSP_dcTerms
);
165 aParser
.registerNamespace( NMSP_officeExtPr
);
166 aParser
.registerNamespace( NMSP_officeCustomPr
);
167 aParser
.registerNamespace( NMSP_officeDocPropsVT
);
168 aParser
.setDocumentHandler( new OOXMLDocPropHandler( mxContext
, rxDocumentProperties
) );
170 if( aCoreStreams
.hasElements() )
171 aParser
.parseStream( aCoreStreams
[ 0 ], true );
172 for( sal_Int32 nIndex
= 0; nIndex
< aExtStreams
.getLength(); ++nIndex
)
173 aParser
.parseStream( aExtStreams
[ nIndex
], true );
174 for( sal_Int32 nIndex
= 0; nIndex
< aCustomStreams
.getLength(); ++nIndex
)
175 aParser
.parseStream( aCustomStreams
[ nIndex
], true );
179 } // namespace docprop
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */