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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* 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_forms.hxx"
#include "model.hxx"
#include "model_helper.hxx"
#include "unohelper.hxx"
#include "binding.hxx"
#include "submission.hxx"
#include "mip.hxx"
#include "evaluationcontext.hxx"
#include "xmlhelper.hxx"
#include "datatyperepository.hxx"
#include "NameContainer.hxx"
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <tools/debug.hxx>
#include <comphelper/propertysetinfo.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <algorithm>
// UNO classes
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/xml/dom/XDocument.hpp>
#include <com/sun/star/xml/dom/XCharacterData.hpp>
#include <com/sun/star/xml/dom/NodeType.hpp>
#include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <com/sun/star/io/XInputStream.hpp>
using com::sun::star::lang::XMultiServiceFactory;
using com::sun::star::lang::XUnoTunnel;
using com::sun::star::beans::XPropertySet;
using com::sun::star::beans::PropertyValue;
using rtl::OUString;
using rtl::OUStringBuffer;
using com::sun::star::beans::PropertyVetoException;
using com::sun::star::beans::UnknownPropertyException;
using com::sun::star::util::VetoException;
using com::sun::star::lang::WrappedTargetException;
using com::sun::star::lang::IllegalArgumentException;
using com::sun::star::ucb::XSimpleFileAccess;
using com::sun::star::io::XInputStream;
using namespace com::sun::star::uno;
using namespace com::sun::star::xml::dom;
using namespace xforms;
#if OSL_DEBUG_LEVEL > 1
#define DBG_INVARIANT_TYPE(TYPE) class DBG_##TYPE { const TYPE* mpT; void check() { mpT->dbg_assertInvariant(); } public: DBG_##TYPE(const TYPE* pT) : mpT(pT) { check(); } ~DBG_##TYPE() { check(); } } _DBG_##TYPE(this);
#define DBG_INVARIANT() DBG_INVARIANT_TYPE(Model)
#else
#define DBG_INVARIANT_TYPE(TYPE)
#define DBG_INVARIANT()
#endif
//
// The Model
//
void Model::ensureAtLeastOneInstance()
{
if( ! mpInstances->hasItems() )
{
// create a default instance
newInstance( OUString(), OUString(), true );
}
}
/** Model default constructor; create empty model */
Model::Model() :
msID(),
mpBindings( NULL ),
mpSubmissions( NULL ),
mpInstances( new InstanceCollection ),
mxNamespaces( new NameContainer<OUString>() ),
mxBindings( mpBindings ),
mxSubmissions( mpSubmissions ),
mxInstances( mpInstances ),
mbInitialized( false ),
mbExternalData( true )
{
initializePropertySet();
// initialize bindings collections
// (not in initializer list to avoid use of incomplete 'this')
mpBindings = new BindingCollection( this );
mxBindings = mpBindings;
mpSubmissions = new SubmissionCollection( this );
mxSubmissions = mpSubmissions;
// invariant only holds after construction
DBG_INVARIANT();
}
Model::~Model() throw()
{
// give up bindings & submissions; the mxBindings/mxSubmissions
// references will then delete them
mpBindings = NULL;
mpSubmissions = NULL;
}
Model* lcl_getModel( const Reference<XUnoTunnel>& xTunnel )
{
Model* pModel = NULL;
if( xTunnel.is() )
pModel = reinterpret_cast<Model*>(
xTunnel->getSomething( Model::getUnoTunnelID() ) );
return pModel;
}
Model* Model::getModel( const Reference<XModel>& xModel )
{
return lcl_getModel( Reference<XUnoTunnel>( xModel, UNO_QUERY ) );
}
EvaluationContext Model::getEvaluationContext()
{
// the default context is the top-level element node. A default
// node (instanceData' is inserted when there is no default node
Reference<XDocument> xInstance = getDefaultInstance();
Reference<XNode> xElement( xInstance->getDocumentElement(), UNO_QUERY );
// no element found? Then insert default element 'instanceData'
if( ! xElement.is() )
{
xElement = Reference<XNode>(
xInstance->createElement( OUSTRING("instanceData") ),
UNO_QUERY_THROW );
Reference<XNode>( xInstance, UNO_QUERY_THROW)->appendChild( xElement );
}
OSL_ENSURE( xElement.is() &&
xElement->getNodeType() == NodeType_ELEMENT_NODE,
"no element in evaluation context" );
return EvaluationContext( xElement, this, mxNamespaces, 0, 1 );
}
Model::IntSequence_t Model::getUnoTunnelID()
{
static cppu::OImplementationId aImplementationId;
return aImplementationId.getImplementationId();
}
Model::XDocument_t Model::getForeignSchema() const
{
return mxForeignSchema;
}
void Model::setForeignSchema( const XDocument_t& rDocument )
{
mxForeignSchema = rDocument;
}
rtl::OUString Model::getSchemaRef() const
{
return msSchemaRef;
}
void Model::setSchemaRef( const rtl::OUString& rSchemaRef )
{
msSchemaRef = rSchemaRef;
}
Model::XNameContainer_t Model::getNamespaces() const
{
return mxNamespaces;
}
void Model::setNamespaces( const XNameContainer_t& rNamespaces )
{
if( rNamespaces.is() )
mxNamespaces = rNamespaces;
}
bool Model::getExternalData() const
{
return mbExternalData;
}
void Model::setExternalData( bool _bData )
{
mbExternalData = _bData;
}
#if OSL_DEBUG_LEVEL > 1
void Model::dbg_assertInvariant() const
{
OSL_ENSURE( mpInstances != NULL, "no instances found" );
OSL_ENSURE( mxInstances.is(), "No instance container!" );
OSL_ENSURE( mpBindings != NULL, "no bindings element" );
OSL_ENSURE( mxBindings.is(), "No Bindings container" );
OSL_ENSURE( mpSubmissions != NULL, "no submissions element" );
OSL_ENSURE( mxSubmissions.is(), "No Submission container" );
}
#endif
//
// MIP managment
//
void Model::addMIP( void* pTag, const XNode_t& xNode, const MIP& rMIP )
{
OSL_ENSURE( pTag != NULL, "empty tag?" );
OSL_ENSURE( xNode.is(), "no node" );
MIPs_t::value_type aValue( xNode, ::std::pair<void*,MIP>( pTag, rMIP ) );
maMIPs.insert( aValue );
}
void Model::removeMIPs( void* pTag )
{
OSL_ENSURE( pTag != NULL, "empty tag?" );
for( MIPs_t::iterator aIter = maMIPs.begin();
aIter != maMIPs.end(); )
{
if( aIter->second.first == pTag )
{
MIPs_t::iterator next( aIter ); ++next;
maMIPs.erase( aIter );
aIter = next;
}
else
++aIter;
}
}
MIP Model::queryMIP( const XNode_t& xNode ) const
{
// travel up inheritance chain and inherit MIPs
MIP aRet;
for( XNode_t xCurrent = xNode;
xCurrent.is();
xCurrent = xCurrent->getParentNode() )
{
// iterate over all MIPs for this node, and join MIPs
MIP aMIP;
MIPs_t::const_iterator aEnd = maMIPs.upper_bound( xCurrent );
MIPs_t::const_iterator aIter = maMIPs.lower_bound( xCurrent );
for( ; aIter != aEnd; ++aIter )
aMIP.join( aIter->second.second );
// inherit from current node (or set if we are at the start node)
if( xCurrent == xNode )
aRet = aMIP;
else
aRet.inherit( aMIP );
}
return aRet;
}
void Model::rebind()
{
OSL_ENSURE( mpBindings != NULL, "bindings?" );
// iterate over all bindings and call update
sal_Int32 nCount = mpBindings->countItems();
for( sal_Int32 i = 0; i < nCount; i++ )
{
Binding* pBind = Binding::getBinding( mpBindings->Collection<XPropertySet_t>::getItem( i ) );
OSL_ENSURE( pBind != NULL, "binding?" );
pBind->update();
}
}
void Model::deferNotifications( bool bDefer )
{
// iterate over all bindings and defer notifications
sal_Int32 nCount = mpBindings->countItems();
for( sal_Int32 i = 0; i < nCount; i++ )
{
Binding* pBind = Binding::getBinding( mpBindings->Collection<XPropertySet_t>::getItem( i ) );
OSL_ENSURE( pBind != NULL, "binding?" );
pBind->deferNotifications( bDefer );
}
}
bool Model::setSimpleContent( const XNode_t& xConstNode,
const rtl::OUString& sValue )
{
OSL_ENSURE( xConstNode.is(), "need node to set data" );
bool bRet = false;
if( xConstNode.is() )
{
// non-const node reference so we can assign children (if necessary)
XNode_t xNode( xConstNode );
switch( xNode->getNodeType() )
{
case NodeType_ELEMENT_NODE:
{
// find first text node child
Reference<XNode> xChild;
for( xChild = xNode->getFirstChild();
xChild.is() && xChild->getNodeType() != NodeType_TEXT_NODE;
xChild = xChild->getNextSibling() )
; // empty loop; only find first text node child
// create text node, if none is found
if( ! xChild.is() )
{
xChild = Reference<XNode>(
xNode->getOwnerDocument()->createTextNode( OUString() ),
UNO_QUERY_THROW );
xNode->appendChild( xChild );
}
xNode = xChild;
OSL_ENSURE( xNode.is() &&
xNode->getNodeType() == NodeType_TEXT_NODE,
"text node creation failed?" );
}
// no break; continue as with text node:
case NodeType_TEXT_NODE:
case NodeType_ATTRIBUTE_NODE:
{
// set the node value (defer notifications)
if( xNode->getNodeValue() != sValue )
{
deferNotifications( true );
xNode->setNodeValue( sValue );
deferNotifications( false );
}
bRet = true;
}
break;
default:
{
OSL_FAIL( "bound to unknown node type?" );
}
break;
}
}
return bRet;
}
void Model::loadInstance( sal_Int32 nInstance )
{
Sequence<PropertyValue> aSequence = mpInstances->getItem( nInstance );
// find URL from instance
OUString sURL;
bool bOnce = false;
getInstanceData( aSequence, NULL, NULL, &sURL, &bOnce );
// if we have a URL, load the document and set it into the instance
if( sURL.getLength() > 0 )
{
try
{
Reference<XInputStream> xInput =
Reference<XSimpleFileAccess>(
createInstance(
OUSTRING("com.sun.star.ucb.SimpleFileAccess") ),
UNO_QUERY_THROW )->openFileRead( sURL );
if( xInput.is() )
{
Reference<XDocument> xInstance =
getDocumentBuilder()->parse( xInput );
if( xInstance.is() )
{
OUString sEmpty;
setInstanceData( aSequence, NULL, &xInstance,
bOnce ? &sEmpty : &sURL, NULL);
mpInstances->setItem( nInstance, aSequence );
}
}
}
catch( const Exception& )
{
// couldn't load the instance -> ignore!
}
}
}
void Model::loadInstances()
{
// iterate over instance array to get PropertyValue-Sequence
const sal_Int32 nInstances = mpInstances->countItems();
for( sal_Int32 nInstance = 0; nInstance < nInstances; nInstance++ )
{
loadInstance( nInstance );
}
}
bool Model::isInitialized() const
{
return mbInitialized;
}
bool Model::isValid() const
{
bool bValid = true;
sal_Int32 nCount = mpBindings->countItems();
for( sal_Int32 i = 0; bValid && i < nCount; i++ )
{
Binding* pBind = Binding::getBinding( mpBindings->Collection<XPropertySet_t>::getItem( i ) );
OSL_ENSURE( pBind != NULL, "binding?" );
bValid = pBind->isValid();
}
return bValid;
}
//
// implement xforms::XModel
//
rtl::OUString Model::getID()
throw( RuntimeException )
{
DBG_INVARIANT();
return msID;
}
void Model::setID( const rtl::OUString& sID )
throw( RuntimeException )
{
DBG_INVARIANT();
msID = sID;
}
void Model::initialize()
throw( RuntimeException )
{
DBG_ASSERT( ! mbInitialized, "model already initialized" );
// load instances
loadInstances();
// let's pretend we're initialized and rebind all bindings
mbInitialized = true;
rebind();
}
void Model::rebuild()
throw( RuntimeException )
{
if( ! mbInitialized )
initialize();
else
rebind();
}
void Model::recalculate()
throw( RuntimeException )
{
rebind();
}
void Model::revalidate()
throw( RuntimeException )
{
// do nothing. We don't validate anyways!
}
void Model::refresh()
throw( RuntimeException )
{
rebind();
}
void SAL_CALL Model::submitWithInteraction(
const rtl::OUString& sID,
const XInteractionHandler_t& _rxHandler )
throw( VetoException,
WrappedTargetException,
RuntimeException )
{
DBG_INVARIANT();
if( mpSubmissions->hasItem( sID ) )
{
Submission* pSubmission =
Submission::getSubmission( mpSubmissions->getItem( sID ) );
OSL_ENSURE( pSubmission != NULL, "no submission?" );
OSL_ENSURE( pSubmission->getModel() == Reference<XModel>( this ),
"wrong model" );
// submit. All exceptions are allowed to leave.
pSubmission->submitWithInteraction( _rxHandler );
}
}
void Model::submit( const rtl::OUString& sID )
throw( VetoException, WrappedTargetException, RuntimeException )
{
submitWithInteraction( sID, NULL );
}
Model::XDataTypeRepository_t SAL_CALL Model::getDataTypeRepository( )
throw( RuntimeException )
{
if ( !mxDataTypes.is() )
mxDataTypes = new ODataTypeRepository;
return mxDataTypes;
}
//
// instance management
//
Model::XSet_t Model::getInstances()
throw( RuntimeException )
{
return mxInstances;
}
Model::XDocument_t Model::getInstanceDocument( const rtl::OUString& rName )
throw( RuntimeException )
{
ensureAtLeastOneInstance();
Reference<XDocument> aInstance;
sal_Int32 nInstance = lcl_findInstance( mpInstances, rName );
if( nInstance != -1 )
getInstanceData( mpInstances->getItem( nInstance ),
NULL, &aInstance, NULL, NULL );
return aInstance;
}
Model::XDocument_t SAL_CALL Model::getDefaultInstance()
throw( RuntimeException )
{
ensureAtLeastOneInstance();
DBG_ASSERT( mpInstances->countItems() > 0, "no instance?" );
Reference<XDocument> aInstance;
getInstanceData( mpInstances->getItem( 0 ), NULL, &aInstance, NULL, NULL );
return aInstance;
}
//
// bindings management
//
Model::XPropertySet_t SAL_CALL Model::createBinding()
throw( RuntimeException )
{
DBG_INVARIANT();
return new Binding();
}
Model::XPropertySet_t Model::cloneBinding( const XPropertySet_t& xBinding )
throw( RuntimeException )
{
DBG_INVARIANT();
XPropertySet_t xNewBinding = createBinding();
copy( xBinding, xNewBinding );
return xNewBinding;
}
Model::XPropertySet_t Model::getBinding( const rtl::OUString& sId )
throw( RuntimeException )
{
DBG_INVARIANT();
return mpBindings->hasItem( sId ) ? mpBindings->getItem( sId ) : NULL;
}
Model::XSet_t Model::getBindings()
throw( RuntimeException )
{
DBG_INVARIANT();
return mxBindings;
}
//
// submission management
//
Model::XSubmission_t Model::createSubmission()
throw( RuntimeException )
{
DBG_INVARIANT();
return new Submission();
}
Model::XSubmission_t Model::cloneSubmission(const XPropertySet_t& xSubmission)
throw( RuntimeException )
{
DBG_INVARIANT();
XSubmission_t xNewSubmission = createSubmission();
XPropertySet_t xAsPropertySet( xNewSubmission.get() );
copy( xSubmission.get(), xAsPropertySet );
return xNewSubmission;
}
Model::XSubmission_t Model::getSubmission( const rtl::OUString& sId )
throw( RuntimeException )
{
DBG_INVARIANT();
XSubmission_t xSubmission;
if ( mpSubmissions->hasItem( sId ) )
xSubmission = xSubmission.query( mpSubmissions->getItem( sId ) );
return xSubmission;
}
Model::XSet_t Model::getSubmissions()
throw( RuntimeException )
{
DBG_INVARIANT();
return mxSubmissions;
}
//
// implement XPropertySet & friends
//
#define HANDLE_ID 0
#define HANDLE_Instance 1
#define HANDLE_InstanceURL 2
#define HANDLE_ForeignSchema 3
#define HANDLE_SchemaRef 4
#define HANDLE_Namespaces 5
#define HANDLE_ExternalData 6
#define REGISTER_PROPERTY( property, type ) \
registerProperty( PROPERTY( property, type ), \
new DirectPropertyAccessor< Model, type >( this, &Model::set##property, &Model::get##property ) );
#define REGISTER_PROPERTY_API( property, type ) \
registerProperty( PROPERTY( property, type ), \
new APIPropertyAccessor< Model, type >( this, &Model::set##property, &Model::get##property ) );
#define REGISTER_BOOL_PROPERTY( property ) \
registerProperty( PROPERTY( property, sal_Bool ), \
new BooleanPropertyAccessor< Model, bool >( this, &Model::set##property, &Model::get##property ) );
void Model::initializePropertySet()
{
REGISTER_PROPERTY_API ( ID, OUString );
REGISTER_PROPERTY ( ForeignSchema, XDocument_t );
REGISTER_PROPERTY ( SchemaRef, OUString );
REGISTER_PROPERTY ( Namespaces, XNameContainer_t );
REGISTER_BOOL_PROPERTY( ExternalData );
}
void Model::update()
throw( RuntimeException )
{
rebuild();
}
sal_Int64 Model::getSomething( const IntSequence_t& xId )
throw( RuntimeException )
{
return reinterpret_cast<sal_Int64>( ( xId == getUnoTunnelID() ) ? this : NULL );
}
Sequence<sal_Int8> Model::getImplementationId()
throw( RuntimeException )
{
return getUnoTunnelID();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|