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); } } } } }
protected virtual void WriteDefaultAttributes(ElementWriter itemElement, ContentItem item) { itemElement.WriteAttribute("id", item.ID); itemElement.WriteAttribute("name", item.ID.ToString() == item.Name ? "" : item.Name); if (item.Parent != null) { if (item.Parent.ID != 0) { itemElement.WriteAttribute("parent", item.Parent.ID.ToString()); } else { itemElement.WriteAttribute("parent", item.Parent.VersionOf.ID.ToString()); if (item.Parent.GetVersionKey() != null) { itemElement.WriteAttribute("parentVersionKey", item.Parent.GetVersionKey()); } } } itemElement.WriteAttribute("title", item.Title); itemElement.WriteAttribute("zoneName", item.ZoneName); itemElement.WriteAttribute("templateKey", item.TemplateKey); if (item.TranslationKey.HasValue) { itemElement.WriteAttribute("translationKey", item.TranslationKey.Value); } itemElement.WriteAttribute("state", (int)item.State); itemElement.WriteAttribute("created", item.Created); itemElement.WriteAttribute("updated", item.Updated); itemElement.WriteAttribute("published", item.Published); itemElement.WriteAttribute("expires", item.Expires); itemElement.WriteAttribute("sortOrder", item.SortOrder); itemElement.WriteAttribute("url", parser.BuildUrl(item)); itemElement.WriteAttribute("visible", item.Visible); itemElement.WriteAttribute("savedBy", item.SavedBy); itemElement.WriteAttribute("typeName", SerializationUtility.GetTypeAndAssemblyName(item.GetContentType())); itemElement.WriteAttribute("discriminator", definitions.GetDefinition(item).Discriminator); itemElement.WriteAttribute("versionIndex", item.VersionIndex); itemElement.WriteAttribute("ancestralTrail", item.AncestralTrail); itemElement.WriteAttribute("alteredPermissions", (int)item.AlteredPermissions); itemElement.WriteAttribute("childState", (int)item.ChildState); if (item.VersionOf.HasValue) { itemElement.WriteAttribute("versionOf", item.VersionOf.ID.Value); } }
protected virtual void WriteDefaultAttributes(ElementWriter itemElement, ContentItem item) { itemElement.WriteAttribute("id", item.ID); itemElement.WriteAttribute("name", item.ID.ToString() == item.Name ? "" : item.Name); itemElement.WriteAttribute("parent", item.Parent != null ? item.Parent.ID.ToString() : string.Empty); itemElement.WriteAttribute("title", item.Title); itemElement.WriteAttribute("zoneName", item.ZoneName); itemElement.WriteAttribute("created", item.Created); itemElement.WriteAttribute("updated", item.Updated); itemElement.WriteAttribute("published", item.Published); itemElement.WriteAttribute("expires", item.Expires); itemElement.WriteAttribute("sortOrder", item.SortOrder); itemElement.WriteAttribute("url", parser.BuildUrl(item)); itemElement.WriteAttribute("visible", item.Visible); itemElement.WriteAttribute("savedBy", item.SavedBy); itemElement.WriteAttribute("typeName", SerializationUtility.GetTypeAndAssemblyName(item.GetContentType())); itemElement.WriteAttribute("discriminator", definitions.GetDefinition(item.GetContentType()).Discriminator); }
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); } } }
protected virtual void ReadDefaultAttributes(Dictionary <string, string> attributes, ContentItem item, ReadingJournal journal) { item.Created = ToNullableDateTime(attributes["created"]).Value; item.Expires = ToNullableDateTime(attributes["expires"]); item.ID = Convert.ToInt32(attributes["id"]); item.Name = attributes["name"]; if (item.ID.ToString() == item.Name) { item.Name = null; } item.Published = ToNullableDateTime(attributes["published"]); item.SavedBy = attributes["savedBy"]; item.SortOrder = Convert.ToInt32(attributes["sortOrder"]); item.Title = attributes["title"]; item.Updated = ToNullableDateTime(attributes["updated"]).Value; item.Visible = Convert.ToBoolean(attributes["visible"]); if (!string.IsNullOrEmpty(attributes["zoneName"])) { item.ZoneName = attributes["zoneName"]; } if (attributes.ContainsKey("templateKey") && !string.IsNullOrEmpty(attributes["templateKey"])) { item.TemplateKey = attributes["templateKey"]; } if (attributes.ContainsKey("translationKey") && !string.IsNullOrEmpty(attributes["translationKey"])) { item.TranslationKey = Convert.ToInt32(attributes["translationKey"]); } if (attributes.ContainsKey("ancestralTrail")) { item.AncestralTrail = attributes["ancestralTrail"]; } if (attributes.ContainsKey("alteredPermissions")) { item.AlteredPermissions = (Permission)Convert.ToInt32(attributes["alteredPermissions"]); } if (attributes.ContainsKey("childState")) { item.ChildState = (Collections.CollectionState)Convert.ToInt32(attributes["childState"]); } if (attributes.ContainsKey("versionIndex")) { item.VersionIndex = Convert.ToInt32(attributes["versionIndex"]); } if (attributes.ContainsKey("versionOf")) { item.VersionOf.ID = Convert.ToInt32(attributes["versionOf"]); item.VersionOf.ValueAccessor = repository.Get; } if (attributes.ContainsKey("parent")) { var parentVersionKey = attributes.ContainsKey("parentVersionKey") ? attributes["parentVersionKey"] : null; HandleParentRelation(item, attributes["parent"], parentVersionKey, journal); } if (attributes.ContainsKey("state") && !string.IsNullOrEmpty(attributes["state"])) { item.State = (ContentState)Convert.ToInt32(attributes["state"]); } else { item.State = SerializationUtility.RecaulculateState(item); } }
protected virtual void WriteDefaultAttributes(ElementWriter itemElement, ContentItem item) { if (itemElement == null) { throw new ArgumentNullException("itemElement"); } if (item == null) { throw new ArgumentNullException("item"); } itemElement.WriteAttribute("id", item.ID); itemElement.WriteAttribute("name", item.ID.ToString() == item.Name ? "" : item.Name, true); if (item.Parent != null) { if (item.Parent.ID != 0) { itemElement.WriteAttribute("parent", item.Parent.ID.ToString()); } else { itemElement.WriteAttribute("parent", item.Parent.VersionOf.ID.ToString()); if (item.Parent.GetVersionKey() != null) { itemElement.WriteAttribute("parentVersionKey", item.Parent.GetVersionKey()); } } } itemElement.WriteAttribute("title", item.Title, true); itemElement.WriteAttribute("zoneName", item.ZoneName, true); itemElement.WriteAttribute("templateKey", item.TemplateKey, true); if (item.TranslationKey.HasValue) { itemElement.WriteAttribute("translationKey", item.TranslationKey.Value); } itemElement.WriteAttribute("state", Enum.GetName(typeof(ContentState), item.State)); // use textual state to avoid future import trouble itemElement.WriteAttribute("created", item.Created); itemElement.WriteAttribute("updated", item.Updated); itemElement.WriteAttribute("published", item.Published); itemElement.WriteAttribute("expires", item.Expires); // TODO expired items could be excluded itemElement.WriteAttribute("sortOrder", item.SortOrder); if (item.IsPage && item.IsPublished()) { itemElement.WriteAttribute("url", (parser != null) ? (string)parser.BuildUrl(item) : item.Url); // generated } itemElement.WriteAttribute("visible", item.Visible); itemElement.WriteAttribute("savedBy", item.SavedBy); itemElement.WriteAttribute("typeName", SerializationUtility.GetTypeAndAssemblyName(item.GetContentType())); itemElement.WriteAttribute("discriminator", definitions.GetDefinition(item).Discriminator); itemElement.WriteAttribute("ancestralTrail", item.AncestralTrail); // generated if (item.AlteredPermissions != 0) { itemElement.WriteAttribute("alteredPermissions", (int)item.AlteredPermissions); } itemElement.WriteAttribute("childState", (int)item.ChildState); if (item.VersionOf.HasValue && item.VersionOf.ID != null) { itemElement.WriteAttribute("versionIndex", item.VersionIndex); // write only if versioned itemElement.WriteAttribute("versionOf", item.VersionOf.ID.Value); } }
protected virtual void ReadDefaultAttributes(Dictionary <string, string> attributes, ContentItem item, ReadingJournal journal) { item.Created = ToDateTime(attributes["created"]); item.Expires = attributes.ContainsKey("expires") ? ToNullableDateTime(attributes["expires"]) : null; item.ID = Convert.ToInt32(attributes["id"]); item.Name = GetStringOrNull(attributes, "name"); if (item.ID.ToString() == item.Name) { item.Name = null; } item.Published = attributes.ContainsKey("published") ? ToNullableDateTime(attributes["published"]) : null; item.SavedBy = attributes.ContainsKey("savedBy") ? attributes["savedBy"] : null; item.SortOrder = Convert.ToInt32(attributes["sortOrder"]); item.Title = attributes.ContainsKey("title") ? attributes["title"] : ""; item.Updated = ToDateTime(attributes["updated"]); item.Visible = Convert.ToBoolean(attributes["visible"]); item.ZoneName = GetStringOrNull(attributes, "zoneName"); // empty string must get converted to null for code to work, see PersistentContentItemList<T>.FindPages() item.TemplateKey = GetStringOrNull(attributes, "templateKey"); if (attributes.ContainsKey("translationKey") && !string.IsNullOrEmpty(attributes["translationKey"])) { item.TranslationKey = Convert.ToInt32(attributes["translationKey"]); } if (attributes.ContainsKey("ancestralTrail")) { item.AncestralTrail = attributes["ancestralTrail"]; } if (attributes.ContainsKey("alteredPermissions")) { item.AlteredPermissions = (Permission)Convert.ToInt32(attributes["alteredPermissions"]); } if (attributes.ContainsKey("childState")) { item.ChildState = (Collections.CollectionState)Convert.ToInt32(attributes["childState"]); } if (attributes.ContainsKey("versionIndex")) { item.VersionIndex = Convert.ToInt32(attributes["versionIndex"]); } if (attributes.ContainsKey("versionOf")) { item.VersionOf.ID = Convert.ToInt32(attributes["versionOf"]); if (repository != null) { item.VersionOf.ValueAccessor = repository.Get; } } if (attributes.ContainsKey("parent")) { var parentVersionKey = attributes.ContainsKey("parentVersionKey") ? attributes["parentVersionKey"] : null; HandleParentRelation(item, attributes["parent"], parentVersionKey, journal); } if (attributes.ContainsKey("state") && !string.IsNullOrEmpty(attributes["state"])) { // be tolerant to read both old numeric and new textual state var val = attributes["state"]; int intval; ContentState newstate; if (int.TryParse(val, out intval)) { newstate = (ContentState)intval; } else if (!Enum.TryParse(val, true, out newstate)) { newstate = SerializationUtility.RecaulculateState(item); } item.State = newstate; } else // TODO: dangerous migration code { item.State = SerializationUtility.RecaulculateState(item); } }
protected virtual void WriteDefaultAttributes(ElementWriter itemElement, ContentItem item) { if (itemElement == null) { throw new ArgumentNullException("itemElement"); } if (item == null) { throw new ArgumentNullException("item"); } itemElement.WriteAttribute("id", item.ID); itemElement.WriteAttribute("name", item.ID.ToString(CultureInfo.InvariantCulture) == item.Name ? "" : item.Name); if (item.Parent != null) { if (item.Parent.ID != 0) { itemElement.WriteAttribute("parent", item.Parent.ID.ToString(CultureInfo.InvariantCulture)); } else { itemElement.WriteAttribute("parent", item.Parent.VersionOf.ID.ToString()); if (item.Parent.GetVersionKey() != null) { itemElement.WriteAttribute("parentVersionKey", item.Parent.GetVersionKey()); } } } string typeAndAssemblyName = null; try { typeAndAssemblyName = SerializationUtility.GetTypeAndAssemblyName(item.GetContentType()); } catch (Exception ex) { throw new Exception("Couldn't get type/assembly name.", ex); } itemElement.WriteAttribute("title", item.Title); itemElement.WriteAttribute("zoneName", item.ZoneName); itemElement.WriteAttribute("templateKey", item.TemplateKey); if (item.TranslationKey.HasValue) { itemElement.WriteAttribute("translationKey", item.TranslationKey.Value); } itemElement.WriteAttribute("state", (int)item.State); itemElement.WriteAttribute("created", item.Created); itemElement.WriteAttribute("updated", item.Updated); itemElement.WriteAttribute("published", item.Published); itemElement.WriteAttribute("expires", item.Expires); itemElement.WriteAttribute("sortOrder", item.SortOrder); itemElement.WriteAttribute("url", item.Url); itemElement.WriteAttribute("visible", item.Visible); itemElement.WriteAttribute("savedBy", item.SavedBy); itemElement.WriteAttribute("typeName", typeAndAssemblyName); itemElement.WriteAttribute("discriminator", definitions.GetDefinition(item).Discriminator); itemElement.WriteAttribute("versionIndex", item.VersionIndex); itemElement.WriteAttribute("ancestralTrail", item.AncestralTrail); itemElement.WriteAttribute("alteredPermissions", (int)item.AlteredPermissions); itemElement.WriteAttribute("childState", (int)item.ChildState); if (item.VersionOf.HasValue) { Debug.Assert(item.VersionOf.ID != null, "item.VersionOf.ID != null"); itemElement.WriteAttribute("versionOf", item.VersionOf.ID.Value); } }
private void WriteItem(ElementWriter propertyElement, ContentItem item) { propertyElement.WriteAttribute("typeName", SerializationUtility.GetTypeAndAssemblyName(typeof(ContentItem))); propertyElement.WriteAttribute("versionKey", item.GetVersionKey()); propertyElement.Write(item.ID.ToString()); }
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); } }
public void WriteAttribute(string attributeName, DateTime?value) { WriteAttribute(attributeName, value.HasValue ? SerializationUtility.ToUniversalString(value.Value) : string.Empty); }
public void WriteAttribute(string attributeName, DateTime value) { WriteAttribute(attributeName, SerializationUtility.ToUniversalString(value)); }