// niklas@protocol7.com
// 75
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SharpVectors.Dom.Stylesheets
{
///
/// The MediaList interface provides the abstraction of an ordered collection of media, without defining or constraining how this collection is implemented. An empty list is the same as a list that contains the medium "all".
/// The items in the MediaList are accessible via an integral index, starting from 0.
///
public sealed class MediaList : IMediaList
{
private static Regex splitter = new Regex(@"\s*,\s*");
#region Constructors
public MediaList() : this(String.Empty)
{
}
public MediaList(string val)
{
_parseString(val);
}
#endregion
private void _parseString(string mediaText)
{
mediaText = mediaText.Trim();
if(mediaText.Length > 0)
{
string[] ms = splitter.Split(mediaText);
foreach(string m in ms)
{
AppendMedium(m);
}
}
}
private void _clear()
{
medias.Clear();
}
#region Public methods
///
/// Compares this MediaList with another and see if the second fits this
///
/// The MediaList to compare
/// True if this list fits the specified
public bool Matches(MediaList inMedia)
{
if(inMedia.Length == 0) return false;
else if(Length == 0 || containsAll)
{
// is empty or this list contains "all"
return true;
}
else
{
for(ulong i = 0; i medias = new List();
#endregion
#region Implementation of IMediaList
///
/// Adds the medium newMedium to the end of the list. If the newMedium is already used, it is first removed.
///
/// The new medium to add.
/// INVALID_CHARACTER_ERR: If the medium contains characters that are invalid in the underlying style language.
/// NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.
public void AppendMedium(string newMedium)
{
if (newMedium.Length > 0)
{
medias.Remove(newMedium);
medias.Add(newMedium);
if (newMedium.Equals("all"))
containsAll = true;
}
}
///
/// Deletes the medium indicated by oldMedium from the list.
///
/// The medium to delete in the media list.
/// NOT_FOUND_ERR: Raised if oldMedium is not in the list.
/// NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.
public void DeleteMedium(string oldMedium)
{
if(oldMedium == "all") containsAll = false;
medias.Remove(oldMedium);
}
///
/// The number of media in the list. The range of valid media is 0 to length-1 inclusive.
///
public ulong Length
{
get
{
return (ulong)medias.Count;
}
}
///
/// The parsable textual representation of the media list. This is a comma-separated list of media.
///
/// SYNTAX_ERR: Raised if the specified string value has a syntax error and is unparsable.
/// NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.
public string MediaText
{
get
{
string result = String.Empty;
foreach(string media in medias)
{
result += media + ",";
}
if(result.Length > 0)
{
result = result.Substring(0, result.Length-1);
}
return result;
}
set
{
_clear();
_parseString(value);
}
}
///
/// Returns the indexth in the list. If index is greater than or equal to the number of media in the list, this returns null.
///
public string this[ulong index]
{
get
{
return (index < Length) ? medias[(int)index] : null;
}
}
#endregion
}
}