/// <summary> /// Gets min, max values /// </summary> /// <param name="in_element"></param> /// <param name="in_value"></param> private void GetAndCheckMinMaxFloat(XPathNavigator in_element, float in_value) { m_float_min = (float)XMLAttributeParser.ConvertAttributeToDouble(in_element, "Min", XMLAttributeParser.atOptional, m_float_min); m_float_max = (float)XMLAttributeParser.ConvertAttributeToDouble(in_element, "Max", XMLAttributeParser.atOptional, m_float_max); if (in_value < m_float_min && in_value > m_float_max) { XMLParserException exception = new XMLParserException(in_element); exception.SetOutOfRangeError(); throw exception; } }
/// <summary> /// Parses value description /// </summary> /// <param name="in_element">Element to parse</param> public void ParseXML(XPathNavigator in_element) { // get id m_id = XMLAttributeParser.ConvertAttributeToString(in_element, "ID", XMLAttributeParser.atObligatory); // get name m_name = XMLAttributeParser.ConvertAttributeToString(in_element, "Name", XMLAttributeParser.atObligatory); // get unit m_units = XMLAttributeParser.ConvertAttributeToString(in_element, "Units", XMLAttributeParser.atOptional); // get description m_description = XMLAttributeParser.ConvertAttributeToString(in_element, "Description", XMLAttributeParser.atOptional); // get value switch (m_value_type) { case ValueType.StringValue: // if string type is specified length must be existing m_binary_length = XMLAttributeParser.ConvertAttributeToInt(in_element, "Length", XMLAttributeParser.atObligatory) + 1; // +1 because of the terminator zero m_value = in_element.Value; // check length if (!string.IsNullOrEmpty((string)m_value)) { if (((string)m_value).Length > m_binary_length) { // throw an exception if string is too long XMLParserException exception = new XMLParserException(in_element); exception.SetStringIsTooLongError(m_name, m_binary_length); throw exception; } } break; case ValueType.Int32Value: m_binary_length = sizeof(Int32); try { m_value = (Int32)in_element.ValueAsInt; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } break; case ValueType.Int8Value: m_binary_length = sizeof(SByte); try { m_value = (SByte)in_element.ValueAsInt; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } break; case ValueType.UInt8Value: { int value; m_binary_length = sizeof(SByte); try { value = in_element.ValueAsInt; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } GetAndCheckMinMaxInt(in_element, value); m_value = (byte)value; } break; case ValueType.UInt16Value: { int value; m_binary_length = sizeof(UInt16); try { value = in_element.ValueAsInt; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } GetAndCheckMinMaxInt(in_element, value); m_value = (UInt16)value; } break; // Fixed values case ValueType.UInt8FixedValue: case ValueType.Int8FixedValue: case ValueType.UInt16FixedValue: case ValueType.Int16FixedValue: { float float_value; object value = 0; m_binary_length = sizeof(Int16); // get multiplier m_multiplier = XMLAttributeParser.ConvertAttributeToInt(in_element, "Multiplier", XMLAttributeParser.atObligatory); if (m_multiplier < 1) { XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidAttributeValue("Multiplier"); throw exception; } m_float_min /= m_multiplier; m_float_max /= m_multiplier; // get fractional digits number m_fractional_digits = XMLAttributeParser.ConvertAttributeToInt(in_element, "FractionalDigits", XMLAttributeParser.atOptional); if (m_fractional_digits < 0) { XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidAttributeValue("FractionalDigits"); throw exception; } // store value try { float_value = (float)in_element.ValueAsDouble; switch (m_value_type) { case ValueType.UInt8FixedValue: value = (byte)Math.Round(float_value * m_multiplier); break; case ValueType.Int8FixedValue: value = (SByte)Math.Round(float_value * m_multiplier); break; case ValueType.UInt16FixedValue: value = (UInt16)Math.Round(float_value * m_multiplier); break; case ValueType.Int16FixedValue: value = (Int16)Math.Round(float_value * m_multiplier); break; } } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } GetAndCheckMinMaxFloat(in_element, float_value); m_value = value; } break; case ValueType.FloatValue: m_binary_length = sizeof(float); try { m_value = (float)in_element.ValueAsDouble; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } break; case ValueType.EnumValue: { m_binary_length = sizeof(byte); m_enumdef_ref = XMLAttributeParser.ConvertAttributeToString(in_element, "Enum", XMLAttributeParser.atObligatory); try { m_value = (byte)in_element.ValueAsInt; } catch { // throw an exception if value is invalid XMLParserException exception = new XMLParserException(in_element); exception.SetInvalidTypeError(m_name); throw exception; } } break; default: throw new InvalidDataException(); } }