Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Stylesheets/StyleSheet.cs @ 13791

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

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

File size: 8.9 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>75</completed>
3
4using System;
5using System.Diagnostics;
6using System.Xml;
7using System.Text.RegularExpressions;
8using System.IO;
9using System.Net;
10using SharpVectors.Dom.Css;
11
12namespace SharpVectors.Dom.Stylesheets
13{
14  /// <summary>
15  /// The StyleSheet interface is the abstract base interface for any type of style sheet. It represents a single style sheet associated with a structured document. In HTML, the StyleSheet interface represents either an external style sheet, included via the HTML LINK element, or an inline STYLE element. In XML, this interface represents an external style sheet, included via a style sheet processing instruction.
16  /// </summary>
17  public class StyleSheet : IStyleSheet
18  {
19        #region Private Fields
20
21        private bool TriedDownload;
22        private bool SucceededDownload;
23        private MediaList _Media;
24        private string _Title;
25        private string _Href;
26        private IStyleSheet _ParentStyleSheet;
27        private XmlNode ownerNode;
28        private bool _Disabled;
29        private string _Type;
30        private string sheetContent;
31
32        #endregion
33
34    #region Constructors and Destructor
35
36        protected StyleSheet(string media)
37    {
38            _Title = String.Empty;
39            _Href  = String.Empty;
40            _Type  = String.Empty;
41
42            if (String.IsNullOrEmpty(media))
43            {
44                _Media = new MediaList();
45            }
46            else
47            {
48                _Media = new MediaList(media);
49            }
50    }
51
52        public StyleSheet(XmlProcessingInstruction pi)
53            : this(String.Empty)
54    {
55      Regex re = new Regex(@"(?<name>[a-z]+)=[""'](?<value>[^""']*)[""']");
56            Match match = re.Match(pi.Data);
57
58      while(match.Success)
59      {
60        string name = match.Groups["name"].Value;
61        string val = match.Groups["value"].Value;
62
63        switch(name)
64        {
65          case "href":
66            _Href = val;
67            break;
68          case "type":
69            _Type = val;
70            break;
71          case "title":
72            _Title = val;
73            break;
74          case "media":
75            _Media = new MediaList(val);
76            break;
77        }
78        match = match.NextMatch();
79      }
80
81      ownerNode = pi;
82    }
83
84        public StyleSheet(XmlElement styleElement)
85            : this(String.Empty)
86    {
87      if (styleElement.HasAttribute("href"))
88                _Href = styleElement.Attributes["href"].Value;
89      if (styleElement.HasAttribute("type"))
90                _Type = styleElement.Attributes["type"].Value;
91      if (styleElement.HasAttribute("title"))
92                _Title = styleElement.Attributes["title"].Value;
93      if (styleElement.HasAttribute("media"))
94                _Media = new MediaList(styleElement.Attributes["media"].Value);
95
96      ownerNode = styleElement;
97    }
98
99    public StyleSheet(XmlNode ownerNode, string href, string type, string title, string media)
100            : this(media)
101    {
102      this.ownerNode = ownerNode;
103
104      _Href  = href;
105      _Type  = type;
106      _Title = title;
107    }
108
109    #endregion
110
111    #region Protected Properties
112
113    internal string SheetContent
114    {
115      get
116      {
117        if(OwnerNode is XmlElement)
118        {
119                    return OwnerNode.InnerText;
120        }
121        else
122        {
123          // a PI
124          if(!TriedDownload)
125          {
126            LoadSheet();
127          }
128          if(SucceededDownload) return sheetContent;
129          else return String.Empty;
130        }
131      }
132
133    }
134
135    #endregion
136
137    #region Internal methods
138
139    /// <summary>
140    /// Used to find matching style rules in the cascading order
141    /// </summary>
142    /// <param name="elt">The element to find styles for</param>
143    /// <param name="pseudoElt">The pseudo-element to find styles for</param>
144    /// <param name="ml">The medialist that the document is using</param>
145    /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
146    protected internal virtual void GetStylesForElement(XmlElement elt, string pseudoElt,
147            MediaList ml, CssCollectedStyleDeclaration csd)
148    {
149     
150    }
151
152        internal XmlNode ResolveOwnerNode()
153        {
154            if (OwnerNode != null) return OwnerNode;
155            else
156            {
157                return ((StyleSheet)ParentStyleSheet).ResolveOwnerNode();
158            }
159        }
160
161    #endregion
162
163    #region Public Methods
164
165    #endregion
166
167    #region Protected Methods
168
169    internal void LoadSheet()
170    {
171      WebRequest request = (WebRequest)WebRequest.Create(AbsoluteHref);
172      TriedDownload = true;
173      try
174      {
175        WebResponse response = (WebResponse)request.GetResponse();
176
177        SucceededDownload = true;
178        System.IO.StreamReader str = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default, true);
179        sheetContent = str.ReadToEnd();
180        str.Close();
181      }
182      catch
183      {
184        SucceededDownload = false;
185        sheetContent = String.Empty;
186      }
187
188    }
189
190    #endregion
191
192    #region IStyleSheet Members
193
194    /// <summary>
195    /// The intended destination media for style information. The media is often specified in the ownerNode. If no media has been specified, the MediaList will be empty. See the media attribute definition for the LINK element in HTML 4.0, and the media pseudo-attribute for the XML style sheet processing instruction . Modifying the media list may cause a change to the attribute disabled.
196    /// </summary>
197    public IMediaList Media
198    {
199      get
200      {
201        return _Media;
202      }
203    }
204
205    /// <summary>
206    /// The advisory title. The title is often specified in the ownerNode. See the title attribute definition for the LINK element in HTML 4.0, and the title pseudo-attribute for the XML style sheet processing instruction.
207    /// </summary>
208    public string Title
209    {
210      get
211      {
212        return _Title;
213      }
214    }
215
216    /// <summary>
217    /// If the style sheet is a linked style sheet, the value of its attribute is its location. For inline style sheets, the value of this attribute is null. See the href attribute definition for the LINK element in HTML 4.0, and the href pseudo-attribute for the XML style sheet processing instruction.
218    /// </summary>
219    public string Href
220    {
221      get
222      {
223        return _Href;
224      }
225    }
226    /// <summary>
227    /// The resolved absolute URL to the stylesheet
228    /// </summary>
229        public Uri AbsoluteHref
230        {
231            get
232            {
233                Uri u = null;
234                if (ownerNode != null)
235                {
236                    if (ownerNode.BaseURI != null)
237                    {
238                        u = new Uri(new Uri(ownerNode.BaseURI), Href);
239                    }
240                    //else
241                    //{                           
242                    //    u = new Uri(ApplicationContext.DocumentDirectoryUri, Href);
243                    //}
244                }
245               
246                if (u == null)
247                {
248                    throw new InvalidDataException();
249                }
250
251                return u;
252            }
253        }
254
255    /// <summary>
256    /// For style sheet languages that support the concept of style sheet inclusion, this attribute represents the including style sheet, if one exists. If the style sheet is a top-level style sheet, or the style sheet language does not support inclusion, the value of this attribute is null.
257    /// </summary>
258        public IStyleSheet ParentStyleSheet
259        {
260            get
261            {
262                return _ParentStyleSheet;
263            }
264            set
265            {
266                _ParentStyleSheet = value;
267            }
268        }
269
270    /// <summary>
271    /// The node that associates this style sheet with the document. For HTML, this may be the corresponding LINK or STYLE element. For XML, it may be the linking processing instruction. For style sheets that are included by other style sheets, the value of this attribute is null.
272    /// </summary>
273    public XmlNode OwnerNode
274    {
275      get
276      {
277        return ownerNode;
278      }
279    }
280
281    /// <summary>
282    /// false if the style sheet is applied to the document. true if it is not. Modifying this attribute may cause a new resolution of style for the document. A stylesheet only applies if both an appropriate medium definition is present and the disabled attribute is false. So, if the media doesn't apply to the current user agent, the disabled attribute is ignored.
283    /// </summary>
284    public bool Disabled
285    {
286      get
287      {
288        return _Disabled;
289      }
290      set
291      {
292        _Disabled = value;
293      }
294    }
295
296    /// <summary>
297    /// This specifies the style sheet language for this style sheet. The style sheet language is specified as a content type (e.g. "text/css"). The content type is often specified in the ownerNode. Also see the type attribute definition for the LINK element in HTML 4.0, and the type pseudo-attribute for the XML style sheet processing instruction.
298    /// </summary>
299    public string Type
300    {
301      get
302      {
303        return _Type;
304      }
305    }
306
307    #endregion
308  }
309}
Note: See TracBrowser for help on using the repository browser.