**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslAvt.cs
blobc7eaa9d154538c8855242dc8854a6407e24ec64d
1 //
2 // XslAvt.cs
3 //
4 // Author:
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
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.
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;
39 namespace Mono.Xml.Xsl.Operations {
40 // Represents an Attribute Value Template in XSL.
41 internal class XslAvt {
43 string simpleString;
44 ArrayList avtParts;
46 public XslAvt (string str, Compiler comp) {
47 if (str.IndexOf ("{") == -1 && str.IndexOf ("}") == -1) {
48 // That was easy ;-).
49 simpleString = str;
50 return;
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 ();
60 switch (c) {
61 case '{':
62 if ((char)r.Peek () == '{') {
63 // {{ == escaped {
64 sb.Append ((char)r.Read ());
65 break;
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;
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);
88 sb.Append (c);
89 break;
90 } // ' or "
91 default:
92 sb.Append (c);
93 break;
95 if (r.Peek () == -1) throw new XsltCompileException ("unexpected end of AVT", null, comp.Input);
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;
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;
120 public static string AttemptPreCalc (ref XslAvt avt)
122 if (avt == null) return null;
123 if (avt.simpleString != null) {
124 string s = avt.simpleString;
125 avt = null;
126 return s;
128 return null;
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 {
152 string val;
153 public SimpleAvtPart (string val)
155 this.val = val;
158 public override string Evaluate (XslTransformProcessor p)
160 return val;
164 sealed class XPathAvtPart : AvtPart {
165 XPathExpression expr;
167 public XPathAvtPart (XPathExpression expr)
169 this.expr = expr;
172 public override string Evaluate (XslTransformProcessor p)
174 return p.EvaluateString (expr);