Files
Umbraco-CMS/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedProperty.cs

78 lines
3.4 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
2013-11-07 17:16:22 +01:00
using System.Xml;
using System.Xml.Serialization;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Xml;
2013-11-07 17:16:22 +01:00
namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
2017-07-20 11:21:28 +02:00
/// <summary>
/// Represents an IDocumentProperty which is created based on an Xml structure.
/// </summary>
[Serializable]
[XmlType(Namespace = "http://umbraco.org/webservices/")]
internal class XmlPublishedProperty : PublishedPropertyBase
{
private readonly string _sourceValue; // the raw, xml node value
2016-06-23 09:39:31 +02:00
// Xml cache not using XPath value... and as for the rest...
// we're single threaded here, keep it simple
2016-06-23 09:39:31 +02:00
private object _objectValue;
private bool _objectValueComputed;
private readonly bool _isPreviewing;
private readonly IPublishedContent _content;
2013-11-07 17:16:22 +01:00
/// <summary>
/// Gets the raw value of the property.
/// </summary>
2018-04-21 09:57:28 +02:00
public override object GetSourceValue(string culture = null, string segment = null) => _sourceValue;
2013-11-07 17:16:22 +01:00
2017-07-20 11:21:28 +02:00
// in the Xml cache, everything is a string, and to have a value
2013-11-07 17:16:22 +01:00
// you want to have a non-null, non-empty string.
2018-04-21 09:57:28 +02:00
public override bool HasValue(string culture = null, string segment = null) => _sourceValue.Trim().Length > 0;
2013-11-07 17:16:22 +01:00
2018-04-21 09:57:28 +02:00
public override object GetValue(string culture = null, string segment = null)
2017-07-20 11:21:28 +02:00
{
2017-12-06 11:51:35 +01:00
// NOT caching the source (intermediate) value since we'll never need it
// everything in Xml cache is per-request anyways
// also, properties should not be shared between requests and therefore
// are single threaded, so the following code should be safe & fast
if (_objectValueComputed) return _objectValue;
var inter = PropertyType.ConvertSourceToInter(_content, _sourceValue, _isPreviewing);
// initial reference cache level always is .Content
_objectValue = PropertyType.ConvertInterToObject(_content, PropertyCacheLevel.Element, inter, _isPreviewing);
_objectValueComputed = true;
return _objectValue;
2016-06-23 09:39:31 +02:00
}
2018-04-21 09:57:28 +02:00
public override object GetXPathValue(string culture = null, string segment = null) { throw new NotImplementedException(); }
2013-11-07 17:16:22 +01:00
public XmlPublishedProperty(PublishedPropertyType propertyType, IPublishedContent content, bool isPreviewing, XmlNode propertyXmlData)
: this(propertyType, content, isPreviewing)
2017-07-20 11:21:28 +02:00
{
if (propertyXmlData == null)
throw new ArgumentNullException(nameof(propertyXmlData), "Property xml source is null");
_sourceValue = XmlHelper.GetNodeValue(propertyXmlData);
2013-11-07 17:16:22 +01:00
}
public XmlPublishedProperty(PublishedPropertyType propertyType, IPublishedContent content, bool isPreviewing, string propertyData)
: this(propertyType, content, isPreviewing)
2013-11-07 17:16:22 +01:00
{
if (propertyData == null)
throw new ArgumentNullException(nameof(propertyData));
_sourceValue = propertyData;
2013-11-07 17:16:22 +01:00
}
public XmlPublishedProperty(PublishedPropertyType propertyType, IPublishedContent content, bool isPreviewing)
: base(propertyType, PropertyCacheLevel.Unknown) // cache level is ignored
2013-11-07 17:16:22 +01:00
{
_sourceValue = string.Empty;
_content = content;
2013-11-07 17:16:22 +01:00
_isPreviewing = isPreviewing;
}
2017-07-20 11:21:28 +02:00
}
}