2004-08-25 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslAvt.cs
1 //
2 // XslAvt.cs
3 //
4 // Author:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Text;
35 using System.Xml;
36 using System.Xml.XPath;
37 using System.Xml.Xsl;
38
39 namespace Mono.Xml.Xsl.Operations {
40         // Represents an Attribute Value Template in XSL.
41         internal class XslAvt {
42                 
43                 string simpleString;
44                 ArrayList avtParts;
45                 
46                 public XslAvt (string str, Compiler comp) {
47                         if (str.IndexOf ("{") == -1 && str.IndexOf ("}") == -1) {
48                                 // That was easy ;-).
49                                 simpleString = str;
50                                 return;
51                         }
52                         avtParts = new ArrayList ();
53                         StringBuilder sb = new StringBuilder ();
54                         StringReader r = new StringReader (str);
55                         
56                         
57                         
58                         while (r.Peek () != -1) {
59                                 char c = (char)r.Read ();
60                                 switch (c) {                                    
61                                 case '{':
62                                         if ((char)r.Peek () == '{') {
63                                                 // {{ == escaped {
64                                                 sb.Append ((char)r.Read ());
65                                                 break;
66                                         }
67                                         
68                                         if (sb.Length != 0) {
69                                                 // Ok, we have already found a text
70                                                 // part, lets save that.
71                                                 avtParts.Add (new SimpleAvtPart (sb.ToString ()));
72                                                 sb.Length = 0;
73                                         }
74                                         
75                                         while ((c = (char)r.Read ()) != '}') {
76                                                 switch (c) {
77                                                 case '\'': case '"': {
78                                                         // We are inside a quote
79                                                         char unq = c;
80                                                         sb.Append (c);
81                                                         while ((c = (char)r.Read ()) != unq) {
82                                                                 sb.Append (c);
83                                                                 if (r.Peek () == -1)
84                                                                         throw new XsltCompileException ("unexpected end of AVT", null, comp.Input);
85                                                         }
86                                                                 
87                                                         
88                                                         sb.Append (c);
89                                                         break;
90                                                 } // ' or "
91                                                 default:
92                                                         sb.Append (c);
93                                                         break;
94                                                 }
95                                                 if (r.Peek () == -1) throw new XsltCompileException ("unexpected end of AVT", null, comp.Input);
96                                         }
97                                         
98                                                 
99                                         avtParts.Add (new XPathAvtPart (comp.CompileExpression (sb.ToString ())));
100                                         sb.Length = 0;
101                                         break;
102                                 case '}':
103                                         c = (char)r.Read ();
104                                         if (c != '}')
105                                                 throw new XsltCompileException ("Braces must be escaped", null, comp.Input);
106                                         goto default;
107                                 default:
108                                         sb.Append (c);
109                                         break;
110                                 }
111                         }
112                         if (sb.Length != 0) {
113                                 // Ok, we have already found a text
114                                 // part, lets save that.
115                                 avtParts.Add (new SimpleAvtPart (sb.ToString ()));
116                                 sb.Length = 0;
117                         }
118                 }
119                 
120                 public static string AttemptPreCalc (ref XslAvt avt)
121                 {
122                         if (avt == null) return null;
123                         if (avt.simpleString != null) {
124                                 string s = avt.simpleString;
125                                 avt = null;
126                                 return s;
127                         }
128                         return null;
129                 }
130                 
131                 public string Evaluate (XslTransformProcessor p)
132                 {
133                         if (simpleString != null) return simpleString;
134                         if (avtParts.Count == 1)
135                                 return ((AvtPart) avtParts [0]).Evaluate (p);
136                                 
137                         StringBuilder sb = p.GetAvtStringBuilder ();
138                         
139                         int len = avtParts.Count;
140                         for (int i = 0; i < len; i++)
141                                 sb.Append (((AvtPart) avtParts [i]).Evaluate (p));
142                         
143                         return p.ReleaseAvtStringBuilder ();
144                 }
145                 
146                 // Represents part of an AVT
147                 abstract class AvtPart {
148                         public abstract string Evaluate (XslTransformProcessor p);
149                 }
150                 
151                 sealed class SimpleAvtPart : AvtPart {
152                         string val;
153                         public SimpleAvtPart (string val)
154                         {
155                                 this.val = val;
156                         }
157                         
158                         public override string Evaluate (XslTransformProcessor p)
159                         {
160                                 return val;
161                         }
162                 }
163                 
164                 sealed class XPathAvtPart : AvtPart {
165                         XPathExpression expr;
166                         
167                         public XPathAvtPart (XPathExpression expr)
168                         {
169                                 this.expr = expr;
170                         }
171                         
172                         public override string Evaluate (XslTransformProcessor p)
173                         {
174                                 return p.EvaluateString (expr);
175                         }
176                 }
177         }
178 }