Beispiel #1
0
        private FeedbackField HandleField(XmlReader reader)
        {
            string name        = reader.GetAttribute("name");
            string intype_str  = reader.GetAttribute("intype");
            string outtype_str = reader.GetAttribute("outtype");
            string dsitem      = reader.GetAttribute("dsitem");
            string units       = reader.GetAttribute("units");

            // figure out the in type
            InType intype  = (InType)Enum.Parse(typeof(InType), intype_str.ToUpper(), false);
            Type   outtype = MapType(outtype_str);

            if (!reader.Read())
            {
                throw new InvalidOperationException();
            }

            if (reader.NodeType != XmlNodeType.Element)
            {
                throw new InvalidOperationException();
            }

            FeedbackField field = null;

            string elementName = reader.LocalName;

            if (elementName == "raw")
            {
                field = new RawField(name, intype, outtype, dsitem, units);
            }
            else if (elementName == "bool")
            {
                field = new BoolField(name, intype, outtype, dsitem, units);
            }
            else if (elementName == "conversion")
            {
                double scale = 0, preOffset = 0, postOffset = 0;
                HandleConversion(reader, ref scale, ref preOffset, ref postOffset);
                field = new ConversionField(name, intype, outtype, dsitem, units, scale, preOffset, postOffset);
            }
            else if (elementName == "enum")
            {
                Dictionary <int, object> map = new Dictionary <int, object>();
                object defVal = null;
                if (!outtype.IsEnum)
                {
                    throw new InvalidOperationException("Error in field " + name + ": outtype is not an enum");
                }
                HandleEnumMap(reader, outtype, ref map, ref defVal);
                field = new EnumField(name, intype, outtype, dsitem, units, map, defVal);
            }
            else if (elementName == "intMap")
            {
                Dictionary <int, int> map = new Dictionary <int, int>();
                int defVal = 0;
                // should check if out type is valid
                HandleIntMap(reader, ref map, ref defVal);
                field = new IntField(name, intype, outtype, dsitem, units, map, defVal);
            }
            else if (elementName == "bitmap")
            {
                string[] fields;
                HandleBitmap(reader, out fields);
                field = new BitmapField(name, intype, outtype, dsitem, units, fields);
            }
            else
            {
                throw new InvalidOperationException();
            }

            reader.Read();
            if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != "field")
            {
                throw new InvalidOperationException();
            }

            return(field);
        }
        private FeedbackField HandleField(XmlReader reader)
        {
            string name = reader.GetAttribute("name");
            string intype_str = reader.GetAttribute("intype");
            string outtype_str = reader.GetAttribute("outtype");
            string dsitem = reader.GetAttribute("dsitem");
            string units = reader.GetAttribute("units");

            // figure out the in type
            InType intype = (InType)Enum.Parse(typeof(InType), intype_str.ToUpper(), false);
            Type outtype = MapType(outtype_str);

            if (!reader.Read())
                throw new InvalidOperationException();

            if (reader.NodeType != XmlNodeType.Element)
                throw new InvalidOperationException();

            FeedbackField field = null;

            string elementName = reader.LocalName;
            if (elementName == "raw") {
                field = new RawField(name, intype, outtype, dsitem, units);
            }
            else if (elementName == "bool") {
                field = new BoolField(name, intype, outtype, dsitem, units);
            }
            else if (elementName == "conversion") {
                double scale = 0, preOffset = 0, postOffset = 0;
                HandleConversion(reader, ref scale, ref preOffset, ref postOffset);
                field = new ConversionField(name, intype, outtype, dsitem, units, scale, preOffset, postOffset);
            }
            else if (elementName == "enum") {
                Dictionary<int, object> map = new Dictionary<int, object>();
                object defVal = null;
                if (!outtype.IsEnum)
                    throw new InvalidOperationException("Error in field " + name + ": outtype is not an enum");
                HandleEnumMap(reader, outtype, ref map, ref defVal);
                field = new EnumField(name, intype, outtype, dsitem, units, map, defVal);
            }
            else if (elementName == "intMap") {
                Dictionary<int, int> map = new Dictionary<int, int>();
                int defVal = 0;
                // should check if out type is valid
                HandleIntMap(reader, ref map, ref defVal);
                field = new IntField(name, intype, outtype, dsitem, units, map, defVal);
            }
            else if (elementName == "bitmap") {
                string[] fields;
                HandleBitmap(reader, out fields);
                field = new BitmapField(name, intype, outtype, dsitem, units, fields);
            }
            else {
                throw new InvalidOperationException();
            }

            reader.Read();
            if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != "field")
                throw new InvalidOperationException();

            return field;
        }