Ejemplo n.º 1
0
 public void Serialize(System.Xml.XmlTextWriter xtw)
 {
     foreach (object obj in m_Array)
     {
         IXCalSerializable serializer = SerializerFactory.Create(obj);
         if (serializer != null)
         {
             serializer.Serialize(xtw);
         }
     }
 }
Ejemplo n.º 2
0
        virtual public void Serialize(XmlTextWriter xtw)
        {
            if (Object.Name != null)
            {
                // Serialize "VERSION" before any other properties
                if (Object.Properties.ContainsKey("VERSION"))
                {
                    Property          p          = (Property)Object.Properties["VERSION"];
                    IXCalSerializable serializer = SerializerFactory.Create(p);
                    if (serializer != null)
                    {
                        serializer.Serialize(xtw);
                    }
                }

                foreach (DictionaryEntry de in Object.Properties)
                {
                    // Don't serialize "VERSION" again, we've already done it above.
                    if (de.Key.Equals("VERSION"))
                    {
                        continue;
                    }

                    Property          p          = (Property)de.Value;
                    IXCalSerializable serializer = SerializerFactory.Create(p);
                    if (serializer != null)
                    {
                        serializer.Serialize(xtw);
                    }
                }

                foreach (DictionaryEntry de in Object.Parameters)
                {
                    Parameter         p          = (Parameter)de.Value;
                    IXCalSerializable serializer = SerializerFactory.Create(p);
                    if (serializer != null)
                    {
                        serializer.Serialize(xtw);
                    }
                }
            }

            foreach (DDay.iCal.Components.iCalObject obj in Object.Children)
            {
                IXCalSerializable serializer = SerializerFactory.Create(obj);
                if (serializer != null)
                {
                    serializer.Serialize(xtw);
                }
            }
        }
        public override void Serialize(System.Xml.XmlTextWriter xtw)
        {
            xtw.WriteStartElement(m_Component.Name.ToLower());

            // Get a list of fields and properties
            List <object> items = this.FieldsAndProperties;

            // Alphabetize the list of fields & properties we just obtained
            items.Sort(new FieldPropertyAlphabetizer());

            // Iterate through each item and attempt to serialize it
            foreach (object item in items)
            {
                FieldInfo    field     = null;
                PropertyInfo prop      = null;
                Type         itemType  = null;
                string       itemName  = null;
                object[]     itemAttrs = null;

                if (item is FieldInfo)
                {
                    field    = (FieldInfo)item;
                    itemType = field.FieldType;
                    itemName = field.Name;
                }
                else
                {
                    prop     = (PropertyInfo)item;
                    itemType = prop.PropertyType;
                    itemName = prop.Name;
                }

                // Get attributes that are attached to each item
                itemAttrs = (field != null) ? field.GetCustomAttributes(true) : prop.GetCustomAttributes(true);

                // Get the item's value
                object obj = (field == null) ? prop.GetValue(Component, null) : field.GetValue(Component);

                // Adjust the items' name to be iCal-compliant
                if (obj is iCalObject)
                {
                    iCalObject ico = (iCalObject)obj;
                    if (ico.Name == null)
                    {
                        ico.Name = itemName.ToUpper().Replace("_", "-");
                    }

                    // If the property is non-standard, then replace
                    // it with an X-name
                    if (!ico.Name.StartsWith("X-"))
                    {
                        foreach (object attr in itemAttrs)
                        {
                            if (attr is NonstandardAttribute)
                            {
                                ico.Name = "X-" + ico.Name;
                                break;
                            }
                        }
                    }
                }

                // Retrieve custom attributes for this field/property
                if (obj is iCalDataType)
                {
                    ((iCalDataType)obj).Attributes = itemAttrs;
                }

                // Get the default value of the object, if available
                object defaultValue = null;
                foreach (Attribute a in itemAttrs)
                {
                    if (a is DefaultValueAttribute)
                    {
                        defaultValue = ((DefaultValueAttribute)a).Value;
                    }
                }

                // Create a serializer for the object
                IXCalSerializable serializer = SerializerFactory.Create(obj);

                // To continue, the default value must either not be set,
                // or it must not match the actual value of the item.
                if (defaultValue == null ||
                    (serializer != null && !serializer.SerializeToString().Equals(defaultValue.ToString())))
                {
                    // FIXME: enum values cannot name themselves; we need to do it for them.
                    // For this to happen, we probably need to wrap enum values into a
                    // class that inherits from iCalObject.
                    if (itemType.IsEnum)
                    {
                        xtw.WriteStartElement(itemName.ToLower().Replace("_", "-"));
                    }

                    // Actually serialize the object
                    if (serializer != null)
                    {
                        serializer.Serialize(xtw);
                    }

                    if (itemType.IsEnum)
                    {
                        xtw.WriteEndElement();
                    }
                }
            }

            // If any extra serialization is necessary, do it now
            base.Serialize(xtw);

            // Close the component
            xtw.WriteEndElement();
        }