Skip to main content
summaryrefslogtreecommitdiffstats
blob: 396c5ca02af0846260816c5e2d8a231ee4a79104 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.importing.parsers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.operation.OperationReporter;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.importing.RoughArtifact;
import org.eclipse.osee.framework.skynet.core.importing.RoughArtifactKind;
import org.eclipse.osee.framework.skynet.core.importing.operations.RoughArtifactCollector;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.word.WordUtil;

/**
 * Test: @link WordOutlineTest
 * 
 * @author Karol M. Wilk
 */
public class WordOutlineExtractorDelegate implements IArtifactExtractorDelegate {
   private static final String WORD_OUTLINE_PARSER_NAME = "Word Outline";
   // Node: <w:t>1.1.1 or Text</w:t>
   private static final Pattern WT_ELEMENT_REGEX = Pattern.compile("<w:t>(.*?)</w:t>");
   // Node: <wx:t wx:val="1.1.1 "/>
   private static final Pattern LIST_ITEM_REGEX = Pattern.compile("<wx:t wx:val=\"([0-9.]+\\s*)\".*/>");
   private static final Pattern OUTLINE_NUMBER = Pattern.compile("((?>\\d+\\.)+\\d*)\\s*");

   // This assumes that the user uses a generated Table of Contents from Word and does not come up with
   // his/hers own version of of a style can call it "TOC\d+"
   private static final Pattern TOC_HYPERLINK_PATTERN = Pattern.compile(".*<w:pStyle w:val=\"TOC\\d+?\"/>.*",
      Pattern.DOTALL);
   private boolean possibleTableOfContents;

   private static String detectedTableOfContentsReportError =
      "Table of Contents found in document. Please remove per the spec on: \n http://wiki.eclipse.org/OSEE/HowTo/ImportArtifactsFromWordML";

   public enum ContentType {
      CONTENT,
      OUTLINE_TITLE
   };

   /**
    * Keeps state whether on what user decided last
    */
   private ContentType lastDeterminedContentType = ContentType.OUTLINE_TITLE;
   /**
    * Keeps state whether user was asked for help.
    */
   private boolean userAskedForHelp;

   private Map<String, RoughArtifact> duplicateCatcher;

   private RoughArtifact previousNamedArtifact;
   private RoughArtifact roughArtifact;
   private StringBuilder wordFormattedContent;

   private StringBuffer lastHeaderNumber;
   private StringBuffer lastHeaderName;
   private StringBuffer lastContent;

   private boolean initalized;

   private final OutlineResolution outlineResolution = new OutlineResolution();
   private IConflictResolvingGui conflictResolvingGui = null;

   public WordOutlineExtractorDelegate() {
      this(new IConflictResolvingGui() {
         @Override
         public ContentType determineContentType(Collection<String> paramList) throws OseeCoreException {
            Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 258, "", null);
            IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(status);
            Object object = null;
            try {
               object = handler.handleStatus(status, paramList);
            } catch (CoreException ex) {
               OseeExceptions.wrapAndThrow(ex);
            }
            return (ContentType) object;
         }
      });
   }

   public WordOutlineExtractorDelegate(IConflictResolvingGui gui) {
      super();
      conflictResolvingGui = gui;
   }

   public IConflictResolvingGui getOutlineResolvingUi() {
      return conflictResolvingGui;
   }

   public interface IConflictResolvingGui {
      ContentType determineContentType(Collection<String> paramList) throws OseeCoreException;
   }

   @Override
   public boolean isApplicable(IArtifactExtractor parser) {
      return parser != null && WORD_OUTLINE_PARSER_NAME.equals(parser.getName());
   }

   /**
    * Subclasses may extend this method to allocate resources
    */
   @Override
   public void initialize() {
      duplicateCatcher = new HashMap<String, RoughArtifact>();
      lastHeaderNumber = new StringBuffer();
      lastHeaderName = new StringBuffer();
      lastContent = new StringBuffer();
      previousNamedArtifact = null;
      roughArtifact = null;
      wordFormattedContent = new StringBuilder();
      initalized = true;
      possibleTableOfContents = false;
      userAskedForHelp = false;
   }

   /**
    * Sublcasses may extend this method to dispose resources.
    */
   @Override
   public void dispose() {
      duplicateCatcher = null;
      previousNamedArtifact = null;
      roughArtifact = null;
      lastHeaderNumber = null;
      lastHeaderName = null;
      lastContent = null;
      initalized = false;
      userAskedForHelp = false;
      possibleTableOfContents = false;
   }

   @Override
   public final void processContent(OperationReporter opReporter, RoughArtifactCollector collector, boolean forceBody, boolean forcePrimaryType, String headerNumber, String listIdentifier, String paragraphStyle, String content, boolean isParagraph) throws OseeCoreException {
      if (Strings.isValid(content) && initalized) {
         if (!possibleTableOfContents) {
            possibleTableOfContents = TOC_HYPERLINK_PATTERN.matcher(content).matches();
            if (possibleTableOfContents && opReporter != null) {
               opReporter.report(detectedTableOfContentsReportError);
            }
         }

         StringBuilder outlineNumber = new StringBuilder(); //Number i.e. 1.1
         StringBuilder outlineName = new StringBuilder(); //Title i.e. Scope
         StringBuilder outlineContent = null; // Content, text, table content, etc.

         boolean newOutlineNumber = processOutlineNumberAndName(content, outlineNumber, outlineName);

         //outline number detection failed, try content
         if (!newOutlineNumber) {
            processContentOfParagraph(content, outlineContent);
         }

         if (collector != null && newOutlineNumber) {
            setContent();
            roughArtifact = setUpNewArtifact(collector, outlineNumber.toString());
            previousNamedArtifact = roughArtifact;
            processHeadingText(roughArtifact, WordUtil.textOnly(outlineName.toString()));
         } else if (roughArtifact != null) {
            wordFormattedContent.append(content);
         }
      } else {
         throw new OseeCoreException(
            "%s::processContent() Either passed in content is invalid or *Delegate hasn't been initialized...",
            toString());
      }
   }

   /**
    * Given content (a <w:p> paragraph), fill outlineContent with extracted content information from
    * grabNameAndTemplateContent()
    * 
    * @param content a <w:p> paragraph.
    * @param outlineContent data structure to fill as new content gets extracted.
    * @return if found any new content or not...
    */
   private boolean processContentOfParagraph(String content, StringBuilder outlineContent) {
      outlineContent = new StringBuilder(300); //average content is larger than 16 chars
      grabNameAndTemplateContent(content, outlineContent);

      boolean newOutlineContent = false;
      newOutlineContent = outlineContent.length() != 0;

      if (newOutlineContent) {
         resetLastContent();
         setLastContent(outlineContent.toString());
      }

      return newOutlineContent;
   }

   /**
    * Gets content and attempts to extract outline number and title, if it fails with regular regex, it tries
    * specializedOutlineNumberTitleExtract()
    */
   private boolean processOutlineNumberAndName(String content, StringBuilder outlineNumber, StringBuilder outlineName) throws OseeCoreException {
      Matcher listItemMatcher = LIST_ITEM_REGEX.matcher(content);
      if (listItemMatcher.find()) { // wx:val grab
         //does duplicate catcher contain this number already?
         String number = listItemMatcher.group(1).trim();
         if (duplicateCatcher.get(number) == null) {
            outlineNumber.append(number);
         }
      } else {
         specializedOutlineNumberTitleExtract(content, outlineNumber, outlineName);
      }

      boolean outlineNumberDetected = outlineNumber.length() != 0;
      if (outlineNumberDetected) {
         resetLastHeaderNumber();
         setLastHeaderNumber(outlineNumber.toString());
         grabNameAndTemplateContent(content, outlineName);

         if (outlineName.length() != 0) {
            resetLastHeaderName();
            setLastHeaderName(outlineName.toString());
         }
      }
      return outlineNumberDetected;
   }

   /**
    * Grabs outline text or content. Stores results in outLineStorage.
    */
   private void grabNameAndTemplateContent(String paragraph, StringBuilder outLineStorage) {
      if (outLineStorage.length() == 0) {
         Matcher wtElementMatcher = WT_ELEMENT_REGEX.matcher(paragraph);
         while (wtElementMatcher.find()) {
            Matcher checkingForOutlineNumber = OUTLINE_NUMBER.matcher(wtElementMatcher.group(1));
            if (!checkingForOutlineNumber.matches()) {
               outLineStorage.append(wtElementMatcher.group(1));
            }
         }
      }
   }

   /**
    * Specializes in extraction of "1. scope" type of outline number and names. Outline name can also be spread out over
    * multiple <w:t>s
    */
   private void specializedOutlineNumberTitleExtract(String paragraph, StringBuilder outlineNumberStorage, StringBuilder outlineTitleStorage) throws OseeCoreException {
      StringBuilder wtStorage = new StringBuilder(paragraph.length());
      Matcher wtElementMatcher = WT_ELEMENT_REGEX.matcher(paragraph);
      while (wtElementMatcher.find()) {
         wtStorage.append(wtElementMatcher.group(1));
      }

      int indexOfFirstSpace = wtStorage.toString().indexOf(" ");
      if (indexOfFirstSpace != -1) {
         CharSequence title = wtStorage.subSequence(0, indexOfFirstSpace);
         Matcher outlineNumberMatcher = OUTLINE_NUMBER.matcher(title);
         if (outlineNumberMatcher.matches()) {
            processSpecializedOutlineNumberAndTitle(outlineNumberMatcher.group(),
               (String) wtStorage.subSequence(indexOfFirstSpace, wtStorage.length()), outlineNumberStorage,
               outlineTitleStorage);
         } else {
            outlineTitleStorage = wtStorage;
         }
      } else {
         //must be just content
         outlineTitleStorage = wtStorage;
      }

   }

   private void processSpecializedOutlineNumberAndTitle(String currentOutlineNumber, String formOfOutlineTitle, StringBuilder outlineNumberStorage, StringBuilder outlineTitleStorage) throws OseeCoreException {
      String lastOutlineNumber = getLastHeaderNumber();

      lastDeterminedContentType = ContentType.OUTLINE_TITLE;

      if (outlineResolution.isInvalidOutlineNumber(currentOutlineNumber, lastOutlineNumber)) {
         if (duplicateCatcher.get(currentOutlineNumber) == null) {
            userAskedForHelp = true;
            Collection<String> paramList = new ArrayList<String>();
            paramList.add(lastOutlineNumber);
            paramList.add(currentOutlineNumber);
            lastDeterminedContentType = conflictResolvingGui.determineContentType(paramList);
         } else {
            lastDeterminedContentType = ContentType.CONTENT;
         }
      } else {
         userAskedForHelp = false;
      }

      switch (lastDeterminedContentType) {
         case CONTENT:
            // do nothing because when method exits it will be processed as content
            break;
         case OUTLINE_TITLE:
         default:
            outlineNumberStorage.append(currentOutlineNumber);
            if (Strings.isValid(formOfOutlineTitle)) {
               outlineTitleStorage.append(formOfOutlineTitle);
            }
            break;
      }
   }

   public boolean userAskedForHelp() {
      return userAskedForHelp;
   }

   private void setContent() {
      if (roughArtifact != null) {
         roughArtifact.addAttribute(CoreAttributeTypes.WordTemplateContent, wordFormattedContent.toString());
         wordFormattedContent.setLength(0);
      }
   }

   @Override
   public void finish() {
      setContent();
   }

   public void processHeadingText(RoughArtifact roughArtifact, String headingText) {
      roughArtifact.addAttribute("Name", headingText.trim());
   }

   /**
    * Checks if another artifact with the same outlineNumber was created
    */
   private RoughArtifact setUpNewArtifact(RoughArtifactCollector collector, String outlineNumber) throws OseeCoreException {
      RoughArtifact duplicateArtifact = duplicateCatcher.get(outlineNumber);
      if (duplicateArtifact == null) {
         RoughArtifact roughArtifact = new RoughArtifact(RoughArtifactKind.PRIMARY);
         duplicateCatcher.put(outlineNumber, roughArtifact);

         collector.addRoughArtifact(roughArtifact);
         roughArtifact.setSectionNumber(outlineNumber);

         roughArtifact.addAttribute(CoreAttributeTypes.ParagraphNumber, outlineNumber);

         return roughArtifact;
      } else {
         throw new OseeStateException("Paragraph %s found more than once following \"%s\" which is a duplicate of %s",
            outlineNumber, previousNamedArtifact.getName(), duplicateArtifact.getName());
      }
   }

   @Override
   public String getName() {
      return "General Outline Documents";
   }

   public String getLastHeaderNumber() {
      return getBufferString(lastHeaderNumber);
   }

   private void setLastHeaderNumber(String headerNumber) {
      lastHeaderNumber.append(headerNumber);
   }

   public String getLastHeaderName() {
      return getBufferString(lastHeaderName);
   }

   private void setLastHeaderName(String headerName) {
      lastHeaderName.append(headerName);
   }

   public String getLastContent() {
      return getBufferString(lastContent);
   }

   private void setLastContent(String content) {
      lastContent.append(content);
   }

   private String getBufferString(StringBuffer builder) {
      return builder != null ? builder.toString() : null;
   }

   private void resetLastHeaderNumber() {
      lastHeaderNumber.setLength(0);
   }

   private void resetLastHeaderName() {
      lastHeaderName.setLength(0);
   }

   private void resetLastContent() {
      lastContent.setLength(0);
   }

}

Back to the top