1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ManifestExport.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include <ManifestExport.hxx>
34 #include <ManifestDefines.hxx>
35 #ifndef _COM_SUN_STAR_XML_SAX_XATTRIBUTELIST_HXX
36 #include <com/sun/star/xml/sax/XAttributeList.hpp>
38 #include <rtl/ustrbuf.hxx>
39 #ifndef _BASE64_CODEC_HXX_
40 #include <Base64Codec.hxx>
42 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
43 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HXX
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
46 #ifndef _COM_SUN_STAR_XML_BEANS_PROPERTYVALUE_HPP
47 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <comphelper/documentconstants.hxx>
51 #include <comphelper/attributelist.hxx>
54 using namespace com::sun::star::beans
;
55 using namespace com::sun::star::uno
;
56 using namespace com::sun::star::xml::sax
;
58 ManifestExport::ManifestExport(Reference
< XDocumentHandler
> xHandler
, const Sequence
< Sequence
< PropertyValue
> > &rManList
)
60 const OUString
sFileEntryElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_FILE_ENTRY
) );
61 const OUString
sManifestElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_MANIFEST
) );
62 const OUString
sEncryptionDataElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ENCRYPTION_DATA
) );
63 const OUString
sAlgorithmElement ( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_ALGORITHM
) );
64 const OUString
sKeyDerivationElement( RTL_CONSTASCII_USTRINGPARAM ( ELEMENT_KEY_DERIVATION
) );
66 const OUString
sCdataAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CDATA
) );
67 const OUString
sMediaTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_MEDIA_TYPE
) );
68 const OUString
sVersionAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_VERSION
) );
69 const OUString
sFullPathAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_FULL_PATH
) );
70 const OUString
sSizeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SIZE
) );
71 const OUString
sSaltAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_SALT
) );
72 const OUString
sInitialisationVectorAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_INITIALISATION_VECTOR
) );
73 const OUString
sIterationCountAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ITERATION_COUNT
) );
74 const OUString
sAlgorithmNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_ALGORITHM_NAME
) );
75 const OUString
sKeyDerivationNameAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_KEY_DERIVATION_NAME
) );
76 const OUString
sChecksumTypeAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM_TYPE
) );
77 const OUString
sChecksumAttribute ( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_CHECKSUM
) );
79 const OUString
sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) );
80 const OUString
sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) );
81 const OUString
sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) );
82 const OUString
sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) );
83 const OUString
sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) );
84 const OUString
sInitialisationVectorProperty( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) );
85 const OUString
sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) );
86 const OUString
sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) );
88 const OUString
sWhiteSpace ( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
89 const OUString
sBlowfish ( RTL_CONSTASCII_USTRINGPARAM ( "Blowfish CFB" ) );
90 const OUString
sPBKDF2 ( RTL_CONSTASCII_USTRINGPARAM ( "PBKDF2" ) );
91 const OUString
sChecksumType ( RTL_CONSTASCII_USTRINGPARAM ( CHECKSUM_TYPE
) );
93 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
94 const Sequence
< PropertyValue
> *pSequence
= rManList
.getConstArray();
95 const sal_uInt32 nManLength
= rManList
.getLength();
97 // find the mediatype of the document if any
98 OUString aDocMediaType
;
99 for (sal_uInt32 nInd
= 0; nInd
< nManLength
; nInd
++ )
104 const PropertyValue
*pValue
= pSequence
[nInd
].getConstArray();
105 for (sal_uInt32 j
= 0, nNum
= pSequence
[nInd
].getLength(); j
< nNum
; j
++, pValue
++)
108 if (pValue
->Name
.equals (sMediaTypeProperty
) )
110 pValue
->Value
>>= aMediaType
;
112 else if (pValue
->Name
.equals (sFullPathProperty
) )
114 pValue
->Value
>>= aPath
;
117 if ( aPath
.getLength() && aMediaType
.getLength() )
121 if ( aPath
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ) )
123 aDocMediaType
= aMediaType
;
128 sal_Bool bProvideDTD
= sal_False
;
129 sal_Bool bAcceptNonemptyVersion
= sal_False
;
130 if ( aDocMediaType
.getLength() )
132 if ( aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
) ) )
133 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
) ) )
134 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
) ) )
135 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
) ) )
136 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
) ) )
137 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
) ) )
138 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
) ) )
139 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
) ) )
140 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
) ) )
142 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
) ) )
143 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
) ) )
144 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
) ) )
145 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
) ) )
146 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
) ) )
147 || aDocMediaType
.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII
) ) ) )
151 pRootAttrList
->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS
) ),
153 OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_OASIS_NAMESPACE
) ) );
154 bAcceptNonemptyVersion
= sal_True
;
158 // even if it is no SO6 format the namespace must be specified
159 // thus SO6 format is used as default one
160 pRootAttrList
->AddAttribute ( OUString( RTL_CONSTASCII_USTRINGPARAM ( ATTRIBUTE_XMLNS
) ),
162 OUString( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_NAMESPACE
) ) );
164 bProvideDTD
= sal_True
;
168 Reference
< XAttributeList
> xRootAttrList (pRootAttrList
);
170 xHandler
->startDocument();
171 Reference
< XExtendedDocumentHandler
> xExtHandler ( xHandler
, UNO_QUERY
);
172 if ( xExtHandler
.is() && bProvideDTD
)
174 OUString
aDocType ( RTL_CONSTASCII_USTRINGPARAM ( MANIFEST_DOCTYPE
) );
175 xExtHandler
->unknown ( aDocType
);
176 xHandler
->ignorableWhitespace ( sWhiteSpace
);
178 xHandler
->startElement( sManifestElement
, xRootAttrList
);
180 for (sal_uInt32 i
= 0 ; i
< nManLength
; i
++)
182 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
183 const PropertyValue
*pValue
= pSequence
[i
].getConstArray();
185 const PropertyValue
*pVector
= NULL
, *pSalt
= NULL
, *pIterationCount
= NULL
, *pDigest
= NULL
;
186 for (sal_uInt32 j
= 0, nNum
= pSequence
[i
].getLength(); j
< nNum
; j
++, pValue
++)
188 if (pValue
->Name
.equals (sMediaTypeProperty
) )
190 pValue
->Value
>>= aString
;
191 pAttrList
->AddAttribute ( sMediaTypeAttribute
, sCdataAttribute
, aString
);
193 else if (pValue
->Name
.equals (sVersionProperty
) )
195 pValue
->Value
>>= aString
;
196 // the version is stored only if it is not empty
197 if ( bAcceptNonemptyVersion
&& aString
.getLength() )
198 pAttrList
->AddAttribute ( sVersionAttribute
, sCdataAttribute
, aString
);
200 else if (pValue
->Name
.equals (sFullPathProperty
) )
202 pValue
->Value
>>= aString
;
203 pAttrList
->AddAttribute ( sFullPathAttribute
, sCdataAttribute
, aString
);
205 else if (pValue
->Name
.equals (sSizeProperty
) )
208 pValue
->Value
>>= nSize
;
209 OUStringBuffer aBuffer
;
210 aBuffer
.append ( nSize
);
211 pAttrList
->AddAttribute ( sSizeAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
213 else if (pValue
->Name
.equals (sInitialisationVectorProperty
) )
215 else if (pValue
->Name
.equals (sSaltProperty
) )
217 else if (pValue
->Name
.equals (sIterationCountProperty
) )
218 pIterationCount
= pValue
;
219 else if (pValue
->Name
.equals ( sDigestProperty
) )
222 xHandler
->ignorableWhitespace ( sWhiteSpace
);
223 Reference
< XAttributeList
> xAttrList ( pAttrList
);
224 xHandler
->startElement( sFileEntryElement
, xAttrList
);
225 if ( pVector
&& pSalt
&& pIterationCount
)
227 ::comphelper::AttributeList
* pNewAttrList
= new ::comphelper::AttributeList
;
228 Reference
< XAttributeList
> xNewAttrList (pNewAttrList
);
229 OUStringBuffer aBuffer
;
230 Sequence
< sal_uInt8
> aSequence
;
232 xHandler
->ignorableWhitespace ( sWhiteSpace
);
235 pNewAttrList
->AddAttribute ( sChecksumTypeAttribute
, sCdataAttribute
, sChecksumType
);
236 pDigest
->Value
>>= aSequence
;
237 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
238 pNewAttrList
->AddAttribute ( sChecksumAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
240 xHandler
->startElement( sEncryptionDataElement
, xNewAttrList
);
242 pNewAttrList
= new ::comphelper::AttributeList
;
243 xNewAttrList
= pNewAttrList
;
245 pNewAttrList
->AddAttribute ( sAlgorithmNameAttribute
, sCdataAttribute
, sBlowfish
);
247 pVector
->Value
>>= aSequence
;
248 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
249 pNewAttrList
->AddAttribute ( sInitialisationVectorAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
251 xHandler
->ignorableWhitespace ( sWhiteSpace
);
252 xHandler
->startElement( sAlgorithmElement
, xNewAttrList
);
253 xHandler
->ignorableWhitespace ( sWhiteSpace
);
254 xHandler
->endElement( sAlgorithmElement
);
256 pNewAttrList
= new ::comphelper::AttributeList
;
257 xNewAttrList
= pNewAttrList
;
259 pNewAttrList
->AddAttribute ( sKeyDerivationNameAttribute
, sCdataAttribute
, sPBKDF2
);
261 sal_Int32 nCount
= 0;
262 pIterationCount
->Value
>>= nCount
;
263 aBuffer
.append (nCount
);
264 pNewAttrList
->AddAttribute ( sIterationCountAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
266 pSalt
->Value
>>= aSequence
;
267 Base64Codec::encodeBase64 ( aBuffer
, aSequence
);
268 pNewAttrList
->AddAttribute ( sSaltAttribute
, sCdataAttribute
, aBuffer
.makeStringAndClear() );
270 xHandler
->ignorableWhitespace ( sWhiteSpace
);
271 xHandler
->startElement( sKeyDerivationElement
, xNewAttrList
);
272 xHandler
->ignorableWhitespace ( sWhiteSpace
);
273 xHandler
->endElement( sKeyDerivationElement
);
274 xHandler
->ignorableWhitespace ( sWhiteSpace
);
275 xHandler
->endElement( sEncryptionDataElement
);
277 xHandler
->ignorableWhitespace ( sWhiteSpace
);
278 xHandler
->endElement( sFileEntryElement
);
280 xHandler
->ignorableWhitespace ( sWhiteSpace
);
281 xHandler
->endElement( sManifestElement
);
282 xHandler
->endDocument();