Subversion Repositories DevTools

Rev

Rev 2132 | 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]";

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

      // Boolean options
      public enum boolean_options_e
      {
         SUPPRESS_PRIVATE_CLASSES = 0,
         SUPPRESS_PRIVATE_ATTRIBUTES,
         SUPPRESS_PRIVATE_METHODS,
         SUPPRESS_METHOD_CHARACTERISTICS,
         SUPPRESS_CLASS_CHARACTERISTICS,
         SUPPRESS_CLASS_REQUIREMENTS,
         SUPPRESS_CLASS_CONSTRAINTS,
         SUPPRESS_CLASS_BASE_CLASSES,
         SUPPRESS_CLASS_REALISES,
         SUPPRESS_ATTRIBUTE_CHARACTERISTICS,
         SUPPRESS_ELEMENT_DESC_MISSING_WARNINGS,
         SUPPRESS_UN_ALLOCATED_RELATIONSHIP_WARNINGS,
         CONSIDER_API_ELEMENTS_ONLY,
         CONSIDER_API_DIAGRAMS_ONLY,
         CONSIDER_API_PACKAGES_ONLY,
         RESTRICT_FOR_LINKED_PACKAGES_ONLY,
         USE_NUM_PARA_FOR_GENERATED_TEST_CASES,
         SUPPRESS_REQUIREMENT_NOTES,
         FORCE_HEADING_TABS_TO_2_5_CM,
         SUPPRESS_PARAMETER_KIND,
         UNKNOWN = -1
      };

      // local class to collect together stuff for a discrete boolean option
      private class boolopt
      {
         public boolean_options_e enumVal;
         public int level;
         public string optstr;
         public string displayName;
         public bool value;
         public boolopt(int l, boolean_options_e e, string o, string dn, bool v)
         {
            enumVal = e;
            level = l;
            optstr = o;
            displayName = dn;
            value = v;
         }
      };

      // A collection of boolean options
      public static ArrayList boolopts;
      private static int i_boolopts;
      

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

      
      public static void initialise()
      {
         // Create discrete boolean options collection. The ordering of these statements dictates the display order in
         // the options form.
         //
         // WARNING: level number can be 1 or 2, but no higher.
         //
         // BIG WARNING
         // Make sure that if any option string is a substring of another option string, then the longest of the
         // two must appear first in this initialisation sequence

         boolopts = new ArrayList();
         boolopts.Add(new boolopt(1, boolean_options_e.CONSIDER_API_ELEMENTS_ONLY,         
                                  "ConsiderAPIClassesOnly", 
                                  "Consider API Stereotyped Elements Only", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.CONSIDER_API_DIAGRAMS_ONLY,         
                                  "ConsiderAPIDiagramsOnly",                    
                                  "Consider API Stereotyped Diagrams Only", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.CONSIDER_API_PACKAGES_ONLY,         
                                  "ConsiderAPIPackagesOnly",                    
                                  "Consider API Stereotyped Packages Only", 
                                  false) );

         boolopts.Add(new boolopt(2, boolean_options_e.RESTRICT_FOR_LINKED_PACKAGES_ONLY,  
                                  "APIPackageRestrictionForLinkedPackagesOnly", 
                                  "Restriction Applies Only To Linked Packages", 
                                  true) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_ATTRIBUTE_CHARACTERISTICS, 
                                  "SuppressAttributeCharacteristics", 
                                  "Suppress Attribute Characteristics", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_CLASS_BASE_CLASSES,        
                                  "SuppressClassBaseClasses",     
                                  "Suppress Class Base Class List", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_CLASS_CHARACTERISTICS,     
                                  "SuppressClassCharacteristics", 
                                  "Suppress Class Characteristics", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_CLASS_CONSTRAINTS,         
                                  "SuppressClassConstraints",     
                                  "Suppress Class Constraints", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_CLASS_REALISES,            
                                  "SuppressClassRealises",        
                                  "Suppress Class Realisation List", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_CLASS_REQUIREMENTS,        
                                  "SuppressClassRequirements",    
                                  "Suppress Class Requirements", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_ELEMENT_DESC_MISSING_WARNINGS,      
                                  "SuppressElementDescriptionMissingWarnings", 
                                  "Suppress Element Description Missing Warnings", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_METHOD_CHARACTERISTICS,    
                                  "SuppressMethodCharacteristics",    
                                  "Suppress Method Characteristics", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_PRIVATE_ATTRIBUTES,        
                                  "SuppressPrivateAttributes",        
                                  "Suppress Private Attributes", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_PRIVATE_CLASSES,           
                                  "SuppressPrivateClasses",           
                                  "Suppress Private Classes", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_PRIVATE_METHODS,           
                                  "SuppressPrivateMethods",           
                                  "Suppress Private Methods", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_UN_ALLOCATED_RELATIONSHIP_WARNINGS, 
                                  "SuppressUnAllocatedRelationshipWarnings",   
                                  "Suppress Un-Allocated Relationship Warnings", 
                                  false) );

         boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_REQUIREMENT_NOTES,                  
                                  "SuppressRequirementNotes", 
                                  "Suppress Use Of Requirement Element Notes", 
                                  true) );

         boolopts.Add(new boolopt(1, boolean_options_e.USE_NUM_PARA_FOR_GENERATED_TEST_CASES,       
                                  "UseNumParaForGeneratedTestCases", 
                                  "Use NumPara Styles For Generated Test Case Sections", 
                                  true) );

         boolopts.Add(new boolopt(1, boolean_options_e.FORCE_HEADING_TABS_TO_2_5_CM,
                                  "ForceHeadingTabsTo2_5cm",
                                  "Force Heading Style Tabs To 2.5cm",
                                  false) );

          boolopts.Add(new boolopt(1, boolean_options_e.SUPPRESS_PARAMETER_KIND,
                                  "SuppressParameterKind",
                                  "Suppress Parameter Kind",
                                  true));
         // Create element types list
         if (opt_ElementTypes == null)
            opt_ElementTypes = new ArrayList();
         opt_ElementTypes.Clear();
         setDefaults();
      }

     
      /// <summary>
      /// Function to return to another class the value of a specified discrete boolean option
      /// </summary>
      /// <param name="e"></param>
      /// <returns></returns>
      public static bool optionValue(boolean_options_e e)
      {
         foreach (boolopt bo in boolopts)
         {
            if (bo.enumVal == e)
            {
               return bo.value;
            }
         }
         return false;
      }

      /// <summary>
      /// Function to allow another class to set the value of a discrete boolean option.
      /// Normally, this will only be done from the editing form class.
      /// </summary>
      /// <param name="e"></param>
      /// <param name="value"></param>
      public static void optionValue(boolean_options_e e, bool value)
      {
         foreach (boolopt bo in boolopts)
         {
            if (bo.enumVal == e)
            {
               bo.value = value;
               break;
            }
         }
      }

      /// <summary>
      /// Function to return the option string to use for a specified
      /// discrete boolean option. Option strings are used in the actual EA element to record
      /// the state of the option. They are (or may be) similar to the display names for the options
      /// but without any spaces, and they may be shortened somewhat too.
      /// </summary>
      /// <param name="e"></param>
      /// <returns></returns>
      private static string optionString(boolean_options_e e)
      {
         foreach (boolopt bo in boolopts)
         {
            if (bo.enumVal == e)
            {
               return bo.optstr;
            }
         }
         return null;
      }

      /// <summary>
      /// Helper for getFirstBooleanOption() and getNextBooleanOption()
      /// </summary>
      /// <param name="index"></param>
      /// <param name="e"></param>
      /// <param name="level"></param>
      /// <param name="displayName"></param>
      /// <param name="value"></param>
      private static void getBooleanOption(int index, out boolean_options_e e, out int level, out string displayName, out bool value)
      {
         boolopt o = (boolopt)boolopts[index];
         e = o.enumVal;
         level = o.level;
         displayName = o.displayName;
         value = o.value;
      }

      /// <summary>
      /// Function to return the details of the first discrete boolean option.
      /// This will be used by the option editing form class to populate a list for user editing.
      /// </summary>
      /// <param name="e"></param>
      /// <param name="level"></param>
      /// <param name="displayName"></param>
      /// <param name="value"></param>
      /// <returns></returns>
      public static bool getFirstBooleanOption(out boolean_options_e e, out int level, out string displayName, out bool value)
      {
         i_boolopts = 0;

         e = boolean_options_e.UNKNOWN;
         level = 1;
         displayName = "";
         value = false;

         if (i_boolopts < boolopts.Count)
         {
            getBooleanOption(i_boolopts, out e, out level, out displayName, out value);
            return true;
         }
         else
         {
            return false;
         }
      }

      /// <summary>
      /// Function to return the details of the next discrete boolean option.
      /// This will be used by the option editing form class to populate a list for user editing.
      /// </summary>
      /// <param name="e"></param>
      /// <param name="level"></param>
      /// <param name="displayName"></param>
      /// <param name="value"></param>
      /// <returns></returns>
      public static bool getNextBooleanOption(out boolean_options_e e, out int level, out string displayName, out bool value)
      {
         i_boolopts++;

         e = boolean_options_e.UNKNOWN;
         level = 1;
         displayName = "";
         value = false;

         if (i_boolopts < boolopts.Count)
         {
            getBooleanOption(i_boolopts, out e, out level, out displayName, out value);
            return true;
         }
         else
         {
            return false;
         }
      }

      /// <summary>
      /// Set default values for the discrete boolean options.
      /// </summary>
      private static void setDefaults()
      {
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_ELEMENT_DESC_MISSING_WARNINGS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_UN_ALLOCATED_RELATIONSHIP_WARNINGS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_PRIVATE_ATTRIBUTES, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_PRIVATE_METHODS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_PRIVATE_CLASSES, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_METHOD_CHARACTERISTICS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_CLASS_CHARACTERISTICS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_CLASS_REALISES, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_CLASS_REQUIREMENTS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_CLASS_CONSTRAINTS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_CLASS_BASE_CLASSES, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_ATTRIBUTE_CHARACTERISTICS, false);
         optionValue(EA_DocGenOptions.boolean_options_e.CONSIDER_API_ELEMENTS_ONLY, false);
         optionValue(EA_DocGenOptions.boolean_options_e.CONSIDER_API_DIAGRAMS_ONLY, false);
         optionValue(EA_DocGenOptions.boolean_options_e.CONSIDER_API_PACKAGES_ONLY, false);
         optionValue(EA_DocGenOptions.boolean_options_e.RESTRICT_FOR_LINKED_PACKAGES_ONLY, true);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_REQUIREMENT_NOTES, false);
         optionValue(EA_DocGenOptions.boolean_options_e.USE_NUM_PARA_FOR_GENERATED_TEST_CASES, true);
         optionValue(EA_DocGenOptions.boolean_options_e.FORCE_HEADING_TABS_TO_2_5_CM, false);
         optionValue(EA_DocGenOptions.boolean_options_e.SUPPRESS_PARAMETER_KIND, false);
      }


      /// <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 EA.Package lookForAndProcess_EA_DocGen_Element( EA.Package pkg )
      {
         options_found = false;

         // iterate up through the package hiearchy
         while (pkg != null)
         {
            // Look for an EA_DocGen element in this package
            foreach(EA.Element theElement in pkg.Elements)
            {
               options_found = lookForAndProcess_EA_DocGen_Element(theElement);
               if (options_found)
               {
                  return pkg;
               }
            }            

            // Didnt find EA_DocGen element, go up one package level ready for the next loop iteration
            if (pkg.ParentID > 0)
            {
               pkg = Main.EA_Repository.GetPackageByID(pkg.ParentID);
            }
            else
            {
               // force loop termination if cannot go up one more package level
               pkg = null;
            }
         }

         return pkg;
      }

      public static bool lookForAndProcess_EA_DocGen_Element( EA.Element theElement )
      {
         initialise();

         options_string = null;
         options_found  = false;

         // 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.Split(delim,400);
               
            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;
               }

               if (parsingGenOpts == true)
               {
                  foreach(boolopt bo in boolopts)
                  {
                     if (s.StartsWith(bo.optstr))
                     {
                        optionValue(bo.enumVal, getOptionValue(s, bo.value));
                     }
                  }
               }
            }         
         }
      }


      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, EA_DocGenOptions.boolean_options_e e)
      {
         boolopt o = (boolopt)boolopts[(int)e];

         sb.Append( o.optstr );
         sb.Append( "=" );
         sb.Append( o.value.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" );

         // output discrete boolean options
         foreach(boolopt bo in boolopts)
         {
            updateEA_DocGen(ref sb, bo.enumVal);
         }

         // 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