c6476fe60549a3fdc0560158b1c42ebb773902cc
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / CompiledAction.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CompiledAction.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml.Xsl.XsltOld {
9     using Res = System.Xml.Utils.Res;
10     using System;
11     using System.Diagnostics; 
12     using System.Xml;
13     using System.Xml.XPath;
14
15     internal abstract class CompiledAction : Action {
16         internal abstract void Compile(Compiler compiler);
17
18         internal virtual bool CompileAttribute(Compiler compiler) {
19             return false;
20         }
21         
22         public void CompileAttributes(Compiler compiler) {
23             NavigatorInput input   = compiler.Input;
24             string         element = input.LocalName;
25
26             if (input.MoveToFirstAttribute()) {
27                 do {
28                     if (input.NamespaceURI.Length != 0) continue;
29
30                     try {
31                         if (CompileAttribute(compiler) == false) {
32                             throw XsltException.Create(Res.Xslt_InvalidAttribute, input.LocalName, element);
33                         }
34                     }catch {
35                         if (! compiler.ForwardCompatibility) {
36                             throw;
37                         }
38                         else {
39                             // In ForwardCompatibility mode we ignoreing all unknown or incorrect attributes
40                             // If it's mandatory attribute we'l notice it absents later.
41                         }
42                     }
43                 }
44                 while (input.MoveToNextAttribute());
45                 input.ToParent();
46             }
47         }
48
49         // For perf reason we precalculating AVTs at compile time.
50         // If we can do this we set original AVT to null
51         internal static string PrecalculateAvt(ref Avt avt) {
52             string result = null;
53             if(avt != null && avt.IsConstant) {
54                 result = avt.Evaluate(null, null);
55                 avt = null;
56             }
57             return result;
58         }
59
60         public void CheckEmpty(Compiler compiler) {
61             // Really EMPTY means no content at all, but the sake of compatibility with MSXML we allow whitespaces
62             string elementName = compiler.Input.Name;
63             if (compiler.Recurse()) {
64                 do {
65                     // Note: <![CDATA[ ]]> will be reported as XPathNodeType.Text
66                     XPathNodeType nodeType = compiler.Input.NodeType;
67                     if (
68                         nodeType != XPathNodeType.Whitespace            &&
69                         nodeType != XPathNodeType.Comment               &&
70                         nodeType != XPathNodeType.ProcessingInstruction
71                     ) {
72                         throw XsltException.Create(Res.Xslt_NotEmptyContents, elementName);
73                     }
74                 }
75                 while (compiler.Advance());
76                 compiler.ToParent();
77             }
78         }
79
80         public void CheckRequiredAttribute(Compiler compiler, object attrValue, string attrName) {
81             CheckRequiredAttribute(compiler, attrValue != null, attrName);
82         }
83
84         public void CheckRequiredAttribute(Compiler compiler, bool attr, string attrName) {
85             if (! attr) {
86                 throw XsltException.Create(Res.Xslt_MissingAttribute, attrName);
87             }
88         }
89     }
90 }