Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssXmlDocument.cs @ 13599

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

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

File size: 10.9 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>80</completed>
3
4using System;
5using System.IO;
6using System.Net;
7using System.Xml;
8using System.Collections.Generic;
9
10using SharpVectors.Xml;
11using SharpVectors.Dom.Stylesheets;
12
13namespace SharpVectors.Dom.Css
14{
15    /// <summary>
16    /// A XmlDocument with CSS support
17    /// </summary>
18    public class CssXmlDocument : Document, IDocumentCss, ICssView
19    {
20        #region Private Fields
21
22        internal List<string[]> styleElements = new List<string[]>();
23        internal MediaList _currentMedia = new MediaList("all");
24        public CssStyleSheet UserAgentStyleSheet;
25        public CssStyleSheet UserStyleSheet;
26
27        private bool _static;
28        private StyleSheetList _StyleSheets;
29        private CssPropertyProfile _cssPropertyProfile = new CssPropertyProfile();
30
31        #endregion
32
33        #region Constructors and Destructor
34
35        /// <summary>
36        /// Initializes a new instance of CssXmlDocument
37        /// </summary>
38        public CssXmlDocument()
39            : base()
40        {
41            setupNodeChangeListeners();
42
43            SharpVectors.Net.ExtendedHttpWebRequest.Register();
44            SharpVectors.Net.DataWebRequest.Register();
45        }
46
47        /// <summary>
48        /// Initializes a new instance of CssXmlDocument
49        /// </summary>
50        /// <param name="nt">The name table to use</param>
51        public CssXmlDocument(XmlNameTable nt)
52            : base(nt)
53        {
54            setupNodeChangeListeners();
55
56            SharpVectors.Net.ExtendedHttpWebRequest.Register();
57            SharpVectors.Net.DataWebRequest.Register();
58        }
59
60        #endregion
61
62        #region Public Properties
63
64        /// <summary>
65        /// Gets or sets a value indicating whether this <see cref="CssXmlDocument"/> handles DOM dynamic changes.
66        /// Sometimes (when loading or rendering) this needs to be disabled.
67        /// See <see href="StaticSection"/> for more information about use
68        /// </summary>
69        /// <value><c>true</c> if static; otherwise, <c>false</c>.</value>
70        public bool Static
71        {
72            get { return _static; }
73            set { _static = value; }
74        }
75
76        public MediaList Media
77        {
78            get
79            {
80                return _currentMedia;
81            }
82            set
83            {
84                _currentMedia = value;
85
86            }
87        }
88
89        public CssPropertyProfile CssPropertyProfile
90        {
91            get
92            {
93                return _cssPropertyProfile;
94            }
95            set
96            {
97                _cssPropertyProfile = value;
98            }
99        }
100
101        public string Url
102        {
103            get
104            {
105                return BaseURI;
106            }
107        }
108
109        /// <summary>
110        /// All the stylesheets associated with this document
111        /// </summary>
112        public IStyleSheetList StyleSheets
113        {
114            get
115            {
116                if (_StyleSheets == null)
117                {
118                    _StyleSheets = new StyleSheetList(this);
119                }
120                return _StyleSheets;
121            }
122        }
123
124        #endregion
125
126        #region Public Methods
127
128        public override XmlElement CreateElement(string prefix, string localName, string ns)
129        {
130            return new CssXmlElement(prefix, localName, ns, this);
131        }
132
133        /// <summary>
134        /// Loads a XML document, compare to XmlDocument.Load()
135        /// </summary>
136        /// <param name="filename"></param>
137        public override void Load(string filename)
138        {
139            using (StaticSection.Use(this))
140            {   
141                // remove any hash (won't work for local files)
142                int hashStart = filename.IndexOf("#");
143                if (hashStart > -1)
144                {
145                    filename = filename.Substring(0, hashStart);
146                }
147                base.Load(filename);
148            }
149        }
150
151        public override void LoadXml(string xml)
152        {
153            using (StaticSection.Use(this))
154            {
155                //base.LoadXml(xml);
156                // we use a stream here, only not to use SvgDocument.Load(XmlReader)
157                using (var xmlStream = new StringReader(xml))
158                {
159                    Load(xmlStream);
160                }
161            }
162        }
163
164        //JR added in
165        public override void Load(XmlReader reader)
166        {
167            using (StaticSection.Use(this))
168            {
169                base.Load(reader);
170            }
171        }
172
173        /// <summary>
174        /// Adds a element type to be used as style elements (e.g. as in the HTML style element)
175        /// </summary>
176        /// <param name="ns">The namespace URI of the element</param>
177        /// <param name="localName">The local-name of the element</param>
178        public void AddStyleElement(string ns, string localName)
179        {
180            styleElements.Add(new string[] { ns, localName });
181        }
182
183        /// <summary>
184        /// Sets the user agent stylesheet for this document
185        /// </summary>
186        /// <param name="href">The URI to the stylesheet</param>
187        public void SetUserAgentStyleSheet(string href)
188        {
189            UserAgentStyleSheet = new CssStyleSheet(null, href, String.Empty, String.Empty, null, CssStyleSheetType.UserAgent);
190        }
191
192        /// <summary>
193        /// Sets the user stylesheet for this document
194        /// </summary>
195        /// <param name="href">The URI to the stylesheet</param>
196        public void SetUserStyleSheet(string href)
197        {
198            UserStyleSheet = new CssStyleSheet(null, href, String.Empty, String.Empty, null, CssStyleSheetType.User);
199        }
200
201        public void AddStyleSheet(string href)
202        {
203
204            UserStyleSheet = new CssStyleSheet(null, href, String.Empty, String.Empty, null, CssStyleSheetType.User);
205        }
206
207        public WebResponse GetResource(Uri absoluteUri)
208        {
209            WebRequest request = WebRequest.Create(absoluteUri);
210            WebResponse response = request.GetResponse();
211
212            return response;
213        }
214
215        #endregion
216
217        #region IDocumentCss Members
218
219        /// <summary>
220        /// This method is used to retrieve the override style declaration for a specified element and a specified pseudo-element.
221        /// </summary>
222        /// <param name="elt">The element whose style is to be modified. This parameter cannot be null.</param>
223        /// <param name="pseudoElt">The pseudo-element or null if none.</param>
224        /// <returns>The override style declaration.</returns>
225        public SharpVectors.Dom.Css.ICssStyleDeclaration GetOverrideStyle(System.Xml.XmlElement elt, string pseudoElt)
226        {
227            throw new NotImplementedException("CssXmlDocument.GetOverrideStyle()");
228        }
229
230        #endregion
231
232        #region IViewCss Members
233
234        /// <summary>
235        /// This method is used to get the computed style as it is defined in [CSS2].
236        /// </summary>
237        /// <param name="elt">The element whose style is to be computed. This parameter cannot be null.</param>
238        /// <param name="pseudoElt">The pseudo-element or null if none.</param>
239        /// <returns>The computed style. The CSSStyleDeclaration is read-only and contains only absolute values.</returns>
240        public SharpVectors.Dom.Css.ICssStyleDeclaration GetComputedStyle(System.Xml.XmlElement elt, string pseudoElt)
241        {
242            if (elt == null) throw new NullReferenceException();
243            else if (!(elt is CssXmlElement)) throw new DomException(DomExceptionType.SyntaxErr, "element must of type CssXmlElement");
244            else
245            {
246                return ((CssXmlElement)elt).GetComputedStyle(pseudoElt);
247            }
248        }
249
250        #endregion
251
252        #region Update handling
253
254        private void setupNodeChangeListeners()
255        {
256            XmlNodeChangedEventHandler handler = new XmlNodeChangedEventHandler(NodeChangedEvent);
257
258            NodeChanged += handler;
259            NodeInserted += handler;
260            //NodeRemoving += handler;
261            NodeRemoved += handler;
262        }
263
264        public void NodeChangedEvent(Object src, XmlNodeChangedEventArgs args)
265        {
266            if (!Static)
267            {
268                #region Attribute updates
269                // xmlns:xml is auto-inserted whenever a selectNode is performed, we don't want those events
270                if (args.Node is XmlText && args.NewParent is XmlAttribute && args.NewParent.Name != "xmlns:xml")
271                {
272                    XmlAttribute attr = args.NewParent as XmlAttribute;
273                    CssXmlElement elm = attr.OwnerElement as CssXmlElement;
274                    if (elm != null)
275                    {
276                        elm.AttributeChange(attr, args);
277                    }
278                }
279                else if (args.Node is XmlAttribute && args.Node.Name != "xmlns:xml")
280                {
281                    // the cause of the change is a XmlAttribute => happens during inserting or removing
282                    CssXmlElement oldElm = args.OldParent as CssXmlElement;
283                    if (oldElm != null) oldElm.AttributeChange(args.Node, args);
284
285                    CssXmlElement newElm = args.NewParent as CssXmlElement;
286                    if (newElm != null) newElm.AttributeChange(args.Node, args);
287                }
288                #endregion
289
290                #region OnElementChange
291                if (args.Node is XmlText && args.NewParent is XmlElement)
292                {
293                    CssXmlElement element = (CssXmlElement)args.NewParent;
294                    element.ElementChange(src, args);
295                }
296                else if (args.Node is CssXmlElement)
297                {
298                    if (args.Action == XmlNodeChangedAction.Insert || args.Action == XmlNodeChangedAction.Change)
299                    {
300                        // Changes to a child XML node may affect the sibling offsets (for example in tspan)
301                        // By calling the parent's OnElementChange, invalidation will propogate back to Node
302                        CssXmlElement newParent = (CssXmlElement)args.NewParent;
303                        newParent.ElementChange(src, args);
304                    }
305
306                    if (args.Action == XmlNodeChangedAction.Remove)
307                    {
308                        // Removing a child XML node may affect the sibling offsets (for example in tspan)
309                        CssXmlElement oldParent = (CssXmlElement)args.OldParent;
310                        oldParent.ElementChange(src, args);
311                    }
312                }
313                #endregion
314
315            }
316        }
317
318        #endregion
319    }
320}
Note: See TracBrowser for help on using the repository browser.