/// <summary> /// Validates a field in a structured type description. /// </summary> private void ValidateField(StructuredType description, Dictionary <string, FieldType> fields, FieldType field) { if (field == null || String.IsNullOrEmpty(field.Name)) { throw Exception("The structured type '{0}' has an unnamed field.", description.Name); } if (fields.ContainsKey(field.Name)) { throw Exception("The structured type '{0}' has a duplicate field name '{1}'.", description.Name, field.Name); } if (IsNull(field.TypeName)) { throw Exception("Field '{1}' in structured type '{0}' has no type specified.", description.Name, field.Name); } if (!m_descriptions.ContainsKey(field.TypeName)) { throw Exception("Field '{1}' in structured type '{0}' has an unrecognized type '{2}'.", description.Name, field.Name, field.TypeName); } if (!String.IsNullOrEmpty(field.LengthField)) { if (!fields.ContainsKey(field.LengthField)) { throw Exception("Field '{1}' in structured type '{0}' references an unknownn length field '{2}'.", description.Name, field.Name, field.LengthField); } if (!IsIntegerType(fields[field.LengthField])) { throw Exception( "Field '{1}' in structured type '{0}' references a length field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField); } } if (!String.IsNullOrEmpty(field.SwitchField)) { if (!fields.ContainsKey(field.SwitchField)) { throw Exception("Field '{1}' in structured type '{0}' references an unknownn switch field '{2}'.", description.Name, field.Name, field.SwitchField); } if (!IsIntegerType(fields[field.SwitchField])) { throw Exception( "Field '{1}' in structured type '{0}' references a switch field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField); } } }
/// <summary> /// Validates a type description. /// </summary> private void ValidateDescription(TypeDescription description) { OpaqueType opaque = description as OpaqueType; if (opaque != null) { if (!opaque.LengthInBitsSpecified) { m_warnings.Add(String.Format(CultureInfo.InvariantCulture, "Warning: The opaque type '{0}' does not have a length specified.", description.Name)); } if (IsNull(opaque.Documentation)) { m_warnings.Add(String.Format(CultureInfo.InvariantCulture, "Warning: The opaque type '{0}' does not have any documentation.", description.Name)); } } EnumeratedType enumerated = description as EnumeratedType; if (enumerated != null) { if (!enumerated.LengthInBitsSpecified) { throw Exception("The enumerated type '{0}' does not have a length specified.", description.Name); } } StructuredType structure = description as StructuredType; if (structure != null) { if (structure.Field == null || structure.Field.Length == 0) { structure.Field = new FieldType[0]; } int bitCount = 0; Dictionary <string, FieldType> fields = new Dictionary <string, FieldType>(); for (int ii = 0; ii < structure.Field.Length; ii++) { FieldType field = structure.Field[ii]; ValidateField(structure, fields, field); int fieldLength = GetFieldLength(field); if (fieldLength == -1) { if (bitCount % 8 != 0) { throw Exception("Field '{1}' in structured type '{0}' is not aligned on a byte boundary .", description.Name, field.Name); } bitCount = 0; } else { bitCount += fieldLength; } fields.Add(field.Name, field); } } }
/// <summary> /// Validates a field in a structured type description. /// </summary> private void ValidateField(StructuredType description, Dictionary<string,FieldType> fields, FieldType field) { if (field == null || String.IsNullOrEmpty(field.Name)) { throw Exception("The structured type '{0}' has an unnamed field.", description.Name); } if (fields.ContainsKey(field.Name)) { throw Exception("The structured type '{0}' has a duplicate field name '{1}'.", description.Name, field.Name); } if (IsNull(field.TypeName)) { throw Exception("Field '{1}' in structured type '{0}' has no type specified.", description.Name, field.Name); } if (!m_descriptions.ContainsKey(field.TypeName)) { throw Exception("Field '{1}' in structured type '{0}' has an unrecognized type '{2}'.", description.Name, field.Name, field.TypeName); } if (!String.IsNullOrEmpty(field.LengthField)) { if (!fields.ContainsKey(field.LengthField)) { throw Exception("Field '{1}' in structured type '{0}' references an unknownn length field '{2}'.", description.Name, field.Name, field.LengthField); } if (!IsIntegerType(fields[field.LengthField])) { throw Exception("Field '{1}' in structured type '{0}' references a length field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField); } } if (!String.IsNullOrEmpty(field.SwitchField)) { if (!fields.ContainsKey(field.SwitchField)) { throw Exception("Field '{1}' in structured type '{0}' references an unknownn switch field '{2}'.", description.Name, field.Name, field.SwitchField); } if (!IsIntegerType(fields[field.SwitchField])) { throw Exception("Field '{1}' in structured type '{0}' references a switch field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField); } } }