Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C#

AidaNet : Network resources inventory

Rate me:
Please Sign up or sign in to vote.
4.13/5 (8 votes)
7 Mar 20044 min read 60.9K   1.2K   23  
XPath script to analyze multiple XML files.
// Allclasses.cs : applications class 

using System;
using System.Collections;        // ArrayList, HashTable , ...
using System.Windows.Forms;      

           
namespace AidaNet
{

 
   // -----------------------------------------------------------------------------
  
   [System.Xml.Serialization.XmlRootAttribute("Config", Namespace="", IsNullable=false)]
   public class ConfigType 
   {
      // <FolderPath>..\\..\\sample</FolderPath>
      public string FolderPath;

      // <Alias Path="/Report"> ... </Alias>
      [System.Xml.Serialization.XmlElementAttribute("Alias")]
      public AliasType RootAliases;

      //<Group Name="OS" Title="Operating Systems">
      //   <Alias>OS_NAME</Alias>
      //   <Alias>OS_LANG</Alias>
      //   <Alias>OS_SP</Alias>
      //</Group>
      [System.Xml.Serialization.XmlElementAttribute("Group")]
      public GroupType[] Group;
    
      // <FileGrid>
      //    <Col>PC_NAME</Col>
      //    <Col>USER_NAME</Col>
      //    <Col>RAM</Col>
      //    <Col>CPU_FULL</Col>
      // </FileGrid>
      [System.Xml.Serialization.XmlArrayItemAttribute("Col", IsNullable=false)]
      public string[] FileGrid;
    
      //<DetailGrid>
      //   <Field>PC_NAME</Field>
      //   <Field>USER_NAME</Field>
      //   <Field>RAM</Field>
      //   <Field>CPU_FULL</Field>
      //</DetailGrid>
      [System.Xml.Serialization.XmlArrayItemAttribute("Field", IsNullable=false)]
      public string[] DetailGrid;

      // ----------------------------------------------------------------------------------
      // Fields or methods not part of the XML seralization
      // ----------------------------------------------------------------------------------

      // all Aliases reference (not stored in xml file : XmlIgnore)
      [System.Xml.Serialization.XmlIgnore()]
      public static Hashtable HashAliases = new Hashtable() ;   // Key : Alias name, Value : Alias object
      
      // return the alias corresponding to a name
      public AliasType getAlias (string aliasName)
      {
       return (AliasType) ConfigType.HashAliases[aliasName] ;
      }

      // ----------------------------------------------------------------------------------
      
      // return the group corresponding to a name
      public GroupType getGroup (string GrpName)
      {
         foreach (GroupType grp in Group)
         {
            if (grp.Name == GrpName)
               return grp ;
         }
         return null ;
      }

      // ----------------------------------------------------------------------------------
      
      // return the object in the detail grid corresponding to a name
      public Object getDetailObject (string GrpAliasName)
      {
         if ( Array.IndexOf (DetailGrid, GrpAliasName) < 0)
            return null ;

         Object res ;
         res = getAlias (GrpAliasName) ;
         if (res == null)
            res = getGroup (GrpAliasName) ;
         return res ;           
      }

      // ----------------------------------------------------------------------------------
      
      // return the object in the file grid corresponding to a name
      public Object getFileObject (string GrpAliasName)
      {
         if ( Array.IndexOf (FileGrid, GrpAliasName) < 0)
            return null ;

         Object res ;
         res = getAlias (GrpAliasName) ;
         if (res == null)
            res = getGroup (GrpAliasName) ;
         return res ;           
      }

      // ----------------------------------------------------------------------------------

      // Finalize construction of Alias, group, filegrid and detailgrid  
      public void validate ()
      {
         foreach (GroupType group in Group)
         {
            foreach (string AliasName in group.Alias)
            {
               AliasType alias = getAlias (AliasName) ;

               if (alias == null)
                  throw new Exception ("Error in group " + group.Name + 
                     " : Alias " + AliasName + " don't exist") ;
               
               if (alias.group != null)
                  throw new Exception ("Error in group " + group.Name + 
                     " : Alias " + AliasName + " already in group " + alias.group.Name) ;

               alias.group = group ;
            }   
         }

         foreach (string AliasName in FileGrid)
         {
            Object fileObject = getFileObject (AliasName) ;
            if (fileObject == null) 
               throw new Exception ("Error in FileGrid : Col " + AliasName + " don't exist") ;
              
            if (fileObject is GroupType) 
               throw new Exception ("Error in FileGrid : Col " + AliasName + " is a Group, not an alias") ;
         }

         foreach (string GrpAlias in DetailGrid)
         {
            Object fileObject = getDetailObject (GrpAlias) ;
            if (fileObject == null) 
               throw new Exception ("Error in DetailGrid : Field " + GrpAlias + " don't exist") ;
         }


      }
   }
 
   // -----------------------------------------------------------------------------

   // Alias tree : contain the name , path and all values from files.
   // <Alias Name="OS_LANG" Path="Item[Title='OS Language']/Value" Title="OS Language"/>

   public class AliasType 
   {
      // add get and set to the XML field. that permit to add sub aliases to ConfigType.HashAliases
      [System.Xml.Serialization.XmlElementAttribute("Alias")]
      public AliasType[] subAliases
      {
         get {return (AliasType[]) _subAliases.ToArray(typeof(AliasType)) ; }
         set 
         {
            _subAliases.Clear() ;
            if (value != null)
            {
               _subAliases.AddRange (value) ;
               foreach (AliasType alias in value)
               {
                  // don't add Alias without name
                  if (alias.Name != null && alias.Name.Length != 0)
                     ConfigType.HashAliases.Add (alias.Name, alias) ; // Key : Alias name, Value : Alias object
               }
            }
         }
      }     
      
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Path;             
    
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Name;             
    
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Title;

      // ----------------------------------------------------------------------------------
      // Fields or methods not part of the XML seralization
      // ----------------------------------------------------------------------------------
      
      [System.Xml.Serialization.XmlIgnore()]
      public ArrayList _subAliases = new ArrayList() ;  // AliasType []

      [System.Xml.Serialization.XmlIgnore()]
      public ArrayList valueRefs ;  // ValueRef[] : example : P3_500:{1,2,3} ; P4_800:{4,5}
     
      [System.Xml.Serialization.XmlIgnore()]
      public GroupType group ;          // pointer to the group. Initialized only if needed.

      // ----------------------------------------------------------------------------------
      
      // default constructor for xml serialisation
      public AliasType()
      {    
         valueRefs = new ArrayList();    
      }    

      // ----------------------------------------------------------------------------------
      
      public override string ToString()
      {
         return Title + " (" + valueRefs.Count + ")"  ;
      }
      
      // ----------------------------------------------------------------------------------
      
      public void AddValue (string str, int fileID)
      {
         if (group == null)
         {
            // search if value exist in the current alias
            bool found = false ;
            foreach (ValueRef vref in valueRefs)
            {
               // if found, add file ref to it
               if (vref.ItemValue == str)
               {
                  vref.FileRefs.Add (fileID) ;
                  found = true ;
                  break ;
               }
            }
            // if not found : add new value and the file id
            if (found == false)
            {
               ValueRef vref = new ValueRef () ;
               vref.ItemValue = str ;
               vref.FileRefs.Add (fileID) ;
               valueRefs.Add (vref) ;
            }
         } 
         else 
         {
            group.AddToGrpToConstruct (this, str, fileID) ;
         }
      }
   }

   // -----------------------------------------------------------------------------
   // one alias value and all associated file reference
   public class ValueRef
   {
      public string ItemValue ;
      public ArrayList FileRefs = new ArrayList();  //  int[] : list of file reference (int)
   }

   // -----------------------------------------------------------------------------
   // a group of aliases
   public class GroupType 
   {
      /// <remarks/>
      [System.Xml.Serialization.XmlElementAttribute("Alias")]
      public string[] Alias;
    
      /// <remarks/>
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Name;
    
      /// <remarks/>
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Title;
     
      [System.Xml.Serialization.XmlIgnore()]
      public Hashtable grpToConstruct ;     // Key : Alias Object , Value = string   

      [System.Xml.Serialization.XmlIgnore()]
      public ArrayList groupValues ;        // GroupValue []  All grouped values with file ID

      
      // default constructor (for use with xml deserialize)
      public GroupType () 
      {         
         grpToConstruct = new Hashtable();  
         groupValues    = new ArrayList(); 
      }
         
      public override string ToString()
      {
         return Title + " (" + Alias.Length + ")";          //  groupValues.Count
      }


      // Add an alias to the grpToConstruct list.
      // when the grpToConstruct is complete, this group of value is added to groupValues
      // or fileId is added to an existing groupValues item.
      public void AddToGrpToConstruct (AliasType alias, string str, int FileId)
      {
         // if the alias already exist in the alias list : ignore it. (script bug)
         if (grpToConstruct.ContainsKey (alias))
            return ;

         // detect if the alias to add to grpToConstruct is declared in Aliases (script problem)
         if (Array.IndexOf (this.Alias, alias.Name) < 0)
            return ;

         // add the alias to the grpToConstruct group 
         grpToConstruct.Add (alias , str) ;

         // when all aliases for that group is complete, add it to groupValues
         //if (grpToConstruct.Count == groupAliases.Count)
         if (grpToConstruct.Count == Alias.Length)
         {
            // find a corresponding groupValue with same value
            bool found = false ;
            foreach (GroupValue groupValue in groupValues) // groupValues : ArrayList
            {
               // this groupValue correspond to grpToConstruct, if for all aliases
               // in "grpToConstruct", corresponding "groupValue" values are identical.
               // iteration of the "grpToConstruct" hashtable elements are DictionaryEntry : Key and Value
               found = true ;  // true by default. If Alias vallue don't correspond : found = false
               foreach (DictionaryEntry dict in grpToConstruct)
               {
                  // check if value for the 2 alias are same
                  // TIPS : it's important to cast to string because dict.Value is see as OBJECT
                  // and groupValue.AliasValues[dict.Key] as string.
                  if ((string) groupValue.AliasValues[dict.Key] != (string) dict.Value)  // key is alias
                  {
                     found = false ;
                     break ;  // don't correspond, no more iterate
                  }               
               }
               // all entries in grpToConstruct have a corresponding Alias
               // and with the same values
               if (found == true)
               {
                  // add the fileid to this groupValue
                  groupValue.FileRefs.Add (FileId) ;
                  // stop searching groupValue
                  break ;
               } 
            }

            // no groupValue in groupValues correspond to grpToConstruct
            if (found == false)
            {
               GroupValue groupValue = new GroupValue() ;

               // copy grpToConstruct elements to the new groupValue
               foreach (DictionaryEntry dict in grpToConstruct)
                  groupValue.AliasValues.Add (dict.Key, dict.Value) ;  // Alias, string value
            
               // add the file id (single entry for now)
               groupValue.FileRefs.Add (FileId) ;

               // finally add it to the list
               groupValues.Add (groupValue) ;
            }

            // clear the grpToConstruct list for next additions.
            grpToConstruct.Clear() ;
         }
      }
   }

   // -----------------------------------------------------------------------------
   // a group of alias/value and corresponding files id
   public class GroupValue
   {
      public ArrayList FileRefs ;    // int []
      public Hashtable AliasValues ;  // Key : Alias Object , Value = string 
      public GroupValue ()
      {
         FileRefs    = new ArrayList(); 
         AliasValues = new Hashtable(); 
      }
   }
   
   // -----------------------------------------------------------------------------
   // -----------------------------------------------------------------------------
   // -----------------------------------------------------------------------------

}      

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions