Subversion Repositories DevTools

Rev

Rev 2104 | Rev 2108 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace EA_DocGen
{
   /// <summary>
   /// Summary description for EA_DocGenOptions.
   /// </summary>
   public class EA_DocGenOptions
   {
      // Strings used in the notes section of an EA_DocGen element that persists the options.
      public static string optstr_GeneralOptions = "[GeneralOptions]";
      public static string optstr_ElementTypes   = "[ElementTypes]";
      public static string optstr_SuppressElementDescriptionMissingWarnings = "SuppressElementDescriptionMissingWarnings";
      public static string optstr_SuppressUnAllocatedRelationshipWarnings = "SuppressUnAllocatedRelationshipWarnings";
      public static string optstr_SuppressPrivateAttributes = "SuppressPrivateAttributes";
      public static string optstr_SuppressPrivateMethods    = "SuppressPrivateMethods";
      public static string optstr_SuppressPrivateClasses    = "SuppressPrivateClasses";
      public static string optstr_ConsiderAPIElementsOnly   = "ConsiderAPIClassesOnly";
      public static string optstr_ConsiderAPIPackagesOnly   = "ConsiderAPIPackagesOnly";
      public static string optstr_ConsiderAPIDiagramsOnly   = "ConsiderAPIDiagramsOnly";
      public static string optstr_RestrictForLinkedPackagesOnly = "APIPackageRestrictionForLinkedPackagesOnly";


      private static string [] options_string = null;
      private static bool options_found = false;

      // General options

      public static bool opt_SuppressElementDescriptionMissingWarnings;
      public static bool opt_SuppressUnAllocatedRelationshipWarnings;


      // Visibility options
      public static bool opt_SuppressPrivateAttributes;
      public static bool opt_SuppressPrivateMethods;
      public static bool opt_SuppressPrivateClasses;

      public static bool opt_ConsiderAPIElementsOnly;
      public static bool opt_ConsiderAPIPackagesOnly;
      public static bool opt_ConsiderAPIDiagramsOnly;
      public static bool opt_RestrictForLinkedPackagesOnly;


      // Element Types list
      public static ArrayList opt_ElementTypes = null;

      public static void initialise()
      {
         opt_ElementTypes = new ArrayList();
         setDefaults();
      }

      private static void setDefaults()
      {
         opt_SuppressElementDescriptionMissingWarnings = false;
         opt_SuppressUnAllocatedRelationshipWarnings = false;
         opt_SuppressPrivateAttributes = false;
         opt_SuppressPrivateMethods    = false;
         opt_SuppressPrivateClasses    = false;
         opt_ConsiderAPIElementsOnly   = false;
         opt_ConsiderAPIDiagramsOnly   = false;
         opt_ConsiderAPIPackagesOnly   = false;
         opt_RestrictForLinkedPackagesOnly = true;
      }


      /// <summary>
      /// Looks for the EA_DocGen element in the selected package. To generate a document
      /// on a package, the package must contain the EA_DocGen element, even if that elements
      /// notes are empty. It serves to denote a package as a document model that is allowed
      /// to be processed by this add-in.
      /// </summary>
      /// <param name="parentPackage"></param>
      /// <returns></returns>
      public static bool lookForAndProcess_EA_DocGen_Element( EA.Package parentPackage )
      {
         options_found  = false;
         setDefaults();

         // Look for an EA_DocGen element in this package
         foreach(EA.Element theElement in parentPackage.Elements)
         {
            options_found = lookForAndProcess_EA_DocGen_Element(theElement);
            if (options_found)
               break;
         }            
         return options_found;
      }

      public static bool lookForAndProcess_EA_DocGen_Element( EA.Element theElement )
      {
         options_string = null;
         options_found  = false;
         setDefaults();

         // Special handling for the EA_DocGen element designed to control this document
         // generator
         if (0 == theElement.Name.CompareTo(EA_Constants.EA_DocGenBaseName))
         {
            options_found = true;

            // Extract the content of the EA_DocGen element notes section, into a list of
            // strings.
            string delimStr = "\n";
            char [] delim = delimStr.ToCharArray();
            options_string = theElement.Notes.ToString().Split(delim,200);
               
            int i = 0;
            foreach(string s in options_string)
            {
               options_string[i] = s.Trim();
               i++;
            }

            // Extract general options from the list of strings
            extractGeneralOptions();

            // Extract element types list from the list of strings
            extractElementTypes();
         }

         return options_found;
      }


      private static string getOptionValueString(string optionLine)
      {
         string delimStr = "=";
         char [] delim = delimStr.ToCharArray();
         string [] optionLineParts = optionLine.Split(delim,2);
         if (optionLineParts.GetLength(0) == 2)
         {
            return optionLineParts[1];
         }
         else
         {
            MessageBox.Show( "Error in EA_DocGen option\n\n" + optionLine + "\n\n" + "Missing value" );
         }
         return null;
      }


      // Overloaded "get option value" methods
      public static int getOptionValue(string s, int defaultReturnValue)
      {
         int returnVal = defaultReturnValue;

         string optionValue = getOptionValueString(s);
         if (optionValue != null)
         {
            try
            {
               returnVal = Convert.ToInt32(optionValue);
            }
            catch(Exception)
            {
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid integer" );
            }
         }   
         return returnVal;
      }

      public static double getOptionValue(string s, double defaultReturnValue)
      {
         double returnVal = defaultReturnValue;

         string optionValue = getOptionValueString(s);
         if (optionValue != null)
         {
            try
            {
               returnVal = Convert.ToDouble(optionValue);
            }
            catch(Exception)
            {
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid double" );
            }
         }   
         return returnVal;
      }

      public static bool getOptionValue(string s, bool defaultReturnValue)
      {
         bool returnVal = defaultReturnValue;

         string optionValue = getOptionValueString(s);
         if (optionValue != null)
         {
            try
            {
               returnVal = Convert.ToBoolean(optionValue);
            }
            catch(Exception)
            {
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid boolean" );
            }
         }   
         return returnVal;
      }


      public static string getOptionValue(string s, string defaultReturnValue)
      {
         string returnVal = defaultReturnValue;

         string optionValue = getOptionValueString(s);
         if (optionValue != null)
         {
            returnVal = optionValue;
         }   
         return returnVal;
      }


      public static char getOptionValue(string s, char defaultReturnValue)
      {
         char returnVal = defaultReturnValue;

         string optionValue = getOptionValueString(s);
         if (optionValue != null && optionValue.Length > 0)
         {
            returnVal = optionValue[0];
         }   
         return returnVal;
      }
   


      /// <summary>
      /// Method to extract all the general options from the string array obtained from
      /// the EA_DocGen element
      /// </summary>
      private static void extractGeneralOptions()
      {
         bool parsingGenOpts = false;
 
         if (options_string != null)
         {
            // iterate through the string array obtained from the EA_DocGen element
            foreach(string s in options_string)
            {
               if (s.StartsWith(optstr_GeneralOptions))
               {
                  parsingGenOpts = true;
                  continue;
               }
               else if (   (parsingGenOpts == true) 
                        && (s.Length >= 1)
                        && (s.Substring(0,1) == "[") )
               {
                  // we have gone past the general options and reached another section in EA_DocGen
                  // so there is no point in continuing. Exit the loop
                  break;
               }

               // BIG WARNING
               // if any option string is a substring of another option string, then the longest of the
               // two must appear first in this if-then-else sequence.

               if (parsingGenOpts == true)
               {
                  if (s.StartsWith(optstr_SuppressElementDescriptionMissingWarnings))
                  {
                     opt_SuppressElementDescriptionMissingWarnings = getOptionValue(s, opt_SuppressElementDescriptionMissingWarnings);
                  }
                  else if (s.StartsWith(optstr_SuppressUnAllocatedRelationshipWarnings))
                  {
                     opt_SuppressUnAllocatedRelationshipWarnings = getOptionValue(s, opt_SuppressUnAllocatedRelationshipWarnings);
                  }
                  else if (s.StartsWith(optstr_SuppressPrivateClasses))
                  {
                     opt_SuppressPrivateClasses = getOptionValue(s, opt_SuppressPrivateClasses);
                  }
                  else if (s.StartsWith(optstr_SuppressPrivateAttributes))
                  {
                     opt_SuppressPrivateAttributes = getOptionValue(s, opt_SuppressPrivateAttributes);
                  }
                  else if (s.StartsWith(optstr_SuppressPrivateMethods))
                  {
                     opt_SuppressPrivateMethods = getOptionValue(s, opt_SuppressPrivateMethods);
                  }
                  else if (s.StartsWith(optstr_ConsiderAPIElementsOnly))
                  {
                     opt_ConsiderAPIElementsOnly = getOptionValue(s, opt_ConsiderAPIElementsOnly);
                  }
                  else if (s.StartsWith(optstr_ConsiderAPIPackagesOnly))
                  {
                     opt_ConsiderAPIPackagesOnly = getOptionValue(s, opt_ConsiderAPIPackagesOnly);
                  }
                  else if (s.StartsWith(optstr_RestrictForLinkedPackagesOnly))
                  {
                     opt_RestrictForLinkedPackagesOnly = getOptionValue(s, opt_RestrictForLinkedPackagesOnly);
                  }
                  else if (s.StartsWith(optstr_ConsiderAPIDiagramsOnly))
                  {
                     opt_ConsiderAPIDiagramsOnly = getOptionValue(s, opt_ConsiderAPIDiagramsOnly);
                  }
                  // add others here

               }
            }         
         }
      }


      private static void extractElementTypes()
      {
         bool parsingElementTypes = false;

         opt_ElementTypes.Clear();

         if (options_string != null)
         {
            // iterate through the string array obtained from the EA_DocGen element
            foreach(string s in options_string)
            {
               if (s.StartsWith(optstr_ElementTypes))
               {
                  parsingElementTypes = true;
                  continue;
               }
               else if (   (parsingElementTypes == true) 
                        && (s.Length >= 1)
                        && (s.Substring(0,1) == "[") )
               {
                  // we have gone past the element types and reached another section in EA_DocGen
                  // so there is no point in continuing. Exit the loop
                  break;
               }

               if (parsingElementTypes == true)
               {
                  string trimmed_s = s.Trim();
                  if (trimmed_s.Length > 0)
                  {
                     opt_ElementTypes.Add( s );
                  }
               }
            }
         }
      }

 
      /// <summary>
      /// This method searches the EA_DocGen string list to find the element types the user
      /// has specified that must be included in the document. If the element type given to
      /// the function is not in that list then it is excluded from the generated document.
      /// The notes of the EA_DocGen element are formed like a .ini file and contain sections 
      /// and section content along the lines of:
      /// 
      /// [sectionName]
      /// value
      /// value
      /// etc
      /// [anotherSectionName]
      /// value
      /// value
      /// etc
      /// 
      /// The [ElementTypes] section is the one this function is interested in
      /// 
      /// </summary>
      /// <param name="elementName"></param>
      /// <returns></returns>
      public static bool elementTypeFoundInEA_DocGen(string elementType)
      {
         if (opt_ElementTypes != null && opt_ElementTypes.Count > 0)
         {
            foreach(string s in opt_ElementTypes)
            {
               if (s.StartsWith(elementType))
               {
                  return true;
               }
            } 
            // elementType is not in the opt_ElementTypes list, so element is filtered out
            return false;
         }
         else
         {
            // opt_ElementTypes is empty so no filtering, return true 
            return true;
         }
      }

      private static void updateEA_DocGen(ref StringBuilder sb, bool bValue, string s)
      {
         sb.Append( s );
         sb.Append( "=" );
         sb.Append( bValue.ToString() );
         sb.Append( "\r\n" );
      }

      private static void updateEA_DocGen(ref StringBuilder sb, int iValue, string s)
      {
         sb.Append( s );
         sb.Append( "=" );
         sb.Append( iValue.ToString() );
         sb.Append( "\r\n" );
      }

      public static void updateEA_DocGen(EA.Element ele)
      {
         StringBuilder sb = new StringBuilder();

         sb.Append( optstr_GeneralOptions );
         sb.Append( "\r\n" );

         // Missing Description handling options
         updateEA_DocGen(ref sb, opt_SuppressElementDescriptionMissingWarnings, optstr_SuppressElementDescriptionMissingWarnings);
         updateEA_DocGen(ref sb, opt_SuppressUnAllocatedRelationshipWarnings, optstr_SuppressUnAllocatedRelationshipWarnings);

         // visibility options
         updateEA_DocGen(ref sb, opt_SuppressPrivateClasses,    optstr_SuppressPrivateClasses);
         updateEA_DocGen(ref sb, opt_SuppressPrivateAttributes, optstr_SuppressPrivateAttributes);
         updateEA_DocGen(ref sb, opt_SuppressPrivateMethods,    optstr_SuppressPrivateMethods);
         updateEA_DocGen(ref sb, opt_ConsiderAPIElementsOnly,   optstr_ConsiderAPIElementsOnly);
         updateEA_DocGen(ref sb, opt_ConsiderAPIDiagramsOnly,   optstr_ConsiderAPIDiagramsOnly);
         updateEA_DocGen(ref sb, opt_ConsiderAPIPackagesOnly,   optstr_ConsiderAPIPackagesOnly);
         updateEA_DocGen(ref sb, opt_RestrictForLinkedPackagesOnly, optstr_RestrictForLinkedPackagesOnly);


         // Element type filtering options
         if (opt_ElementTypes.Count > 0)
         {
            sb.Append( "\r\n" );
            sb.Append( optstr_ElementTypes );
            sb.Append( "\r\n" );
            foreach (string s in opt_ElementTypes)
            {
               sb.Append( s );
               sb.Append( "\r\n" );
            }
         }

         ele.Notes = sb.ToString();
         ele.Update();

      }

   } // end of class
} // end of namespace