summaryrefslogtreecommitdiff
path: root/configmgr/source/backend/binarywritehandler.cxx
blob: bb168cb8a42f53660bb1dd0785e8890ab838b9d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: binarywritehandler.cxx,v $
 * $Revision: 1.12 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_configmgr.hxx"
#include "binarywritehandler.hxx"
#include "binarycache.hxx"
#include <com/sun/star/util/XTimeStamped.hpp>
#include "typeconverter.hxx"
#include "simpletypehelper.hxx"

#define ASCII(x) rtl::OUString::createFromAscii(x)		
namespace configmgr
{
    // -----------------------------------------------------------------------------
    namespace backend
    {
        
        namespace css = com::sun::star;
        namespace util = css::util ;
        
        // -----------------------------------------------------------------------------
            
        BinaryWriteHandler::BinaryWriteHandler( rtl::OUString const & _aFileURL, 
                                                rtl::OUString const & _aComponentName, 
                                                uno::Reference<lang::XMultiServiceFactory> const & _aFactory)
        : m_BinaryWriter(_aFileURL,_aFactory)
        , m_aComponentName(_aComponentName)
        {

        }			
        // -----------------------------------------------------------------------------
        static
        binary::ValueFlags::Type convertTypeToValueType(uno::Type const& _aType)
        {
            binary::ValueFlags::Type eType = binary::ValueFlags::val_invalid; 
            uno::TypeClass const aClass = _aType.getTypeClass();
            switch(aClass)
            {
            case uno::TypeClass_ANY:
                eType = binary::ValueFlags::val_any;
                break;
            case uno::TypeClass_BOOLEAN:
                eType = binary::ValueFlags::val_boolean;
                break;
            case uno::TypeClass_SHORT:
                eType = binary::ValueFlags::val_int16;
                break;
            case uno::TypeClass_LONG:
                eType = binary::ValueFlags::val_int32;
                break;
            case uno::TypeClass_HYPER:
                eType = binary::ValueFlags::val_int64;
                break;
            case uno::TypeClass_DOUBLE:
                eType = binary::ValueFlags::val_double;
                break;
            case uno::TypeClass_STRING:
                eType = binary::ValueFlags::val_string;
                break;
            case uno::TypeClass_SEQUENCE:
                if (_aType == SimpleTypeHelper::getBinaryType())
                {
                    eType = binary::ValueFlags::val_binary;
                }
                else
                {
                    uno::Type aType = configmgr::getSequenceElementType(_aType);
                    eType = convertTypeToValueType(aType);

                    OSL_ENSURE(!(eType & binary::ValueFlags::seq), "Binary Writer - Invalid value type: Multiple nesting of sequences");
                    eType = binary::ValueFlags::Type( eType | binary::ValueFlags::seq );
                }
                break;
            default:
                OSL_ENSURE(false, "Binary Writer - Invalid value type: not supported");
                break;
            }
            return eType;
        }
            
        // -----------------------------------------------------------------------------
        template <class T>
        inline /* make performance crew happy ;-) */
        void writeDirectly(BinaryWriter& _rWriter, T const& _aVal)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            _rWriter.write(_aVal);
        }
        // -----------------------------------------------------------------------------
        static
        inline
        void writeDirectly(BinaryWriter& _rWriter, uno::Sequence<sal_Int8> const& _aBinary);
        // -----------------------------------------------------------------------------
        template <class T>
        inline /* make performance crew happy ;-) */
        void writeFromAny(BinaryWriter& _rWriter,uno::Any const& _aValue, T& _aVar)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            OSL_VERIFY(_aValue >>= _aVar ); // "Invalid Any for value"
            writeDirectly(_rWriter,_aVar);
        }
        // -----------------------------------------------------------------------------

        template <class Element>
        void writeSequence(BinaryWriter& _rWriter, uno::Sequence< Element > const& aSequence)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            sal_Int32 const nLength = aSequence.getLength();
            _rWriter.write(nLength);

            for(sal_Int32 i=0; i<nLength; ++i) 
            {
                writeDirectly(_rWriter, aSequence[i]);
            }
        }
        // -----------------------------------------------------------------------------
        static
        inline
        void writeDirectly(BinaryWriter& _rWriter, uno::Sequence<sal_Int8> const& _aBinary)
        {
            writeSequence(_rWriter,_aBinary);
        }
        // -----------------------------------------------------------------------------
        #define CASE_WRITE_SEQUENCE(TYPE_CLASS, DATA_TYPE)	\
        case TYPE_CLASS:								\
        {												\
            uno::Sequence< DATA_TYPE > aData;				\
            OSL_ASSERT( ::getCppuType(aData.getConstArray()).getTypeClass() == (TYPE_CLASS) );	\
            OSL_VERIFY( _aValue >>= aData );										\
            writeSequence(_rWriter,aData);						\
        }	break						
        
        // -----------------------------------------------------------------------------
        static
        void  writeSequenceValue(BinaryWriter& _rWriter,  uno::Any const& _aValue, uno::Type const& aElementType)
        SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            switch(aElementType.getTypeClass())
            {
                CASE_WRITE_SEQUENCE( uno::TypeClass_BOOLEAN, sal_Bool );

                CASE_WRITE_SEQUENCE( uno::TypeClass_SHORT, sal_Int16 );

                CASE_WRITE_SEQUENCE( uno::TypeClass_LONG, sal_Int32 );

                CASE_WRITE_SEQUENCE( uno::TypeClass_HYPER, sal_Int64 );

                CASE_WRITE_SEQUENCE( uno::TypeClass_DOUBLE, double );

                CASE_WRITE_SEQUENCE( uno::TypeClass_STRING, rtl::OUString );

                CASE_WRITE_SEQUENCE( uno::TypeClass_SEQUENCE, uno::Sequence<sal_Int8> );

                default: 
                    OSL_ENSURE(false, "Unexpected typeclass for sequence elements");
                    break;
            }
        }
        #undef CASE_WRITE_SEQUENCE
        
        // -----------------------------------------------------------------------------
        static
        void  writeSimpleValue(BinaryWriter& _rWriter, uno::Any const& _aValue, uno::Type const& _aType)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            // PRE: Header must be written
            uno::TypeClass aDestinationClass = _aType.getTypeClass();
            switch (aDestinationClass)
            {
                case uno::TypeClass_BOOLEAN:
                {
                    sal_Bool bValue = sal_False;
                    writeFromAny(_rWriter, _aValue, bValue);
                    break;
                }
                case uno::TypeClass_BYTE:
                {
                    sal_Int8 nValue = 0;
                    writeFromAny(_rWriter, _aValue, nValue);
                    break;
                }
                case uno::TypeClass_SHORT:
                {
                    sal_Int16 nValue = 0;
                    writeFromAny(_rWriter, _aValue, nValue);
                    break;
                }
                case uno::TypeClass_LONG:
                {
                    sal_Int32 nValue = 0;
                    writeFromAny(_rWriter, _aValue, nValue);
                    break;
                }
                case uno::TypeClass_HYPER:
                {
                    sal_Int64 nValue = 0;
                    writeFromAny(_rWriter, _aValue, nValue);
                    break;
                }
                case uno::TypeClass_DOUBLE:
                {
                    double nValue = 0;
                    writeFromAny(_rWriter, _aValue, nValue);
                    break;
                }
                case uno::TypeClass_STRING:
                {
                    rtl::OUString aStr;
                    writeFromAny(_rWriter, _aValue, aStr);
                    break;
                }
                case uno::TypeClass_SEQUENCE:
                {
                    OSL_ENSURE (_aType == SimpleTypeHelper::getBinaryType(),"Unexpected sequence as simple type");
                    uno::Sequence<sal_Int8> aBinary;
                    writeFromAny(_rWriter, _aValue, aBinary);
                    break;
                }
                default: 
                    OSL_ENSURE(false, "Unexpected typeclass for simple value");
                    break;
            }
        }
                    
        // -----------------------------------------------------------------------------
            
        void BinaryWriteHandler::writeValue(uno::Any const& _aValue)
        {
            bool bSeq;
            uno::Type aTargetType = getBasicType(_aValue.getValueType(), bSeq);

            if (!bSeq)
            {
                writeSimpleValue(m_BinaryWriter,_aValue, aTargetType);
            }
            else
            {
                writeSequenceValue(m_BinaryWriter,_aValue, aTargetType);
            }
        }
        // -----------------------------------------------------------------------------	

        void BinaryWriteHandler::writeFileHeader(   rtl::OUString const & _aSchemaVersion, 
                                                    const uno::Sequence<rtl::OUString> & aKnownLocales,
                                                    const uno::Sequence<rtl::OUString> & aDataLocales  )
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            m_BinaryWriter.write(binary::CFG_BINARY_MAGIC);
            m_BinaryWriter.write(binary::CFG_BINARY_VERSION);
            m_BinaryWriter.write(_aSchemaVersion);
            writeSequence(m_BinaryWriter,aKnownLocales);
            writeSequence(m_BinaryWriter,aDataLocales);
            m_BinaryWriter.write(m_aComponentName);
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::writeGroupNode(rtl::OUString const& _aName,node::Attributes const& _aAttributes )
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            writeAttributes(_aAttributes );
            m_BinaryWriter.write(_aName);
        }
        
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::writeSetNode(rtl::OUString const& _aName,
                                              rtl::OUString const& _aTemplateName,
                                              rtl::OUString const& _aTemplateModule,
                                              node::Attributes const& _aAttributes)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            writeAttributes(_aAttributes );
            m_BinaryWriter.write(_aName);
            m_BinaryWriter.write(_aTemplateName);
            m_BinaryWriter.write(_aTemplateModule);
        }
        // -----------------------------------------------------------------------------
        
        void BinaryWriteHandler::writeLayerInfoList(uno::Reference<backenduno::XLayer> const * pLayers, sal_Int32 nNumLayers)                    
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            
            m_BinaryWriter.write(nNumLayers);
            for (sal_Int32 i = 0 ; i < nNumLayers ; ++ i) 
            {
                uno::Reference<util::XTimeStamped> xTimeStamp = uno::Reference<util::XTimeStamped>(pLayers[i], uno::UNO_QUERY);

                rtl::OUString aTimeStamp = xTimeStamp.is() ? xTimeStamp->getTimestamp() : rtl::OUString();
                m_BinaryWriter.write(aTimeStamp);
            }
            
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::writeNodeType(binary::NodeType::Type _eType)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            sal_Int8 nValue = static_cast< sal_Int8 >( _eType );
            m_BinaryWriter.write(nValue);
        
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::writeAttributes(node::Attributes const& _aAttributes)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
                    sal_Int8 nValue = static_cast< sal_Int8 >( _aAttributes.state() );

            OSL_ASSERT(0 <= nValue && nValue <= 3);

            OSL_ENSURE(!_aAttributes.isReadonly() || !_aAttributes.isFinalized(),"Unexpected attribute mix: node is both read-only and finalized");

            nValue |= (_aAttributes.isReadonly()     ? 1 : 0) << 2;
            nValue |= (_aAttributes.isFinalized()    ? 1 : 0) << 3;

            nValue |= (_aAttributes.isNullable()	  ? 1 : 0) << 4;
            nValue |= (_aAttributes.isLocalized()	  ? 1 : 0) << 5;

            nValue |= (_aAttributes.isMandatory()	  ? 1 : 0) << 6;
            nValue |= (_aAttributes.isRemovable()	  ? 1 : 0) << 7;
            
            m_BinaryWriter.write(nValue);
        
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::writeValueNode(rtl::OUString const& _aName, 
                                                node::Attributes const& _aAttributes,
                                                uno::Type const& _aType, 
                                                uno::Any const& _aUserValue, 
                                                uno::Any const& _aDefaultValue)
        SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            //write value flags
            binary::ValueFlags::Type eType = convertTypeToValueType(_aType);
            sal_Int8 nValueType = sal_Int8(eType);
            
            bool hasUserValue   = _aUserValue.hasValue();
            bool hasDefault     = _aDefaultValue.hasValue();

            if (!hasUserValue)  nValueType |=  binary::ValueFlags::first_value_NULL;
            if (!hasDefault)    nValueType |=  binary::ValueFlags::second_value_NULL;

            m_BinaryWriter.write(nValueType);
            writeAttributes(_aAttributes );
            m_BinaryWriter.write(_aName);	
            
            if(hasUserValue)    
            {
                OSL_ENSURE(_aUserValue.getValueType() == _aType, "Type mismatch in value node");
                writeValue(_aUserValue);
            }
            if(hasDefault)
            {
                OSL_ENSURE(_aDefaultValue.getValueType() == _aType, "Type mismatch in value node");
                writeValue(_aDefaultValue);
            }
        }
        // -----------------------------------------------------------------------------
        
        void BinaryWriteHandler::writeStop()
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            sal_Int8 nStopValue = 0;
            m_BinaryWriter.write(nStopValue);
        }
         // -----------------------------------------------------------------------------	

        void BinaryWriteHandler::writeComponentTree(const ISubtree * _pComponentTree)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            this->writeNodeType(binary::NodeType::component);
            if (_pComponentTree)
            {
                this->writeTree(*_pComponentTree);
                this->writeStop();
            }
            else
            {
                this->writeNodeType(binary::NodeType::nodata);
            }
        }
         // -----------------------------------------------------------------------------	

        void BinaryWriteHandler::writeTemplatesTree(const ISubtree * _pTemplatesTree)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            this->writeNodeType(binary::NodeType::templates);
            if (_pTemplatesTree)
            {
                this->writeTree(*_pTemplatesTree);
                this->writeStop();
            }
            else
            {
                this->writeNodeType(binary::NodeType::nodata);
            }
        }
         // -----------------------------------------------------------------------------	

        void BinaryWriteHandler::writeTree(const ISubtree & rTree)
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            if ( rTree.isSetNode() )
            {
                this->writeNodeType(binary::NodeType::setnode);
                this->writeSetNode( rTree.getName(),
                                    rTree.getElementTemplateName(),
                                    rTree.getElementTemplateModule(),
                                    rTree.getAttributes());
            }
            else
            {
                this->writeNodeType(binary::NodeType::groupnode);
                this->writeGroupNode( rTree.getName(), rTree.getAttributes() );
            }	
        
            // process children
            this->applyToChildren(rTree);	

            this->writeStop();
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::handle(const ISubtree & rTree)
        {
            writeTree(rTree);
        }
        // -----------------------------------------------------------------------------

        void BinaryWriteHandler::handle(const ValueNode & rValue)
        {
            this->writeNodeType(binary::NodeType::valuenode);

            this->writeValueNode(   rValue.getName(), 
                                    rValue.getAttributes(),
                                    rValue.getValueType(),
                                    rValue.getUserValue(),
                                    rValue.getDefault() );
        }
        // -----------------------------------------------------------------------------

        bool BinaryWriteHandler::generateHeader(const uno::Reference<backenduno::XLayer> * pLayers,
                                                 sal_Int32 nNumLayers,
                                                const rtl::OUString& aEntity, 
                                                const std::vector< com::sun::star::lang::Locale > & aKnownLocales )
            SAL_THROW( (io::IOException, uno::RuntimeException) )
        {
            //Open the writer
            if (!m_BinaryWriter.open())
                return false;

            this->writeFileHeader(  aEntity, 
                                    localehelper::makeIsoSequence(aKnownLocales), 
                                    getAvailableLocales(pLayers,nNumLayers) );

            this->writeLayerInfoList(pLayers, nNumLayers);
            return true; 
        }
        // -----------------------------------------------------------------------------

    }
   // -----------------------------------------------------------------------------
}