summaryrefslogtreecommitdiffstats
path: root/tags/cpp-1.0-incubating-M2-RC3/sca/runtime/core/src/tuscany/sca/util/Utils.cpp
blob: 447f498effcc5eda276f04784476ae9341c93537 (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
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#if defined(WIN32)  || defined (_WINDOWS)
#pragma warning(disable: 4786)
#endif

#include "tuscany/sca/util/Utils.h"

using namespace std;
using namespace commonj::sdo;
namespace tuscany
{
    namespace sca
    {
        void Utils::tokeniseUri(const string& uri, string& token1, string& token2)
        {
            tokeniseString("/", uri, token1, token2);
        }
    
        void Utils::tokeniseQName(const string& qname, string& uri, string& name)
        {
            tokeniseString("#", qname, uri, name);
            if (name == "")
            {
                name =  uri;
                uri = "";
            }
        }

        void Utils::tokeniseString(
            const string& separator,
            const string& str,
            string& token1,
            string& token2)
        {
            string::size_type sep = str.find(separator);
            if (sep != string::npos)
            {
                token1 = str.substr(0, sep);
                if ( (sep+1) < str.length())
                {
                    token2 = str.substr(sep+1);
                }
                else
                {
                    token2 = "";
                }
            }
            else
            {
                token1 = str;
                token2 = "";
            }
        }

        void Utils::rTokeniseString(
            const string& separator,
            const string& str,
            string& token1,
            string& token2)
        {
            string::size_type sep = str.rfind(separator);
            if (sep != string::npos)
            {
                token1 = str.substr(0, sep);
                if ( (sep+1) < str.length())
                {
                    token2 = str.substr(sep+1);
                }
                else
                {
                    token2 = "";
                }
            }
            else
            {
                token1 = "";
                token2 = str;
            }
        }
        
        void Utils::breakpoint() {
            // dummy method used to set breakpoints
        }
        
        //////////////////////////////////////////////////////////////////////////
        // Print a DatObject tree
        //////////////////////////////////////////////////////////////////////////
        void Utils::tabs(int inc)
        {
            for (int ind=0; ind <inc; ind++)
            {
                cout << "  ";
            }            
        }        

        const bool Utils::compareProperties(DataObjectPtr dataObject1, const Property& prop1, DataObjectPtr dataObject2, const Property& prop2, string& diff)
        {            
            if(strcmp(prop1.getName(),prop2.getName()) != 0)
            {
                diff.append("Differing names for Properties:\n");
                diff.append(prop1.getName());
                diff.append("\n");
                diff.append(prop2.getName());                        
                return false;
            }

            const Type& propertyType1 = prop1.getType();
            const Type& propertyType2 = prop2.getType();

            if(strcmp(propertyType1.getName(), propertyType2.getName()) != 0 ||
               strcmp(propertyType1.getURI(),propertyType2.getURI()) != 0 )
            {
                diff.append("Differing types for Properties:\n");
                diff.append(propertyType1.getName());
                diff.append("#");
                diff.append(propertyType1.getURI());
                diff.append("\n");
                diff.append(propertyType2.getName());
                diff.append("#");
                diff.append(propertyType2.getURI());
                return false;
            }
            if (dataObject1->isSet(prop1) != dataObject2->isSet(prop2))
            {
                diff.append("Property ");
                diff.append(prop1.getName());
                diff.append(" is set on one DataObject but not the other"); 
                return false;
            }
                                
            if (dataObject1->isSet(prop1))
            {
                
                if (prop1.isMany() != prop2.isMany())
                {
                    diff.append("Property ");
                    diff.append(prop1.getName());
                    diff.append(" is many on one DataObject but not the other"); 
                    return false;
                }
                if (propertyType1.isDataType() != propertyType2.isDataType())
                {
                    diff.append("Property ");
                    diff.append(prop1.getName());
                    diff.append(" is dataType on one DataObject but not the other"); 
                    return false;
                }

                //////////////////////////////////////////////////////////////////////
                // For a many-valued property get the list of values
                //////////////////////////////////////////////////////////////////////
                if (prop1.isMany())
                {
                    DataObjectList& dol1 = dataObject1->getList(prop1);
                    DataObjectList& dol2 = dataObject2->getList(prop2);
                    if (dol1.size() != dol2.size())
                    {
                        diff.append("Property ");
                        diff.append(prop1.getName());
                        diff.append(" is many but has differing number of elements"); 
                        return false;
                    }

                    for (int j = 0; j <dol1.size(); j++)
                    {                               

                        if (propertyType1.isDataType())
                        {
                            if( strcmp(dol1.getCString(j), dol2.getCString(j)) != 0)
                            {
                                diff.append("Differing value for Property ");
                                diff.append(prop1.getName());
                                diff.append("[");                               
                                diff += ((int)j);
                                diff.append("]:\n");
                                diff.append(dol1.getCString(j));
                                diff.append("\n");
                                diff.append(dol2.getCString(j));
                                return false;
                            }
                        }
                        else
                        {
                            if(!compareDataObjects(dol1[j], dol2[j], diff))
                            {
                                return false;
                            }
                        }
                    }
                } // end IsMany
                
                
                //////////////////////////////////////////////////////////////////////
                // For a primitive data type compare the values
                //////////////////////////////////////////////////////////////////////
                else if (propertyType1.isDataType())
                {
                    if( strcmp(dataObject1->getCString(prop1), dataObject2->getCString(prop2)) != 0)
                    {
                        diff.append("Differing value for Property ");
                        diff.append(prop1.getName());
                        diff.append(":\n");
                        diff.append(dataObject1->getCString(prop1));
                        diff.append("\n");
                        diff.append(dataObject2->getCString(prop2));
                        return false;
                    }
                }
                
                //////////////////////////////////////////////////////////////////////
                // For a dataobject compare the DOs
                //////////////////////////////////////////////////////////////////////
                else
                {
                    if(!compareDataObjects(dataObject1->getDataObject(prop1), dataObject2->getDataObject(prop2), diff))
                    {
                        return false;
                    }
                }
            }                    
            return true;
        }

        const bool Utils::compareDataObjects(DataObjectPtr dataObject1, DataObjectPtr dataObject2, string& diff)
        {
            if (!dataObject1 || !dataObject2)
            {
                diff.append("Cannot compare null DataObjects");
                return false;
            }

            const Type& dataObject1Type = dataObject1->getType();
            const Type& dataObject2Type = dataObject2->getType();

            if( strcmp(dataObject1Type.getURI(), dataObject2Type.getURI()) != 0 ||
                strcmp(dataObject1Type.getName(), dataObject2Type.getName()) != 0 )
            {
                diff.append("DataObject Types differ:\n");
                diff.append(dataObject1Type.getURI());
                diff.append("#");
                diff.append(dataObject1Type.getName());
                diff.append("\n");
                diff.append(dataObject2Type.getURI());
                diff.append("#");
                diff.append(dataObject2Type.getName());
                return false;
            }
            
            //////////////////////////////////////////////////////////////////////////
            // Iterate over all the properties
            //////////////////////////////////////////////////////////////////////////
            PropertyList pl1 = dataObject1->getInstanceProperties();
            PropertyList pl2 = dataObject2->getInstanceProperties();
            if (pl1.size() != pl2.size())
            {
                diff.append("Differing number of properties");
                return false;
            }

            if (pl1.size() != 0)
            {
                for (int i = 0; i < pl1.size(); i++)
                {
                    if(!compareProperties(dataObject1, pl1[i], dataObject2, pl2[i], diff))
                    {
                        return false;
                    }
                }
            }
            else
            {
                if(dataObject1->getType().isOpenType() != dataObject2->getType().isOpenType() && 
                   dataObject1->getType().isDataObjectType() != dataObject2->getType().isDataObjectType())
                {
                    diff.append("DataObject is open & DO type on one but not the other"); 
                    return false;
                }

                // Compare elements under an open DataObject 
                if(dataObject1->getType().isOpenType() && dataObject1->getType().isDataObjectType())
                {
                    SequencePtr sequence1 = dataObject1->getSequence();
                    SequencePtr sequence2 = dataObject2->getSequence();

                    if (sequence1 != NULL && sequence2 != NULL)
                    {
                        if (sequence1->size() != sequence1->size())
                        {
                            diff.append("Open DataObjects have differing number of elements"); 
                            return false;
                        }

                        for (int i = 0; i < sequence1->size(); i++)
                        {
                            if (sequence1->isText(i) != sequence2->isText(i))
                            {
                                diff.append("Open DataObjects have differing element types at position "); 
                                diff += ((int) i);
                                return false;
                            }
                            if (sequence1->isText(i))
                            {                                
                                if( strcmp(sequence1->getCStringValue(i), sequence2->getCStringValue(i)) != 0)
                                {
                                    diff.append("Differing value for element at position ");
                                    diff += ((int) i);
                                    diff.append(":\n");
                                    diff.append(sequence1->getCStringValue(i));
                                    diff.append("\n");
                                    diff.append(sequence2->getCStringValue(i));
                                    return false;
                                }
                            }
                            else 
                            {
                                const Property& p1 = sequence1->getProperty(i);
                                const Property& p2 = sequence2->getProperty(i);

                                if(!compareProperties(dataObject1, p1, dataObject2, p2, diff))
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }
    
        void Utils::printDO(DataObjectPtr dataObject, int increment)
        {
            int inc=increment;
            if (!dataObject)
                return;
            const Type& dataObjectType = dataObject->getType();
            tabs(inc);
            cout << "DataObject type: " << dataObjectType.getURI()<< "#" << dataObjectType.getName() << endl;
            inc++;
            
            //////////////////////////////////////////////////////////////////////////
            // Iterate over all the properties
            //////////////////////////////////////////////////////////////////////////
            PropertyList pl = dataObject->getInstanceProperties();
            if (pl.size() != 0)
            {
                for (int i = 0; i < pl.size(); i++)
                {
                    tabs(inc);
                    cout << "Property: " << pl[i].getName() << endl;
                    
                    const Type& propertyType = pl[i].getType();
                    
                    tabs(inc);
                    cout << "Property Type: " << propertyType.getURI()<< "#" << propertyType.getName() << endl;
                    
                    if (dataObject->isSet(pl[i]))
                    {
                        
                        //////////////////////////////////////////////////////////////////////
                        // For a many-valued property get the list of values
                        //////////////////////////////////////////////////////////////////////
                        if (pl[i].isMany())
                        {
                            inc++;
                            DataObjectList& dol = dataObject->getList(pl[i]);
                            for (int j = 0; j <dol.size(); j++)
                            {
                                tabs(inc);
                                cout << "Value " << j <<endl;
                                inc++;
                                
                                if (propertyType.isDataType())
                                {
                                    tabs(inc);
                                    cout<< "Property Value: " << dol.getCString(j) <<endl ; 
                                }
                                else
                                    printDO(dol[j], inc);
                                inc--;
                            }
                            inc--;
                        } // end IsMany
                        
                        
                        //////////////////////////////////////////////////////////////////////
                        // For a primitive data type print the value
                        //////////////////////////////////////////////////////////////////////
                        else if (propertyType.isDataType())
                        {
                            tabs(inc);
                            cout<< "Property Value: " << dataObject->getCString(pl[i]) <<endl ; 
                        }
                        
                        //////////////////////////////////////////////////////////////////////
                        // For a dataobject print the do
                        //////////////////////////////////////////////////////////////////////
                        else
                        {
                            inc++;
                            printDO(dataObject->getDataObject(pl[i]), inc);
                            inc--;
                        }
                    }
                    else
                    {
                        tabs(inc);
                        cout<< "Property Value: not set" <<endl ; 
                    }
                    
                }
            }
            else
            {
                // Print elements under an open DataObject 
                if(dataObject->getType().isOpenType() && dataObject->getType().isDataObjectType())
                {
                    SequencePtr sequence = dataObject->getSequence();
                    if (sequence != NULL)
                    {
                        for (int i = 0; i < sequence->size(); i++)
                        {
                            if (sequence->isText(i))
                            {
                                tabs(inc);
                                cout<< "Text Value: " << sequence->getCStringValue(i) <<endl ; 
                            }
                            else {
                                const Property& p = sequence->getProperty(i);
                                
                                tabs(inc);
                                cout << "Property: " << p.getName() << endl;
                                
                                const Type& propertyType = p.getType();
                                
                                tabs(inc);
                                cout << "Property Type: " << propertyType.getURI()<< "#" << propertyType.getName() << endl;
                                
                                if (dataObject->isSet(p))
                                {
                                    
                                    //////////////////////////////////////////////////////////////////////
                                    // For a many-valued property get the list of values
                                    //////////////////////////////////////////////////////////////////////
                                    if (p.isMany())
                                    {
                                        inc++;
                                        DataObjectList& dol = dataObject->getList(p);
                                        for (int j = 0; j <dol.size(); j++)
                                        {
                                            tabs(inc);
                                            cout << "Value " << j <<endl;
                                            inc++;
                                            
                                            if (propertyType.isDataType())
                                            {
                                                tabs(inc);
                                                cout<< "Property Value: " << dol.getCString(j) <<endl ; 
                                            }
                                            else
                                                printDO(dol[j], inc);
                                            inc--;
                                        }
                                        inc--;
                                    } // end IsMany
                                    
                                    
                                    //////////////////////////////////////////////////////////////////////
                                    // For a primitive data type print the value
                                    //////////////////////////////////////////////////////////////////////
                                    else if (propertyType.isDataType())
                                    {
                                        tabs(inc);
                                        cout<< "Property Value: " << dataObject->getCString(p) <<endl ; 
                                    }
                                    
                                    //////////////////////////////////////////////////////////////////////
                                    // For a dataobject print the do
                                    //////////////////////////////////////////////////////////////////////
                                    else
                                    {
                                        inc++;
                                        printDO(dataObject->getDataObject(p), inc);
                                        inc--;
                                    }
                                }
                                else
                                {
                                    tabs(inc);
                                    cout<< "Property Value: not set" <<endl ; 
                                }
                            }
                        }
                    }
                }
            }
            inc--;
        }
        
        void Utils::printTypes(DataFactoryPtr df) 
        {
            //////////////////////////////////////////////////////////////////////////
            // Retrieve the DataFactory from the mediator
            // get the list of Types in the DataFactory and list them
            //////////////////////////////////////////////////////////////////////////
            TypeList tl = df->getTypes();
            for (int i = 0; i < tl.size(); i++)
            {
                cout << "Type: " << tl[i].getURI()<< "#" << tl[i].getName() << endl;
                PropertyList pl = tl[i].getProperties();
                for (int j = 0; j < pl.size(); j++)
                {
                    cout << "\tProperty: " << pl[j].getName()
                        << " type: " <<pl[j].getType().getURI()<<"#"<<pl[j].getType().getName()<< endl;
                    
                }
            }
            
        }
        
        void Utils::printType(const Type& type, int increment) 
        {
            int inc = increment;
            tabs(inc);
            cout << "Type: " << type.getURI()<< "#" << type.getName() << endl;
            inc++;
            PropertyList pl = type.getProperties();
            for (int j = 0; j < pl.size(); j++)
            {
                tabs(inc);
                cout << "\tProperty: " << pl[j].getName()
                    << " type: " <<pl[j].getType().getURI()<<"#"<<pl[j].getType().getName()<< endl;
                inc++;
                printType(pl[j].getType(), inc);
                inc--;
            }
        }
        
    } // End namespace sca
} // End namespace tuscany