Author: spouliot
Date: 2005-03-11 11:05:07 -0500 (Fri, 11 Mar 2005)
New Revision: 41704

Modified:
   trunk/cecil/permview/ChangeLog
   trunk/cecil/permview/permview.cs
Log:
2005-03-11  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * permview.cs: Added XML output of permissions;



Modified: trunk/cecil/permview/ChangeLog
===================================================================
--- trunk/cecil/permview/ChangeLog      2005-03-11 16:03:25 UTC (rev 41703)
+++ trunk/cecil/permview/ChangeLog      2005-03-11 16:05:07 UTC (rev 41704)
@@ -1,3 +1,7 @@
+2005-03-11  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * permview.cs: Added XML output of permissions;
+
 2005-03-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * ChangeLog: Track changes to permview;

Modified: trunk/cecil/permview/permview.cs
===================================================================
--- trunk/cecil/permview/permview.cs    2005-03-11 16:03:25 UTC (rev 41703)
+++ trunk/cecil/permview/permview.cs    2005-03-11 16:05:07 UTC (rev 41704)
@@ -31,12 +31,14 @@
                        Console.WriteLine ("Usage: permview [options] 
assembly{0}", Environment.NewLine);
                        Console.WriteLine ("where options are:");
                        Console.WriteLine (" -output filename  Output 
information into specified file.");
-                       Console.WriteLine (" -decl             Show declarative 
security attributes on classes and methods.");
+                       Console.WriteLine (" -decl             Show declarative 
security attributes on classes and methods.");
+                       Console.WriteLine (" -xml              Output in XML 
format");
                        Console.WriteLine (" -help             Show help 
informations (this text)");
                        Console.WriteLine ();
                }
 
                static bool declarative = false;
+               static bool xmloutput = false;
 
                static void ShowPermissionSet (TextWriter tw, string header, 
PermissionSet ps)
                {
@@ -67,6 +69,11 @@
                                case "--OUTPUT":
                                        tw = (TextWriter) new StreamWriter 
(args [++i]);
                                        break;
+                               case "/XML":
+                               case "-XML":
+                               case "--XML":
+                                       xmloutput = true;
+                                       break;
                                case "/HELP":
                                case "/H":
                                case "-HELP":
@@ -149,6 +156,73 @@
                        return true;
                }
 
+               static void AddAttribute (SecurityElement se, string attr, 
string value)
+               {
+                       value = value.Replace ("&", "&amp;");
+                       se.AddAttribute (attr, value);
+               }
+
+               static SecurityElement AddSecurityXml 
(ISecurityDeclarationCollection declarations)
+               {
+                       SecurityElement se = new SecurityElement ("Actions");
+                       foreach (ISecurityDeclaration declsec in declarations) {
+                               SecurityElement child = new SecurityElement 
("Action");
+                               AddAttribute (child, "Name", 
declsec.Action.ToString ());
+                               child.AddChild (declsec.PermissionSet.ToXml ());
+                               se.AddChild (child);
+                       }
+                       return se;
+               }
+
+               static bool ProcessAssemblyXml (TextWriter tw, 
IAssemblyDefinition ad)
+               {
+                       SecurityElement se = new SecurityElement 
("DeclarativeSecurityPermissions");
+                       se.AddAttribute ("AssemblyName", ad.Name.FullName);
+
+                       if (ad.SecurityDeclarations.Count > 0) {
+                               SecurityElement assembly = new SecurityElement 
("Assembly");
+                               assembly.AddChild (AddSecurityXml 
(ad.SecurityDeclarations));
+                               se.AddChild (assembly);
+                       }
+
+                       foreach (IModuleDefinition module in ad.Modules) {
+
+                               foreach (ITypeDefinition type in module.Types) {
+
+                                       SecurityElement klass = new 
SecurityElement ("Class");
+                                       SecurityElement methods = new 
SecurityElement ("Methods");
+
+                                       SecurityElement typelem = null;
+                                       if (type.SecurityDeclarations.Count > 
0) {
+                                               typelem = AddSecurityXml 
(type.SecurityDeclarations);
+                                       }
+
+                                       foreach (IMethodDefinition method in 
type.Methods) {
+                                               if 
(method.SecurityDeclarations.Count > 0) {
+                                                       if (typelem == null)
+                                                               typelem = 
AddSecurityXml (type.SecurityDeclarations);
+
+                                                       SecurityElement meth = 
new SecurityElement ("Method");
+                                                       AddAttribute (meth, 
"Name", method.ToString ());
+                                                       meth.AddChild 
(AddSecurityXml (method.SecurityDeclarations));
+                                                       methods.AddChild (meth);
+                                               }
+                                       }
+
+                                       if (typelem != null) {
+                                               AddAttribute (klass, "Name", 
type.ToString ());
+                                               klass.AddChild (typelem);
+                                               if ((methods.Children != null) 
&& (methods.Children.Count > 0))
+                                                       klass.AddChild 
(methods);
+                                               se.AddChild (klass);
+                                       }
+                               }
+                       }
+
+                       tw.WriteLine (se.ToString ());
+                       return true;
+               }
+
                [STAThread]
                static int Main (string[] args) 
                {
@@ -166,9 +240,19 @@
                                string assemblyName = args [args.Length - 1];
                                IAssemblyDefinition ad = 
AssemblyFactory.GetAssembly (assemblyName, LoadingType.Lazy);
                                if (ad != null) {
-                                       bool complete = (declarative ?
-                                               ProcessAssemblyComplete (tw, 
ad) :
-                                               ProcessAssemblyOnly (tw, ad));
+                                       bool complete = false;
+                                       
+                                       if (declarative) {
+                                               // full output 
(assembly+classes+methods)
+                                               complete = 
ProcessAssemblyComplete (tw, ad);
+                                       } else if (xmloutput) {
+                                               // full output in XML (for 
easier diffs after c14n)
+                                               complete = ProcessAssemblyXml 
(tw, ad);
+                                       } else {
+                                               // default (assembly only)
+                                               complete = ProcessAssemblyOnly 
(tw, ad);
+                                       }
+
                                        if (!complete) {
                                                Console.Error.WriteLine 
("Couldn't reflect informations.");
                                                return 1;

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to