2005-11-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslAttributeSet.cs
1 //
2 // XslAttributeSet.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //      
8 // (C) 2003 Ben Maurer
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Xml;
37 using System.Xml.Schema;
38 using System.Xml.XPath;
39 using System.Xml.Xsl;
40 using Mono.Xml.Xsl.Operations;
41
42 using QName = System.Xml.XmlQualifiedName;
43
44 namespace Mono.Xml.Xsl {
45         internal class XslAttributeSet : XslCompiledElement {
46                 QName name;
47                 // [QName]=>XslAttributeSet
48                 ArrayList usedAttributeSets = new ArrayList ();
49                 
50                 // [QName]=>XslAttribute
51                 ArrayList attributes = new ArrayList ();
52                 
53                 public XslAttributeSet (Compiler c) : base (c) {}
54                 
55                 public QName Name {
56                         get { return name; }
57                 }
58
59                 protected override void Compile (Compiler c)
60                 {
61                         this.name = c.ParseQNameAttribute ("name");
62                         
63                         QName [] attrSets = c.ParseQNameListAttribute ("use-attribute-sets");
64                         if (attrSets != null)
65                                 foreach (QName q in attrSets)
66                                         usedAttributeSets.Add (q);
67
68                         
69                         if (!c.Input.MoveToFirstChild ()) return;
70                                 
71                         do {
72                                 if (c.Input.NodeType != XPathNodeType.Element)
73                                         continue;
74                                 if (c.Input.NamespaceURI != XsltNamespace || c.Input.LocalName != "attribute")
75                                         throw new XsltCompileException ("Invalid attr set content", null, c.Input);
76                                 attributes.Add (new XslAttribute (c));
77                         } while (c.Input.MoveToNext ());
78                         
79                         c.Input.MoveToParent ();
80                         
81                 }
82                 
83                 public void Merge (XslAttributeSet s)
84                 {
85                         attributes.AddRange (s.attributes);
86                         
87                         foreach (QName q in s.usedAttributeSets)
88                                 if (!usedAttributeSets.Contains (q))
89                                         usedAttributeSets.Add (q);
90                 }
91                 
92                 public override void Evaluate (XslTransformProcessor p) {
93                         p.SetBusy (this);
94                         
95                         if (usedAttributeSets != null) {
96                                 for (int i = 0; i < usedAttributeSets.Count; i++) {
97                                         QName set = (QName) usedAttributeSets [i];
98                                         XslAttributeSet s = p.ResolveAttributeSet (set);
99                                         if (s == null)
100                                                 throw new XsltException ("Could not resolve attribute set", null, p.CurrentNode);
101                                         
102                                         if (p.IsBusy (s))
103                                                 throw new XsltException ("circular dependency", null, p.CurrentNode);
104                                         
105                                         s.Evaluate (p);
106                                 }
107                         }
108                                                 
109                         for (int i = 0; i < attributes.Count; i++)
110                                 ((XslAttribute) attributes [i]).Evaluate (p);
111                         
112                         p.SetFree (this);
113                 }
114         }
115 }