PropertyValueConverter - add converters for built-in data types

This commit is contained in:
Stephan
2013-10-04 20:14:38 +02:00
parent a264342bcc
commit c2e7fc2f10
9 changed files with 265 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
using System;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(int))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class IntegerValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.Integer).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null) return 0;
// in XML an integer is a string
var sourceString = source as string;
if (sourceString != null)
{
int i;
return (int.TryParse(sourceString, out i)) ? i : 0;
}
// in the database an integer is an integer
// default value is zero
return (source is int) ? source : 0;
}
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(IEnumerable<string>))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class MultipleTextStringValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.MultipleTextstring).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
// data is (both in database and xml):
// <keyFeatureList>
// <values>
// <value>Strong</value>
// <value>Flexible</value>
// <value>Efficient</value>
// </values>
// </keyFeatureList>
var sourceString = source.ToString();
if (string.IsNullOrWhiteSpace(sourceString)) return Enumerable.Empty<string>();
var values = new List<string>();
var pos = sourceString.IndexOf("<value>", StringComparison.Ordinal);
while (pos >= 0)
{
pos += "<value>".Length;
var npos = sourceString.IndexOf("<", pos, StringComparison.Ordinal);
var value = sourceString.Substring(pos, npos - pos);
values.Add(value);
pos = sourceString.IndexOf("<value>", pos, StringComparison.Ordinal);
}
return values.ToArray();
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
var d = new XmlDocument();
var e = d.CreateElement("values");
d.AppendChild(e);
var values = (IEnumerable<string>) source;
foreach (var value in values)
{
var ee = d.CreateElement("value");
ee.InnerText = value;
e.AppendChild(ee);
}
return d.CreateNavigator();
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Web;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(IHtmlString))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class SimpleEditorValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.UltraSimpleEditor).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
// in xml a string is: string
// in the database a string is: string
// default value is: null
return source;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return new HtmlString(source == null ? string.Empty : (string)source);
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source;
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(string))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class TextStringValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.Textbox).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
// in xml a string is: string
// in the database a string is: string
// default value is: null
return source;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source ?? string.Empty;
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source;
}
}
}

View File

@@ -194,6 +194,10 @@
<Compile Include="PropertyEditors\PropertyValueTypeAttribute.cs" />
<Compile Include="PropertyEditors\PropertyCacheLevel.cs" />
<Compile Include="PropertyEditors\PropertyValueConverterBase.cs" />
<Compile Include="PropertyEditors\ValueConverters\IntegerValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\MultipleTextStringValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\SimpleEditorValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\TextStringValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\TinyMceValueConverter.cs" />
<Compile Include="PropertyEditors\PropertyValueConvertersResolver.cs" />
<Compile Include="PropertyEditors\IPropertyValueConverter.cs" />

View File

@@ -0,0 +1,43 @@
using System;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Templates;
namespace Umbraco.Web.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(IHtmlString))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Request)]
public class SimpleEditorValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.UltraSimpleEditor).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null) return null;
var sourceString = source.ToString();
// ensures string is parsed for {localLink} and urls are resolved correctly
sourceString = TemplateUtilities.ParseInternalLinks(sourceString, preview);
sourceString = TemplateUtilities.ResolveUrlsFromTextString(sourceString);
return sourceString;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return new HtmlString(source == null ? string.Empty : (string)source);
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source;
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Templates;
namespace Umbraco.Web.PropertyEditors.ValueConverters
{
[PropertyValueType(typeof(string))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Request)]
public class TextStringValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return Guid.Parse(Constants.PropertyEditors.Textbox).Equals(propertyType.PropertyEditorGuid);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null) return null;
var sourceString = source.ToString();
// ensures string is parsed for {localLink} and urls are resolved correctly
sourceString = TemplateUtilities.ParseInternalLinks(sourceString, preview);
sourceString = TemplateUtilities.ResolveUrlsFromTextString(sourceString);
return sourceString;
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source ?? string.Empty;
}
public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
// source should come from ConvertSource and be a string (or null) already
return source;
}
}
}

View File

@@ -298,6 +298,8 @@
<Compile Include="Controllers\RegisterController.cs" />
<Compile Include="Models\ProfileModel.cs" />
<Compile Include="Models\LoginStatusModel.cs" />
<Compile Include="PropertyEditors\ValueConverters\SimpleEditorValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\TextStringValueConverter.cs" />
<Compile Include="PublishedContentPropertyExtension.cs" />
<Compile Include="Models\UmbracoProperty.cs" />
<Compile Include="Mvc\MergeParentContextViewDataAttribute.cs" />

View File

@@ -293,8 +293,11 @@ namespace Umbraco.Web
// discovered when CoreBootManager configures the converters. We HAVE to remove one of them
// here because there cannot be two converters for one property editor - and we want the full
// RteMacroRenderingValueConverter that converts macros, etc. So remove TinyMceValueConverter.
// (why it exists in in the first place, I'm not sure to understand)
// (the limited one, defined in Core, is there for tests)
PropertyValueConvertersResolver.Current.RemoveType<TinyMceValueConverter>();
// same for other converters
PropertyValueConvertersResolver.Current.RemoveType<Core.PropertyEditors.ValueConverters.TextStringValueConverter>();
PropertyValueConvertersResolver.Current.RemoveType<Core.PropertyEditors.ValueConverters.SimpleEditorValueConverter>();
PublishedCachesResolver.Current = new PublishedCachesResolver(new PublishedCaches(
new PublishedCache.XmlPublishedCache.PublishedContentCache(),