Subversion Repositories DevTools

Rev

Rev 2130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2126 ghuddy 1
using System;
2
using System.Collections;
3
using System.Text;
4
using Word;
2130 ghuddy 5
using Microsoft.Office.Core;
2126 ghuddy 6
 
2130 ghuddy 7
 
2126 ghuddy 8
// TODO
9
//
2130 ghuddy 10
// 1) Possible let user and a tag simply with </>
2126 ghuddy 11
 
12
 
13
 
14
namespace EA_DocGen
15
{
16
	/// <summary>
17
	/// Summary description for TextParser.
18
	/// </summary>
2130 ghuddy 19
   public class TextParser
20
   {
2126 ghuddy 21
      // An embedded formatting tag can be one of two types
22
      // * An EA_DocGen special format
23
      // * An MS-Word Style based format
24
      private enum style_type_e
25
      {
2130 ghuddy 26
         STYLE_EA_DOCGEN = 0,    // style is an EA_DocGen specific style or pattern 
27
         STYLE_MS_WORD,          // style is an MS-WORD style of character/font attribute
2126 ghuddy 28
         STYLE_UNKNOWN = -1
29
      };
2130 ghuddy 30
 
31
      private enum style_handling_e
32
      {
33
         STYLE_POST_FORMAT,      // text will require formatting AFTER the entire string has been serialised.
34
         STYLE_USE_TAG_STYLE,    // text will require the tag-implied style to be applied to it.
35
         STYLE_USE_CALLER_STYLE  // text will require the callers specified style to be applied to it.
36
      };
37
 
38
      // IDs for all of the styles we will process.
39
      // Note that the arrangement of enum values in the following type, has been made to allow
40
      // for some simple comparison tests during parsing, so do not re-arrange these enums without
41
      // addressing the necessary code changes in the parsing function.
42
      private enum style_id_e
43
      {
44
         // EA_DocGen styles (0..99)
45
         STYLE_ID_TABLE = 0,
46
         // add more EA_DocGen styles here when we need to
47
 
48
         // MS-WORD styles (100..)
49
         STYLE_ID_BODY1 = 100,
50
         STYLE_ID_BOLD,
51
         STYLE_ID_ITALIC,
52
         STYLE_ID_UNDERLINE,
53
         STYLE_ID_NORMAL,
54
         STYLE_ID_WARNING,
55
         STYLE_ID_NOTE,
56
         STYLE_ID_CODETEXT,
57
         // add more MS-WORD styles here when we need to
58
 
59
         // ALL bullet/list styles must be >= 200 for easy detection during post-formatting
60
         STYLE_ID_LIST_BULLET_0 = 200,
61
         STYLE_ID_LIST_BULLET_1,
62
         STYLE_ID_LIST_BULLET_2,
63
         STYLE_ID_LIST_BULLET_3,
64
         STYLE_ID_LIST_BULLET_4,
65
         STYLE_ID_LIST_INDENT_0,
66
         STYLE_ID_LIST_INDENT_1,
67
         STYLE_ID_LIST_INDENT_2,
68
         STYLE_ID_LIST_INDENT_3,
69
         STYLE_ID_LIST_INDENT_4,
70
         // Below here go lists that display numbering or lettering and so "restart numbering"
71
         // attributes are important
72
         STYLE_ID_LIST_NUMBER_0,
73
         STYLE_ID_LIST_NUMBER_1,
74
         STYLE_ID_LIST_NUMBER_2,
75
         STYLE_ID_LIST_NUMBER_3,
76
         STYLE_ID_LIST_NUMBER_4,
77
         STYLE_ID_LIST_ALPHA_0,
78
         STYLE_ID_LIST_ALPHA_1,
79
         STYLE_ID_LIST_ALPHA_2,
80
         STYLE_ID_LIST_ALPHA_3,
81
         STYLE_ID_LIST_ALPHA_4
82
         // do not put anything below here
83
      };
84
 
2126 ghuddy 85
      // A token is a block of text associated with a tag (style) name and type
86
      private struct token_type
87
      {
2130 ghuddy 88
         public string txt;                     // This is the text content enclosed by the tag
89
         public style_type_e styleType;         // The type MS-WORD or EA_DocGen
90
         public string styleName;               // The name of the style
91
         public style_id_e styleId;             // The id of the style
92
         public int level;                      // level (for list items only)
93
         public style_handling_e styleHandling; // Handling attribute for the style
2126 ghuddy 94
      };
95
 
2130 ghuddy 96
      // Some formatting has to be applied after ALL the text of a description has been
2126 ghuddy 97
      // appended to the word document. For this formatting, we need to remember the word
2130 ghuddy 98
      // ranges that denote the text and the style name to apply. So we do it in a list of
99
      // the following item.
2126 ghuddy 100
      private struct postFormat_type
101
      {
102
         public postFormat_type(Word.Range wr, token_type tk)
103
         {
104
            m_wr = wr;
105
            m_tk = tk;
106
         }
107
         public Word.Range m_wr;
108
         public token_type m_tk;
109
      };
110
 
111
      // Use a hash table for recording the allowed tags and their attributes, facilitating rapid
112
      // lookup during parsing.
113
      private static Hashtable styleDefs = null;
114
 
115
 
116
      /// <summary>
117
      /// Class initialisation function
118
      /// </summary>
119
      public static void initialise()
120
      {
121
         // initialsie the hash table
122
         styleDefs = new Hashtable();
123
 
124
         // This list dictates what tags a user can use in the notes text of an EA element. When adding new items
2130 ghuddy 125
         // to this list, we may have to update the parsing function obviously.
2126 ghuddy 126
 
2130 ghuddy 127
         // The hash key is the short tag name that end-users will use in their descriptions. Tags found in
2126 ghuddy 128
         // user text is matched to these keys, and the style definition if found can then be used.
129
 
130
         // EA_DocGen tags
2130 ghuddy 131
         styleDefs.Add( EA_Constants.EA_DocGenTable, formStyleDef( style_type_e.STYLE_EA_DOCGEN, style_id_e.STYLE_ID_TABLE, EA_Constants.EA_DocGenTable, 0, style_handling_e.STYLE_USE_TAG_STYLE ) );
2126 ghuddy 132
 
133
         // MS-Word formatting tags
2130 ghuddy 134
         styleDefs.Add( "b", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_BOLD, EA_Constants.styleName_Bold      , 0, style_handling_e.STYLE_POST_FORMAT) );
135
         styleDefs.Add( "i", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_ITALIC, EA_Constants.styleName_Italic    , 0, style_handling_e.STYLE_POST_FORMAT) );
136
         styleDefs.Add( "u", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_UNDERLINE, EA_Constants.styleName_Underline , 0, style_handling_e.STYLE_POST_FORMAT) );
2126 ghuddy 137
 
2130 ghuddy 138
         styleDefs.Add( "lb0", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_BULLET_0, EA_Constants.styleName_ListBullet0,1, style_handling_e.STYLE_POST_FORMAT ) );
139
         styleDefs.Add( "lb1", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_BULLET_1, EA_Constants.styleName_ListBullet1,2, style_handling_e.STYLE_POST_FORMAT ) );
140
         styleDefs.Add( "lb2", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_BULLET_2, EA_Constants.styleName_ListBullet2,3, style_handling_e.STYLE_POST_FORMAT ) );
141
         styleDefs.Add( "lb3", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_BULLET_3, EA_Constants.styleName_ListBullet3,4, style_handling_e.STYLE_POST_FORMAT ) );
142
         styleDefs.Add( "lb4", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_BULLET_4, EA_Constants.styleName_ListBullet4,5, style_handling_e.STYLE_POST_FORMAT ) );
143
         styleDefs.Add( "ln0", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_NUMBER_0, EA_Constants.styleName_ListNumber0,1, style_handling_e.STYLE_POST_FORMAT ) );
144
         styleDefs.Add( "ln1", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_NUMBER_1, EA_Constants.styleName_ListNumber1,2, style_handling_e.STYLE_POST_FORMAT ) );
145
         styleDefs.Add( "ln2", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_NUMBER_2, EA_Constants.styleName_ListNumber2,3, style_handling_e.STYLE_POST_FORMAT ) );
146
         styleDefs.Add( "ln3", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_NUMBER_3, EA_Constants.styleName_ListNumber3,4, style_handling_e.STYLE_POST_FORMAT ) );
147
         styleDefs.Add( "ln4", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_NUMBER_4, EA_Constants.styleName_ListNumber4,5, style_handling_e.STYLE_POST_FORMAT ) );
148
         styleDefs.Add( "li0", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_INDENT_0, EA_Constants.styleName_ListIndent0,1, style_handling_e.STYLE_POST_FORMAT ) );
149
         styleDefs.Add( "li1", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_INDENT_1, EA_Constants.styleName_ListIndent1,2, style_handling_e.STYLE_POST_FORMAT ) );
150
         styleDefs.Add( "li2", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_INDENT_2, EA_Constants.styleName_ListIndent2,3, style_handling_e.STYLE_POST_FORMAT ) );
151
         styleDefs.Add( "li3", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_INDENT_3, EA_Constants.styleName_ListIndent3,4, style_handling_e.STYLE_POST_FORMAT ) );
152
         styleDefs.Add( "li4", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_INDENT_4, EA_Constants.styleName_ListIndent4,5, style_handling_e.STYLE_POST_FORMAT ) );
153
         styleDefs.Add( "la0", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_ALPHA_0, EA_Constants.styleName_AlphaList0, 1, style_handling_e.STYLE_POST_FORMAT ) );
154
         styleDefs.Add( "la1", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_ALPHA_1, EA_Constants.styleName_AlphaList1, 2, style_handling_e.STYLE_POST_FORMAT ) );
155
         styleDefs.Add( "la2", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_ALPHA_2, EA_Constants.styleName_AlphaList2, 3, style_handling_e.STYLE_POST_FORMAT ) );
156
         styleDefs.Add( "la3", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_ALPHA_3, EA_Constants.styleName_AlphaList3, 4, style_handling_e.STYLE_POST_FORMAT ) );
157
         styleDefs.Add( "la4", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_LIST_ALPHA_4, EA_Constants.styleName_AlphaList4, 5, style_handling_e.STYLE_POST_FORMAT ) );
2126 ghuddy 158
 
2130 ghuddy 159
         styleDefs.Add( "code", formStyleDef( style_type_e.STYLE_MS_WORD,   style_id_e.STYLE_ID_CODETEXT, EA_Constants.styleName_CodeText, 0, style_handling_e.STYLE_USE_TAG_STYLE ) );
160
         styleDefs.Add( "normal", formStyleDef( style_type_e.STYLE_MS_WORD, style_id_e.STYLE_ID_NORMAL,   EA_Constants.styleName_Normal  , 0, style_handling_e.STYLE_USE_TAG_STYLE ) );
161
         styleDefs.Add( "note", formStyleDef( style_type_e.STYLE_MS_WORD,   style_id_e.STYLE_ID_NOTE,     EA_Constants.styleName_Note    , 0, style_handling_e.STYLE_USE_TAG_STYLE ) );
162
         styleDefs.Add( "warn", formStyleDef( style_type_e.STYLE_MS_WORD,   style_id_e.STYLE_ID_WARNING,  EA_Constants.styleName_Warning , 0, style_handling_e.STYLE_USE_TAG_STYLE ) );
2126 ghuddy 163
      }
164
 
165
      /// <summary>
166
      /// Helper for initialise() function
167
      /// </summary>
168
      /// <param name="styleName"></param>
2130 ghuddy 169
      /// <param name="styleName"></param>
2126 ghuddy 170
      /// <returns></returns>
2130 ghuddy 171
      private static token_type formStyleDef(style_type_e styleType, style_id_e styleId, string styleName, int levelvar, style_handling_e styleHandling)
2126 ghuddy 172
      {
173
         token_type tokenType = new token_type();
2130 ghuddy 174
         tokenType.styleType = styleType; // The kind of style (ms-word or ea_docgen)
175
         tokenType.styleId   = styleId;   // The specific type of style
176
         tokenType.styleName = styleName; // The name of the style
177
         tokenType.txt = null;            // This is the actual text to be formatted
178
         tokenType.level = levelvar;      // used to record level numbering - only really useful for bullet/list styles
179
         tokenType.styleHandling = styleHandling;
2126 ghuddy 180
         return tokenType;
181
      }
182
 
183
 
2130 ghuddy 184
      private static void initialiseToken(out token_type token)
185
      {
186
         token.styleName = EA_Constants.styleName_Body1;
187
         token.styleId   = style_id_e.STYLE_ID_BODY1;
188
         token.styleType = style_type_e.STYLE_MS_WORD;
189
         token.styleHandling = style_handling_e.STYLE_USE_CALLER_STYLE;
190
         token.txt = null;
191
         token.level = 0;
192
      }
2126 ghuddy 193
 
2134 ghuddy 194
 
195
      private static string convert_EA7_1_RTF_ListTag(string s, string open, string close, string replacement_open, string replacement_close)
196
      {
197
         int pos;
198
         int pos_2;
199
 
200
         pos = s.IndexOf(open,0);
201
         if (pos >= 0)
202
         {
203
            while (pos >= 0)
204
            {
205
               pos_2 = s.IndexOf(close,pos);
206
 
207
               string seg = s.Substring(pos+4, pos_2 - (pos+4));
208
               seg = seg.Replace("\t<li>", replacement_open);
209
               seg = seg.Replace("</li>", replacement_close);
210
 
211
               s = s.Substring(0, pos) + seg + s.Substring(pos_2+5, s.Length - (pos_2+5));
212
 
213
               pos = s.IndexOf(open,0);
214
            }
215
         }
216
         return s;
217
      }
218
 
219
 
220
 
221
 
2126 ghuddy 222
      /// <summary>
223
      /// Parse the notes of an element and use the results to form document content.
224
      /// </summary>
225
      /// <param name="theElement"></param>
2130 ghuddy 226
      /// <param name="callerStyle"></param>
2126 ghuddy 227
      /// <returns></returns>
2130 ghuddy 228
      public static bool parse(string s, int id, string callerStyle, float indent_pts, bool continuation)
2126 ghuddy 229
      {
230
         int pos;
231
         int pos_LeftBracket = 0;
232
         int pos_RightBracket = 0;
233
         int pos_tagName;
234
         int pos_ContentStart;
235
         int pos_ContentEnd;
236
         bool lookingForTagEnd;
237
         bool foundError = false;
238
         Word.Range wr_body;
239
 
2134 ghuddy 240
         // Convert EA7.1 embedded RTF controls into EA_DocGen controls where possible
241
         s = convert_EA7_1_RTF_ListTag(s, "<ol>\r\n", "</ol>", "<ln0>", "</ln0>");
242
         s = convert_EA7_1_RTF_ListTag(s, "<ul>\r\n", "</ul>", "<lb0>", "</lb0>");
2130 ghuddy 243
 
2134 ghuddy 244
         // In EAv7.1, Sparx allows users to embed RTF into notes text. RTF uses <> to enclose controls just as EA_DocGen
245
         // does for its own controls which this function decodes. However, EA_DocGen controls are entered as text by
246
         // human users and so EAv7.1 converts < and > chars into a form such that they do not look like RTF tags.
247
         // We have to convert EA's escaping mechanism back into ascii text chars otherwise the parser wont work.
248
         // There may be issues to resolve with this parser if EAv7.1 users have used any of EA's RTF editing features
249
         // because if they do, the < and > chars there will not be escaped and the parser will find them and try to
250
         // decode them. Most will probably not be decodable.
251
         s = s.Replace("&lt;","<");
252
         s = s.Replace("&gt;",">");
253
 
2130 ghuddy 254
         // Begin to construct a range that will eventually encompass ALL of the text we will serialize during the execution
255
         // of this function. This is needed later only if the caller has specified a requirement style that needs to have 
256
         // global strikethrough or italicising applied to (all of) the text
257
         object startLocation;
258
         object endLocation;
259
         startLocation = createWordDoc.WordDocument.Content.End - 1;
260
 
261
         // Requirement element text must be indented according to the level number of the requirement tag. Caller
262
         // passes in absolute indentation value but for some items such as bullets/lists, we need a relative adjustment
263
         // since bullet/list styles have their own indentation settings and we only want to offset them rather than overwrite
264
         // them.
265
         // Get relative (to 2.5cm) indentation user has commanded. The 2.5cm mark is the standard point where body 1 text
266
         // begins. At 2.5cm, the pts value is 70.866. We only indent, never outdent so end stop at 0. 
2126 ghuddy 267
         float relative_indent_adjustment = 0;
268
         if (indent_pts > 0)
269
         {
270
            relative_indent_adjustment = indent_pts - (float)70.866;
271
            if (relative_indent_adjustment < 0)
272
               relative_indent_adjustment = 0;
273
         }
274
 
2130 ghuddy 275
         // A working variable and a list for completed tokens
2126 ghuddy 276
         token_type token;
277
         ArrayList tokens = new ArrayList();
278
 
279
         // default starting token - may be updated later
2130 ghuddy 280
         initialiseToken(out token);
2126 ghuddy 281
 
2130 ghuddy 282
 
2126 ghuddy 283
         lookingForTagEnd = false;
284
         pos_ContentStart = 0;
285
 
2130 ghuddy 286
         // PARSING LOOP -
287
         // Break up the input string into tokens that identify what kind of action is to be performed
288
         // with the token text. The default is as seen just above - apply Body1 MS-Word style. However,
289
         // if user has used formatting tags, the token style name, ID, and type will be updated accordingly.
290
         // This must be done iteratively until we have exhausted the input string.
291
 
2126 ghuddy 292
         // look for a tag
293
         pos = s.IndexOf("<", 0);
294
         while ((pos >= 0) && (pos < s.Length))
295
         {
2128 ghuddy 296
            if (createWordDoc.abortCreationThread)
297
               return false;
298
 
2126 ghuddy 299
            // record position of tag
300
            pos_LeftBracket = pos;
301
 
302
            // tag name begins at the next char
303
            pos_tagName = pos_LeftBracket + 1;
304
 
305
            // Check if this is a closing tag
306
            bool isEnding = false;
307
            if (pos < (s.Length-1))
308
            {
309
               if (s[pos+1] == '/')
310
               {
311
                  // skip past the / char
312
                  isEnding = true;
313
                  pos_tagName++;
314
               }
315
            }
2130 ghuddy 316
 
2126 ghuddy 317
            // We found a possible tag, now figure out if this is one of the tags we recognise
318
            bool found = false;
319
 
320
            // look for the closing bracket of the tag
321
            pos = s.IndexOf(">", pos+1);
322
            if (pos >= 0)
323
            {
2130 ghuddy 324
               // use hash table to identify the tag
2126 ghuddy 325
               found = styleDefs.Contains(s.Substring(pos_tagName, pos - pos_tagName));
326
            }
327
            else
328
            {
329
               // Cannot find any '>' so we should just exit the loop
330
               break;
331
            }
332
 
333
            // if the tag was recognised
334
            if (found)
335
            {
336
               // record position of the closing bracket of the tag
337
               pos_RightBracket = pos;
338
 
339
               // if this is an end tag, ie. </tagname>
340
               if (isEnding)
341
               {
342
                  pos_ContentEnd = pos_LeftBracket - 1;  // not sure if we really need to compute pos_ContentEnd
2130 ghuddy 343
 
2126 ghuddy 344
                  // check for out of sequence error
345
                  if (!lookingForTagEnd)
346
                  {
347
                     if (!foundError)
348
                     {
349
                        Main.WriteOutput( string.Format("ERROR, Found out of sequence style tag ({0}), generated document text may be formatted incorrectly.", s.Substring(pos_LeftBracket, pos_RightBracket - pos_LeftBracket + 1)), id);
350
                        foundError = true;
351
                     }
352
                  }
353
                  else
354
                  {
355
                     // Check that the end tag matches the start tag
2130 ghuddy 356
                     // NOTE: If we were to allow lazy end-tagging (ie using '</>' only) we would have to do away with this
357
                     // check.
2126 ghuddy 358
                     token_type tt = ((token_type)styleDefs[s.Substring(pos_tagName, pos_RightBracket-pos_tagName)]);
2130 ghuddy 359
                     if (token.styleType == tt.styleType &&
360
                        token.styleId == tt.styleId &&
361
                        token.styleName == tt.styleName)
2126 ghuddy 362
                     {
363
                        // Update the token's text field and add the now complete token to our list
364
                        // for processing a little later on.
365
                        token.txt = s.Substring(pos_ContentStart, pos_LeftBracket - pos_ContentStart);
366
                        tokens.Add(token);
367
 
368
                        // re-initialise token for next tag search
2130 ghuddy 369
                        initialiseToken(out token);
2126 ghuddy 370
 
371
                        lookingForTagEnd = false;
372
 
373
                        pos_ContentStart = pos_RightBracket + 1;
374
                     }
375
                     else
376
                     {
377
                        // end tag does not seem to be the same as the starting tag, so ignore it
378
                        if (!foundError)
379
                        {
380
                           Main.WriteOutput(string.Format("ERROR, Found unmatched style tag ({0}), generated document text may be formatted incorrectly.", s.Substring(pos_LeftBracket, pos_RightBracket - pos_LeftBracket + 1)), id);
381
                           foundError = true;
382
                        }
383
                     }
384
                  }
385
               }
386
               else
387
               {
388
                  // If there is content prior to now that has not been consumed, tokenise it now
389
                  if ((pos_LeftBracket - pos_ContentStart) > 0)
390
                  {
391
                     token.txt = s.Substring(pos_ContentStart, pos_LeftBracket - pos_ContentStart);
392
                     tokens.Add(token);
393
                  }
2130 ghuddy 394
 
2126 ghuddy 395
                  if (lookingForTagEnd)
396
                  {
397
                     if (!foundError)
398
                     {
399
                        Main.WriteOutput(string.Format("ERROR, Found nested style tag ({0}), generated document text may be formatted incorrectly.", s.Substring(pos_LeftBracket, pos_RightBracket - pos_LeftBracket + 1)), id);
400
                        foundError = true;
401
                     }
402
                  }
403
                  else
404
                  {
2130 ghuddy 405
                     // update the token variable with this tags atributes from the hash table lookup. This overwrites
406
                     // the default values assigned when parsing began, or when we resumed parsing after dealing with
407
                     // the previous token found.
408
                     token_type lookupToken = ((token_type)styleDefs[s.Substring(pos_tagName, pos_RightBracket-pos_tagName)]);
409
                     token.styleId   = lookupToken.styleId;
410
                     token.styleType = lookupToken.styleType;
411
                     token.styleName = lookupToken.styleName;
412
                     token.level     = lookupToken.level;
413
                     token.styleHandling = lookupToken.styleHandling;
2126 ghuddy 414
                     token.txt = null; // we dont know what the text content will be yet. This is obtained when we encounter the end tag
415
 
416
                     pos_ContentStart = pos_RightBracket + 1;
417
 
418
                     lookingForTagEnd = true;
419
                  }
420
               }
421
            }
422
            else
423
            {
424
               // the tag was not recognised so for now we just treat it as if it were plain text and continue
425
            }
426
 
427
            // look for next tag
428
            pos = s.IndexOf("<", pos);
429
 
430
         } // end of the loop
431
 
432
 
433
         // take care of the last token, if there is one
434
         if (pos_ContentStart < s.Length)
435
         {
436
            // Update the token's text field
437
            token.txt = s.Substring(pos_ContentStart, s.Length - pos_ContentStart);
438
            tokens.Add(token);
439
         }
440
 
441
         if (lookingForTagEnd)
442
         {
443
            if (!foundError)
444
            {
445
               Main.WriteOutput(string.Format("ERROR, Found incomplete style tag ({0}), generated document text may be formatted incorrectly.", s.Substring(pos_LeftBracket, pos_RightBracket - pos_LeftBracket + 1)), id);
446
               foundError = true;
447
            }
448
         }
449
 
2130 ghuddy 450
         // The way MS-Word works makes it necessary to do some formatting after we have serialised all of the text.
451
         // So, we need another list. This will contain elements that have the token AND the word range object that we
452
         // obtain when we serialise the text.
2126 ghuddy 453
         ArrayList postFormats = new ArrayList();
454
 
2130 ghuddy 455
         // SERIALISATION LOOP - Now process all the tokens we have found
456
         int tt_i = 0;
457
         for (tt_i = 0; tt_i < tokens.Count; tt_i++)
2126 ghuddy 458
         {
2130 ghuddy 459
            token_type tt = (token_type)tokens[tt_i];
460
 
2128 ghuddy 461
            if (createWordDoc.abortCreationThread)
462
               return false;
463
 
2126 ghuddy 464
            if (tt.txt != null && tt.txt.Length > 0)
465
            {
466
 
467
               switch (tt.styleType)
468
               {
469
                  case style_type_e.STYLE_EA_DOCGEN:
2130 ghuddy 470
                     switch (tt.styleId)
2126 ghuddy 471
                     {
2130 ghuddy 472
                        case style_id_e.STYLE_ID_TABLE:
2126 ghuddy 473
                           TabularContent.processTableElement(tt.txt, 0, indent_pts);
474
                           continuation = false;
475
 
476
                           // flag list numbering restart
477
                           postFormats.Add( new postFormat_type(null, tt) );
478
                           break;
479
 
480
                        default:
481
                           break;
482
                     }
483
                     break;
484
 
485
                  case style_type_e.STYLE_MS_WORD:
2130 ghuddy 486
                     switch (tt.styleHandling)
2126 ghuddy 487
                     {
2130 ghuddy 488
                        case style_handling_e.STYLE_POST_FORMAT:
489
                           // Replace <br> with actual required characters, and use the caller supplied style when serialising
490
                           // the text
491
                           tt.txt = tt.txt.Replace("<br>","\r\n");
492
 
493
                           // Serialise the text, initially applying callers style but since post-formatting will be
494
                           // done later, the texts appearance will change from what this initially applied style
495
                           // implies.
496
                           wr_body = TextualContent.appendAndSelectText( tt.txt, callerStyle, continuation );
2126 ghuddy 497
                           continuation = true;
2130 ghuddy 498
 
499
                           while (wr_body.Characters.Last.Text.Equals("\r") || wr_body.Characters.Last.Text.Equals("\n"))
500
                              wr_body.End = wr_body.End - 1;  // don't format the /n or \r char at the end - doing so causes wierd ms-word exceptions later on
2126 ghuddy 501
                           postFormats.Add( new postFormat_type(wr_body, tt) );
502
                           break;
503
 
2130 ghuddy 504
                        case style_handling_e.STYLE_USE_TAG_STYLE:
505
                           // Replace <br> with actual required characters, and use the caller supplied style when serialising
506
                           // the text
507
                           tt.txt = tt.txt.Replace("<br>","\r\n");
508
 
509
                           // Serialise the text, applying the tag's style 
2126 ghuddy 510
                           wr_body = TextualContent.appendAndSelectText( tt.txt, tt.styleName, continuation );
511
                           continuation = true;
2130 ghuddy 512
 
513
                           // Indent according to callers specified amount
2126 ghuddy 514
                           if (indent_pts > 0)
515
                              wr_body.ParagraphFormat.LeftIndent = indent_pts;
516
 
2130 ghuddy 517
                           // flag list numbering restart if this is printable text.
518
                           if (tt.txt.Trim().Length > 0)
519
                              postFormats.Add( new postFormat_type(null, tt) );
2126 ghuddy 520
                           break;
521
 
522
 
2130 ghuddy 523
                        case style_handling_e.STYLE_USE_CALLER_STYLE:
524
                           // Replace <br> with actual required characters, and use the caller supplied style when serialising
525
                           // the text
526
                           tt.txt = tt.txt.Replace("<br>","\r\n");
527
 
528
                           // Serialise the text, applying callers style 
529
                           wr_body = TextualContent.appendAndSelectText( tt.txt, callerStyle, continuation );
2126 ghuddy 530
                           continuation = true;
2130 ghuddy 531
 
532
                           // Indent according to callers specified amount
2126 ghuddy 533
                           if (indent_pts > 0)
534
                              wr_body.ParagraphFormat.LeftIndent = indent_pts;
535
 
536
                           // flag list numbering restart if this is printable text.
537
                           if (tt.txt.Trim().Length > 0)
538
                              postFormats.Add( new postFormat_type(null, tt) );
539
                           break;
540
 
541
                        default:
542
                           break;
543
                     }
544
                     break;
545
 
546
                  default:
547
                     break;
548
               }
549
            }
2130 ghuddy 550
         } // end of serialisation loop
2126 ghuddy 551
 
2130 ghuddy 552
 
553
         // POST-FORMATTING LOOP - Now apply post formatting commands to text already serialised in previous loop
2126 ghuddy 554
         int last_list_level = 0;
555
         foreach (postFormat_type pf in postFormats)
556
         {
557
            object style;
558
 
2128 ghuddy 559
            if (createWordDoc.abortCreationThread)
560
               return false;
561
 
2126 ghuddy 562
            // a null word range implies we must restart numbering for any lists
563
            if (pf.m_wr == null)
564
            {
565
               last_list_level = 0;
566
            }
567
            else
568
            {
2130 ghuddy 569
               switch (pf.m_tk.styleId)
2126 ghuddy 570
               {
2130 ghuddy 571
                  case style_id_e.STYLE_ID_BOLD:
2126 ghuddy 572
                     pf.m_wr.Select();
573
                     createWordDoc.WordApp.Selection.Range.Bold = 1;
574
                     last_list_level = 0;
575
                     break;
576
 
2130 ghuddy 577
                  case style_id_e.STYLE_ID_ITALIC:
2126 ghuddy 578
                     pf.m_wr.Select();
579
                     createWordDoc.WordApp.Selection.Range.Italic = 1;
580
                     last_list_level = 0;
581
                     break;
582
 
2130 ghuddy 583
                  case style_id_e.STYLE_ID_UNDERLINE:
2126 ghuddy 584
                     pf.m_wr.Select();
585
                     createWordDoc.WordApp.Selection.Range.Underline = Word.WdUnderline.wdUnderlineSingle;
586
                     last_list_level = 0;
587
                     break;
588
 
2130 ghuddy 589
                  default:
590
                     // Handle bullets/lists
591
                     if (pf.m_tk.styleId >= style_id_e.STYLE_ID_LIST_BULLET_0)
592
                     {
593
                        style = pf.m_tk.styleName;
594
                        pf.m_wr.Select();
595
                        createWordDoc.WordApp.Selection.Range.set_Style(ref style);
2126 ghuddy 596
 
2130 ghuddy 597
                        // Figure out if we have to restart numbering
598
                        if (last_list_level < pf.m_tk.level)
599
                        {
600
                           // only need to restart numbering if this list displays numbering - bullets and simple
601
                           // indents do not, but alpha and numeric lists do. The style_id_e type has been organised to
602
                           // make this a simple test.
603
                           if (pf.m_tk.styleId >= style_id_e.STYLE_ID_LIST_NUMBER_0)
604
                           {
605
                              // To restart numbering, the only way to do it is to (re-)apply the list template to the 
606
                              // selection with a "continue previous list" setting of false, and an "apply to"
607
                              // setting of "whole list". 
608
                              object continuePreviousList = false;
609
                              object applyTo = Word.WdListApplyTo.wdListApplyToWholeList;
610
                              object defListBehavour = Word.WdDefaultListBehavior.wdWord10ListBehavior;
611
 
612
                              Word.ListTemplate lt = createWordDoc.WordApp.Selection.Range.ListFormat.ListTemplate;
2126 ghuddy 613
 
2130 ghuddy 614
                              createWordDoc.WordApp.Selection.Range.ListFormat.ApplyListTemplate(
615
                                 lt, ref continuePreviousList, ref applyTo, ref defListBehavour);
616
                           }
617
                        }
618
 
619
                        // shift content right by relative indent adjustment we calculated earlier
620
                        if (relative_indent_adjustment > 0)
2126 ghuddy 621
                        {
2130 ghuddy 622
                           createWordDoc.WordApp.Selection.Range.ParagraphFormat.LeftIndent += relative_indent_adjustment;
2126 ghuddy 623
                        }
624
 
2130 ghuddy 625
                        last_list_level = pf.m_tk.level;
2126 ghuddy 626
                     }
627
                     break;
628
               }
629
            }
2130 ghuddy 630
         } // end of post-formatting loop
631
 
632
         // Special handling for Proposed and Rejected requirement sections - here the text must be italicies or
633
         // struck through (see definition of the styleName_ReqPropBody and styleName_ReqRejBody styles in StyleContent.cs).
634
         if (callerStyle.Equals(EA_Constants.styleName_ReqPropBody))
635
         {
636
            // Complete construction of a range that will encompass ALL of the text we will serialize during the execution
637
            // of this function
638
            endLocation = createWordDoc.WordDocument.Content.End;
639
            Word.Range wr_total = createWordDoc.WordDocument.Range(ref startLocation, ref endLocation);
640
            while (wr_total.Characters.Last.Text.Equals("\r") || wr_total.Characters.Last.Text.Equals("\n"))
641
               wr_total.End = wr_total.End - 1;  // don't format the \r\n char at the end - doing so causes wierd ms-word exceptions later on
642
 
643
            // italicise
644
            wr_total.Font.Italic = (int)MsoTriState.msoTrue;
2126 ghuddy 645
         }
2130 ghuddy 646
         else if (callerStyle.Equals(EA_Constants.styleName_ReqRejBody))
647
         {
648
            // Complete construction of a range that will encompass ALL of the text we will serialize during the execution
649
            // of this function
650
            endLocation = createWordDoc.WordDocument.Content.End;
651
            Word.Range wr_total = createWordDoc.WordDocument.Range(ref startLocation, ref endLocation);
652
            while (wr_total.Characters.Last.Text.Equals("\r") || wr_total.Characters.Last.Text.Equals("\n"))
653
               wr_total.End = wr_total.End - 1;  // don't format the \r\n char at the end - doing so causes wierd ms-word exceptions later on
654
 
655
            // strikethrough - use msoCTrue since msoTrue simply toggles strikethough attribute, or so it seems
656
            wr_total.Font.StrikeThrough = (int)MsoTriState.msoCTrue;
657
         }
658
 
2126 ghuddy 659
         return true;
660
      }
661
 
662
 
663
 
664
 
2130 ghuddy 665
 
666
   }
2126 ghuddy 667
}