示例#1
0
        private IEnumerable <string> SerialiseElement(Object tree, FieldInfo[] fI, PropertyInfo[] pI, uint indentLevel)
        {
            foreach (var info in pI)
            {
                ElementAttribute att = AttributeCache <ElementAttribute, MemberInfo> .Get(info);

                if (att != null)
                {
                    object objData = info.GetValue(tree);
                    if (objData != null)
                    {
                        foreach (var serialiseAttribute in SerialiseAttributeData(objData, info.PropertyType, att, indentLevel))
                        {
                            yield return(serialiseAttribute);
                        }
                    }
                }
            }
            foreach (var info in fI)
            {
                ElementAttribute att = AttributeCache <ElementAttribute, MemberInfo> .Get(info);

                if (att != null)
                {
                    object objData = info.GetValue(tree);
                    if (objData != null)
                    {
                        foreach (var serialiseAttribute in SerialiseAttributeData(objData, info.FieldType, att, indentLevel))
                        {
                            yield return(serialiseAttribute);
                        }
                    }
                }
            }
        }
示例#2
0
        private IEnumerable <string> SerialiseAttributeData(Object objData, Type objType, ElementAttribute att, uint indentLevel)
        {
            if (att.IsComment)
            {
                yield return(Environment.NewLine);

                StringBuilder tmp = Indent(indentLevel);
                tmp.Append("/*");
                tmp.Append(objData.ToString());
                tmp.Append("*/");
                tmp.Append(Environment.NewLine);
                yield return(tmp.ToString());
            }
            else if ((att.IsArgument || att.IsString) && !att.IsList)
            {
                string data = "";
                if (att.Comment != null)
                {
                    yield return(Environment.NewLine);

                    StringBuilder tmp = Indent(indentLevel);
                    tmp.Append("/*");
                    tmp.Append(att.Comment);
                    tmp.Append("*/ ");
                    yield return(tmp.ToString());
                }
                if (att.Name != null && att.Name != "")
                {
                    data += Environment.NewLine;
                    data += Indent(indentLevel).Append(att.Name).Append(" ").ToString();
                }
                else if (att.ForceNewLine)
                {
                    data += Environment.NewLine;
                    data += Indent(indentLevel).ToString();
                }
                else if (att.Comment == null)
                {
                    data = " ";
                }

                if (att.IsString)
                {
                    String value = objData.ToString();
                    data += "\"" + value + "\"";
                    yield return(data);
                }
                else
                {
                    if (objType.IsEnum)
                    {
                        String value = Enum.GetName(objType, objData);
                        data += value;
                        yield return(data);
                    }
                    else if (att.CodeAsHex)
                    {
                        UInt64 tmp   = (UInt64)objData;
                        String value = "0x" + tmp.ToString("X");
                        data += value;
                        yield return(data);
                    }
                    else if (objType == typeof(decimal))
                    {
                        decimal tmp = (decimal)objData;
                        data += tmp.ToString(CultureInfo.InvariantCulture);
                        yield return(data);
                    }
                    else
                    {
                        yield return(data + objData.ToString());
                    }
                }
            }
            else if (att.IsDictionary)
            {
                Dictionary <string, object> dict = ToDict <string, object>(objData);

                if (dict.Count > 0)
                {
                    if (att.Comment != null)
                    {
                        yield return(Environment.NewLine);

                        StringBuilder tmp = Indent(indentLevel);
                        tmp.Append("/*");
                        tmp.Append(att.Comment);
                        tmp.Append("*/");
                        tmp.Append(Environment.NewLine);
                        yield return(tmp.ToString());
                    }
                    else if (att.ForceNewLine)
                    {
                        yield return(Environment.NewLine);
                    }

                    foreach (object elem in dict.Values)
                    {
                        foreach (string dicElement in SerialiseNode(elem, indentLevel))
                        {
                            yield return(dicElement);
                        }
                    }
                }
            }
            else if (att.IsList)
            {
                if (objData is IList)
                {
                    var list = ((IList)objData);
                    if (list.Count > 0)
                    {
                        if (att.Comment != null)
                        {
                            yield return(Environment.NewLine);

                            StringBuilder tmp = Indent(indentLevel);
                            tmp.Append("/*");
                            tmp.Append(att.Comment);
                            tmp.Append("*/");
                            yield return(tmp.ToString());
                        }
                        else if (att.ForceNewLine)
                        {
                            yield return(Environment.NewLine);
                        }

                        if (list[0].GetType().BaseType == typeof(Asap2Base))
                        {
                            /* If the is list is List<Asap2Base> sort the list and then iterate over the sorted list. */
                            IEnumerable <Asap2Base> tmp =
                                from Asap2Base item in list
                                orderby item.OrderID
                                select item;

                            foreach (var item in tmp)
                            {
                                foreach (string listElement in SerialiseNode(item, indentLevel, att))
                                {
                                    yield return(listElement);
                                }
                            }
                        }
                        else
                        {
                            /* Generic data elements. */
                            foreach (var item in list)
                            {
                                foreach (string listElement in SerialiseNode(item, indentLevel, att))
                                {
                                    yield return(listElement);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (objData != null)
                {
                    foreach (string dataNode in SerialiseNode(objData, indentLevel))
                    {
                        yield return(dataNode);
                    }
                }
            }
        }
示例#3
0
        private IEnumerable <string> SerialiseNode(Object tree, uint indentLevel, ElementAttribute nodeElemAtt = null)
        {
            BaseAttribute baseAtt = AttributeCache <BaseAttribute, MemberInfo> .Get(tree.GetType());

            string elementName = null;

            if (baseAtt != null)
            {
                var pI = SortedPropertyCache.Get(tree.GetType());
                var fI = SortedFieldsCache.Get(tree.GetType());

                for (int i = 0; i < pI.Length; i++)
                {
                    ElementAttribute elemAtt = AttributeCache <ElementAttribute, MemberInfo> .Get(pI[i]);

                    if (elemAtt != null)
                    {
                        if (elemAtt.IsName)
                        {
                            elementName = (string)pI[i].GetValue(tree);
                        }
                    }
                }

                if (elementName == null)
                {
                    for (int i = 0; i < fI.Length; i++)
                    {
                        ElementAttribute elemAtt = AttributeCache <ElementAttribute, MemberInfo> .Get(fI[i]);

                        if (elemAtt != null)
                        {
                            if (elemAtt.IsName)
                            {
                                elementName = (string)fI[i].GetValue(tree);
                            }
                        }
                    }
                }
                {
                    yield return(Environment.NewLine);

                    if (elementName == null)
                    {
                        elementName = tree.GetType().Name.ToUpper();
                    }

                    if (baseAtt.IsSimple)
                    {
                        yield return(Indent(indentLevel).Append(elementName).ToString());
                    }
                    else
                    {
                        yield return(Indent(indentLevel).Append("/begin " + elementName).ToString());
                    }
                }


                if (fI.Length > 0)
                {
                    foreach (string resultData in SerialiseElement(tree, fI, pI, indentLevel + 1))
                    {
                        yield return(resultData);
                    }
                }

                if (tree.GetType() == typeof(Asap2.MODULE))
                {
                    // Handle internal module data.
                    SortedList <ulong, Asap2Base> elements = new SortedList <ulong, Asap2Base>();
                    var moduleObj = tree as Asap2.MODULE;

                    if (moduleObj.elements != null)
                    {
                        foreach (var obj in moduleObj.elements)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.AxisPtsCharacteristicMeasurement != null)
                    {
                        foreach (var obj in moduleObj.AxisPtsCharacteristicMeasurement.Values)
                        {
                            elements.Add(obj.orderID(), (Asap2Base)obj);
                        }
                    }

                    if (moduleObj.CompuTabCompuVtabCompuVtabRanges != null)
                    {
                        foreach (var obj in moduleObj.CompuTabCompuVtabCompuVtabRanges.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.CompuMethods != null)
                    {
                        foreach (var obj in moduleObj.CompuMethods.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.Frames != null)
                    {
                        foreach (var obj in moduleObj.Frames.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.Functions != null)
                    {
                        foreach (var obj in moduleObj.Functions.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.Groups != null)
                    {
                        foreach (var obj in moduleObj.Groups.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.Record_layouts != null)
                    {
                        foreach (var obj in moduleObj.Record_layouts.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.Units != null)
                    {
                        foreach (var obj in moduleObj.Units.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    if (moduleObj.User_rights != null)
                    {
                        foreach (var obj in moduleObj.User_rights.Values)
                        {
                            elements.Add(obj.OrderID, obj);
                        }
                    }

                    foreach (var element in elements.Values)
                    {
                        foreach (string dataNode in SerialiseNode(element, indentLevel + 1))
                        {
                            yield return(dataNode);
                        }
                    }
                }

                if (!baseAtt.IsSimple)
                {
                    yield return(Environment.NewLine);

                    yield return(Indent(indentLevel).Append("/end " + elementName).ToString());
                }
            }
            else if (nodeElemAtt != null)
            {
                // Pure data node
                string data = data = Environment.NewLine + Indent(indentLevel).ToString();

                if (nodeElemAtt.IsString)
                {
                    String value = tree.ToString();
                    data += "\"" + value + "\"";
                    yield return(Indent(indentLevel).Append(data).ToString());
                }
                else
                {
                    if (tree.GetType().IsEnum)
                    {
                        String value = Enum.GetName(tree.GetType(), tree);
                        data += value;
                        yield return(data);
                    }
                    else if (nodeElemAtt.CodeAsHex)
                    {
                        UInt64 tmp   = (UInt64)tree;
                        String value = "0x" + tmp.ToString("X");
                        data += value;
                        yield return(data);
                    }
                    else if (tree.GetType() == typeof(decimal))
                    {
                        decimal value = (decimal)tree;
                        data += value.ToString(CultureInfo.InvariantCulture);
                        yield return(data);
                    }
                    else
                    {
                        data += tree.ToString();
                        yield return(data);
                    }
                }
            }
        }