public bool Transform(ContentItem item) { string text = item[Name] as string; if(text != null) { string detailName = Name + "_Tokens"; int i = 0; var p = new Parser(new TemplateAnalyzer()); foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand)) { var dc = item.GetDetailCollection(detailName, true); var cd = new ContentDetail(item, detailName, c.Tokens.Select(t => t.Fragment).StringJoin()) { EnclosingCollection = dc, IntValue = c.Tokens.First().Index }; if (dc.Details.Count > i) dc.Details[i] = cd; else dc.Details.Add(cd); i++; } if (i > 0) { var dc = item.GetDetailCollection(detailName, true); for (int j = dc.Details.Count - 1; j >= i; i--) { dc.Details.RemoveAt(j); } return true; } } return false; }
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)); detailElement.WriteAttribute("meta", detail.Meta); var prop = item.GetType().GetProperty(detail.Name); if (prop != null) { if (Attribute.IsDefined(prop, typeof(N2.Details.XMLTranslateAttribute))) { string translate = "Value"; //if (options.HasFlag(ExportOptions.TranslateDetailName)) // translate += ",Name"; //if (options.HasFlag(ExportOptions.TranslateDetailTitle)) // translate += ",Title"; detailElement.WriteAttribute("xmlTranslate", translate); } } WriteInnerContents(item, detail, detail.ValueTypeKey, detailElement); } }
private object WriteDetailValue(Details.ContentDetail d) { if (d.ValueTypeKey == ContentDetail.TypeKeys.LinkType) { return(d.LinkedItem.ID); } return(d.Value); }
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); } }
public void Constructor_Double() { var detail = new ContentDetail(null, "Hello", 123.456); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.DoubleValue, Is.EqualTo(123.456)); Assert.That(detail.Value, Is.EqualTo(123.456)); Assert.That(detail.ValueType, Is.EqualTo(typeof(double))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.DoubleType)); }
public void Constructor_Int() { var detail = new ContentDetail(null, "Hello", 234); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.IntValue, Is.EqualTo(234)); Assert.That(detail.Value, Is.EqualTo(234)); Assert.That(detail.ValueType, Is.EqualTo(typeof(int))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.IntType)); }
public void Constructor_Object() { var detail = new ContentDetail(null, "Hello", new[] { "World" }); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.Value, Is.InstanceOf<string[]>()); Assert.That(((string[])detail.Value)[0], Is.EqualTo("World")); Assert.That(detail.ValueType, Is.EqualTo(typeof(object))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.ObjectType)); }
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)); WriteInnerContents(item, detail, detail.ValueTypeKey, detailElement); } }
public void Constructor_Bool() { var detail = new ContentDetail(null, "Hello", true); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.BoolValue, Is.True); Assert.That(detail.Value, Is.EqualTo(true)); Assert.That(detail.ValueType, Is.EqualTo(typeof(bool))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.BoolType)); }
public void Constructor_DateTime() { var detail = new ContentDetail(null, "Hello", new DateTime(2010, 06, 16)); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.DateTimeValue, Is.EqualTo(new DateTime(2010, 06, 16))); Assert.That(detail.Value, Is.EqualTo(new DateTime(2010, 06, 16))); Assert.That(detail.ValueType, Is.EqualTo(typeof(DateTime))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.DateTimeType)); }
public void Constructor_Link() { var item = new Definitions.Definitions.SideshowItem { ID = 123 }; var detail = new ContentDetail(null, "Hello", item); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.LinkedItem, Is.EqualTo(item)); Assert.That(detail.Value, Is.EqualTo(item)); Assert.That(detail.ValueType, Is.EqualTo(typeof(ContentItem))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.LinkType)); }
/// <summary> /// Copies values from the other detail onto itself. /// </summary> /// <param name="other"></param> public virtual void Extract(ContentDetail other) { ValueTypeKey = other.ValueTypeKey; Meta = other.Meta; BoolValue = other.BoolValue; IntValue = other.intValue; DoubleValue = other.DoubleValue; DateTimeValue = other.DateTimeValue; LinkedItem = other.LinkedItem; ObjectValue = other.ObjectValue; StringValue = other.StringValue; }
private ContentDetail GetDetail(object val) { ContentDetail detail; if (val is ContentDetail) detail = (ContentDetail)val; else detail = ContentDetail.New(EnclosingItem, null, val); if(detail.Name == null || !detail.Name.StartsWith(this.Name)) detail.Name = this.Name; detail.EnclosingItem = this.EnclosingItem; detail.EnclosingCollection = this; return detail; }
/// <summary>Clones the collection and </summary> /// <returns></returns> public virtual DetailCollection Clone() { DetailCollection collection = new DetailCollection(); collection.ID = 0; collection.Name = this.Name; collection.EnclosingItem = this.EnclosingItem; foreach (ContentDetail detail in this.Details) { ContentDetail cloned = detail.Clone(); cloned.EnclosingCollection = collection; collection.Add(cloned); } return collection; }
/// <summary>Creates a cloned object with the id set to 0.</summary> /// <returns>A new ContentDetail with the same Name and Value.</returns> public virtual ContentDetail Clone() { ContentDetail cloned = new ContentDetail(); cloned.ID = 0; cloned.Name = this.Name; cloned.BoolValue = this.BoolValue; cloned.DateTimeValue = this.DateTimeValue; cloned.DoubleValue = this.DoubleValue; cloned.IntValue = this.IntValue; cloned.LinkedItem = this.LinkedItem; cloned.ObjectValue = this.ObjectValue; cloned.StringValue = this.StringValue; cloned.ValueTypeKey = this.ValueTypeKey; return(cloned); }
public bool Transform(ContentItem item) { string text = item[Name] as string; if (text != null) { string collectionName = Name + CollectionSuffix; int i = 0; var p = new Parser(new TemplateAnalyzer()); foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand)) { var dc = item.GetDetailCollection(collectionName, true); var cd = ContentDetail.Multi(collectionName, stringValue: c.Tokens.Select(t => t.Fragment).StringJoin(), integerValue: c.Tokens.First().Index); cd.EnclosingItem = item; cd.EnclosingCollection = dc; if (dc.Details.Count > i) { dc.Details[i] = cd; } else { dc.Details.Add(cd); } i++; } if (i > 0) { var dc = item.GetDetailCollection(collectionName, true); for (int j = dc.Details.Count - 1; j >= i; j--) { dc.Details.RemoveAt(j); } return(true); } else if (i == 0) { var dc = item.GetDetailCollection(collectionName, false); if (dc != null) { item.DetailCollections.Remove(dc); return(true); } } } return(false); }
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()); } } }
private ContentDetail GetDetail(object val) { ContentDetail detail; if (val is ContentDetail) { detail = (ContentDetail)val; } else { detail = ContentDetail.New(EnclosingItem, null, val); } detail.Name = this.Name; detail.EnclosingItem = this.EnclosingItem; detail.EnclosingCollection = this; return(detail); }
/// <summary>Gets the associated property name for an enumerable collection used for storing In or NotIn comparison values.</summary> /// <param name="value"></param> /// <returns></returns> public static string GetAssociatedEnumerablePropertyName(IEnumerable value) { if (value == null) { throw new NotSupportedException(); } Type collectionType = value.GetType(); if (collectionType.IsArray && collectionType.GetElementType() != typeof(object)) { return(ContentDetail.GetAssociatedPropertyName(collectionType.GetElementType())); } if (collectionType.IsGenericType && collectionType.GetGenericArguments()[0] != typeof(object)) { return(ContentDetail.GetAssociatedPropertyName(collectionType.GetGenericArguments()[0])); } return(ContentDetail.GetAssociatedPropertyName(value.OfType <object>().FirstOrDefault())); }
/// <summary>Creates a cloned object with the id set to 0.</summary> /// <returns>A new ContentDetail with the same Name and Value.</returns> public virtual ContentDetail Clone() { ContentDetail cloned = new ContentDetail(); cloned.ID = 0; cloned.Name = this.Name; cloned.Meta = this.Meta; cloned.BoolValue = this.BoolValue; cloned.DateTimeValue = this.DateTimeValue; cloned.DoubleValue = this.DoubleValue; cloned.IntValue = this.IntValue; cloned.LinkedItem = this.LinkedItem; if (ObjectValue == null) { cloned.ObjectValue = null; } else { if (ObjectValue is ICloneable) { cloned.objectValue = (ObjectValue as ICloneable).Clone(); } else if (ObjectValue.GetType().IsValueType) { cloned.objectValue = ObjectValue; } else if (ObjectValue.GetType().IsSerializable) { cloned.objectValue = ObjectCopier.Clone(ObjectValue); } else { throw new InvalidDataException("Detail cannot be cloned: " + Name + " - " + ObjectValue.GetType().FullName); } } cloned.StringValue = this.StringValue; cloned.ValueTypeKey = this.ValueTypeKey; return(cloned); }
public bool Transform(ContentItem item) { string text = item[Name] as string; if (text != null) { string detailName = Name + "_Tokens"; int i = 0; var p = new Parser(new TemplateAnalyzer()); foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand)) { var dc = item.GetDetailCollection(detailName, true); var cd = new ContentDetail(item, detailName, c.Tokens.Select(t => t.Fragment).StringJoin()) { EnclosingCollection = dc, IntValue = c.Tokens.First().Index }; if (dc.Details.Count > i) { dc.Details[i] = cd; } else { dc.Details.Add(cd); } i++; } if (i > 0) { var dc = item.GetDetailCollection(detailName, true); for (int j = dc.Details.Count - 1; j >= i; i--) { dc.Details.RemoveAt(j); } return(true); } } return(false); }
public static void SetTranslation(this IContentList<DetailCollection> collections, string key, string value, string collectionKey = DefaultCollectionKey) { if (string.IsNullOrEmpty(key)) return; var collection = collections[collectionKey]; var detail = collection.Details.Where(cd => cd.Meta == key).FirstOrDefault(); if (detail == null) { if (value == null) return; detail = new ContentDetail(collection.EnclosingItem, collection.Name, value) { Meta = key }; detail.AddTo(collection); } else if (value == null) { collection.Details.Remove(detail); } else { detail.StringValue = value; } }
private void Update(ContentItem referrer, ContentDetail detail, string name, string newUrl, IEnumerable<ContentDetail> subsequentReferences) { int startIndex = detail.IntValue.Value; string oldUrl = detail.StringValue; string html = referrer[name] as string; if (html == null || html.Length < (startIndex + oldUrl.Length) || html.Substring(startIndex, oldUrl.Length) != oldUrl) return; // change the html referrer[name] = html.Substring(0, startIndex) + newUrl + html.Substring(startIndex + detail.StringValue.Length); // and update the reference detail.StringValue = newUrl; logger.DebugFormat("Updating links on {0}. Detail = {1}, Start index = {2}, old url = '{3}', new url = '{4}'", referrer, name, startIndex, oldUrl, newUrl); // adapt other reference indexes to the new string length foreach (var subsequent in subsequentReferences.Where(d => name.Equals(d.Meta))) { subsequent.IntValue = subsequent.IntValue.Value - oldUrl.Length + newUrl.Length; } }
private void InitialializeRelations(ContentDetail detail, IEventSource session) { if (detail == null) return; detail.LinkedItem.ValueAccessor = session.Get<ContentItem>; }
private void WriteMultiValue(ContentItem item, ContentDetail detail, string valueTypeKey, object value, XmlTextWriter writer) { if (value == null) return; using (ElementWriter multiElement = new ElementWriter("value", writer)) { multiElement.WriteAttribute("key", valueTypeKey); WriteInnerContents(item, detail, valueTypeKey, multiElement); } }
/// <summary>Creates a cloned object with the id set to 0.</summary> /// <returns>A new ContentDetail with the same Name and Value.</returns> public virtual ContentDetail Clone() { ContentDetail cloned = new ContentDetail(); cloned.ID = 0; cloned.Name = this.Name; cloned.BoolValue = this.BoolValue; cloned.DateTimeValue = this.DateTimeValue; cloned.DoubleValue = this.DoubleValue; cloned.IntValue = this.IntValue; cloned.LinkedItem = this.LinkedItem; cloned.ObjectValue = this.ObjectValue; cloned.StringValue = this.StringValue; cloned.ValueTypeKey = this.ValueTypeKey; return cloned; }
public void Constructor_String() { var detail = new ContentDetail(null, "Hello", "World"); Assert.That(detail, Is.InstanceOf<ContentDetail>()); Assert.That(detail.StringValue, Is.EqualTo("World")); Assert.That(detail.Value, Is.EqualTo("World")); Assert.That(detail.ValueType, Is.EqualTo(typeof(string))); Assert.That(detail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.StringType)); }
private void InitialializeRelations(ContentDetail detail, IEventSource session) { if (detail == null) return; detail.LinkedItem.ValueAccessor = o => GetByID(o, session); }
/// <summary>Inserts a value in the collection.</summary> /// <param name="index">The index to insert into.</param> /// <param name="value">The value to insert.</param> public virtual void Insert(int index, object value) { ContentDetail detail = GetDetail(value); Details.Insert(index, detail); }
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); } }