Subversion Repositories DevTools

Rev

Rev 2094 | 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;

namespace EA_DocGen
{
   /// <summary>
   /// Summary description for EA_DocGenOptions.
   /// </summary>
   public class EA_DocGenOptions
   {
      private string [] options_string = null;
      private bool options_found = false;

      // General options
      public  int opt_ElementHeadingTransitionLevel;
      public bool opt_DisplayRequirementElementsAsSections;
      public string opt_RequirementElementDisplayFormat;

      public bool opt_DisplayRequirementsWithStatus;

      // Element Types list
      private ArrayList opt_ElementTypes = null;


      private EA_Utilities EA_Utils = null;

      public EA_DocGenOptions(EA_Utilities EA_UtilsRef)
      {
         EA_Utils = EA_UtilsRef;
         opt_ElementTypes = new ArrayList();
         setDefaults();
      }


      private void setDefaults()
      {
         opt_ElementHeadingTransitionLevel = 0;
         opt_DisplayRequirementElementsAsSections = false;
         opt_RequirementElementDisplayFormat = "[REQUIREMENT: %s]";
         opt_DisplayRequirementsWithStatus = 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 bool lookForAndProcess_EA_DocGen_Element( EA.Package parentPackage )
      {
         options_string = null;
         options_found  = false;
         setDefaults();

         // Look for an EA_DocGen element in this package
         foreach(EA.Element theElement in parentPackage.Elements)
         {
            // Special handling for the EA_DocGen element designed to control this document
            // generator
            if (theElement.Name.ToString() == "EA_DocGen")
            {
               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();
               break;
            }
         }            
         

         return options_found;
      }


      private 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 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 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 string getOptionValue(string s, string defaultReturnValue)
      {
         string returnVal = defaultReturnValue;

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


      public 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 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("[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)
               {
                  if (s.StartsWith("ElementHeadingStyleTransitionsToNumParaAtLevel"))
                  {
                     opt_ElementHeadingTransitionLevel = getOptionValue(s, opt_ElementHeadingTransitionLevel);
                  }
                  else if (s.StartsWith("DisplayRequirementElementsAsSections"))
                  {
                     opt_DisplayRequirementElementsAsSections = getOptionValue(s, opt_DisplayRequirementElementsAsSections);
                  }
                  else if (s.StartsWith("RequirementElementDisplayFormat"))
                  {
                     opt_RequirementElementDisplayFormat = getOptionValue(s, opt_RequirementElementDisplayFormat);
                  }
                  else if (s.StartsWith("DisplayRequirementsWithStatus"))
                  {
                     opt_DisplayRequirementsWithStatus = getOptionValue(s, opt_DisplayRequirementsWithStatus);
                  }


                  // add others here

               }
            }         
         }
      }


      private 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("[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 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;
         }
      }


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