summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/Source/BuildAssembler/SyntaxComponents/AspNetSyntax.cs
blob: 50d720100c250840f6ae810921ec03af36eac8b6 (plain)
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
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;

namespace Microsoft.Ddue.Tools {


    public class AspNetSyntaxGenerator : SyntaxGenerator {

        public AspNetSyntaxGenerator (XPathNavigator configuration) : base(configuration) {
        }

        protected static XPathExpression nameExpression = XPathExpression.Compile("string(apidata/@name)");
        protected static XPathExpression groupExpression = XPathExpression.Compile("string(apidata/@group)");
        protected static XPathExpression subgroupExpression = XPathExpression.Compile("string(apidata/@subgroup)");

        protected static XPathExpression containingTypeExpression = XPathExpression.Compile("containers/type");
        protected static XPathExpression declaringTypeExpression = XPathExpression.Compile("string(containers/type/@api)");
        protected static XPathExpression propertyTypeExpression = XPathExpression.Compile("string(returns/type/@api)");
        protected static XPathExpression propertyIsSettable = XPathExpression.Compile("boolean(propertydata/@set='true')");
        protected static XPathExpression eventHandlerTypeExpression = XPathExpression.Compile("string(eventhandler/type/@api)");

        protected static XPathExpression typeIsWebControl = XPathExpression.Compile("boolean(family/ancestors/type[@api='T:System.Web.UI.Control'])");

        protected static XPathExpression propertyIsInnerProperty = XPathExpression.Compile("boolean(attributes/attribute[type/@api='T:System.Web.UI.PersistenceModeAttribute' and argument/enumValue/field/@name='InnerProperty'])");

        protected static XPathExpression containingNamespaceExpression = XPathExpression.Compile("string(containers/namespace/@api)");

        private string language = "AspNet";

        public string Language {
            get {
                return (language);
            }
        }

        public override void WriteSyntax (XPathNavigator reflection, SyntaxWriter writer) {

            string group = (string)reflection.Evaluate(groupExpression);
            string subgroup = (string)reflection.Evaluate(subgroupExpression);

            if (group == "type" && subgroup == "class") {
                string prefix = WebControlPrefix(reflection);
                if (!String.IsNullOrEmpty(prefix)) {
                    WriteClassSyntax(reflection, writer, prefix);
                }
            }

            if (group == "member") {

                string prefix = null;
                XPathNavigator containingType = reflection.SelectSingleNode(containingTypeExpression);
                if (containingType != null) prefix = WebControlPrefix(containingType);

                if (!String.IsNullOrEmpty(prefix)) {
                    if (subgroup == "property") {
                        WritePropertySyntax(reflection, writer, prefix);
                    } else if (subgroup == "event") {
                        WriteEventSyntax(reflection, writer, prefix);
                    }
                }
            }


        }

        private string WebControlPrefix (XPathNavigator reflection) {
            if ((bool)reflection.Evaluate(typeIsWebControl)) {
                string name = (string)reflection.Evaluate(nameExpression);
                string space = (string)reflection.Evaluate(containingNamespaceExpression);
                if ((space == "N:System.Web.UI") && ((name == "Page") || (name == "ScriptControl") || (name == "UserControl"))) {
                    return (null);
                } else {
                    if (space == "N:System.Web.UI.MobileControls") {
                        return ("mobile");
                    } else {
                        return ("asp");
                    }
                }
            } else {
                return (null);
            }
        }

        private void WriteClassSyntax (XPathNavigator reflection, SyntaxWriter writer, string prefix) {

            string name = (string)reflection.Evaluate(nameExpression);

            writer.WriteStartBlock(Language);

            writer.WriteString("<");
            writer.WriteString(prefix);
            writer.WriteString(":");
            writer.WriteString(name);
            writer.WriteString(" />");

            writer.WriteEndBlock();

        }

        private void WritePropertySyntax (XPathNavigator reflection, SyntaxWriter writer, string prefix) {

            bool set = (bool) reflection.Evaluate(propertyIsSettable);
            if (!set) return;

            string name = (string) reflection.Evaluate(nameExpression);
            string declaringType = (string) reflection.Evaluate(declaringTypeExpression);
            string propertyType = (string) reflection.Evaluate(propertyTypeExpression);

            bool isInnerProperty = (bool)reflection.Evaluate(propertyIsInnerProperty);

            writer.WriteStartBlock(Language);

            if (isInnerProperty) {

                // inner property logic

                writer.WriteString("<");
                writer.WriteString(prefix);
                writer.WriteString(":");
                writer.WriteReferenceLink(declaringType);
                writer.WriteString(">");

                writer.WriteLine();
                writer.WriteString("\t");

                writer.WriteString("<");
                writer.WriteString(name);
                writer.WriteString(">");
                if (String.IsNullOrEmpty(propertyType)) {
                    writer.WriteParameter("value");
                } else {
                    if (propertyType == "T:System.Boolean") {
                        writer.WriteString("True|False");
                    } else {
                        writer.WriteReferenceLink(propertyType);
                    }
                }
                writer.WriteString("</");
                writer.WriteString(name);
                writer.WriteString(">");

                writer.WriteLine();

                writer.WriteString("</");
                writer.WriteString(prefix);
                writer.WriteString(":");
                writer.WriteReferenceLink(declaringType);
                writer.WriteString(">");

            } else {

                // normal property logic

                writer.WriteString("<");
                writer.WriteString(prefix);
                writer.WriteString(":");
                writer.WriteReferenceLink(declaringType);
                writer.WriteString(" ");
                writer.WriteString(name);
                writer.WriteString("=\"");
                if (String.IsNullOrEmpty(propertyType)) {
                    writer.WriteParameter("value");
                } else {
                    if (propertyType == "T:System.Boolean") {
                        writer.WriteString("True|False");
                    } else {
                        writer.WriteReferenceLink(propertyType);
                    }
                }
                writer.WriteString("\" />");

            }

            writer.WriteEndBlock();

        }

        private void WriteEventSyntax (XPathNavigator reflection, SyntaxWriter writer, string prefix) {

            string name = (string)reflection.Evaluate(nameExpression);
            string declaringType = (string)reflection.Evaluate(declaringTypeExpression);
            string handlerType = (string)reflection.Evaluate(eventHandlerTypeExpression);

            writer.WriteStartBlock(Language);

            writer.WriteString("<");
            writer.WriteString(prefix);
            writer.WriteString(":");
            writer.WriteReferenceLink(declaringType);
            writer.WriteString(" ");
            writer.WriteString("On");
            writer.WriteString(name);
            writer.WriteString("=\"");
            writer.WriteReferenceLink(handlerType);
            writer.WriteString("\" />");

            writer.WriteEndBlock();

        }

    }

}