Пример #1
0
      //============================================================================
      /// <summary>
      /// Writes the SDML value as XML.
      /// </summary>
      /// <param name="attrInfo">The typed value to use.</param>
      /// <param name="indent">Indent spaces to use. -1 = no indent.</param>
      /// <param name="tab">Number of spaces in each tab</param>
      /// <returns>The XML for the SDML value.</returns>
      //============================================================================
       protected override String writeFieldInfo(TTypedValue attrInfo, int indent, int tab)
       {
           uint i;
           int oneIndent;
           int nextIndent;
           int startIndent;
           String CR = "";

           //determine how much to indent this description
           oneIndent = 0;
           startIndent = 0;
           if (indent > -1)
           {
               oneIndent = tab;
               startIndent = indent;
               CR = "\r\n";
           }
           String sIndent = new String(' ', startIndent);   //begin at this level
           nextIndent = indent + oneIndent;

           StringBuilder xml = new StringBuilder("");
           if (attrInfo.baseType() != TTypedValue.TBaseType.ITYPE_DEF)
               xml.Append(" kind=\"" + attrInfo.typeName() + "\"");
           if (attrInfo.isArray())
               xml.Append(" array=\"T\"");
           if ((attrInfo.units().Length > 0) && (attrInfo.units()[0] != '-'))
               xml.Append(" unit=\"" + attrInfo.units() + "\"");

           xml.Append(">" + CR);

           if (attrInfo.isScalar()) // Scalars - use a <val> element
           {
               xml.Append(sIndent + "<val>" + scalarString(attrInfo) + "</val>" + CR);
           }
           else
           {
               //now nest into the fields/elements
               for (i = 1; i <= attrInfo.count(); i++)
               {
                   if (attrInfo.isArray() && (attrInfo.baseType() != TTypedValue.TBaseType.ITYPE_DEF))
                   {
                       xml.Append(new String(' ', oneIndent) + "<val>" + scalarString(attrInfo.item(i)) + "</val>" + CR); // Scalar array, indented format
                   }
                   else if (attrInfo.isArray())                                          // All other arrays
                       xml.Append(sIndent + "<element"
                                   + writeFieldInfo(attrInfo.item(i), nextIndent, oneIndent)
                                   + sIndent + "</element>" + CR);
                   else if (attrInfo.isRecord())                                         // Records
                       xml.Append(sIndent + "<field name=\"" + attrInfo.item(i).Name + "\""
                                   + writeFieldInfo(attrInfo.item(i), nextIndent, oneIndent)
                                   + sIndent + "</field>" + CR);
               }
           }

           return xml.ToString();
       }
Пример #2
0
       //============================================================================
       /// <summary>
       /// Return the string for the scalar ttypedvalue.
       /// Numeric types are returned without any rounding or escaping.
       /// </summary>
       /// <param name="attrInfo">The scalar TTypedValue</param>
       /// <returns>String represenation of the scalar</returns>
       //============================================================================
       protected String scalarString(TTypedValue attrInfo)
       {
           String strVal;

           if ((attrInfo.baseType() >= TTypedValue.TBaseType.ITYPE_SINGLE) && (attrInfo.baseType() <= TTypedValue.TBaseType.ITYPE_DOUBLE))
           {
               strVal = attrInfo.asDouble().ToString(); //full precision
           }
           else
           {
               if ((attrInfo.baseType() >= TTypedValue.TBaseType.ITYPE_INT1) && (attrInfo.baseType() <= TTypedValue.TBaseType.ITYPE_INT8))
               {
                   strVal = attrInfo.asInt().ToString(); //no need to escape this   
               }
               else
               {
                   strVal = attrInfo.asEscapedString();
               }
           }
           return strVal;
       }
Пример #3
0
        //============================================================================
        /// <summary>
        /// The new member to add to this structure.
        /// </summary>
        /// <param name="typedValue">Typed value to copy.</param>
        // N.Herrmann Apr 2002
        //============================================================================
        protected void initTypeCopy(TTypedValue typedValue)
        {
            uint i;

            Name = typedValue.Name;
            FBaseType = typedValue.baseType();
            FIsScalar = typedValue.isScalar();
            FIsArray = typedValue.isArray();
            FIsRecord = typedValue.isRecord();
            setUnits(typedValue.units());

            if (FIsScalar)
            {
                createScalar();
                switch (FBaseType)
                {                                           //For scalars, copy the value data.
                    case TBaseType.ITYPE_INT1:                                            //Data pertaining to arrays and records
                    case TBaseType.ITYPE_INT2:                                            //   is ultimately stored in their
                    case TBaseType.ITYPE_INT4:                                            //   constituent scalars
                    case TBaseType.ITYPE_INT8: setValue(typedValue.asInt()); break;
                    case TBaseType.ITYPE_SINGLE: setValue(typedValue.asSingle()); break;
                    case TBaseType.ITYPE_DOUBLE: setValue(typedValue.asDouble()); break;
                    case TBaseType.ITYPE_BOOL: setValue(typedValue.asBool()); break;
                    case TBaseType.ITYPE_CHAR:
                    case TBaseType.ITYPE_WCHAR: setValue(typedValue.asChar()); break;
                    case TBaseType.ITYPE_STR:
                    case TBaseType.ITYPE_WSTR: setValue(typedValue.asStr()); break;
                }
            }
            else if (FIsArray || FIsRecord)
            {
                uint iCount = typedValue.count();
                if (FIsArray && (iCount == 0))
                {
                    if (typedValue.item(0) != null)
                        newMember(typedValue.item(0));
                    setElementCount(0);
                }
                else
                    for (i = 1; i <= iCount; i++)
                        newMember(typedValue.item(i)); //clones and adds this typed value
            }
        }
Пример #4
0
        //======================================================================
        /// <summary>
        /// Assignment from a TTypedValue that need not be of identical type, but must   
        /// be type-compatible.
        /// When converting from a scalar string to a numeric an exception will be thrown
        /// if the source string is not a valid numeric.
        /// </summary>
        /// <param name="srcValue">The source typed value.</param>
        /// <returns>True is the value can be set.</returns>
        //======================================================================
        public Boolean setValue(TTypedValue srcValue)
        {
            bool result = false;
            bool bCompatible;

            if (srcValue != null)
            {
                bCompatible = ((FIsScalar == srcValue.isScalar()) && (FIsArray == srcValue.isArray()));
                if (FBaseType == TBaseType.ITYPE_DEF)
                {
                    bCompatible = (bCompatible && (srcValue.baseType() == TBaseType.ITYPE_DEF));
                }
                if (!bCompatible)
                {
                    String error = String.Format("Incompatible assignment from {0} to {1}\nCannot convert {2} to {3}", Name, srcValue.Name, srcValue.baseType().ToString(), FBaseType.ToString());
                    throw (new TypeMisMatchException(error));
                }
                if (FIsScalar)
                {
                    try
                    {
                        switch (FBaseType)
                        {
                            case TBaseType.ITYPE_INT1:
                            case TBaseType.ITYPE_INT2:
                            case TBaseType.ITYPE_INT4:
                                {
                                    result = setValue(srcValue.asInt());
                                    break;
                                }
                            case TBaseType.ITYPE_INT8:
                                result = setValue(Convert.ToInt64(srcValue.asDouble()));
                                break;
                            case TBaseType.ITYPE_DOUBLE:
                                {
                                    result = setValue(srcValue.asDouble());
                                    break;
                                }
                            case TBaseType.ITYPE_SINGLE:
                                {
                                    result = setValue(srcValue.asSingle());
                                    break;
                                }
                            case TBaseType.ITYPE_BOOL:
                                {
                                    result = setValue(srcValue.asBool());
                                    break;
                                }
                            default:
                                {
                                    result = setValue(srcValue.asStr());
                                    break;
                                }
                        }
                    }
                    catch
                    {
                        throw (new Exception("setValue() cannot convert " + srcValue.asStr() + " to " + FBaseType.ToString()));
                    }
                }
                else
                {
                    if (FIsArray)
                    {
                        setElementCount(srcValue.count());
                    }
                    uint iCount = count();
                    for (uint Idx = 1; Idx <= iCount; Idx++)
                    {
                        result = item(Idx).setValue(srcValue.item(Idx));
                    }
                }
            }
            return result;
        }
Пример #5
0
        //============================================================================
        /// <summary>
        /// Tests for identity of two TTypedValue objects.
        /// </summary>
        /// <param name="otherValue">Typed value to test against this one.</param>
        /// <returns>True if it matches in type, size, and structure.</returns>
        // N.Herrmann Apr 2002
        //============================================================================
        public Boolean equals(TTypedValue otherValue)
        {
            uint i;
            Boolean bEqual = false;

            if ((otherValue != null) &&
               (FBaseType == otherValue.baseType()) &&
               (FIsArray == otherValue.isArray()) &&
               (FIsRecord == otherValue.isRecord()) &&
               (count() == otherValue.count()) &&
               (FDataSize == otherValue.sizeBytes()))
                bEqual = true;

            if (bEqual)
            {
                if (FIsScalar)
                    bEqual = bEqual && (asStr() == otherValue.asStr());    //str comparison of the scalar (needs refinement)
            }
            else
            {
                for (i = 1; i <= count(); i++)
                    bEqual = bEqual && item(i).equals(otherValue.item(i));
            }
            return bEqual;
        }
Пример #6
0
        //============================================================================
        /// <summary>
        /// Recursive routine for checking whether two types are (a) identical,
        /// (b) different but compatible, (c) incompatible.
        /// <para>Note:</para>
        /// <para>1. Type compatibility is not a transitive relationship.</para>
        /// <para>2. Unit compatibility needs further implementation.</para>
        /// </summary>
        /// <param name="srcValue">The TTypedValue to compare with.</param>
        /// <returns>Returns: 0 - exact match, 1 - compatible, -1 - cannot match</returns>
        //============================================================================
        public int canAssignFrom(TTypedValue srcValue)
        {
            int result = ctBAD;
            uint Idx;

            if (srcValue.isScalar())
            {
                if (!FIsScalar)
                    result = ctBAD;
                else if (srcValue.baseType() == FBaseType)
                    result = ctSAME;
                else if ((srcValue.baseType() <= TBaseType.ITYPE_INT8) && (srcValue.baseType() >= TBaseType.ITYPE_INT1) &&
                         (FBaseType <= TBaseType.ITYPE_INT8) && (FBaseType >= TBaseType.ITYPE_INT1))
                    result = ctCOMP;  //both integers
                else if ((FBaseType >= TBaseType.ITYPE_SINGLE) && (FBaseType <= TBaseType.ITYPE_DOUBLE) &&           //These conditions are not transitive
                         (srcValue.baseType() >= TBaseType.ITYPE_INT1) && (srcValue.baseType() <= TBaseType.ITYPE_DOUBLE))
                    result = ctCOMP;  //can match an int/single source to single/double destination
                else if ((srcValue.baseType() == TBaseType.ITYPE_CHAR) &&
                    ((FBaseType == TBaseType.ITYPE_WCHAR) ||
                    (FBaseType == TBaseType.ITYPE_STR) ||
                    (FBaseType == TBaseType.ITYPE_WSTR)))
                    result = ctCOMP;
                else if ((srcValue.baseType() == TBaseType.ITYPE_WCHAR) && (FBaseType == TBaseType.ITYPE_WSTR))
                    result = ctCOMP;
                else if ((srcValue.baseType() == TBaseType.ITYPE_STR) && (FBaseType == TBaseType.ITYPE_WSTR))
                    result = ctCOMP;
                // A sop to the old APSIM manager, which sends out all request-set values as strings
                else if ((srcValue.baseType() == TBaseType.ITYPE_STR) || (FBaseType == TBaseType.ITYPE_WSTR))
                    result = ctDODGY;
                else
                    result = ctBAD;

                if ((FBaseType >= TBaseType.ITYPE_INT1) && (FBaseType <= TBaseType.ITYPE_DOUBLE) &&
                      (!unitsMatch(units(), srcValue.units())))
                    result = ctBAD;
            }
            else if (srcValue.isArray())
            {   //an array
                if (!FIsArray)
                    result = ctBAD;
                else
                {
                    if (count() == 0)
                        setElementCount(1);  //addElement();
                    if (srcValue.count() == 0)
                        srcValue.setElementCount(1); //addElement();
                    result = member(1).canAssignFrom(srcValue.member(1));
                }
            }
            else
            {   //a record
                if (!isRecord())
                    result = ctBAD;
                else
                {
                    uint iCount = count();
                    result = ctCOMP;                                                        // First, test for identity
                    if (iCount == srcValue.count())
                    {
                        result = ctSAME;
                        for (Idx = 1; Idx <= iCount; Idx++)
                        {
                            if ((member(Idx).Name.ToLower() != srcValue.member(Idx).Name.ToLower()) ||
                                  (member(Idx).canAssignFrom(srcValue.member(Idx)) != ctSAME))
                                result = ctCOMP;
                        }
                    }

                    if (result == ctCOMP)
                    {                                                //If not same, test for compatibility
                        String elemName;
                        for (Idx = 1; Idx <= srcValue.count(); Idx++)
                        {
                            elemName = srcValue.member(Idx).Name;                 //field name
                            if (!hasField(elemName) ||
                                  (member(elemName).canAssignFrom(srcValue.member(Idx)) == ctBAD))
                                result = ctBAD;
                        }
                    }
                }
            }

            return result;
        }