1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
//
// XslTemplate.cs
//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
// Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
//
// (C) 2003 Ben Maurer
// (C) 2003 Atsushi Enomoto
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Mono.Xml.Xsl.Operations;
using Mono.Xml.XPath;
using QName = System.Xml.XmlQualifiedName;
namespace Mono.Xml.Xsl {
internal class XslModedTemplateTable {
class TemplateWithPriority : IComparable {
public readonly double Priority;
public readonly XslTemplate Template;
public readonly Pattern Pattern;
public readonly int TemplateID;
public TemplateWithPriority (XslTemplate t, Pattern p)
{
Template = t;
Pattern = p;
Priority = p.DefaultPriority;
TemplateID = t.Id;
}
public TemplateWithPriority (XslTemplate t, double p)
{
Template = t;
Pattern = t.Match;
Priority = p;
TemplateID = t.Id;
}
public int CompareTo (object o)
{
TemplateWithPriority a = this,
b = (TemplateWithPriority)o;
//Debug.WriteLine (a.Pattern.ToString () + " ? " + b.Pattern.ToString ());
//Debug.WriteLine (a.Priority + " " + b.Priority);
int r0 = a.Priority.CompareTo (b.Priority);
//Debug.WriteLine (r0);
if (r0 != 0) return r0;
int r1 = a.TemplateID.CompareTo (b.TemplateID);
//Debug.WriteLine (r1);
return r1;
}
public bool Matches (XPathNavigator n, XslTransformProcessor p)
{
//Debug.WriteLine (Pattern.ToString ());
return p.Matches (Pattern, n);
}
}
// [QName name]=>XslTemplate
ArrayList unnamedTemplates = new ArrayList ();
XmlQualifiedName mode;
public XslModedTemplateTable (XmlQualifiedName mode)
{
if (mode == null)
throw new InvalidOperationException ();
this.mode = mode;
}
public XmlQualifiedName Mode {
get { return mode; }
}
public void Add (XslTemplate t)
{
if (!double.IsNaN (t.Priority))
unnamedTemplates.Add (new TemplateWithPriority (t, t.Priority));
else
Add (t, t.Match);
}
public void Add (XslTemplate t, Pattern p)
{
if (p is UnionPattern) {
Add (t, ((UnionPattern)p).p0);
Add (t, ((UnionPattern)p).p1);
return;
}
unnamedTemplates.Add (new TemplateWithPriority (t, p));
}
bool sorted = false;
public XslTemplate FindMatch (XPathNavigator node, XslTransformProcessor p)
{
//Debug.WriteLine ("...");
if (!sorted) {
unnamedTemplates.Sort ();
unnamedTemplates.Reverse ();
sorted = true;
}
for (int i = 0; i < unnamedTemplates.Count; i++) {
TemplateWithPriority t = (TemplateWithPriority) unnamedTemplates [i];
if (t.Matches (node, p))
return t.Template;
}
return null;
}
}
internal class XslTemplateTable {
// [QName mode]=>XslTemplateTable
Hashtable templateTables = new Hashtable ();
Hashtable namedTemplates = new Hashtable ();
XslStylesheet parent;
public XslTemplateTable (XslStylesheet parent)
{
this.parent = parent;
}
public Hashtable TemplateTables {
get { return templateTables; }
}
public XslModedTemplateTable this [XmlQualifiedName mode] {
get {
return templateTables [mode] as XslModedTemplateTable;
}
}
public void Add (XslTemplate template)
{
if (template.Name != XmlQualifiedName.Empty) {
if (namedTemplates [template.Name] != null)
throw new InvalidOperationException ("Named template " + template.Name + " is already registered.");
namedTemplates [template.Name] = template;
}
if (template.Match == null) return;
XslModedTemplateTable tbl = this [template.Mode];
if (tbl == null) {
tbl = new XslModedTemplateTable (template.Mode);
Add (tbl);
}
tbl.Add (template);
}
public void Add (XslModedTemplateTable table)
{
if (this [table.Mode] != null)
throw new InvalidOperationException ("Mode " + table.Mode + " is already registered.");
templateTables.Add (table.Mode, table);
}
public XslTemplate FindMatch (XPathNavigator node, XmlQualifiedName mode, XslTransformProcessor p)
{
XslTemplate ret;
if (this [mode] != null)
{
ret = this [mode].FindMatch (node, p);
if (ret != null) return ret;
}
for (int i = parent.Imports.Count - 1; i >= 0; i--)
{
XslStylesheet s = (XslStylesheet)parent.Imports [i];
ret = s.Templates.FindMatch (node, mode, p);
if (ret != null)
return ret;
}
return null;
}
public XslTemplate FindTemplate (XmlQualifiedName name)
{
XslTemplate ret = (XslTemplate)namedTemplates [name];
if (ret != null) return ret;
for (int i = parent.Imports.Count - 1; i >= 0; i--) {
XslStylesheet s = (XslStylesheet)parent.Imports [i];
ret = s.Templates.FindTemplate (name);
if (ret != null)
return ret;
}
return null;
}
}
internal class XslTemplate
{
XmlQualifiedName name;
Pattern match;
XmlQualifiedName mode;
double priority = double.NaN;
ArrayList parameters;
XslOperation content;
static int nextId = 0;
public readonly int Id = nextId ++;
XslStylesheet style;
int stackSize;
public XslTemplate (Compiler c)
{
if (c == null) return; // built in template
this.style = c.CurrentStylesheet;
c.PushScope ();
if (c.Input.Name == "template" &&
c.Input.NamespaceURI == Compiler.XsltNamespace &&
c.Input.MoveToAttribute ("mode", String.Empty)) {
c.Input.MoveToParent ();
if (!c.Input.MoveToAttribute ("match", String.Empty))
throw new XsltCompileException ("XSLT 'template' element must not have 'mode' attribute when it does not have 'match' attribute", null, c.Input);
c.Input.MoveToParent ();
}
if (c.Input.NamespaceURI != Compiler.XsltNamespace) {
this.name = QName.Empty;
this.match = c.CompilePattern ("/", c.Input);
this.mode = QName.Empty;
} else {
this.name = c.ParseQNameAttribute ("name");
this.match = c.CompilePattern (c.GetAttribute ("match"), c.Input);
this.mode = c.ParseQNameAttribute ("mode");
string pri = c.GetAttribute ("priority");
if (pri != null) {
try {
this.priority = double.Parse (pri, CultureInfo.InvariantCulture);
} catch (FormatException ex) {
throw new XsltException ("Invalid priority number format", ex, c.Input);
}
}
}
Parse (c);
stackSize = c.PopScope ().VariableHighTide;
}
public XmlQualifiedName Name {
get { return name; }
}
public Pattern Match {
get {
return match;
}
}
public XmlQualifiedName Mode {
get { return mode; }
}
public double Priority {
get { return priority; }
}
public XslStylesheet Parent {
get { return style; }
}
private void Parse (Compiler c) {
if (c.Input.NamespaceURI != Compiler.XsltNamespace) {
content = c.CompileTemplateContent ();
return;
}
if (c.Input.MoveToFirstChild ()) {
bool alldone = true;
XPathNavigator contentStart = c.Input.Clone ();
bool shouldMove = false;
do {
if (shouldMove) {
shouldMove = false;
contentStart.MoveTo (c.Input);
}
if (c.Input.NodeType == XPathNodeType.Text)
{ alldone = false; break; }
if (c.Input.NodeType != XPathNodeType.Element)
continue;
if (c.Input.NamespaceURI != Compiler.XsltNamespace)
{ alldone = false; break; }
if (c.Input.LocalName != "param")
{ alldone = false; break; }
if (this.parameters == null)
this.parameters = new ArrayList ();
parameters.Add (new XslLocalParam (c));
shouldMove = true;
} while (c.Input.MoveToNext ());
if (!alldone) {
c.Input.MoveTo (contentStart);
content = c.CompileTemplateContent ();
}
c.Input.MoveToParent ();
}
}
string LocationMessage {
get {
XslCompiledElementBase op = (XslCompiledElementBase) content;
return String.Format (" from\nxsl:template {0} at {1} ({2},{3})", Match, style.BaseURI, op.LineNumber, op.LinePosition);
}
}
void AppendTemplateFrame (XsltException ex)
{
ex.AddTemplateFrame (LocationMessage);
}
public virtual void Evaluate (XslTransformProcessor p, Hashtable withParams)
{
if (XslTransform.TemplateStackFrameError) {
try {
EvaluateCore (p, withParams);
} catch (XsltException ex) {
AppendTemplateFrame (ex);
throw ex;
} catch (Exception ex) {
// Note that this catch causes different
// type of error to be thrown (esp.
// this causes NUnit test regression).
XsltException e = new XsltException ("Error during XSLT processing: ", null, p.CurrentNode);
AppendTemplateFrame (e);
throw e;
}
}
else
EvaluateCore (p, withParams);
}
void EvaluateCore (XslTransformProcessor p, Hashtable withParams)
{
if (XslTransform.TemplateStackFrameOutput != null)
XslTransform.TemplateStackFrameOutput.WriteLine (LocationMessage);
p.PushStack (stackSize);
if (parameters != null) {
if (withParams == null) {
int len = parameters.Count;
for (int i = 0; i < len; i++) {
XslLocalParam param = (XslLocalParam)parameters [i];
param.Evaluate (p);
}
} else {
int len = parameters.Count;
for (int i = 0; i < len; i++) {
XslLocalParam param = (XslLocalParam)parameters [i];
object o = withParams [param.Name];
if (o != null)
param.Override (p, o);
else
param.Evaluate (p);
}
}
}
if (content != null)
content.Evaluate (p);
p.PopStack ();
}
public void Evaluate (XslTransformProcessor p)
{
Evaluate (p, null);
}
}
internal class XslDefaultNodeTemplate : XslTemplate {
QName mode;
static XslDefaultNodeTemplate instance = new XslDefaultNodeTemplate (QName.Empty);
public XslDefaultNodeTemplate (QName mode) : base (null)
{
this.mode = mode;
}
public static XslTemplate Instance {
get { return instance; }
}
public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
{
p.ApplyTemplates (p.CurrentNode.SelectChildren (XPathNodeType.All), mode, null);
}
}
internal class XslEmptyTemplate : XslTemplate {
static XslEmptyTemplate instance = new XslEmptyTemplate ();
XslEmptyTemplate () : base (null) {}
public static XslTemplate Instance {
get { return instance; }
}
public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
{
}
}
internal class XslDefaultTextTemplate: XslTemplate {
static XslDefaultTextTemplate instance = new XslDefaultTextTemplate ();
XslDefaultTextTemplate () : base (null) {}
public static XslTemplate Instance {
get { return instance; }
}
public override void Evaluate (XslTransformProcessor p, Hashtable withParams)
{
if (p.CurrentNode.NodeType == XPathNodeType.Whitespace) {
if (p.PreserveWhitespace ())
p.Out.WriteWhitespace (p.CurrentNode.Value);
}
else
p.Out.WriteString (p.CurrentNode.Value);
}
}
}
|