5 // Ben Maurer (bmaurer@users.sourceforge.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
36 using System.Xml.XPath;
39 namespace Mono.Xml.Xsl.Operations {
40 // Represents an Attribute Value Template in XSL.
41 internal class XslAvt {
46 public XslAvt (string str, Compiler comp) {
47 if (str.IndexOf ("{") == -1 && str.IndexOf ("}") == -1) {
52 avtParts = new ArrayList ();
53 StringBuilder sb = new StringBuilder ();
54 StringReader r = new StringReader (str);
58 while (r.Peek () != -1) {
59 char c = (char)r.Read ();
62 if ((char)r.Peek () == '{') {
64 sb.Append ((char)r.Read ());
69 // Ok, we have already found a text
70 // part, lets save that.
71 avtParts.Add (new SimpleAvtPart (sb.ToString ()));
75 while ((c = (char)r.Read ()) != '}') {
77 case '\'': case '"': {
78 // We are inside a quote
81 while ((c = (char)r.Read ()) != unq) {
84 throw new XsltCompileException ("unexpected end of AVT", null, comp.Input);
95 if (r.Peek () == -1) throw new XsltCompileException ("unexpected end of AVT", null, comp.Input);
99 avtParts.Add (new XPathAvtPart (comp.CompileExpression (sb.ToString ())));
105 throw new XsltCompileException ("Braces must be escaped", null, comp.Input);
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 ()));
120 public static string AttemptPreCalc (ref XslAvt avt)
122 if (avt == null) return null;
123 if (avt.simpleString != null) {
124 string s = avt.simpleString;
131 public string Evaluate (XslTransformProcessor p)
133 if (simpleString != null) return simpleString;
134 if (avtParts.Count == 1)
135 return ((AvtPart) avtParts [0]).Evaluate (p);
137 StringBuilder sb = p.GetAvtStringBuilder ();
139 int len = avtParts.Count;
140 for (int i = 0; i < len; i++)
141 sb.Append (((AvtPart) avtParts [i]).Evaluate (p));
143 return p.ReleaseAvtStringBuilder ();
146 // Represents part of an AVT
147 abstract class AvtPart {
148 public abstract string Evaluate (XslTransformProcessor p);
151 sealed class SimpleAvtPart : AvtPart {
153 public SimpleAvtPart (string val)
158 public override string Evaluate (XslTransformProcessor p)
164 sealed class XPathAvtPart : AvtPart {
165 XPathExpression expr;
167 public XPathAvtPart (XPathExpression expr)
172 public override string Evaluate (XslTransformProcessor p)
174 return p.EvaluateString (expr);