xref: /aoo42x/main/xmloff/source/meta/xmlversion.cxx (revision 940681c7)
163bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
363bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
463bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
563bba73cSAndrew Rist  * distributed with this work for additional information
663bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
763bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
863bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
963bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
1063bba73cSAndrew Rist  *
1163bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1263bba73cSAndrew Rist  *
1363bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1463bba73cSAndrew Rist  * software distributed under the License is distributed on an
1563bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1663bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
1763bba73cSAndrew Rist  * specific language governing permissions and limitations
1863bba73cSAndrew Rist  * under the License.
1963bba73cSAndrew Rist  *
2063bba73cSAndrew Rist  *************************************************************/
2163bba73cSAndrew Rist 
2263bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
29cdf0e10cSrcweir #include <xmlversion.hxx>
30cdf0e10cSrcweir #include <xmloff/xmlmetae.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
33cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
34cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
35cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
36cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
38cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <tools/string.hxx>
41cdf0e10cSrcweir class SvStringsDtor;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir using ::rtl::OUString;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // ------------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir sal_Char __FAR_DATA XMLN_VERSIONSLIST[] = "VersionList.xml";
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // ------------------------------------------------------------------------
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // #110897#
XMLVersionListExport(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> xServiceFactory,const com::sun::star::uno::Sequence<com::sun::star::util::RevisionTag> & rVersions,const OUString & rFileName,Reference<XDocumentHandler> & rHandler)55cdf0e10cSrcweir XMLVersionListExport::XMLVersionListExport(
56cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
57cdf0e10cSrcweir     const com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions,
58cdf0e10cSrcweir     const OUString &rFileName,
59cdf0e10cSrcweir     Reference< XDocumentHandler > &rHandler )
60cdf0e10cSrcweir :   SvXMLExport( xServiceFactory, rFileName, rHandler ),
61cdf0e10cSrcweir 	maVersions( rVersions )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_DC_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC),
64cdf0e10cSrcweir                                    xmloff::token::GetXMLToken(xmloff::token::XML_N_DC), XML_NAMESPACE_DC );
65cdf0e10cSrcweir     _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
66cdf0e10cSrcweir                                    xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir // ------------------------------------------------------------------------
exportDoc(enum::xmloff::token::XMLTokenEnum)70cdf0e10cSrcweir sal_uInt32 XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     GetDocHandler()->startDocument();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     sal_uInt16 nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_DC );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
77cdf0e10cSrcweir                              _GetNamespaceMap().GetNameByIndex ( nPos ) );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_FRAMEWORK );
80cdf0e10cSrcweir     AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
81cdf0e10cSrcweir                              _GetNamespaceMap().GetNameByIndex ( nPos ) );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         // the following object will write all collected attributes in its dtor
85cdf0e10cSrcweir         SvXMLElementExport aRoot( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_LIST, sal_True, sal_True );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         for ( sal_Int32 n=0; n<maVersions.getLength(); n++ )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             const util::RevisionTag& rInfo = maVersions[n];
90cdf0e10cSrcweir             AddAttribute( XML_NAMESPACE_FRAMEWORK,
91cdf0e10cSrcweir                           xmloff::token::XML_TITLE,
92cdf0e10cSrcweir                           OUString( rInfo.Identifier ) );
93cdf0e10cSrcweir             AddAttribute( XML_NAMESPACE_FRAMEWORK,
94cdf0e10cSrcweir                           xmloff::token::XML_COMMENT,
95cdf0e10cSrcweir                           OUString( rInfo.Comment ) );
96cdf0e10cSrcweir             AddAttribute( XML_NAMESPACE_FRAMEWORK,
97cdf0e10cSrcweir                           xmloff::token::XML_CREATOR,
98cdf0e10cSrcweir                           OUString( rInfo.Author ) );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             OUString aDateStr =
101cdf0e10cSrcweir                 SvXMLMetaExport::GetISODateTimeString( rInfo.TimeStamp );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             AddAttribute( XML_NAMESPACE_DC, xmloff::token::XML_DATE_TIME, aDateStr );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             // the following object will write all collected attributes in its dtor
106cdf0e10cSrcweir             SvXMLElementExport aEntry( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_ENTRY, sal_True, sal_True );
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir     GetDocHandler()->endDocument();
110cdf0e10cSrcweir     return 0;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir // ------------------------------------------------------------------------
114cdf0e10cSrcweir // ------------------------------------------------------------------------
115cdf0e10cSrcweir 
116cdf0e10cSrcweir // #110897#
XMLVersionListImport(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> xServiceFactory,com::sun::star::uno::Sequence<com::sun::star::util::RevisionTag> & rVersions)117cdf0e10cSrcweir XMLVersionListImport::XMLVersionListImport(
118cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
119cdf0e10cSrcweir     com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions )
120cdf0e10cSrcweir :	SvXMLImport(xServiceFactory),
121cdf0e10cSrcweir     maVersions( rVersions )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
124cdf0e10cSrcweir                                   xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // ------------------------------------------------------------------------
~XMLVersionListImport(void)128cdf0e10cSrcweir XMLVersionListImport::~XMLVersionListImport( void ) throw()
129cdf0e10cSrcweir {}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir // ------------------------------------------------------------------------
CreateContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)132cdf0e10cSrcweir SvXMLImportContext *XMLVersionListImport::CreateContext(
133cdf0e10cSrcweir         sal_uInt16 nPrefix,
134cdf0e10cSrcweir         const OUString& rLocalName,
135cdf0e10cSrcweir         const Reference< XAttributeList > & xAttrList )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     SvXMLImportContext *pContext = 0;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     if ( XML_NAMESPACE_FRAMEWORK == nPrefix &&
140cdf0e10cSrcweir         rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_LIST) )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         pContext = new XMLVersionListContext( *this, nPrefix, rLocalName, xAttrList );
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     else
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList );
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     return pContext;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir // ------------------------------------------------------------------------
154cdf0e10cSrcweir // ------------------------------------------------------------------------
155cdf0e10cSrcweir 
XMLVersionListContext(XMLVersionListImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> &)156cdf0e10cSrcweir XMLVersionListContext::XMLVersionListContext( XMLVersionListImport& rImport,
157cdf0e10cSrcweir                                         sal_uInt16 nPrefix,
158cdf0e10cSrcweir                                         const OUString& rLocalName,
159cdf0e10cSrcweir                                         const Reference< XAttributeList > & )
160cdf0e10cSrcweir     : SvXMLImportContext( rImport, nPrefix, rLocalName )
161cdf0e10cSrcweir     , rLocalRef( rImport )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir // ------------------------------------------------------------------------
~XMLVersionListContext(void)166cdf0e10cSrcweir XMLVersionListContext::~XMLVersionListContext( void )
167cdf0e10cSrcweir {}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir // ------------------------------------------------------------------------
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)170cdf0e10cSrcweir SvXMLImportContext *XMLVersionListContext::CreateChildContext( sal_uInt16 nPrefix,
171cdf0e10cSrcweir                                         const OUString& rLocalName,
172cdf0e10cSrcweir                                         const Reference< XAttributeList > & xAttrList )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     SvXMLImportContext *pContext = 0;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     if ( nPrefix == XML_NAMESPACE_FRAMEWORK &&
177cdf0e10cSrcweir          rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_ENTRY) )
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         pContext = new XMLVersionContext( rLocalRef, nPrefix, rLocalName, xAttrList );
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     else
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName );
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     return pContext;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir // ------------------------------------------------------------------------
190cdf0e10cSrcweir // ------------------------------------------------------------------------
191cdf0e10cSrcweir 
XMLVersionContext(XMLVersionListImport & rImport,sal_uInt16 nPref,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)192cdf0e10cSrcweir XMLVersionContext::XMLVersionContext( XMLVersionListImport& rImport,
193cdf0e10cSrcweir                                         sal_uInt16 nPref,
194cdf0e10cSrcweir                                         const OUString& rLocalName,
195cdf0e10cSrcweir                                         const Reference< XAttributeList > & xAttrList )
196cdf0e10cSrcweir     : SvXMLImportContext( rImport, nPref, rLocalName )
197cdf0e10cSrcweir     , rLocalRef( rImport )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     if ( !nAttrCount )
202cdf0e10cSrcweir         return;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     util::RevisionTag aInfo;
205cdf0e10cSrcweir     for ( sal_Int16 i=0; i < nAttrCount; i++ )
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         OUString        aLocalName;
208cdf0e10cSrcweir         const OUString& rAttrName   = xAttrList->getNameByIndex( i );
209cdf0e10cSrcweir         sal_uInt16      nPrefix     = rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         if ( XML_NAMESPACE_FRAMEWORK == nPrefix )
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_TITLE) )
214cdf0e10cSrcweir             {
215cdf0e10cSrcweir                 const OUString& rAttrValue = xAttrList->getValueByIndex( i );
216cdf0e10cSrcweir                 aInfo.Identifier = rAttrValue;
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir             else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_COMMENT) )
219cdf0e10cSrcweir             {
220cdf0e10cSrcweir                 const OUString& rAttrValue = xAttrList->getValueByIndex( i );
221cdf0e10cSrcweir                 aInfo.Comment = rAttrValue;
222cdf0e10cSrcweir             }
223cdf0e10cSrcweir             else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_CREATOR) )
224cdf0e10cSrcweir             {
225cdf0e10cSrcweir                 const OUString& rAttrValue = xAttrList->getValueByIndex( i );
226cdf0e10cSrcweir                 aInfo.Author = rAttrValue;
227cdf0e10cSrcweir             }
228cdf0e10cSrcweir         }
229cdf0e10cSrcweir         else if ( ( XML_NAMESPACE_DC == nPrefix ) &&
230cdf0e10cSrcweir                   ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_DATE_TIME) ) )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             const OUString& rAttrValue = xAttrList->getValueByIndex( i );
233cdf0e10cSrcweir             util::DateTime aTime;
234cdf0e10cSrcweir             if ( ParseISODateTimeString( rAttrValue, aTime ) )
235cdf0e10cSrcweir                 aInfo.TimeStamp = aTime;
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     uno::Sequence < util::RevisionTag >& aList = rLocalRef.GetList();
240cdf0e10cSrcweir     sal_Int32 nLength = aList.getLength();
241cdf0e10cSrcweir     aList.realloc( nLength+1 );
242cdf0e10cSrcweir     aList[nLength] = aInfo;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246cdf0e10cSrcweir // ------------------------------------------------------------------------
~XMLVersionContext(void)247cdf0e10cSrcweir XMLVersionContext::~XMLVersionContext( void )
248cdf0e10cSrcweir {}
249cdf0e10cSrcweir 
250cdf0e10cSrcweir // ------------------------------------------------------------------------
251cdf0e10cSrcweir // static
ParseISODateTimeString(const rtl::OUString & rString,util::DateTime & rDateTime)252cdf0e10cSrcweir sal_Bool XMLVersionContext::ParseISODateTimeString(
253cdf0e10cSrcweir                                 const rtl::OUString& rString,
254cdf0e10cSrcweir                                 util::DateTime& rDateTime )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     sal_Bool bSuccess = sal_True;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     OUString aDateStr, aTimeStr;
259cdf0e10cSrcweir     sal_Int32 nPos = rString.indexOf( (sal_Unicode) 'T' );
260cdf0e10cSrcweir     if ( nPos >= 0 )
261cdf0e10cSrcweir     {
262cdf0e10cSrcweir         aDateStr = rString.copy( 0, nPos );
263cdf0e10cSrcweir         aTimeStr = rString.copy( nPos + 1 );
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir     else
266cdf0e10cSrcweir         aDateStr = rString;         // no separator: only date part
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     sal_Int32 nYear  = 0;
269cdf0e10cSrcweir     sal_Int32 nMonth = 1;
270cdf0e10cSrcweir     sal_Int32 nDay   = 1;
271cdf0e10cSrcweir     sal_Int32 nHour  = 0;
272cdf0e10cSrcweir     sal_Int32 nMin   = 0;
273cdf0e10cSrcweir     sal_Int32 nSec   = 0;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     const sal_Unicode* pStr = aDateStr.getStr();
276cdf0e10cSrcweir     sal_Int32 nDateTokens = 1;
277cdf0e10cSrcweir     while ( *pStr )
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         if ( *pStr == '-' )
280cdf0e10cSrcweir             nDateTokens++;
281cdf0e10cSrcweir         pStr++;
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir     if ( nDateTokens > 3 || aDateStr.getLength() == 0 )
284cdf0e10cSrcweir         bSuccess = sal_False;
285cdf0e10cSrcweir     else
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         sal_Int32 n = 0;
288cdf0e10cSrcweir         nYear = aDateStr.getToken( 0, '-', n ).toInt32();
289cdf0e10cSrcweir         if ( nYear > 9999 )
290cdf0e10cSrcweir             bSuccess = sal_False;
291cdf0e10cSrcweir         else if ( nDateTokens >= 2 )
292cdf0e10cSrcweir         {
293cdf0e10cSrcweir             nMonth = aDateStr.getToken( 0, '-', n ).toInt32();
294cdf0e10cSrcweir             if ( nMonth > 12 )
295cdf0e10cSrcweir                 bSuccess = sal_False;
296cdf0e10cSrcweir             else if ( nDateTokens >= 3 )
297cdf0e10cSrcweir             {
298cdf0e10cSrcweir                 nDay = aDateStr.getToken( 0, '-', n ).toInt32();
299cdf0e10cSrcweir                 if ( nDay > 31 )
300cdf0e10cSrcweir                     bSuccess = sal_False;
301cdf0e10cSrcweir             }
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir     if ( bSuccess && aTimeStr.getLength() > 0 )         // time is optional
306cdf0e10cSrcweir     {
307cdf0e10cSrcweir         pStr = aTimeStr.getStr();
308cdf0e10cSrcweir         sal_Int32 nTimeTokens = 1;
309cdf0e10cSrcweir         while ( *pStr )
310cdf0e10cSrcweir         {
311cdf0e10cSrcweir             if ( *pStr == ':' )
312cdf0e10cSrcweir                 nTimeTokens++;
313cdf0e10cSrcweir             pStr++;
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir         if ( nTimeTokens > 3 )
316cdf0e10cSrcweir             bSuccess = sal_False;
317cdf0e10cSrcweir         else
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             sal_Int32 n = 0;
320cdf0e10cSrcweir             nHour = aTimeStr.getToken( 0, ':', n ).toInt32();
321cdf0e10cSrcweir             if ( nHour > 23 )
322cdf0e10cSrcweir                 bSuccess = sal_False;
323cdf0e10cSrcweir             else if ( nTimeTokens >= 2 )
324cdf0e10cSrcweir             {
325cdf0e10cSrcweir                 nMin = aTimeStr.getToken( 0, ':', n ).toInt32();
326cdf0e10cSrcweir                 if ( nMin > 59 )
327cdf0e10cSrcweir                     bSuccess = sal_False;
328cdf0e10cSrcweir                 else if ( nTimeTokens >= 3 )
329cdf0e10cSrcweir                 {
330cdf0e10cSrcweir                     nSec = aTimeStr.getToken( 0, ':', n ).toInt32();
331cdf0e10cSrcweir                     if ( nSec > 59 )
332cdf0e10cSrcweir                         bSuccess = sal_False;
333cdf0e10cSrcweir                 }
334cdf0e10cSrcweir             }
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     if ( bSuccess )
339cdf0e10cSrcweir     {
340cdf0e10cSrcweir         rDateTime.Day = sal::static_int_cast< sal_uInt16 >(nDay);
341cdf0e10cSrcweir         rDateTime.Month = sal::static_int_cast< sal_uInt16 >(nMonth);
342cdf0e10cSrcweir         rDateTime.Year = sal::static_int_cast< sal_uInt16 >(nYear);
343cdf0e10cSrcweir         rDateTime.Hours = sal::static_int_cast< sal_uInt16 >(nHour);
344cdf0e10cSrcweir         rDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(nMin);
345cdf0e10cSrcweir         rDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(nSec);
346cdf0e10cSrcweir     }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir     return bSuccess;
349cdf0e10cSrcweir }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 
352cdf0e10cSrcweir // ------------------------------------------------------------------------
353cdf0e10cSrcweir // ------------------------------------------------------------------------
354cdf0e10cSrcweir 
store(const uno::Reference<embed::XStorage> & xRoot,const uno::Sequence<util::RevisionTag> & rVersions)355cdf0e10cSrcweir void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions )
356cdf0e10cSrcweir     throw (::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
357cdf0e10cSrcweir {
358cdf0e10cSrcweir     // no storage, no version list!
359cdf0e10cSrcweir     if ( xRoot.is() )
360cdf0e10cSrcweir     {
361cdf0e10cSrcweir         // get the services needed for writing the xml data
362cdf0e10cSrcweir         Reference< lang::XMultiServiceFactory > xServiceFactory =
363cdf0e10cSrcweir                 comphelper::getProcessServiceFactory();
364cdf0e10cSrcweir         DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir         Reference< XInterface > xWriter (xServiceFactory->createInstance(
367cdf0e10cSrcweir                 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer"))));
368cdf0e10cSrcweir         DBG_ASSERT( xWriter.is(), "com.sun.star.xml.sax.Writer service missing" );
369cdf0e10cSrcweir 
370*940681c7SMatthias Seidel         // check whether there's already a sub storage with the version info
371cdf0e10cSrcweir         // and delete it
372cdf0e10cSrcweir         OUString sVerName( RTL_CONSTASCII_USTRINGPARAM( XMLN_VERSIONSLIST ) );
373cdf0e10cSrcweir 
374cdf0e10cSrcweir         // is this really needed, we set the size to zero before doing
375cdf0e10cSrcweir         // anything with this stream?
376cdf0e10cSrcweir /*      if ( xRoot->IsContained( sVerName ) )
377cdf0e10cSrcweir         {
378cdf0e10cSrcweir             xRoot->Remove( sVerName );
379cdf0e10cSrcweir             xRoot->Commit();
380cdf0e10cSrcweir         }
381cdf0e10cSrcweir */
382cdf0e10cSrcweir 		try {
383cdf0e10cSrcweir         	// open (create) the sub storage with the version info
384cdf0e10cSrcweir         	uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement(
385cdf0e10cSrcweir 											sVerName,
386cdf0e10cSrcweir 											embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
387cdf0e10cSrcweir 			if ( !xVerStream.is() )
388cdf0e10cSrcweir 				throw uno::RuntimeException();
389cdf0e10cSrcweir 
39086e1cf34SPedro Giffuni //REMOVE	        	// SetSize should not be necessary because OpenStream( WRITE|TRUNC ) should already
391cdf0e10cSrcweir //REMOVE	        	// have set the size to zero
392cdf0e10cSrcweir //REMOVE		//      xVerStream->SetSize ( 0L );
393cdf0e10cSrcweir //REMOVE	        	xVerStream->SetBufferSize( 16*1024 );
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         	Reference< io::XOutputStream > xOut = xVerStream->getOutputStream();
396cdf0e10cSrcweir 			if ( !xOut.is() )
397cdf0e10cSrcweir 				throw uno::RuntimeException(); // the stream was successfuly opened for writing already
398cdf0e10cSrcweir 
399cdf0e10cSrcweir         	Reference< io::XActiveDataSource > xSrc( xWriter, uno::UNO_QUERY );
400cdf0e10cSrcweir         	xSrc->setOutputStream(xOut);
401cdf0e10cSrcweir 
402cdf0e10cSrcweir         	Reference< XDocumentHandler > xHandler( xWriter, uno::UNO_QUERY );
403cdf0e10cSrcweir 
404cdf0e10cSrcweir             // XMLVersionListExport aExp( pList, sVerName, xHandler );
405cdf0e10cSrcweir             XMLVersionListExport aExp( xServiceFactory, rVersions, sVerName, xHandler );
406cdf0e10cSrcweir 
407cdf0e10cSrcweir         	aExp.exportDoc( ::xmloff::token::XML_VERSION );
408cdf0e10cSrcweir 
409cdf0e10cSrcweir //REMOVE	        	xVerStream->Commit();
410cdf0e10cSrcweir 			xVerStream = uno::Reference< io::XStream >(); // use refcounting for now to dispose
411cdf0e10cSrcweir 	//      xRoot->Commit();
412cdf0e10cSrcweir 		}
413cdf0e10cSrcweir 		catch( uno::Exception& )
414cdf0e10cSrcweir 		{
415cdf0e10cSrcweir             // TODO: error handling
416cdf0e10cSrcweir 		}
417cdf0e10cSrcweir     }
418cdf0e10cSrcweir }
419cdf0e10cSrcweir 
420cdf0e10cSrcweir // ------------------------------------------------------------------------
load(const uno::Reference<embed::XStorage> & xRoot)421cdf0e10cSrcweir uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot )
422cdf0e10cSrcweir         throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
423cdf0e10cSrcweir {
424cdf0e10cSrcweir     com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag > aVersions;
425cdf0e10cSrcweir 
426cdf0e10cSrcweir     const OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_VERSIONSLIST ) );
427cdf0e10cSrcweir 	uno::Reference< container::XNameAccess > xRootNames( xRoot, uno::UNO_QUERY );
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	try {
430cdf0e10cSrcweir     	if ( xRootNames.is() && xRootNames->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) )
431cdf0e10cSrcweir     	{
432cdf0e10cSrcweir         	Reference< lang::XMultiServiceFactory > xServiceFactory =
433cdf0e10cSrcweir                 	comphelper::getProcessServiceFactory();
434cdf0e10cSrcweir         	DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
435cdf0e10cSrcweir 
436cdf0e10cSrcweir         	InputSource aParserInput;
437cdf0e10cSrcweir 
438cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY );
439cdf0e10cSrcweir 			OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!\n" );
440cdf0e10cSrcweir 			if ( xProps.is() )
441cdf0e10cSrcweir 			{
442cdf0e10cSrcweir 				try {
443cdf0e10cSrcweir 					xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "URL" ) ) >>= aParserInput.sSystemId;
444cdf0e10cSrcweir 				}
445cdf0e10cSrcweir 				catch( uno::Exception& )
446cdf0e10cSrcweir 				{}
447cdf0e10cSrcweir 			}
448cdf0e10cSrcweir 
449cdf0e10cSrcweir         	uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement(
450cdf0e10cSrcweir 															sDocName,
451cdf0e10cSrcweir 															embed::ElementModes::READ );
452cdf0e10cSrcweir 			if ( !xDocStream.is() )
453cdf0e10cSrcweir 				throw uno::RuntimeException();
454cdf0e10cSrcweir 
455cdf0e10cSrcweir //REMOVE	        	xDocStream->Seek( 0L );
456cdf0e10cSrcweir //REMOVE	        	xDocStream->SetBufferSize( 16*1024 );
457cdf0e10cSrcweir 
458cdf0e10cSrcweir         	aParserInput.aInputStream = xDocStream->getInputStream();
459cdf0e10cSrcweir 			OSL_ENSURE( aParserInput.aInputStream.is(),
460cdf0e10cSrcweir 						"The stream was successfuly opened for reading, the input part must be accessible!\n" );
461cdf0e10cSrcweir 			if ( !aParserInput.aInputStream.is() )
462cdf0e10cSrcweir 				throw uno::RuntimeException();
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         	// get parser
465cdf0e10cSrcweir         	Reference< XInterface > xXMLParser = xServiceFactory->createInstance(
466cdf0e10cSrcweir             	OUString::createFromAscii("com.sun.star.xml.sax.Parser") );
467cdf0e10cSrcweir         	DBG_ASSERT( xXMLParser.is(),
468cdf0e10cSrcweir             		"XMLReader::Read: com.sun.star.xml.sax.Parser service missing" );
469cdf0e10cSrcweir 
470cdf0e10cSrcweir         	// get filter
471cdf0e10cSrcweir             // Reference< XDocumentHandler > xFilter = new XMLVersionListImport( pList );
472cdf0e10cSrcweir             Reference< XDocumentHandler > xFilter = new XMLVersionListImport( xServiceFactory, aVersions );
473cdf0e10cSrcweir 
474cdf0e10cSrcweir         	// connect parser and filter
475cdf0e10cSrcweir         	Reference< XParser > xParser( xXMLParser, UNO_QUERY );
476cdf0e10cSrcweir         	xParser->setDocumentHandler( xFilter );
477cdf0e10cSrcweir 
478cdf0e10cSrcweir         	// parse
479cdf0e10cSrcweir         	try
480cdf0e10cSrcweir         	{
481cdf0e10cSrcweir             	xParser->parseStream( aParserInput );
482cdf0e10cSrcweir         	}
483cdf0e10cSrcweir         	catch( SAXParseException&  ) {}
484cdf0e10cSrcweir         	catch( SAXException&  )      {}
485cdf0e10cSrcweir         	catch( io::IOException& )    {}
486cdf0e10cSrcweir     	}
487cdf0e10cSrcweir 	}
488cdf0e10cSrcweir 	catch( uno::Exception& )
489cdf0e10cSrcweir 	{
490cdf0e10cSrcweir         // TODO: error handling
491cdf0e10cSrcweir 	}
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     return aVersions;
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
XMLVersionListPersistence_getSupportedServiceNames()496cdf0e10cSrcweir uno::Sequence< rtl::OUString > SAL_CALL XMLVersionListPersistence_getSupportedServiceNames()
497cdf0e10cSrcweir 	throw()
498cdf0e10cSrcweir {
499cdf0e10cSrcweir 	const rtl::OUString aServiceName(
500cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.DocumentRevisionListPersistence" ) );
501cdf0e10cSrcweir 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
502cdf0e10cSrcweir 	return aSeq;
503cdf0e10cSrcweir }
504cdf0e10cSrcweir 
XMLVersionListPersistence_getImplementationName()505cdf0e10cSrcweir rtl::OUString SAL_CALL XMLVersionListPersistence_getImplementationName() throw()
506cdf0e10cSrcweir {
507cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLVersionListPersistence" ) );
508cdf0e10cSrcweir }
509cdf0e10cSrcweir 
XMLVersionListPersistence_createInstance(const uno::Reference<lang::XMultiServiceFactory> &)510cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL XMLVersionListPersistence_createInstance(
511cdf0e10cSrcweir 		const uno::Reference< lang::XMultiServiceFactory > &)
512cdf0e10cSrcweir 	throw( uno::Exception )
513cdf0e10cSrcweir {
514cdf0e10cSrcweir     return (cppu::OWeakObject*)new XMLVersionListPersistence;
515cdf0e10cSrcweir }
516cdf0e10cSrcweir 
XMLVersionImExportOOO_getSupportedServiceNames()517cdf0e10cSrcweir uno::Sequence< rtl::OUString > SAL_CALL XMLVersionImExportOOO_getSupportedServiceNames()
518cdf0e10cSrcweir 	throw()
519cdf0e10cSrcweir {
520cdf0e10cSrcweir 	const rtl::OUString aServiceName(
521cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.DocumentRevisionListPersistence" ) );
522cdf0e10cSrcweir 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
523cdf0e10cSrcweir 	return aSeq;
524cdf0e10cSrcweir }
525cdf0e10cSrcweir 
XMLVersionImExportOOO_getImplementationName()526cdf0e10cSrcweir rtl::OUString SAL_CALL XMLVersionImExportOOO_getImplementationName() throw()
527cdf0e10cSrcweir {
528cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLVersionImExportOOo" ) );
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
XMLVersionImExportOOO_createInstance(const uno::Reference<lang::XMultiServiceFactory> &)531cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL XMLVersionImExportOOO_createInstance(
532cdf0e10cSrcweir 		const uno::Reference< lang::XMultiServiceFactory > &)
533cdf0e10cSrcweir 	throw( uno::Exception )
534cdf0e10cSrcweir {
535cdf0e10cSrcweir     return (cppu::OWeakObject*)new XMLVersionListPersistence;
536cdf0e10cSrcweir }
537cdf0e10cSrcweir 
538