Skip to main content
summaryrefslogtreecommitdiffstats
blob: 270a43ec5ef89260ecb8763c0e311c7bf718d6dc (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
/*******************************************************************************
 * 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.define.traceability;

import java.io.File;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public class TraceabilityExtractor {
   private static final Pattern ofpTraceabilityPattern = Pattern.compile("\\^SRS\\s*([^;\n\r]+);");
   private static final Pattern scriptTraceabilityPattern =
         Pattern.compile("addTraceability\\s*\\(\\s*\\\"\\s*(?:SubDD|SRS|CSID)?\\s*([^\\\"]+)\\\"");
   private static final Pattern invalidTraceabilityPattern = Pattern.compile("(\\[[A-Za-z]|USES_).*");

   private static final Pattern embeddedVolumePattern = Pattern.compile("\\{\\d+ (.*)\\}[ .]*");
   private static final Pattern nonWordPattern = Pattern.compile("[^A-Z_0-9]");
   private static final Pattern structuredReqNamePattern = Pattern.compile("\\[?(\\{[^\\}]+\\})(.*)");
   private static final Pattern stripTrailingReqNamePatern = Pattern.compile("(\\}|\\])(.*)");

   private static TraceabilityExtractor instance = null;
   private final Matcher scriptReqTraceMatcher;
   private final Matcher ofpReqTraceMatcher;
   private final Matcher invalidTraceMatcher;
   private final Matcher embeddedVolumeMatcher;
   private final Matcher nonWordMatcher;
   private final Matcher structuredRequirementMatcher;
   private final Matcher stripTrailingReqNameMatcher;

   private TraceabilityExtractor() {
      this.ofpReqTraceMatcher = ofpTraceabilityPattern.matcher("");
      this.scriptReqTraceMatcher = scriptTraceabilityPattern.matcher("");
      this.invalidTraceMatcher = invalidTraceabilityPattern.matcher("");
      this.embeddedVolumeMatcher = embeddedVolumePattern.matcher("");
      this.nonWordMatcher = nonWordPattern.matcher("");
      this.structuredRequirementMatcher = structuredReqNamePattern.matcher("");
      this.stripTrailingReqNameMatcher = stripTrailingReqNamePatern.matcher("");
   }

   public static TraceabilityExtractor getInstance() {
      if (instance == null) {
         instance = new TraceabilityExtractor();
      }
      return instance;
   }

   public List<String> getTraceMarksFromFile(File sourceFile) throws IOException {
      CharBuffer buffer = Lib.fileToCharBuffer(sourceFile);
      Matcher matcher = isScriptFile(sourceFile) ? getScriptTraceMarkMatcher() : getCodeTraceMarkMatcher();
      return getTraceMarks(buffer, matcher);
   }

   public List<String> getTraceMarks(CharBuffer buffer, Matcher matcher) {
      List<String> toReturn = new ArrayList<String>();
      matcher.reset(buffer);
      while (matcher.find() != false) {
         String mark = matcher.group(1);
         if (Strings.isValid(mark) != false) {
            toReturn.add(mark);
         }
      }
      return toReturn;
   }

   public boolean isValidTraceMark(String toCheck) {
      invalidTraceMatcher.reset(toCheck);
      return invalidTraceMatcher.matches() != true;
   }

   public Matcher getScriptTraceMarkMatcher() {
      return scriptReqTraceMatcher;
   }

   public Matcher getCodeTraceMarkMatcher() {
      return ofpReqTraceMatcher;
   }

   public boolean isScriptFile(File sourceFile) {
      return sourceFile.getName().endsWith("java");
   }

   public String getCanonicalRequirementName(String requirementMark) {
      String canonicalReqReference = requirementMark;
      if (Strings.isValid(requirementMark) != false) {
         canonicalReqReference = requirementMark.toUpperCase();

         embeddedVolumeMatcher.reset(canonicalReqReference);
         if (embeddedVolumeMatcher.find()) {
            canonicalReqReference = embeddedVolumeMatcher.group(1);
         }

         // Added to strip trailing artifact descriptive names } ... or ] ....
         stripTrailingReqNameMatcher.reset(canonicalReqReference);
         if (stripTrailingReqNameMatcher.find()) {
            String trail = stripTrailingReqNameMatcher.group(2);
            if (Strings.isValid(trail) && !trail.startsWith(".")) {
               canonicalReqReference = canonicalReqReference.substring(0, stripTrailingReqNameMatcher.start(1) + 1);
            }
         }

         nonWordMatcher.reset(canonicalReqReference);
         canonicalReqReference = nonWordMatcher.replaceAll("");

      }
      return canonicalReqReference;
   }

   // [{SUBSCRIBER}.ID] and example procedure {CURSOR_ACKNOWLEDGE}.NORMAL
   public Pair<String, String> getStructuredRequirement(String requirementMark) {
      Pair<String, String> toReturn = null;
      structuredRequirementMatcher.reset(requirementMark);
      if (structuredRequirementMatcher.matches() != false) {
         String primary = structuredRequirementMatcher.group(1);
         String secondary = structuredRequirementMatcher.group(2);
         if (Strings.isValid(primary) != false) {
            toReturn = new Pair<String, String>(primary, secondary);
         }
      }
      return toReturn;
   }
}

Back to the top