/**
* Copyright (c) 2007, Ritu Jain, Chinmay Nagarkar and Sahi Technologies Pvt Ltd
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Sahi Technologies Pvt. Ltd./Esahi.com nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using org.drools.;
using org.drools.spi;
using java.lang;
namespace org.drools.dotnet.semantics{
public class DotnetFieldFactory
{
private static readonly DotnetFieldFactory INSTANCE = new DotnetFieldFactory();
public static DotnetFieldFactory getInstance() {
return INSTANCE;
}
public static FieldValue getFieldValue(string value,
java.lang.Integer valueTypeObj) {
int valueType = valueTypeObj.intValue();
FieldValue field = null;
if ( value == null ) {
valueType = Evaluator.__Fields.NULL_TYPE;
}
switch ( valueType ) {
case Evaluator.__Fields.NULL_TYPE :
field = new FieldImpl( null );
break;
case Evaluator.__Fields.CHAR_TYPE :
field = new FieldImpl(char.Parse(value));
break;
case Evaluator.__Fields.BYTE_TYPE :
//byte b = Encoding.UTF8.GetBytes(value)[0];
field = new FieldImpl(byte.Parse(value));
break;
case Evaluator.__Fields.SHORT_TYPE :
field = new FieldImpl( short.Parse(value ) );
break;
case Evaluator.__Fields.INTEGER_TYPE :
field = new FieldImpl( int.Parse( value ) );
break;
case Evaluator.__Fields.LONG_TYPE :
field = new FieldImpl( long.Parse( value ) );
break;
case Evaluator.__Fields.FLOAT_TYPE :
field = new FieldImpl( float.Parse( value ) );
break;
case Evaluator.__Fields.DOUBLE_TYPE :
field = new FieldImpl( double.Parse( value ) );
break;
case Evaluator.__Fields.BOOLEAN_TYPE :
field = new FieldImpl( bool.Parse ( value ) );
break;
case Evaluator.__Fields.STRING_TYPE :
field = new FieldImpl( value );
break;
case Evaluator.__Fields.DATE_TYPE :
//MN: I think its fine like this, seems to work !
field = new FieldImpl( DateTime.Parse(value) );
break;
case Evaluator.__Fields.ARRAY_TYPE :
//MN: I think its fine like this.
field = new FieldImpl( value );
break;
case Evaluator.__Fields.OBJECT_TYPE :
field = new FieldImpl( value );
break;
}
return field;
}
public static FieldValue getFieldValueForStaticField(string classname, string fieldName, TypeResolver resolver)
{
Type type = ((DotnetClassTypeResolver)resolver).resolveDotnetType(classname);
object obj = type.GetField(fieldName).GetValue(null);
return new FieldImpl(obj);
}
}
}
|