home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / ClsView / TOC.aspx < prev    next >
Encoding:
Text File  |  2000-06-23  |  13.3 KB  |  468 lines

  1. <%
  2. /*=====================================================================
  3.   File:      TOC.aspx
  4.  
  5.   Summary:   Brief summary of the file contents and purpose.
  6.  
  7. ---------------------------------------------------------------------
  8.   This file is part of the Microsoft NGWS SDK Code Samples.
  9.  
  10.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  11.  
  12. This source code is intended only as a supplement to Microsoft
  13. Development Tools and/or on-line documentation.  See these other
  14. materials for detailed information regarding Microsoft code samples.
  15.  
  16. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  17. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  19. PARTICULAR PURPOSE.
  20. =====================================================================*/
  21.  
  22. %>
  23.  
  24. <%@ Page Language="C#" %>
  25.  
  26. <%@ Import Namespace="System" %>
  27. <%@ Import Namespace="System.Reflection"%>
  28. <%@ Import Namespace="System.Collections"%>
  29. <%@ Import Namespace="System.IO"%>
  30. <%@ Import Namespace="System.Data"%>
  31. <%@ Import Namespace="System.Data.XML"%>
  32. <%@ Import Namespace="System.Data.XML.DOM"%>
  33.  
  34.  
  35. <HTML>
  36.    <HEAD>
  37.       <TITLE>Contents</TITLE>
  38.         <STYLE TYPE="text/css">
  39.  
  40.          UL.toc {cursor:hand}
  41.  
  42.          /* Set image for the bulleted list. */
  43.          
  44.          UL.toc UL {list-style:none}
  45.  
  46.          /* Hide the child elements by default. */
  47.          UL.toc UL {display:none} 
  48.          
  49.             /* Display the child elements. */
  50.          UL.toc UL.expanded {display:block}
  51.  
  52.         
  53.       </STYLE>
  54.       <style type="text/css">
  55.          @import url(backTOC1.css);
  56.         </style>
  57.  
  58.  
  59.       <BASE TARGET="DEMO">
  60.       <SCRIPT LANGUAGE="JavaScript">
  61.            
  62.          var curSelection = null;
  63.  
  64.          function setStyle(src, toClass) {
  65.             // Format the element to the specified class.
  66.             if (null != src) 
  67.                src.className = toClass;
  68.          }
  69.  
  70.          function mouseEnters() {  
  71.             // Be sure the element is not the current selection and
  72.             // that it is an anchor.
  73.             if ((curSelection != event.toElement) &&
  74.                   ("A" == event.toElement.tagName))
  75.                setStyle(event.toElement,"over");
  76.          }
  77.  
  78.          function mouseLeaves() {
  79.             // Again, be sure the element is not the current selection
  80.             // and that it is an anchor.
  81.             if ((curSelection != event.fromElement) &&
  82.                   ("A" == event.fromElement.tagName))
  83.                setStyle(event.fromElement, "");
  84.          }
  85.  
  86.          function outliner() {
  87.             var child = null, el = null;
  88.             /* Assumes that the DIV containing the child
  89.                elements immediately follows the heading anchor. */
  90.             switch (event.srcElement.tagName) {
  91.             case "H6": 
  92.                el = event.srcElement.parentElement;
  93.                child = document.all[event.srcElement.sourceIndex + 1];
  94.                break;
  95.             case "LI":
  96.                el = event.srcElement;
  97.                child = document.all[event.srcElement.sourceIndex + 2];
  98.                break;
  99.             }
  100.             /* Be sure the child element exists and that it is the
  101.                child LI. */
  102.             if ((null != child) && ("UL" == child.tagName) &&
  103.                   ("LI" == child.parentElement.tagName)) {
  104.                if ("" == child.className) {
  105.                   // Collapse the item.
  106.                   child.className = "expanded";
  107.                   el.className = "open";
  108.                }
  109.                else { 
  110.                   // Expand the item.
  111.                   child.className = "";
  112.                   el.className = "closed";
  113.                }
  114.             }
  115.  
  116.  
  117.             if ("A" == event.srcElement.tagName) {
  118.                if (null != curSelection)
  119.                   setStyle(curSelection, "");
  120.                // Save and highlight new selection.
  121.                curSelection = event.srcElement;
  122.                setStyle(curSelection, "select");
  123.             }
  124.          }
  125.       </SCRIPT>     
  126.  
  127.  
  128. <script runat=server>
  129.  
  130. private Hashtable Hash;
  131. private String[] Namespaces;
  132. private int Pivot;
  133. private ArrayList Assemblies = null;
  134.  
  135. void Page_Load(Object sender, EventArgs e){
  136.  
  137.     if (Request.Form.Count > 0){
  138.         if (Request.Form["Remove"] != null){
  139.             Assemblies = ((ArrayList)Session["Assems"]);
  140.             
  141.             // if Remove, then parse the line to break it up into a string[].
  142.             string unparsedassems = Request.Form["Remove"];
  143.             Char[] separators = {','};
  144.             string[] ParsedAssems = unparsedassems.Split(separators);
  145.  
  146.             for (int index = 0; index < ParsedAssems.Length; ++index){
  147.                 if (Assemblies.IndexOf(ParsedAssems[index]) != -1)
  148.                     Assemblies.Remove(ParsedAssems[index]);
  149.             }
  150.  
  151.             Session["Assems"] = Assemblies;
  152.         }
  153.         else if (Request.Form["Add"] != null){
  154.             LoadModule(Request.Form["Add"]);
  155.         }
  156.     }
  157.     else if (Request.QueryString.Count > 0 && Request.QueryString["Add"] != null){
  158.         string File = Request.QueryString["Add"];
  159.         LoadModule(Directory.Combine(Request.QueryString["CurrentDir"], Request.QueryString["Add"]));
  160.         Response.Write("Command is to Add: <BR/>   \"" + Directory.Combine(Request.QueryString["CurrentDir"], Request.QueryString["Add"]) + "\"<BR>");
  161.     
  162.     }
  163.     else{
  164.         // Write the function that loads the assemblies into Session State.
  165.         // Then modify below to load assemblies from state, rather than from strings.
  166.         // Then when you modify state, the toc will always reload the newly modified 
  167.         // state.
  168.         LoadInitialState(Session);
  169.             
  170.     }
  171.  
  172.     System.Collections.Hashtable hash = PopulateHash();
  173.     hash = Alphabetize(hash);
  174.  
  175.     string[] tempkeys = new string[hash.Keys.Count];
  176.     hash.Keys.CopyTo(tempkeys, 0);
  177.     Namespaces = (string[])tempkeys;    
  178.  
  179.     Array.Sort(Namespaces);
  180.     Hash = hash;
  181. }
  182.  
  183. /* 
  184.  
  185.     LoadModule attempts to load an assembly and if it can, adds the load string to 
  186.     the session TOC state. If it's unsuccessful, it writes an error and returns.
  187.     First it attempts to load using the ModulePath as a known assembly name. If that
  188.     fails, it attempts to load using the ModulePath as a fully qualified path name.
  189.  
  190.  
  191. */
  192.  
  193. private void LoadModule(string ModulePath){
  194.     Assembly NewAssembly = null;
  195.     try{
  196.         // if you just happen to know the Assembly name and it's discoverable.
  197.         if ((NewAssembly = Assembly.Load(ModulePath)) == null){
  198.             NewAssembly = Assembly.LoadFrom(ModulePath);
  199.             if (NewAssembly == null){
  200.                 Page.Response.Write(ModulePath + " does not resolve to a NGWS SDK assembly.<BR>");
  201.                 // so it does nothing if it couldn't find your thingie. It should write an error.
  202.                 return;
  203.             }
  204.         }
  205.         // if you got here, NewAssembly did resolve to a NGWS SDK assembly. 
  206.         Assemblies = ((ArrayList)Session["Assems"]);
  207.         Assemblies.Add(ModulePath);    
  208.         Session["Assems"] = Assemblies;
  209.     }
  210.     catch(Exception Ex){
  211.         ; 
  212.     }
  213.  
  214. }
  215.  
  216.  
  217. private    void LoadInitialState(HttpSessionState AssemblyState){
  218.  
  219.         // Load the session state with all the assemblies.
  220.  
  221.         Assemblies = new System.Collections.ArrayList();
  222.  
  223.         // Create your XmlReader for the file
  224.         string StaticAssembliesPath = Request.PhysicalApplicationPath + "Assemblies.xml";
  225.         
  226.         XmlReader XReader = new XmlReader(StaticAssembliesPath);
  227.         
  228.         XmlDocument XMLDoc = new XmlDocument();
  229.  
  230.         // XmlDocuments require an XmlReader to load if you don't have a DataSet.
  231.         // load the XmlReader for this file
  232.         XMLDoc.Load(XReader);
  233.  
  234.         // Get an XML DOM-based document -- party from here in the standard way.
  235.         // if you're not going to use XmlReader and XmlWriter and DataNavigator.
  236.  
  237.         IDocument IDoc = XMLDoc.DomDocument;
  238.         IElement docElement = IDoc.documentElement;
  239.         INodeList Nodes = docElement.getElementsByTagName("Assembly");
  240.         INode ThisNode = null;
  241.         while((ThisNode = Nodes.nextNode) != null){
  242.             Assemblies.Add(ThisNode.text);
  243.             //this.Response.Write("Added: " + ThisNode.text + "<BR>");
  244.         }
  245.         AssemblyState["Assems"] = Assemblies;
  246. }
  247.  
  248.  
  249. private System.Collections.Hashtable PopulateHash(){
  250.     
  251.     System.Collections.Hashtable hash = new System.Collections.Hashtable((int)Math.Pow((double)2,(double)5));
  252.     Assemblies = (ArrayList)(Session["Assems"]);
  253.     if (Assemblies == null){
  254.         throw new NullReferenceException("Unable to retrieve the AssemState[\"Assems\"] state variable.");
  255.     }
  256.     Assembly ThisAssembly = null;
  257.     for (int i = 0; i < Assemblies.Count; ++i) {
  258.         try{
  259.             ThisAssembly = Assembly.Load((string)Assemblies[i]);
  260.         }
  261.         catch(Exception Ex){
  262.             throw new Exception("Error trying to load assembly \"" + (string)Assemblies[i], Ex);
  263.             continue;
  264.         }
  265.  
  266.         Module[] ModuleArray;
  267.         if (ThisAssembly == null){
  268.             Response.Write(((string)Assemblies[i]) + " was null.<BR>");
  269.             continue;
  270.         }
  271.             
  272.         ModuleArray = ThisAssembly.GetModules();
  273.             
  274.             for (int k = 0; k < ModuleArray.Length; ++k){
  275.                 try {
  276.                     if (ModuleArray == null || ModuleArray[k] == null) {
  277.                         continue;
  278.                     }
  279.                     else {
  280.                         Type[] types = ModuleArray[k].GetTypes();
  281.  
  282.                         for(int j = 0; j < types.Length; j++) {
  283.                             try{
  284.                                 if (types[j] == null || !types[j].IsPublic) continue;
  285.                                 String key = types[j].Namespace;        
  286.                                 if (key == null) key = String.Empty;
  287.                                 
  288.                                 if (hash.ContainsKey(key)) {
  289.                                     hash[key] = String.Format("{0}{1};", (string)hash[key], types[j].Name);
  290.                                 }
  291.                                 else {
  292.                                     hash.Add(key, String.Format("{0};", types[j].Name));                        
  293.                                 }
  294.                             }
  295.                             catch(ReflectionTypeLoadException TypeEx){
  296.                                 Response.Write("Problem with : " + types[j].Name);
  297.                                 continue;
  298.                             }
  299.                         }
  300.                     }
  301.                 }
  302.                 catch(Exception Ex)
  303.                 {
  304.                     Response.Write("<p>Unable to load a module from " + ((string)Assemblies[i]) + ". The exception was " + Ex.GetType().Name + " at " + Ex.TargetSite.Name + ".</p>");
  305.                     Exception Inner = Ex.InnerException;
  306.                     while(Inner != null){
  307.                         Response.Write("Inner: " + Inner.GetType().Name);
  308.                         Inner = Inner.InnerException;
  309.                     }
  310.                     continue;
  311.                 }
  312.             }    
  313.         }
  314.     return hash;
  315.  
  316. }  //end PopulateHash
  317.  
  318. private System.Collections.Hashtable Alphabetize(System.Collections.Hashtable hash)
  319. {
  320.  
  321.     string[] tempkeys = new string[hash.Keys.Count];
  322.     hash.Keys.CopyTo(tempkeys, 0);
  323.     string[] keys = (string[])tempkeys;    
  324.  
  325.     char[] separator = new char[1];
  326.     separator[0] = ';';
  327.     
  328.     for (int c = 0; c < keys.Length; c++)
  329.     {
  330.         String key = keys[c];
  331.         String value = (String)hash[key];
  332.         String[] temp = value.Split(separator);
  333.         Array.Sort(temp);
  334.         value = String.Join(";", temp, 1, temp.Length - 1);
  335.         value = String.Format("{0};", value);
  336.         hash[key] = value;
  337.     }
  338.  
  339.  
  340.  
  341.     return hash;
  342. } //end Alphabetize(System.Collections.Hashtable hash)
  343.  
  344.  
  345. private int[] GetOrder()
  346. {
  347.  
  348.     int index1 = 0;
  349.     for (int i = 0; i < Namespaces.Length; i++)
  350.     {
  351.         if (Namespaces[i].Equals("System")) 
  352.         {
  353.             index1 = i;
  354.             break;
  355.         }
  356.     }
  357.  
  358.     int index2 = Namespaces.Length - 1;
  359.  
  360.     for(int i = Namespaces.Length - 1; i > -1; i--)
  361.     {
  362.         if (Namespaces[i].IndexOf("S") == 0) 
  363.         {
  364.             index2 = i;
  365.             break;
  366.         }
  367.     }
  368.  
  369.     int[] result = new int[Namespaces.Length];
  370.     for(int i = 0; i < index1; i++)
  371.     {
  372.         result[index2 - index1 + 1 + i] = i;
  373.     }
  374.  
  375.  
  376.     for(int i = index1; i < index2 + 1; i++)
  377.     {
  378.         result[i - index1] = i;
  379.     }
  380.  
  381.     for(int i = index2 + 1; i < result.Length; i++)
  382.     {
  383.         result[i] = i;
  384.     }
  385.  
  386.     Pivot = index2 - index1 + 1;
  387.     return result;
  388. }
  389.  
  390. </script>
  391.  
  392.  
  393. </HEAD>
  394.  
  395. <!-- *********************************************************************** -->
  396. <!-- *********************************************************************** -->
  397. <!-- *********************************************************************** -->
  398. <!-- *********************************************************************** -->
  399. <!-- *********************************************************************** -->
  400.  
  401. <BODY>
  402. <!--<SPAN 
  403.     Title="Click to reload all the original assemblies at once."
  404.     id=Reset 
  405.     onclick="javascript:window.navigate(toc.aspx);" 
  406.     onmouseout="Reset.style.color='blue'" 
  407.     onmouseover="Reset.style.color='red'" 
  408.     style="CURSOR: hand; color:blue">
  409.     [Reset all assemblies] ain't working.
  410. </SPAN>
  411. -->
  412. <BR>
  413. <% 
  414.  
  415. int[] order = GetOrder();
  416.  
  417. for (int k = 0; k < order.Length; k++)
  418. {
  419.     if (k == 0 || k == Pivot)
  420.     {
  421.  
  422.         Response.Write("<UL CLASS=\"toc\" ONCLICK=\"outliner();\"\r\n");
  423.         Response.Write("ONSELECTSTART=\"return false;\"\r\n"); 
  424.         Response.Write("ONMOUSEOVER=\"mouseEnters();\"\r\n");
  425.         Response.Write("ONMOUSEOUT=\"mouseLeaves();\">\r\n");
  426.         if(k == Pivot)
  427.         {
  428.             Response.Write("<p> </p>");
  429.         }
  430.  
  431.     }
  432.         
  433.     Response.Write("<LI><H6>");
  434.     if (order[k] == 0 && Namespaces[order[k]].Length == 0) 
  435.         Response.Write("Empty Namespace");
  436.     else 
  437.         Response.Write(Namespaces[order[k]]);
  438.     Response.Write("</H6>\r\n<UL>\r\n");
  439.     
  440.     char[] separator = new char[1];
  441.     separator[0] = ';';
  442.     String [] classes = ((String)Hash[Namespaces[order[k]]]).Split(separator);
  443.     // Last element in classes is String.Empty, so we loop until Length - 1. 
  444.     for(int j = 0; j < classes.Length - 1; j++)
  445.     {
  446.         Response.Write("<LI>");
  447.         
  448.         String link = String.Format("<A HREF=\"browser.aspx?class={0}.{1}\"" +
  449.                                     " Title={0}.{1}" +
  450.                                     " target=\"main\">{1}</A></LI>\r\n", Namespaces[order[k]], classes[j]);
  451.         Response.Write(link);        
  452.  
  453.     }
  454.     Response.Write("</UL>\r\n");
  455.  
  456.     if (k == Pivot - 1 || k == order.Length - 1)
  457.     {
  458.         Response.Write("</UL>\r\n");
  459.     }
  460.  
  461. }
  462. %>
  463.  
  464.  
  465. </BODY>
  466. </HTML>
  467.  
  468.