Subversion Repositories DevTools

Rev

Rev 2094 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2088 ghuddy 1
using System;
2
using System.Windows.Forms;
3
using System.IO;
4
using System.Collections;
5
 
6
namespace EA_DocGen
7
{
8
   /// <summary>
9
   /// Summary description for EA_DocGenOptions.
10
   /// </summary>
11
   public class EA_DocGenOptions
12
   {
13
      private string [] options_string = null;
14
      private bool options_found = false;
15
 
16
      // General options
17
      public  int opt_ElementHeadingTransitionLevel;
18
      public bool opt_DisplayRequirementElementsAsSections;
19
      public string opt_RequirementElementDisplayFormat;
20
 
21
      public bool opt_DisplayRequirementsWithStatus;
22
 
23
      // Element Types list
24
      private ArrayList opt_ElementTypes = null;
25
 
26
 
27
      private EA_Utilities EA_Utils = null;
28
 
29
      public EA_DocGenOptions(EA_Utilities EA_UtilsRef)
30
      {
31
         EA_Utils = EA_UtilsRef;
32
         opt_ElementTypes = new ArrayList();
33
         setDefaults();
34
      }
35
 
36
 
37
      private void setDefaults()
38
      {
39
         opt_ElementHeadingTransitionLevel = 0;
40
         opt_DisplayRequirementElementsAsSections = false;
41
         opt_RequirementElementDisplayFormat = "[REQUIREMENT: %s]";
42
         opt_DisplayRequirementsWithStatus = true;
43
      }
44
 
45
 
46
      /// <summary>
47
      /// Looks for the EA_DocGen element in the selected package. To generate a document
48
      /// on a package, the package must contain the EA_DocGen element, even if that elements
49
      /// notes are empty. It serves to denote a package as a document model that is allowed
50
      /// to be processed by this add-in.
51
      /// </summary>
52
      /// <param name="parentPackage"></param>
53
      /// <returns></returns>
54
      public bool lookForAndProcess_EA_DocGen_Element( EA.Package parentPackage )
55
      {
56
         options_string = null;
57
         options_found  = false;
58
         setDefaults();
59
 
60
         // Look for an EA_DocGen element in this package
61
         foreach(EA.Element theElement in parentPackage.Elements)
62
         {
63
            // Special handling for the EA_DocGen element designed to control this document
64
            // generator
65
            if (theElement.Name.ToString() == "EA_DocGen")
66
            {
67
               options_found = true;
68
 
69
               // Extract the content of the EA_DocGen element notes section, into a list of
70
               // strings.
71
               string delimStr = "\n";
72
               char [] delim = delimStr.ToCharArray();
73
               options_string = theElement.Notes.ToString().Split(delim,200);
74
 
75
               int i = 0;
76
               foreach(string s in options_string)
77
               {
78
                  options_string[i] = s.Trim();
79
                  i++;
80
               }
81
 
82
               // Extract general options from the list of strings
83
               extractGeneralOptions();
84
 
85
               // Extract element types list from the list of strings
86
               extractElementTypes();
87
               break;
88
            }
89
         }            
90
 
91
 
92
         return options_found;
93
      }
94
 
95
 
96
      private string getOptionValueString(string optionLine)
97
      {
98
         string delimStr = "=";
99
         char [] delim = delimStr.ToCharArray();
100
         string [] optionLineParts = optionLine.Split(delim,2);
101
         if (optionLineParts.GetLength(0) == 2)
102
         {
103
            return optionLineParts[1];
104
         }
105
         else
106
         {
107
            MessageBox.Show( "Error in EA_DocGen option\n\n" + optionLine + "\n\n" + "Missing value" );
108
         }
109
         return null;
110
      }
111
 
112
 
113
      // Overloaded "get option value" methods
114
      public int getOptionValue(string s, int defaultReturnValue)
115
      {
116
         int returnVal = defaultReturnValue;
117
 
118
         string optionValue = getOptionValueString(s);
119
         if (optionValue != null)
120
         {
121
            try
122
            {
123
               returnVal = Convert.ToInt32(optionValue);
124
            }
125
            catch(Exception)
126
            {
127
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid integer" );
128
            }
129
         }   
130
         return returnVal;
131
      }
132
 
133
 
134
      public bool getOptionValue(string s, bool defaultReturnValue)
135
      {
136
         bool returnVal = defaultReturnValue;
137
 
138
         string optionValue = getOptionValueString(s);
139
         if (optionValue != null)
140
         {
141
            try
142
            {
143
               returnVal = Convert.ToBoolean(optionValue);
144
            }
145
            catch(Exception)
146
            {
147
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid boolean" );
148
            }
149
         }   
150
         return returnVal;
151
      }
152
 
153
 
154
      public string getOptionValue(string s, string defaultReturnValue)
155
      {
156
         string returnVal = defaultReturnValue;
157
 
158
         string optionValue = getOptionValueString(s);
159
         if (optionValue != null)
160
         {
161
            returnVal = optionValue;
162
         }   
163
         return returnVal;
164
      }
165
 
166
 
167
      public char getOptionValue(string s, char defaultReturnValue)
168
      {
169
         char returnVal = defaultReturnValue;
170
 
171
         string optionValue = getOptionValueString(s);
172
         if (optionValue != null && optionValue.Length > 0)
173
         {
174
            returnVal = optionValue[0];
175
         }   
176
         return returnVal;
177
      }
178
 
179
 
180
 
181
      /// <summary>
182
      /// Method to extract all the general options from the string array obtained from
183
      /// the EA_DocGen element
184
      /// </summary>
185
      private void extractGeneralOptions()
186
      {
187
         bool parsingGenOpts = false;
188
 
189
         if (options_string != null)
190
         {
191
            // iterate through the string array obtained from the EA_DocGen element
192
            foreach(string s in options_string)
193
            {
194
               if (s.StartsWith("[GeneralOptions]"))
195
               {
196
                  parsingGenOpts = true;
197
                  continue;
198
               }
199
               else if (   (parsingGenOpts == true) 
200
                        && (s.Length >= 1)
201
                        && (s.Substring(0,1) == "[") )
202
               {
203
                  // we have gone past the general options and reached another section in EA_DocGen
204
                  // so there is no point in continuing. Exit the loop
205
                  break;
206
               }
207
 
208
               if (parsingGenOpts == true)
209
               {
210
                  if (s.StartsWith("ElementHeadingStyleTransitionsToNumParaAtLevel"))
211
                  {
212
                     opt_ElementHeadingTransitionLevel = getOptionValue(s, opt_ElementHeadingTransitionLevel);
213
                  }
214
                  else if (s.StartsWith("DisplayRequirementElementsAsSections"))
215
                  {
216
                     opt_DisplayRequirementElementsAsSections = getOptionValue(s, opt_DisplayRequirementElementsAsSections);
217
                  }
218
                  else if (s.StartsWith("RequirementElementDisplayFormat"))
219
                  {
220
                     opt_RequirementElementDisplayFormat = getOptionValue(s, opt_RequirementElementDisplayFormat);
221
                  }
222
                  else if (s.StartsWith("DisplayRequirementsWithStatus"))
223
                  {
224
                     opt_DisplayRequirementsWithStatus = getOptionValue(s, opt_DisplayRequirementsWithStatus);
225
                  }
226
 
227
 
228
                  // add others here
229
 
230
               }
231
            }         
232
         }
233
      }
234
 
235
 
236
      private void extractElementTypes()
237
      {
238
         bool parsingElementTypes = false;
239
 
240
         opt_ElementTypes.Clear();
241
 
242
         if (options_string != null)
243
         {
244
            // iterate through the string array obtained from the EA_DocGen element
245
            foreach(string s in options_string)
246
            {
247
               if (s.StartsWith("[ElementTypes]"))
248
               {
249
                  parsingElementTypes = true;
250
                  continue;
251
               }
252
               else if (   (parsingElementTypes == true) 
253
                        && (s.Length >= 1)
254
                        && (s.Substring(0,1) == "[") )
255
               {
256
                  // we have gone past the element types and reached another section in EA_DocGen
257
                  // so there is no point in continuing. Exit the loop
258
                  break;
259
               }
260
 
261
               if (parsingElementTypes == true)
262
               {
263
                  string trimmed_s = s.Trim();
264
                  if (trimmed_s.Length > 0)
265
                  {
266
                     opt_ElementTypes.Add( s );
267
                  }
268
               }
269
            }
270
         }
271
      }
272
 
273
 
274
      /// <summary>
275
      /// This method searches the EA_DocGen string list to find the element types the user
276
      /// has specified that must be included in the document. If the element type given to
277
      /// the function is not in that list then it is excluded from the generated document.
278
      /// The notes of the EA_DocGen element are formed like a .ini file and contain sections 
279
      /// and section content along the lines of:
280
      /// 
281
      /// [sectionName]
282
      /// value
283
      /// value
284
      /// etc
285
      /// [anotherSectionName]
286
      /// value
287
      /// value
288
      /// etc
289
      /// 
290
      /// The [ElementTypes] section is the one this function is interested in
291
      /// 
292
      /// </summary>
293
      /// <param name="elementName"></param>
294
      /// <returns></returns>
295
      public bool elementTypeFoundInEA_DocGen(string elementType)
296
      {
297
         if (opt_ElementTypes != null && opt_ElementTypes.Count > 0)
298
         {
299
            foreach(string s in opt_ElementTypes)
300
            {
301
               if (s.StartsWith(elementType))
302
               {
303
                  return true;
304
               }
305
            } 
306
            // elementType is not in the opt_ElementTypes list, so element is filtered out
307
            return false;
308
         }
309
         else
310
         {
311
            // opt_ElementTypes is empty so no filtering, return true 
312
            return true;
313
         }
314
      }
315
 
316
 
317
   } // end of class
318
} // end of namespace