5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 using System.Collections;
15 using System.Xml.XPath;
18 namespace Mono.Xml.Xsl.Operations {
19 // Represents an Attribute Value Template in XSL.
25 public XslAvt (string str, Compiler comp) {
26 if (str.IndexOf ("{") == -1 && str.IndexOf ("}") == -1) {
31 avtParts = new ArrayList ();
32 StringBuilder sb = new StringBuilder ();
33 StringReader r = new StringReader (str);
37 while (r.Peek () != -1) {
38 char c = (char)r.Read ();
41 if ((char)r.Peek () == '{') {
43 sb.Append ((char)r.Read ());
48 // Ok, we have already found a text
49 // part, lets save that.
50 avtParts.Add (new SimpleAvtPart (sb.ToString ()));
54 while ((c = (char)r.Read ()) != '}') {
56 case '\'': case '"': {
57 // We are inside a quote
60 while ((c = (char)r.Read ()) != unq) {
62 if (r.Peek () == -1) throw new Exception ("unexpected end of AVT");
73 if (r.Peek () == -1) throw new Exception ("unexpected end of AVT");
77 avtParts.Add (new XPathAvtPart (comp.CompileExpression (sb.ToString ())));
83 throw new Exception ("Braces must be escaped");
91 // Ok, we have already found a text
92 // part, lets save that.
93 avtParts.Add (new SimpleAvtPart (sb.ToString ()));
98 public static string AttemptPreCalc (ref XslAvt avt)
100 if (avt == null) return null;
101 if (avt.simpleString != null) {
102 string s = avt.simpleString;
109 public string Evaluate (XslTransformProcessor p)
111 if (simpleString != null) return simpleString;
113 StringBuilder sb = new StringBuilder ();
115 foreach (AvtPart part in avtParts)
116 sb.Append (part.Evaluate (p));
118 return sb.ToString ();
121 // Represents part of an AVT
122 abstract class AvtPart {
123 public abstract string Evaluate (XslTransformProcessor p);
126 sealed class SimpleAvtPart : AvtPart {
128 public SimpleAvtPart (string val)
133 public override string Evaluate (XslTransformProcessor p)
139 sealed class XPathAvtPart : AvtPart {
140 XPathExpression expr;
142 public XPathAvtPart (XPathExpression expr)
147 public override string Evaluate (XslTransformProcessor p)
149 return p.EvaluateString (expr);