diff options
author | Ryan D. Brooks | 2013-12-17 17:34:55 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2013-12-17 17:34:55 +0000 |
commit | 7538a69869182ee93944e381a5cc71ddbd1eb8db (patch) | |
tree | f186a51e7ea7016cec4447af0c837a67fb681dbd /plugins | |
parent | 2d596bc9a86daadcc56befd97c981249832de146 (diff) | |
download | org.eclipse.osee-7538a69869182ee93944e381a5cc71ddbd1eb8db.tar.gz org.eclipse.osee-7538a69869182ee93944e381a5cc71ddbd1eb8db.tar.xz org.eclipse.osee-7538a69869182ee93944e381a5cc71ddbd1eb8db.zip |
feature[ats_SMCYL]: Add configurable traceability extraction
Change-Id: Ib8ba849f1a13234d7ef1e4a47c12a27f97726575
Diffstat (limited to 'plugins')
2 files changed, 247 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceAccumulator.java b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceAccumulator.java new file mode 100644 index 00000000000..32f8201f467 --- /dev/null +++ b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceAccumulator.java @@ -0,0 +1,154 @@ +/******************************************************************************* + * Copyright (c) 2013 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.report.internal; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.Scanner; +import java.util.Set; +import java.util.regex.Pattern; +import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException; +import org.eclipse.osee.framework.jdk.core.type.OseeCoreException; +import org.eclipse.osee.framework.jdk.core.util.Lib; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.SetMultimap; + +/** + * @author David W. Miller + */ +public class TraceAccumulator { + + private final SetMultimap<String, String> traceMarkToFiles = HashMultimap.create(); + private final SetMultimap<String, String> fileToMalformeddMarks = HashMultimap.create(); + private final LinkedList<String> noTraceFiles = new LinkedList<String>(); + private final Pattern filePattern; + private final Iterable<TraceMatch> traceMatches; + private SetMultimap<String, String> fileToTraceMarks; + private String relativePath; + + public TraceAccumulator(String filePattern, Iterable<TraceMatch> traceMatches) { + this.filePattern = Pattern.compile(filePattern); + this.traceMatches = traceMatches; + } + + public TraceAccumulator(String filePattern, TraceMatch traceMatch) { + this(filePattern, Collections.singletonList(traceMatch)); + } + + public void extractTraces(File root) throws IOException { + if (root == null || root.getParentFile() == null) { + throw new OseeArgumentException("The path [%s] is invalid.", root); + } + checkDirectory(root); + if (root.isFile()) { + for (String path : Lib.readListFromFile(root, true)) { + traceFile(new File(path)); + } + } else if (root.isDirectory()) { + traceFile(root); + } else { + throw new OseeArgumentException("Invalid directory path [%s]", root.getCanonicalPath()); + } + + fileToTraceMarks = Multimaps.invertFrom(traceMarkToFiles, HashMultimap.<String, String> create()); + } + + private void traceFile(File root) throws IOException { + int prefixLength = root.getCanonicalPath().length(); + for (File sourceFile : Lib.recursivelyListFilesAndDirectories(new ArrayList<File>(400), root, filePattern, false)) { + /** + * use prefixLength + 1 to account for trailing file separator which is not included in the root canonical path + */ + relativePath = sourceFile.toString().substring(prefixLength + 1); + int traceCount = parseInputStream(new FileInputStream(sourceFile)); + + if (traceCount == 0) { + noTraceFiles.add(sourceFile.getPath()); + } + } + } + + public void addInvalidTrace(String invalidTrace) { + fileToMalformeddMarks.put(relativePath, invalidTrace); + } + + public void addValidTrace(String traceMark) { + traceMarkToFiles.put(traceMark, relativePath); + } + + public int parseInputStream(InputStream providedStream) { + int numTracesInStream = 0; + Scanner scanner = new Scanner(providedStream, "UTF-8"); + try { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + for (TraceMatch traceMatch : traceMatches) { + int numTracesInLine = traceMatch.processLine(line, this); + numTracesInStream += numTracesInLine; + if (numTracesInLine > 0) { + break; + } + } + } + } finally { + scanner.close(); + } + return numTracesInStream; + } + + private void checkDirectory(File file) throws IOException { + if (!file.exists()) { + throw new OseeArgumentException("Input file does not exist: %s", file.getPath()); + } + if (file.isFile()) { + for (String path : Lib.readListFromFile(file, true)) { + File embeddedPath = new File(path); + if (!embeddedPath.exists()) { + throw new OseeCoreException("Bad path embedded in file: %s", path); + } + } + } + } + + public Set<String> getFiles(String requirement) { + return traceMarkToFiles.get(requirement); + } + + public Set<String> getFiles() { + return fileToTraceMarks.keySet(); + } + + public Set<String> getTraceMarks(String codeUnit) { + return fileToTraceMarks.get(codeUnit); + } + + public Set<String> getTraceMarks() { + return traceMarkToFiles.keySet(); + } + + public LinkedList<String> getNoTraceFiles() { + return noTraceFiles; + } + + public Set<String> getMalformedMarks(String file) { + return fileToMalformeddMarks.get(file); + } + + public Set<String> getFilesWithMalformedMarks() { + return fileToMalformeddMarks.keySet(); + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceMatch.java b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceMatch.java new file mode 100644 index 00000000000..04a9d9a9ada --- /dev/null +++ b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/TraceMatch.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2013 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.report.internal; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException; + +/** + * @author David W. Miller + * @author Ryan D. Brooks + */ +public class TraceMatch { + private Matcher primaryMatcher = null; + private Matcher secondaryMatcher = null; + + public TraceMatch(String primaryRegex, String secondaryRegex) { + if (primaryRegex == null || primaryRegex.isEmpty()) { + throw new OseeArgumentException("invalid Primary Regular Expression"); + } + + primaryMatcher = Pattern.compile(primaryRegex).matcher(""); + + if (secondaryRegex != null && !secondaryRegex.isEmpty()) { + secondaryMatcher = Pattern.compile(secondaryRegex).matcher(""); + } + } + + public int processLine(String inProcessing, TraceAccumulator accumulator) { + int count = 0; + + primaryMatcher.reset(inProcessing); + while (primaryMatcher.find()) { + String primaryMatch = getMatchResult(primaryMatcher); + + if (secondaryMatcher == null) { + accumulator.addValidTrace(primaryMatch); + count++; + } else { + int numFound = processSecondary(primaryMatch, accumulator); + if (numFound == 0) { + accumulator.addInvalidTrace(primaryMatch); + } + count += numFound; + } + } + return count; + } + + private String getMatchResult(Matcher matcher) { + String match; + int groupCount = matcher.groupCount(); + if (groupCount == 0) { + match = matcher.group(0); + } else if (groupCount == 1) { + match = matcher.group(1); + } else { + StringBuilder strB = new StringBuilder(); + for (int i = 1; i <= groupCount; ++i) { + String subMatch = matcher.group(i); + if (subMatch != null) { + strB.append(subMatch); + } + } + match = strB.toString(); + } + return match; + } + + private int processSecondary(String primaryMatch, TraceAccumulator accumulator) { + int count = 0; + secondaryMatcher.reset(primaryMatch); + while (secondaryMatcher.find()) { + String secondaryMatch = getMatchResult(secondaryMatcher); + accumulator.addValidTrace(secondaryMatch); + count++; + } + return count; + } + + @Override + public String toString() { + return String.format("primary=[%s] secondary=[%s]", primaryMatcher.pattern(), secondaryMatcher.pattern()); + } +}
\ No newline at end of file |