Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/embed/ElementModes.hpp>
21 : #include <tools/debug.hxx>
22 : #include <unotools/streamwrap.hxx>
23 : #include <xmlversion.hxx>
24 : #include <xmloff/xmlmetae.hxx>
25 :
26 : #include <xmloff/xmltoken.hxx>
27 : #include <comphelper/processfactory.hxx>
28 : #include <com/sun/star/io/XActiveDataSource.hpp>
29 : #include <com/sun/star/io/XOutputStream.hpp>
30 : #include <com/sun/star/util/DateTime.hpp>
31 : #include <com/sun/star/util/MeasureUnit.hpp>
32 : #include <com/sun/star/xml/sax/InputSource.hpp>
33 : #include <com/sun/star/xml/sax/Parser.hpp>
34 : #include <com/sun/star/xml/sax/Writer.hpp>
35 :
36 : using namespace ::com::sun::star::xml::sax;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star;
39 :
40 : const char XMLN_VERSIONSLIST[] = "VersionList.xml";
41 :
42 : // #110897#
43 0 : XMLVersionListExport::XMLVersionListExport(
44 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
45 : const com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions,
46 : const OUString &rFileName,
47 : Reference< XDocumentHandler > &rHandler )
48 : : SvXMLExport( xContext, "", rFileName, util::MeasureUnit::CM, rHandler ),
49 0 : maVersions( rVersions )
50 : {
51 0 : _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_DC_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC),
52 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_DC), XML_NAMESPACE_DC );
53 0 : _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
54 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
55 0 : }
56 :
57 0 : sal_uInt32 XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum )
58 : {
59 0 : GetDocHandler()->startDocument();
60 :
61 0 : addChaffWhenEncryptedStorage();
62 :
63 0 : sal_uInt16 nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_DC );
64 :
65 0 : AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
66 0 : _GetNamespaceMap().GetNameByIndex ( nPos ) );
67 :
68 0 : nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_FRAMEWORK );
69 0 : AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
70 0 : _GetNamespaceMap().GetNameByIndex ( nPos ) );
71 :
72 : {
73 : // the following object will write all collected attributes in its dtor
74 0 : SvXMLElementExport aRoot( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_LIST, true, true );
75 :
76 0 : for ( sal_Int32 n=0; n<maVersions.getLength(); n++ )
77 : {
78 0 : const util::RevisionTag& rInfo = maVersions[n];
79 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
80 : xmloff::token::XML_TITLE,
81 0 : OUString( rInfo.Identifier ) );
82 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
83 : xmloff::token::XML_COMMENT,
84 0 : OUString( rInfo.Comment ) );
85 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
86 : xmloff::token::XML_CREATOR,
87 0 : OUString( rInfo.Author ) );
88 :
89 : OUString aDateStr =
90 0 : SvXMLMetaExport::GetISODateTimeString( rInfo.TimeStamp );
91 :
92 0 : AddAttribute( XML_NAMESPACE_DC, xmloff::token::XML_DATE_TIME, aDateStr );
93 :
94 : // the following object will write all collected attributes in its dtor
95 0 : SvXMLElementExport aEntry( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_ENTRY, true, true );
96 0 : }
97 : }
98 0 : GetDocHandler()->endDocument();
99 0 : return 0;
100 : }
101 :
102 0 : XMLVersionListImport::XMLVersionListImport(
103 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
104 : com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions )
105 : : SvXMLImport(xContext, ""),
106 0 : maVersions( rVersions )
107 : {
108 0 : GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
109 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
110 0 : }
111 :
112 0 : XMLVersionListImport::~XMLVersionListImport( void ) throw()
113 0 : {}
114 :
115 0 : SvXMLImportContext *XMLVersionListImport::CreateContext(
116 : sal_uInt16 nPrefix,
117 : const OUString& rLocalName,
118 : const Reference< XAttributeList > & xAttrList )
119 : {
120 0 : SvXMLImportContext *pContext = 0;
121 :
122 0 : if ( XML_NAMESPACE_FRAMEWORK == nPrefix &&
123 0 : rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_LIST) )
124 : {
125 0 : pContext = new XMLVersionListContext( *this, nPrefix, rLocalName, xAttrList );
126 : }
127 : else
128 : {
129 0 : pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList );
130 : }
131 :
132 0 : return pContext;
133 : }
134 :
135 0 : XMLVersionListContext::XMLVersionListContext( XMLVersionListImport& rImport,
136 : sal_uInt16 nPrefix,
137 : const OUString& rLocalName,
138 : const Reference< XAttributeList > & )
139 : : SvXMLImportContext( rImport, nPrefix, rLocalName )
140 0 : , rLocalRef( rImport )
141 : {
142 0 : }
143 :
144 0 : XMLVersionListContext::~XMLVersionListContext( void )
145 0 : {}
146 :
147 0 : SvXMLImportContext *XMLVersionListContext::CreateChildContext( sal_uInt16 nPrefix,
148 : const OUString& rLocalName,
149 : const Reference< XAttributeList > & xAttrList )
150 : {
151 0 : SvXMLImportContext *pContext = 0;
152 :
153 0 : if ( nPrefix == XML_NAMESPACE_FRAMEWORK &&
154 0 : rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_ENTRY) )
155 : {
156 0 : pContext = new XMLVersionContext( rLocalRef, nPrefix, rLocalName, xAttrList );
157 : }
158 : else
159 : {
160 0 : pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName );
161 : }
162 :
163 0 : return pContext;
164 : }
165 :
166 0 : XMLVersionContext::XMLVersionContext( XMLVersionListImport& rImport,
167 : sal_uInt16 nPref,
168 : const OUString& rLocalName,
169 : const Reference< XAttributeList > & xAttrList )
170 : : SvXMLImportContext( rImport, nPref, rLocalName )
171 0 : , rLocalRef( rImport )
172 : {
173 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
174 :
175 0 : if ( !nAttrCount )
176 0 : return;
177 :
178 0 : util::RevisionTag aInfo;
179 0 : for ( sal_Int16 i=0; i < nAttrCount; i++ )
180 : {
181 0 : OUString aLocalName;
182 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
183 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
184 :
185 0 : if ( XML_NAMESPACE_FRAMEWORK == nPrefix )
186 : {
187 0 : if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_TITLE) )
188 : {
189 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
190 0 : aInfo.Identifier = rAttrValue;
191 : }
192 0 : else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_COMMENT) )
193 : {
194 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
195 0 : aInfo.Comment = rAttrValue;
196 : }
197 0 : else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_CREATOR) )
198 : {
199 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
200 0 : aInfo.Author = rAttrValue;
201 : }
202 : }
203 0 : else if ( ( XML_NAMESPACE_DC == nPrefix ) &&
204 0 : ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_DATE_TIME) ) )
205 : {
206 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
207 0 : util::DateTime aTime;
208 0 : if ( ParseISODateTimeString( rAttrValue, aTime ) )
209 0 : aInfo.TimeStamp = aTime;
210 : }
211 0 : }
212 :
213 0 : uno::Sequence < util::RevisionTag >& aList = rLocalRef.GetList();
214 0 : sal_Int32 nLength = aList.getLength();
215 0 : aList.realloc( nLength+1 );
216 0 : aList[nLength] = aInfo;
217 : }
218 :
219 0 : XMLVersionContext::~XMLVersionContext( void )
220 0 : {}
221 :
222 0 : sal_Bool XMLVersionContext::ParseISODateTimeString(
223 : const OUString& rString,
224 : util::DateTime& rDateTime )
225 : {
226 0 : sal_Bool bSuccess = sal_True;
227 :
228 0 : OUString aDateStr, aTimeStr;
229 0 : sal_Int32 nPos = rString.indexOf( (sal_Unicode) 'T' );
230 0 : if ( nPos >= 0 )
231 : {
232 0 : aDateStr = rString.copy( 0, nPos );
233 0 : aTimeStr = rString.copy( nPos + 1 );
234 : }
235 : else
236 0 : aDateStr = rString; // no separator: only date part
237 :
238 0 : sal_Int32 nYear = 0;
239 0 : sal_Int32 nMonth = 1;
240 0 : sal_Int32 nDay = 1;
241 0 : sal_Int32 nHour = 0;
242 0 : sal_Int32 nMin = 0;
243 0 : sal_Int32 nSec = 0;
244 :
245 0 : const sal_Unicode* pStr = aDateStr.getStr();
246 0 : sal_Int32 nDateTokens = 1;
247 0 : while ( *pStr )
248 : {
249 0 : if ( *pStr == '-' )
250 0 : nDateTokens++;
251 0 : pStr++;
252 : }
253 0 : if ( nDateTokens > 3 || aDateStr.isEmpty() )
254 0 : bSuccess = sal_False;
255 : else
256 : {
257 0 : sal_Int32 n = 0;
258 0 : nYear = aDateStr.getToken( 0, '-', n ).toInt32();
259 0 : if ( nYear > 9999 )
260 0 : bSuccess = sal_False;
261 0 : else if ( nDateTokens >= 2 )
262 : {
263 0 : nMonth = aDateStr.getToken( 0, '-', n ).toInt32();
264 0 : if ( nMonth > 12 )
265 0 : bSuccess = sal_False;
266 0 : else if ( nDateTokens >= 3 )
267 : {
268 0 : nDay = aDateStr.getToken( 0, '-', n ).toInt32();
269 0 : if ( nDay > 31 )
270 0 : bSuccess = sal_False;
271 : }
272 : }
273 : }
274 :
275 0 : if ( bSuccess && !aTimeStr.isEmpty() ) // time is optional
276 : {
277 0 : pStr = aTimeStr.getStr();
278 0 : sal_Int32 nTimeTokens = 1;
279 0 : while ( *pStr )
280 : {
281 0 : if ( *pStr == ':' )
282 0 : nTimeTokens++;
283 0 : pStr++;
284 : }
285 0 : if ( nTimeTokens > 3 )
286 0 : bSuccess = sal_False;
287 : else
288 : {
289 0 : sal_Int32 n = 0;
290 0 : nHour = aTimeStr.getToken( 0, ':', n ).toInt32();
291 0 : if ( nHour > 23 )
292 0 : bSuccess = sal_False;
293 0 : else if ( nTimeTokens >= 2 )
294 : {
295 0 : nMin = aTimeStr.getToken( 0, ':', n ).toInt32();
296 0 : if ( nMin > 59 )
297 0 : bSuccess = sal_False;
298 0 : else if ( nTimeTokens >= 3 )
299 : {
300 0 : nSec = aTimeStr.getToken( 0, ':', n ).toInt32();
301 0 : if ( nSec > 59 )
302 0 : bSuccess = sal_False;
303 : }
304 : }
305 : }
306 : }
307 :
308 0 : if ( bSuccess )
309 : {
310 0 : rDateTime.Day = sal::static_int_cast< sal_uInt16 >(nDay);
311 0 : rDateTime.Month = sal::static_int_cast< sal_uInt16 >(nMonth);
312 0 : rDateTime.Year = sal::static_int_cast< sal_uInt16 >(nYear);
313 0 : rDateTime.Hours = sal::static_int_cast< sal_uInt16 >(nHour);
314 0 : rDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(nMin);
315 0 : rDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(nSec);
316 : }
317 :
318 0 : return bSuccess;
319 : }
320 :
321 0 : void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions )
322 : throw (::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception)
323 : {
324 : // no storage, no version list!
325 0 : if ( xRoot.is() )
326 : {
327 : // get the services needed for writing the xml data
328 : Reference< uno::XComponentContext > xContext =
329 0 : comphelper::getProcessComponentContext();
330 :
331 0 : Reference< XWriter > xWriter = Writer::create(xContext);
332 :
333 : // check whether there's already a sub storage with the version info
334 : // and delete it
335 0 : OUString sVerName( XMLN_VERSIONSLIST );
336 :
337 : try {
338 : // open (create) the sub storage with the version info
339 0 : uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement(
340 : sVerName,
341 0 : embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
342 0 : if ( !xVerStream.is() )
343 0 : throw uno::RuntimeException();
344 :
345 0 : Reference< io::XOutputStream > xOut = xVerStream->getOutputStream();
346 0 : if ( !xOut.is() )
347 0 : throw uno::RuntimeException(); // the stream was successfully opened for writing already
348 :
349 0 : Reference< io::XActiveDataSource > xSrc( xWriter, uno::UNO_QUERY );
350 0 : xSrc->setOutputStream(xOut);
351 :
352 0 : Reference< XDocumentHandler > xHandler( xWriter, uno::UNO_QUERY );
353 :
354 0 : XMLVersionListExport aExp( xContext, rVersions, sVerName, xHandler );
355 :
356 0 : aExp.exportDoc( ::xmloff::token::XML_VERSION );
357 :
358 0 : xVerStream = uno::Reference< io::XStream >(); // use refcounting for now to dispose
359 : }
360 0 : catch( uno::Exception& )
361 : {
362 : // TODO: error handling
363 0 : }
364 : }
365 0 : }
366 :
367 0 : uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot )
368 : throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception)
369 : {
370 0 : com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag > aVersions;
371 :
372 0 : const OUString sDocName( XMLN_VERSIONSLIST );
373 0 : uno::Reference< container::XNameAccess > xRootNames( xRoot, uno::UNO_QUERY );
374 :
375 : try {
376 0 : if ( xRootNames.is() && xRootNames->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) )
377 : {
378 0 : Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
379 :
380 0 : InputSource aParserInput;
381 :
382 0 : uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY );
383 : OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!\n" );
384 0 : if ( xProps.is() )
385 : {
386 : try {
387 0 : xProps->getPropertyValue("URL") >>= aParserInput.sSystemId;
388 : }
389 0 : catch( uno::Exception& )
390 : {}
391 : }
392 :
393 0 : uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement(
394 : sDocName,
395 0 : embed::ElementModes::READ );
396 0 : if ( !xDocStream.is() )
397 0 : throw uno::RuntimeException();
398 :
399 0 : aParserInput.aInputStream = xDocStream->getInputStream();
400 : OSL_ENSURE( aParserInput.aInputStream.is(),
401 : "The stream was successfully opened for reading, the input part must be accessible!\n" );
402 0 : if ( !aParserInput.aInputStream.is() )
403 0 : throw uno::RuntimeException();
404 :
405 : // get filter
406 0 : Reference< XDocumentHandler > xFilter = new XMLVersionListImport( xContext, aVersions );
407 :
408 : // connect parser and filter
409 0 : Reference< XParser > xParser = xml::sax::Parser::create(xContext);
410 0 : xParser->setDocumentHandler( xFilter );
411 :
412 : // parse
413 : try
414 : {
415 0 : xParser->parseStream( aParserInput );
416 : }
417 0 : catch( SAXParseException& ) {}
418 0 : catch( SAXException& ) {}
419 0 : catch( io::IOException& ) {}
420 : }
421 : }
422 0 : catch( uno::Exception& )
423 : {
424 : // TODO: error handling
425 : }
426 :
427 0 : return aVersions;
428 : }
429 :
430 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
431 0 : XMLVersionListPersistence_get_implementation(
432 : css::uno::XComponentContext *,
433 : css::uno::Sequence<css::uno::Any> const &)
434 : {
435 0 : return cppu::acquire(new XMLVersionListPersistence());
436 : }
437 :
438 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|