~/SrcViewer.aspx.cs

C#
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
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CSSFriendly;
using Wilco.SyntaxHighlighting;
using Wilco.Web.SyntaxHighlighting;

public partial class SrcViewer : DynamicAdaptersPage
{
    public string InspectUrl
    {
        get
        {
            string strRet = "";
            TreeNode node = null;

            if (SrcCodeViewerTree.SelectedNode != null)
            {
                strRet = UrlForNode(SrcCodeViewerTree.SelectedNode);
            }
            else if (!String.IsNullOrEmpty(Request.QueryString["inspect"]))
            {
                strRet = UrlForNode(NodeForUrl(Request.QueryString["inspect"]));
            }

            return strRet;
        }
    }

    public string InspectDiskPath
    {
        get { return (!String.IsNullOrEmpty(InspectUrl)) ? Server.MapPath(InspectUrl) : ""; }
    }

    public string Language
    {
        get
        {
            string lang = "XML";
            if (!String.IsNullOrEmpty(InspectDiskPath))
            {
                FileInfo fInfo = new FileInfo(InspectDiskPath);
                if (fInfo != null)
                {
                    string extension = fInfo.Extension.ToLower().Substring(1);
                    foreach (HighlighterBase h in Register.Instance.Highlighters)
                    {
                        if (h.FileExtensions.Contains(extension))
                        {
                            lang = h.Name;
                            break;
                        }
                    }
                }
            }
            return lang;
        }
    }

    public string FileContents
    {
        get
        {
            string str = "";
            if (!String.IsNullOrEmpty(InspectUrl))
            {
                using (StreamReader sr = File.OpenText(InspectDiskPath))
                {
                    String input;
                    while ((input = sr.ReadLine()) != null)
                    {
                        str += input + "\n";
                    }
                    sr.Close();
                }
            }
            return str;
        }
    }

    public bool ShowTree
    {
        get { return (Request.QueryString["notree"] == null) || (Request.QueryString["notree"].IndexOf("true", StringComparison.OrdinalIgnoreCase) == -1); }
    }

    public void Page_Load(Object sender, EventArgs e)
    {
        string ctrl = "";
        if (!ShowTree)
        {
            SourceTreeDS.XPath = "./assets/logicalUnit";
        }
        else if (Request.Cookies["LastVisitedExampleControl"] != null)
        {
            ctrl = Request.Cookies["LastVisitedExampleControl"].Value;
            if (!String.IsNullOrEmpty(ctrl))
            {
                SourceTreeDS.XPath = "./assets/logicalUnit[@id='" + ctrl + "']";
            }
        }

        DataBind();

        FileBanner.InnerText = InspectUrl;
        SrcCodeViewerPanel.Language = Language;
        SrcCodeViewerPanel.Text = FileContents;
        
        ReturnBtn.Attributes["xml:lang"] = "en";
        ReturnBtn.Attributes["onmouseover"] = "this.className='button-hover'";
        ReturnBtn.Attributes["onmouseout"] = "this.className='button'";

        SCVTree.Visible = ShowTree;
    }

    protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
    {
        base.RaisePostBackEvent(sourceControl, eventArgument);

        FileBanner.InnerText = InspectUrl;
        SrcCodeViewerPanel.Language = Language;
        SrcCodeViewerPanel.Text = FileContents;
    }

    protected void ReturnBtnClicked(object sender, EventArgs e)
    {
        if (Request.Cookies["LastVisitedExampleUrl"] != null)
        {
            Response.Redirect(Request.Cookies["LastVisitedExampleUrl"].Value);
        }
    }

    protected string UrlForNode(TreeNode node)
    {
        string strRet = "";
        if (node != null)
        {
            string parentFolderPath = "";
            TreeNode parent = node.Parent;
            if ((parent != null) && (parent.ValuePath.IndexOf("/") > -1))
            {
                parentFolderPath = parent.ValuePath.Substring(parent.ValuePath.IndexOf("/") + 1);
                parentFolderPath += "/";
            }
            strRet += "~/" + parentFolderPath + node.Text;
        }
        return strRet;
    }

    protected TreeNode NodeForUrl(string url)
    {
        return NodeForUrl(url, SrcCodeViewerTree.Nodes);
    }

    protected TreeNode NodeForUrl(string url, TreeNodeCollection nodes)
    {
        string givenPath = Server.MapPath(url);

        TreeNode answer = null;
        foreach (TreeNode node in nodes)
        {
            if (Server.MapPath(UrlForNode(node)).Equals(givenPath, StringComparison.OrdinalIgnoreCase))
            {
                answer = node;
                break;
            }
            if (node.ChildNodes.Count > 0)
            {
                answer = NodeForUrl(url, node.ChildNodes);
            }
            if (answer != null)
            {
                break;
            }
        }
        return answer;
    }
}