Ejemplo n.º 1
0
        private void WriteInnerContents(ContentItem item, ContentDetail detail, string valueTypeKey, ElementWriter element)
        {
            switch (valueTypeKey)
            {
            case ContentDetail.TypeKeys.BoolType:
                element.Write(detail.BoolValue.HasValue ? detail.BoolValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.DateTimeType:
                element.Write(SerializationUtility.ToUniversalString(detail.DateTimeValue));
                return;

            case ContentDetail.TypeKeys.DoubleType:
                element.Write(detail.DoubleValue.HasValue ? detail.DoubleValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.IntType:
                element.Write(detail.IntValue.HasValue ? detail.IntValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.LinkType:
                element.Write(detail.LinkValue.HasValue ? detail.LinkValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.MultiType:
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.BoolType, detail.BoolValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.DateTimeType, detail.DateTimeValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.DoubleType, detail.DoubleValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.IntType, detail.IntValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.LinkType, detail.LinkedItem, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.ObjectType, detail.ObjectValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.StringType, SerializationUtility.RemoveInvalidCharacters(detail.StringValue), element.Writer);
                return;

            case ContentDetail.TypeKeys.ObjectType:
                string base64representation = SerializationUtility.ToBase64String(detail.ObjectValue);
                element.Write(base64representation);
                return;

            case ContentDetail.TypeKeys.EnumType:                     /* TODO: Do we need to write out the 'meta' value here? */
            case ContentDetail.TypeKeys.StringType:
                string value = detail.StringValue;

                if (!string.IsNullOrEmpty(value))
                {
                    value = ExecuteRelativityTransformers(item, detail.Name, value);
                    element.WriteAttribute("encoded", true);
                    value = HttpUtility.HtmlEncode(SerializationUtility.RemoveInvalidCharacters(value));
                    element.WriteCData(value);
                }
                return;


            default:
                throw new InvalidOperationException("Invalid detail type: " + valueTypeKey);
            }
        }
Ejemplo n.º 2
0
        public virtual void WriteDetail(ContentItem item, ContentDetail detail, XmlTextWriter writer)
        {
            using (ElementWriter detailElement = new ElementWriter("detail", writer))
            {
                detailElement.WriteAttribute("name", detail.Name);
                detailElement.WriteAttribute("typeName", SerializationUtility.GetTypeAndAssemblyName(detail.ValueType));

                if (detail.ValueType == typeof(object))
                {
                    string base64representation = SerializationUtility.ToBase64String(detail.Value);
                    detailElement.Write(base64representation);
                }
                else if (detail.ValueType == typeof(ContentItem))
                {
                    detailElement.Write(detail.LinkValue.HasValue ? detail.LinkValue.Value.ToString() : "0");
                }
                else if (detail.ValueType == typeof(string))
                {
                    string value = detail.StringValue;

                    if (!string.IsNullOrEmpty(value))
                    {
                        if (value.StartsWith(applicationPath, StringComparison.InvariantCultureIgnoreCase))
                        {
                            var pi = item.GetContentType().GetProperty(detail.Name);
                            if (pi != null)
                            {
                                var transformers = pi.GetCustomAttributes(typeof(IRelativityTransformer), false);
                                foreach (IRelativityTransformer transformer in transformers)
                                {
                                    if (transformer.RelativeWhen == RelativityMode.Always || transformer.RelativeWhen == RelativityMode.ImportingOrExporting)
                                    {
                                        value = transformer.Rebase(value, applicationPath, "~/");
                                    }
                                }
                            }
                        }

                        detailElement.WriteCData(value);
                    }
                }
                else if (detail.ValueType == typeof(DateTime))
                {
                    detailElement.Write(ElementWriter.ToUniversalString(detail.DateTimeValue));
                }
                else
                {
                    detailElement.Write(detail.Value.ToString());
                }
            }
        }
        public void Write(ContentItem item, System.Xml.XmlTextWriter writer)
        {
            using (ElementWriter propertiesElement = new ElementWriter("properties", writer))
            {
                foreach (var persistable in definitions.GetDefinition(item).NamedOperators.OfType <PersistableAttribute>())
                {
                    string name  = ((IUniquelyNamed)persistable).Name;
                    object value = item[name];
                    if (value == null)
                    {
                        continue;
                    }

                    using (ElementWriter detailElement = new ElementWriter("property", writer))
                    {
                        detailElement.WriteAttribute("name", name);
                        Type type = value.GetType();

                        if (type == typeof(string))
                        {
                            Write(detailElement, type, (string)value, true);
                        }
                        else if (type == typeof(short) || type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(decimal))
                        {
                            Write(detailElement, type, value.ToString(), false);
                        }
                        else if (type == typeof(DateTime))
                        {
                            Write(detailElement, type, SerializationUtility.ToUniversalString(((DateTime)value)), false);
                        }
                        else if (type.IsEnum)
                        {
                            Write(detailElement, type, ((int)value).ToString(), false);
                        }
                        else if (typeof(ContentItem).IsAssignableFrom(type))
                        {
                            Write(detailElement, typeof(ContentItem), (((ContentItem)value).ID).ToString(), false);
                        }
                        else
                        {
                            Write(detailElement, typeof(object), SerializationUtility.ToBase64String(value), false);
                        }
                    }
                }
            }
        }
        private void WriteProperty(System.Xml.XmlTextWriter writer, string name, object value)
        {
            using (ElementWriter propertyElement = new ElementWriter("property", writer))
            {
                propertyElement.WriteAttribute("name", name);

                if (value == null)
                {
                    return;
                }
                Type type = value.GetType();

                if (type == typeof(string))
                {
                    Write(propertyElement, type, (string)value, true);
                }
                else if (type == typeof(short) || type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(decimal))
                {
                    Write(propertyElement, type, value.ToString(), false);
                }
                else if (type == typeof(DateTime))
                {
                    Write(propertyElement, type, SerializationUtility.ToUniversalString(((DateTime)value)), false);
                }
                else if (type.IsEnum)
                {
                    Write(propertyElement, type, ((int)value).ToString(), false);
                }
                else if (typeof(ContentItem).IsAssignableFrom(type))
                {
                    WriteItem(propertyElement, (ContentItem)value);
                }
                else if (type.IsContentItemEnumeration())
                {
                    WriteItems(propertyElement, (IEnumerable)value);
                }
                else
                {
                    Write(propertyElement, typeof(object), SerializationUtility.ToBase64String(value), false);
                }
            }
        }
Ejemplo n.º 5
0
        private void WriteInnerContents(ContentItem item, ContentDetail detail, string valueTypeKey, ElementWriter element)
        {
            switch (valueTypeKey)
            {
            case ContentDetail.TypeKeys.BoolType:
                element.Write(detail.BoolValue.HasValue ? detail.BoolValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.DateTimeType:
                element.Write(SerializationUtility.ToUniversalString(detail.DateTimeValue));
                return;

            case ContentDetail.TypeKeys.DoubleType:
                element.Write(detail.DoubleValue.HasValue ? detail.DoubleValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.IntType:
                element.Write(detail.IntValue.HasValue ? detail.IntValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.LinkType:
                element.Write(detail.LinkValue.HasValue ? detail.LinkValue.Value.ToString() : "0");
                return;

            case ContentDetail.TypeKeys.MultiType:
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.BoolType, detail.BoolValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.DateTimeType, detail.DateTimeValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.DoubleType, detail.DoubleValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.IntType, detail.IntValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.LinkType, detail.LinkedItem, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.ObjectType, detail.ObjectValue, element.Writer);
                WriteMultiValue(item, detail, ContentDetail.TypeKeys.StringType, detail.StringValue, element.Writer);
                return;

            case ContentDetail.TypeKeys.ObjectType:
                string base64representation = SerializationUtility.ToBase64String(detail.ObjectValue);
                element.Write(base64representation);
                return;

            case ContentDetail.TypeKeys.StringType:
                string value = detail.StringValue;

                if (!string.IsNullOrEmpty(value))
                {
                    var pi = item.GetContentType().GetProperty(detail.Name);
                    if (pi != null)
                    {
                        var transformers = pi.GetCustomAttributes(typeof(IRelativityTransformer), false);
                        foreach (IRelativityTransformer transformer in transformers)
                        {
                            if (transformer.RelativeWhen == RelativityMode.Always || transformer.RelativeWhen == RelativityMode.ImportingOrExporting)
                            {
                                value = transformer.Rebase(value, applicationPath, "~/");
                            }
                        }
                    }

                    element.WriteCData(value);
                }
                return;

            default:
                throw new InvalidOperationException("Invalid detail type: " + valueTypeKey);
            }
        }