Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2263 kivins 1
//---------------------------------------------------------------------------
2
 
3
#include <vcl.h>
4
#include <IniFiles.hpp>	// Pascal unit
5
#pragma hdrstop
6
 
7
#include "TXMLSchema.h"
8
#include "XMLSchemaHandles.h"
9
#include "XMLSchemaProxyDatatypes.h"
10
 
11
#include "ISchemaElement.h"
12
#include "ISchemaValidationObserver.h"
13
 
14
#include "IXmlSchemaWrapperFactory.h"
15
#include "IXmlSchemaWrapperHandle.h"
16
#include "IXmlSchemaWrapperQualification.h"
17
#include "IXmlSchemaWrapperSchema.h"
18
#include "IXmlSchemaWrapperStream.h"
19
#include "IXmlSchemaWrapperString.h"
20
#include "IXmlSchemaWrapperStringCollection.h"
21
 
22
using namespace XMLSchema;
23
 
24
#pragma package(smart_init)
25
 
26
class nullValidationObserver : public virtual ISchemaValidationObserver
27
{
28
public:
29
	virtual void validationFailure( const XMLSchema::ISchemaElement & schemaElement,
30
									const char *constraint,
31
									const char *message )
32
	{
33
	}
34
 
35
	virtual void validationSuccess( const XMLSchema::ISchemaElement& schemaElement,
36
									const char *constraint,
37
									const char *message )
38
	{
39
	}
40
};
41
 
42
 
43
//---------------------------------------------------------------------------
44
// ValidCtrCheck is used to assure that the components created do not have
45
// any pure virtual functions.
46
//
47
 
48
static inline void ValidCtrCheck(TXMLSchema *)
49
{
50
	new TXMLSchema(NULL);
51
}
52
//---------------------------------------------------------------------------
53
__fastcall TXMLSchema::TXMLSchema(TComponent* Owner)
54
		: TComponent(Owner),
55
		  m_xmlSchemaWrapper( 0 ),
56
		  m_getXmlSchemaWrapperFactory( 0 ),
57
		  m_xmlSchemaWrapperFactory( 0 ),
58
		  m_xmlSchema( 0 )
59
{
60
	setProfile( "XMLSchema.ini" );
61
	readProfile();
62
}
63
//---------------------------------------------------------------------------
64
namespace Xmlschema
65
{
66
	void __fastcall PACKAGE Register()
67
	{
68
    	TComponentClass classes[1] = {__classid(TXMLSchema)};
69
        RegisterComponents("XML Schema", classes, 0);
70
    }
71
}
72
//------------------------------------------------------------------------------
73
 
74
 
75
//------------------------------------------------------------------------------
76
void __fastcall TXMLSchema::setProfile( const AnsiString & profile )
77
{
78
	m_profile = profile;
79
}
80
 
81
 
82
//------------------------------------------------------------------------------
83
AnsiString & __fastcall TXMLSchema::prependMassConfig( AnsiString & string, const AnsiString & filename )
84
{
85
	// any absolute path characters at start of filename implies we ignore MASS_CONFIG
86
	switch ( filename[ 1 ] )
87
	{
88
		case '.':
89
		case '/':
90
		case '\\':
91
		case 0:		// save checking length == 0 for next test
92
			return ( string = filename );
93
	}
94
 
95
	// check if drive letter at start of filename
96
	if ( filename.Length() >= 3
97
			&& isalpha( filename[ 1 ] ) != 0			// a-z or A-Z
98
			&& filename[ 2 ] == ':'						// :
99
			&& strchr( "/\\", filename[ 3 ] ) != 0 )	// / or \\
100
	{
101
		return ( string = filename );
102
	}
103
 
104
	char path[ 1024 ];
105
	if ( GetEnvironmentVariable( "CONFIGPATH", path, sizeof( path ) ) )
106
	{
107
		string = path;
108
	}
109
	else
110
	{
111
		string = "";
112
	}
113
 
114
	// make sure we have the path delimiter
115
	if ( !string.IsEmpty() &&
116
		 strchr( "/\\", string[ string.Length() ] ) == 0 )
117
	{
118
		string += "\\";
119
	}
120
	string += filename;
121
 
122
	return ( string );
123
}
124
 
125
 
126
//------------------------------------------------------------------------------
127
void __fastcall TXMLSchema::readProfile()
128
{
129
	TIniFile * profile = 0;
130
	try
131
	{
132
		AnsiString file;
133
 
134
		profile = new TIniFile( prependMassConfig( file, m_profile ) );
135
 
136
		m_xmlSchemaWrapperLibrary = profile->ReadString( "Init", "Wrapper", "XmlSchemaWrapperD.dll" );
137
 
138
		if ( m_xmlSchemaWrapperLibrary.IsEmpty() )
139
		{
140
			Application->MessageBox(
141
				"Attribute \"Wrapper\" in section \"Init\" is empty.  Cannot load XML Schema Wrapper.",
142
				"TXMLSchema",
143
				MB_OK );
144
		}
145
	}
146
	__finally
147
	{
148
		delete profile;
149
		profile = 0;
150
	}
151
}
152
 
153
 
154
//------------------------------------------------------------------------------
155
void __fastcall TXMLSchema::SetActive(bool Value)
156
{
157
	if ( Value == false )
158
	{
159
		m_active = false;
160
 
161
		if ( m_xmlSchemaWrapperFactory )
162
		{
163
			if ( m_xmlSchema )
164
			{
165
				m_xmlSchema->close();
166
 
167
				m_xmlSchemaWrapperFactory->destroySchema( *m_xmlSchema );
168
				m_xmlSchema = 0;
169
			}
170
 
171
			m_xmlSchemaWrapperFactory->stop();
172
			m_xmlSchemaWrapperFactory = 0;
173
		}
174
		m_getXmlSchemaWrapperFactory = 0;
175
		if ( m_xmlSchemaWrapper )
176
		{
177
			::FreeLibrary( m_xmlSchemaWrapper );
178
			m_xmlSchemaWrapper = 0;
179
		}
180
	}
181
	else
182
	{
183
		if ( !m_xmlSchemaWrapperLibrary.IsEmpty() )
184
		{
185
			m_xmlSchemaWrapper = ::LoadLibrary( m_xmlSchemaWrapperLibrary.c_str() );
186
			if ( m_xmlSchemaWrapper )
187
			{
188
				m_getXmlSchemaWrapperFactory = ( getXmlSchemaWrapperFactory_t )::GetProcAddress( m_xmlSchemaWrapper, "getXmlSchemaWrapperFactory" );
189
				if ( m_getXmlSchemaWrapperFactory )
190
				{
191
					m_xmlSchemaWrapperFactory = &m_getXmlSchemaWrapperFactory();
192
					if ( m_xmlSchemaWrapperFactory )
193
					{
194
						if ( m_xmlSchemaWrapperFactory->start( m_profile.c_str() ) )
195
						{
196
							m_xmlSchema = &m_xmlSchemaWrapperFactory->createSchema();
197
							m_xmlSchema->open();
198
 
199
							m_moduleName	= m_xmlSchema->getModuleName();
200
							m_moduleVersion	= m_xmlSchema->getModuleVersion();
201
						}
202
					}
203
				}
204
			}
205
		}
206
		else
207
		{
208
			Application->MessageBox(
209
				"Attribute \"Wrapper\" in section \"Init\" is empty.  Cannot load XML Schema Wrapper.",
210
				"TXMLSchema",
211
				MB_OK );
212
		}
213
    }
214
}
215
 
216
//------------------------------------------------------------------------------
217
 
218
bool __fastcall TXMLSchema::Refresh()
219
{
220
	SetActive(false);
221
 
222
    SetActive(true);
223
 
224
	return true;
225
}
226
 
227
//------------------------------------------------------------------------------
228
 
229
bool __fastcall TXMLSchema::GetSchemas(std::vector<std::string> &schemas)
230
{
231
	if ( m_xmlSchema )
232
	{
233
		IXmlSchemaWrapperStringCollection * collection = 0;
234
 
235
		try
236
		{
237
			collection = &m_xmlSchemaWrapperFactory->createStringCollection();
238
			if ( m_xmlSchema->getSchemas( *collection ) )
239
			{
240
				schemas.clear();
241
				const unsigned int size = collection->size();
242
				for ( unsigned int position = 0; position < size; ++position )
243
				{
244
					schemas.push_back( collection->operator[]( position ) );
245
				}
246
				return ( true );
247
			}
248
		}
249
		__finally
250
		{
251
			m_xmlSchemaWrapperFactory->destroyStringCollection( *collection );
252
			collection = 0;
253
		}
254
	}
255
 
256
	return ( false );
257
}
258
 
259
bool __fastcall TXMLSchema::GetAttributes(const std::string& handle, std::vector<std::string> &attributes, bool specificationOnly)
260
{
261
	if ( m_xmlSchema )
262
	{
263
		IXmlSchemaWrapperStringCollection * collection = 0;
264
 
265
		try
266
		{
267
			collection = &m_xmlSchemaWrapperFactory->createStringCollection();
268
			if ( m_xmlSchema->getAttributes( handle.c_str(), *collection, specificationOnly ) )
269
			{
270
				attributes.clear();
271
				const unsigned int size = collection->size();
272
				for ( unsigned int position = 0; position < size; ++position )
273
				{
274
					attributes.push_back( collection->operator[]( position ) );
275
				}
276
				return ( true );
277
			}
278
		}
279
		__finally
280
		{
281
			m_xmlSchemaWrapperFactory->destroyStringCollection( *collection );
282
			collection = 0;
283
		}
284
	}
285
 
286
	return ( false );
287
}
288
 
289
bool __fastcall TXMLSchema::GetAttributeProperty(const std::string &handle, const std::string &property_name, std::string &value)
290
{
291
	if ( m_xmlSchema )
292
	{
293
		IXmlSchemaWrapperString * attributeValue = 0;
294
 
295
		try
296
		{
297
			attributeValue = &m_xmlSchemaWrapperFactory->createString();
298
			if ( m_xmlSchema->getAttributeProperty( handle.c_str(), "", property_name.c_str(), *attributeValue ) )
299
			{
300
				value = attributeValue->c_str();
301
				return ( true );
302
			}
303
		}
304
		__finally
305
		{
306
			m_xmlSchemaWrapperFactory->destroyString( *attributeValue );
307
			attributeValue = 0;
308
		}
309
	}
310
 
311
	return ( false );
312
}
313
 
314
bool __fastcall TXMLSchema::Create (const char *structure_name, unsigned short version, std::string &handle, const char *qualifier)
315
{
316
	if ( m_xmlSchema )
317
	{
318
		IXmlSchemaWrapperHandle * wrapperHandle = 0;
319
 
320
		try
321
		{
322
			wrapperHandle = &m_xmlSchemaWrapperFactory->createHandle();
323
			if ( m_xmlSchema->createStructure( structure_name, version, *wrapperHandle, qualifier ) )
324
			{
325
				handle = wrapperHandle->c_str();
326
				return ( true );
327
			}
328
		}
329
		__finally
330
		{
331
			m_xmlSchemaWrapperFactory->destroyHandle( *wrapperHandle );
332
			wrapperHandle = 0;
333
		}
334
	}
335
 
336
	return ( false );
337
}
338
 
339
bool __fastcall TXMLSchema::Create(const char *schema, const char *structure_name, unsigned short version, std::string &handle, const char *qualifier)
340
{
341
	if ( m_xmlSchema )
342
	{
343
		IXmlSchemaWrapperHandle * wrapperHandle = 0;
344
 
345
		try
346
		{
347
			wrapperHandle = &m_xmlSchemaWrapperFactory->createHandle();
348
			if ( m_xmlSchema->createStructure( structure_name, version, *wrapperHandle, qualifier ) )
349
			{
350
				handle = wrapperHandle->c_str();
351
				return ( true );
352
			}
353
		}
354
		__finally
355
		{
356
			m_xmlSchemaWrapperFactory->destroyHandle( *wrapperHandle );
357
			wrapperHandle = 0;
358
		}
359
	}
360
 
361
	return ( false );
362
}
363
 
364
bool __fastcall TXMLSchema::Destroy(std::string& handle)
365
{
366
	if ( m_xmlSchema )
367
	{
368
		return ( m_xmlSchema->destroyStructure( handle.c_str() ) );
369
	}
370
 
371
	return ( false );
372
}
373
 
374
bool __fastcall TXMLSchema::Copy (const std::string &src_handle, const std::string &dest_handle, const XMLSchema::XMLSchemaCopyArgs &copyargs)
375
{
376
	if ( m_xmlSchema )
377
	{
378
		return ( m_xmlSchema->copyStructure(
379
			dest_handle.c_str(),
380
			src_handle.c_str(),
381
			copyargs.searchByName,
382
			copyargs.searchByTag,
383
			copyargs.errorOnFindFailure ) );
384
	}
385
 
386
	return ( false );
387
}
388
 
389
bool __fastcall TXMLSchema::Deserialise(const std::string& handle, const std::string& streamtype, const std::string& stream_args, int &required_alignment, const char *buffer, unsigned int bufsize, XMLSchema::StreamCallback callbackfn)
390
{
391
	if ( m_xmlSchema )
392
	{
393
		IXmlSchemaWrapperStream * stream = 0;
394
 
395
		try
396
		{
397
			if ( streamtype == "XDR" )
398
			{
399
				stream = &m_xmlSchemaWrapperFactory->createXdrStream();
400
			}
401
			else if ( streamtype == "XML" )
402
			{
403
				stream = &m_xmlSchemaWrapperFactory->createXmlStream();
404
			}
405
			else
406
			{
407
				stream = &m_xmlSchemaWrapperFactory->createBinaryStream();
408
			}
409
 
410
			stream->putData( buffer, bufsize );
411
			if ( callbackfn )
412
			{
413
				stream->setCallback( callbackfn );
414
			}
415
			if ( !stream_args.empty() )
416
			{
417
				stream->setArguments( stream_args.c_str() );
418
			}
419
 
420
			return ( m_xmlSchema->deserialise(
421
				handle.c_str(),
422
				*stream,
423
				required_alignment ) );
424
		}
425
		__finally
426
		{
427
			m_xmlSchemaWrapperFactory->destroyStream( *stream );
428
			stream = 0;
429
		}
430
	}
431
 
432
	return ( false );
433
}
434
 
435
bool __fastcall TXMLSchema::Serialise (const std::string &handle, const std::string& streamtype, const std::string& stream_args, void *&stream, unsigned int& stream_size, XMLSchema::IXmlSchemaWrapperQualification * qualifier, const char *serialise_qualifier, unsigned char required_alignment)
436
{
437
	if ( m_xmlSchema )
438
	{
439
		IXmlSchemaWrapperStream * wrapperStream = 0;
440
 
441
		try
442
		{
443
			if ( streamtype == "XDR" )
444
			{
445
				wrapperStream = &m_xmlSchemaWrapperFactory->createXdrStream();
446
			}
447
			else if ( streamtype == "XML" )
448
			{
449
				wrapperStream = &m_xmlSchemaWrapperFactory->createXmlStream();
450
			}
451
			else
452
			{
453
				wrapperStream = &m_xmlSchemaWrapperFactory->createBinaryStream();
454
			}
455
 
456
			if ( !stream_args.empty() )
457
			{
458
				wrapperStream->setArguments( stream_args.c_str() );
459
			}
460
 
461
			const bool serialised = m_xmlSchema->serialise(
462
				handle.c_str(),
463
				*wrapperStream,
464
				qualifier,
465
				serialise_qualifier,
466
				required_alignment );
467
 
468
			void *			buffer = 0;
469
			unsigned int	length = 0;
470
 
471
			if ( wrapperStream->getData( buffer, length ) )
472
			{
473
				stream_size = length;
474
				stream = malloc( stream_size );
475
				memcpy( stream, buffer, stream_size );
476
			}
477
			else
478
			{
479
				stream_size = 0;
480
				stream = 0;
481
			}
482
 
483
			return ( serialised );
484
		}
485
		__finally
486
		{
487
			m_xmlSchemaWrapperFactory->destroyStream( *wrapperStream );
488
			wrapperStream = 0;
489
		}
490
	}
491
 
492
	return ( false );
493
}
494
 
495
bool __fastcall TXMLSchema::SetUDTArgs(const char *udtName, int argc, const char** argv)
496
{
497
	if ( m_xmlSchema )
498
	{
499
		return ( m_xmlSchema->setUdtArguments( udtName, argc, argv ) );
500
	}
501
 
502
	return ( false );
503
}
504
 
505
bool __fastcall TXMLSchema::GetAttributeValue(const std::string &handle, const std::string& attribute_path, std::string &value)
506
{
507
	if ( m_xmlSchema )
508
	{
509
		IXmlSchemaWrapperString * attributeValue = 0;
510
 
511
		try
512
		{
513
			attributeValue = &m_xmlSchemaWrapperFactory->createString();
514
			if ( m_xmlSchema->getAttributeString( handle.c_str(), attribute_path.c_str(), *attributeValue ) )
515
			{
516
				value = attributeValue->c_str();
517
				return ( true );
518
			}
519
		}
520
		__finally
521
		{
522
			m_xmlSchemaWrapperFactory->destroyString( *attributeValue );
523
			attributeValue = 0;
524
		}
525
	}
526
 
527
	return ( false );
528
}
529
 
530
bool __fastcall TXMLSchema::GetAttributeValue(const std::string &handle, const std::string& attribute_path, int &value)
531
{
532
	if ( m_xmlSchema )
533
	{
534
		return ( m_xmlSchema->getAttributeInteger32( handle.c_str(), attribute_path.c_str(), value ) );
535
	}
536
 
537
	return ( false );
538
}
539
 
540
bool __fastcall TXMLSchema::GetAttributeValue(const std::string &handle, const std::string& attribute_path, AnsiString &value)
541
{
542
	std::string string_value;
543
 
544
	bool result = GetAttributeValue(handle, attribute_path, string_value);
545
 
546
    if (result)
547
    {
548
    	value = string_value.c_str();
549
    }
550
 
551
    return result;
552
}
553
 
554
bool __fastcall TXMLSchema::SetAttributeValue (const std::string &handle, const std::string& attribute_path, const char *value)
555
{
556
	if ( m_xmlSchema )
557
	{
558
		return ( m_xmlSchema->setAttributeString( handle.c_str(), attribute_path.c_str(), value ) );
559
	}
560
 
561
	return ( false );
562
}
563
 
564
bool __fastcall TXMLSchema::SetAttributeValue (const std::string &handle, const std::string& attribute_path, const int &value)
565
{
566
	if ( m_xmlSchema )
567
	{
568
		return ( m_xmlSchema->setAttributeInteger32( handle.c_str(), attribute_path.c_str(), value ) );
569
	}
570
 
571
	return ( false );
572
}
573
 
574
bool __fastcall TXMLSchema::SetAttributeValue (const std::string &handle, const std::string& attribute_path, void *value, unsigned int len)
575
{
576
	if ( m_xmlSchema )
577
	{
578
		return ( m_xmlSchema->setAttributeByteArray( handle.c_str(), attribute_path.c_str(), value, len ) );
579
	}
580
 
581
	return ( false );
582
}
583
 
584
bool __fastcall TXMLSchema::AddRepeatEntry (const std::string &handle, const unsigned int entry)
585
{
586
	if ( m_xmlSchema )
587
	{
588
		return ( m_xmlSchema->addRepeatEntry( handle.c_str(), entry ) );
589
	}
590
 
591
	return ( false );
592
}
593
 
594
bool __fastcall TXMLSchema::RemoveRepeatEntry (const std::string &handle, const unsigned int entry)
595
{
596
	if ( m_xmlSchema )
597
	{
598
		return ( m_xmlSchema->removeRepeatEntry( handle.c_str(), entry ) );
599
	}
600
 
601
	return ( false );
602
}
603
 
604
bool __fastcall TXMLSchema::Sort(const std::string &handle, const std::string &repeat_name, std::vector<std::string> &sort_fields )
605
{
606
	if ( m_xmlSchema )
607
	{
608
		IXmlSchemaWrapperStringCollection * collection = 0;
609
 
610
		try
611
		{
612
			collection = &m_xmlSchemaWrapperFactory->createStringCollection();
613
			for ( std::vector< std::string >::iterator
614
					where = sort_fields.begin();
615
				  where != sort_fields.end();
616
				  ++where )
617
			{
618
				collection->push_back( where->c_str() );
619
			}
620
			return ( m_xmlSchema->sort( handle.c_str(), repeat_name.c_str(), *collection ) );
621
		}
622
		__finally
623
		{
624
			m_xmlSchemaWrapperFactory->destroyStringCollection( *collection );
625
			collection = 0;
626
		}
627
	}
628
 
629
	return ( false );
630
}
631
 
632
bool __fastcall TXMLSchema::SetAttributeValue (const std::string &handle, const std::string& attribute_path, const AnsiString &value)
633
{
634
	return SetAttributeValue( handle, attribute_path, value.c_str() );
635
}
636
 
637
bool __fastcall TXMLSchema::GetEnumerationValues (const std::string &handle, const char *enumeration, std::vector<std::string> &values)
638
{
639
	if ( m_xmlSchema )
640
	{
641
		IXmlSchemaWrapperStringCollection * collection = 0;
642
 
643
		try
644
		{
645
			collection = &m_xmlSchemaWrapperFactory->createStringCollection();
646
			if ( m_xmlSchema->getEnumerationValues( handle.c_str(), enumeration, *collection ) )
647
			{
648
				values.clear();
649
				const unsigned int size = collection->size();
650
				for ( unsigned int position = 0; position < size; ++position )
651
				{
652
					values.push_back( collection->operator[]( position ) );
653
				}
654
				return ( true );
655
			}
656
		}
657
		__finally
658
		{
659
			m_xmlSchemaWrapperFactory->destroyStringCollection( *collection );
660
			collection = 0;
661
		}
662
	}
663
 
664
	return ( false );
665
}
666
 
667
bool __fastcall TXMLSchema::GetEnumerationIdValue (const std::string &handle, const char *enumeration, const char *id, std::string &value)
668
{
669
	if ( m_xmlSchema )
670
	{
671
		IXmlSchemaWrapperString * enumerationValue = 0;
672
 
673
		try
674
		{
675
			enumerationValue = &m_xmlSchemaWrapperFactory->createString();
676
			if ( m_xmlSchema->getEnumerationIdValue( handle.c_str(), enumeration, id, *enumerationValue ) )
677
			{
678
				value = enumerationValue->c_str();
679
				return ( true );
680
			}
681
		}
682
		__finally
683
		{
684
			m_xmlSchemaWrapperFactory->destroyString( *enumerationValue );
685
			enumerationValue = 0;
686
		}
687
	}
688
 
689
	return ( false );
690
}
691
 
692
 
693
void __fastcall TXMLSchema::GetVersionInfo(AnsiString &module_name, AnsiString &module_version)
694
{
695
	module_name 	= m_moduleName;
696
    module_version	= m_moduleVersion;
697
}
698
 
699
bool __fastcall TXMLSchema::Validate (const std::string &handle)
700
{
701
	if ( m_xmlSchema )
702
	{
703
		nullValidationObserver observer;
704
 
705
		return ( m_xmlSchema->validate( handle.c_str(), observer ) );
706
	}
707
 
708
	return ( false );
709
}
710
 
711
XMLSchema::IXmlSchemaWrapperFactory * TXMLSchema::GetSchemaWrapperFactory( void )
712
{
713
	return ( m_xmlSchemaWrapperFactory );
714
}
715
 
716
XMLSchema::IXmlSchemaWrapperSchema * TXMLSchema::GetSchema( void )
717
{
718
	return ( m_xmlSchema );
719
}
720
 
721
XMLSchema::IXmlSchemaWrapperElement * TXMLSchema::CreateSchemaElementAccess( const std::string & handle, const std::string & attribute_path ) const
722
{
723
	if ( m_xmlSchema )
724
	{
725
		return ( m_xmlSchema->createSchemaElement( handle.c_str(), attribute_path.c_str() ) );
726
	}
727
 
728
	return ( 0 );
729
}
730
 
731
void TXMLSchema::DestroySchemaElementAccess( const XMLSchema::IXmlSchemaWrapperElement& instance ) const
732
{
733
	if ( m_xmlSchema )
734
	{
735
		m_xmlSchema->destroySchemaElement( instance );
736
	}
737
}
738