Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssValue.cs @ 13398

Last change on this file since 13398 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 4.6 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>80</completed>
3
4using System;
5using System.Xml;
6using System.Globalization;
7using System.Text.RegularExpressions;
8
9namespace SharpVectors.Dom.Css
10{
11  /// <summary>
12  /// The CSSValue interface represents a simple or a complex value. A CSSValue object only occurs in a context of a CSS property
13  /// </summary>
14  public class CssValue : ICssValue
15  {
16    #region Static members
17    private static string numberPattern = @"[\-\+]?[0-9]*\.?[0-9]+";
18    public static string LengthUnitPattern = "(?<lengthUnit>in|cm|mm|px|em|ex|pc|pt|%)?";
19    public static string AngleUnitPattern = "(?<angleUnit>deg|rad|grad)?";
20    public static string LengthPattern = @"(?<lengthNumber>" + numberPattern + ")" + LengthUnitPattern;
21    public static string AnglePattern = @"(?<angleNumber>" + numberPattern + ")" + AngleUnitPattern;
22
23    private static string cssPrimValuePattern = @"^(?<primitiveValue>"
24      + @"(?<func>(?<funcname>attr|url|counter|rect|rgb)\((?<funcvalue>[^\)]+)\))"
25      + @"|(?<length>" + LengthPattern + ")"
26      + @"|(?<angle>" + AnglePattern + ")"
27      + @"|(?<freqTimeNumber>(?<numberValue2>" + numberPattern + ")(?<unit2>Hz|kHz|in|s|ms|%)?)"
28      + @"|(?<string>[""'](?<stringvalue>(.|\n)*?)[""'])"
29      + @"|(?<colorIdent>([A-Za-z]+)|(\#[A-Fa-f0-9]{6})|(\#[A-Fa-f0-9]{3}))"
30      + @")";
31
32    private static Regex reCssPrimitiveValue = new Regex(cssPrimValuePattern + "$");
33    private static Regex reCssValueList = new Regex(cssPrimValuePattern + @"(\s*,\s*)+$");
34
35    /// <summary>
36    /// Detects what kind of value cssText contains and returns an instance of the correct CssValue class
37    /// </summary>
38    /// <param name="cssText">The text to parse for a CSS value</param>
39    /// <param name="readOnly">Specifies if this instance is read-only</param>
40    /// <returns>The correct type of CSS value</returns>
41    static public CssValue GetCssValue(string cssText, bool readOnly)
42    {
43      if(cssText == "inherit")
44      {
45        // inherit
46        return new CssValue(CssValueType.Inherit, cssText, readOnly);
47      }
48      else
49      {
50        Match match = reCssPrimitiveValue.Match(cssText);
51        if(match.Success)
52        {
53          // single primitive value
54          return CssPrimitiveValue.Create(match, readOnly);
55        }
56       
57        match = reCssValueList.Match(cssText);
58        if(match.Success)
59        {
60          // list of primitive values
61          throw new NotImplementedException("Value lists not implemented");
62        }
63        else
64        {
65          // custom value
66          return new CssValue(CssValueType.Custom, cssText, readOnly);
67        }
68      }
69    }
70    #endregion
71
72    #region Constructors
73    /// <summary>
74    /// Constructor for CssValue
75    /// </summary>
76    /// <param name="type">The type of value</param>
77    /// <param name="cssText">The entire content of the value</param>
78    /// <param name="readOnly">Specifies if the instance is read-only</param>
79    public CssValue(CssValueType type, string cssText, bool readOnly)
80    {
81      _cssText = cssText.Trim();
82      _cssValueType = type;
83      _readOnly = readOnly;
84    }
85
86    /// <summary>
87    /// Only for internal use
88    /// </summary>
89    protected CssValue()
90    {
91      _readOnly = true;
92    }
93    #endregion
94
95    #region Public methods
96    public virtual CssValue GetAbsoluteValue(string propertyName, XmlElement elm)
97    {
98      return new CssAbsValue(this, propertyName, elm);
99    }
100    #endregion
101
102    private bool _readOnly;
103    public virtual bool ReadOnly
104    {
105      get
106      {
107        return _readOnly;
108      }
109    }
110
111    #region ICssValue Members
112
113    private string _cssText;
114        protected CssValueType _cssValueType;
115
116    /// <summary>
117    /// A string representation of the current value.
118    /// </summary>
119    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified CSS string value has a syntax error (according to the attached property) or is unparsable.</exception>
120    /// <exception cref="DomException">INVALID_MODIFICATION_ERR: Raised if the specified CSS string value represents a different type of values than the values allowed by the CSS property</exception>
121    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this value is readonly.</exception>
122    public virtual string CssText
123    {
124      get
125      {
126        return _cssText;
127      }
128      set
129      {
130        if(ReadOnly)
131        {
132          throw new DomException(DomExceptionType.InvalidModificationErr, "The CssValue is read-only");
133        }
134        else
135        {
136          _cssText = value;
137        }
138      }
139    }
140
141    /// <summary>
142    /// A code defining the type of the value as defined above
143    /// </summary>
144    public virtual CssValueType CssValueType
145    {
146      get
147      {
148        return _cssValueType;
149      }
150    }
151
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.