This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                                 switch (c.Input.NodeType) {
73                                 case XPathNodeType.Element:
74                                         break;
75                                 case XPathNodeType.Whitespace:
76                                         continue;
77                                 default:
78                                         if (c.CurrentStylesheet.Version == "1.0")
79                                                 throw new XsltCompileException ("Content " + c.Input.NodeType + " is not allowed in XSLT attribute-set element.", null, c.Input);
80                                         break;
81                                 }
82                                         
83                                 if (c.Input.NamespaceURI != XsltNamespace || c.Input.LocalName != "attribute")
84                                         throw new XsltCompileException ("Invalid attr set content", null, c.Input);
85                                 attributes.Add (new XslAttribute (c));
86                         } while (c.Input.MoveToNext ());
87                         
88                         c.Input.MoveToParent ();
89                         
90                 }
91                 
92                 public void Merge (XslAttributeSet s)
93                 {
94                         attributes.AddRange (s.attributes);
95                         
96                         foreach (QName q in s.usedAttributeSets)
97                                 if (!usedAttributeSets.Contains (q))
98                                         usedAttributeSets.Add (q);
99                 }
100                 
101                 public override void Evaluate (XslTransformProcessor p) {
102                         p.SetBusy (this);
103                         
104                         if (usedAttributeSets != null) {
105                                 for (int i = 0; i < usedAttributeSets.Count; i++) {
106                                         QName set = (QName) usedAttributeSets [i];
107                                         XslAttributeSet s = p.ResolveAttributeSet (set);
108                                         if (s == null)
109                                                 throw new XsltException ("Could not resolve attribute set", null, p.CurrentNode);
110                                         
111                                         if (p.IsBusy (s))
112                                                 throw new XsltException ("circular dependency", null, p.CurrentNode);
113                                         
114                                         s.Evaluate (p);
115                                 }
116                         }
117                                                 
118                         for (int i = 0; i < attributes.Count; i++)
119                                 ((XslAttribute) attributes [i]).Evaluate (p);
120                         
121                         p.SetFree (this);
122                 }
123         }
124 }