Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e8cae8c12940013083aecc33f36dbb3c3736d28 (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
/*******************************************************************************
 * Copyright (c) 2010 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.coverage.internal.vcast.operations;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.coverage.internal.Activator;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * Represents the <dir>.wrk/vcast.vcp file which lists all the source files and results files specified in this
 * directory.
 * 
 * @author Donald G. Dunne
 */
public class VCastVcpLoadOperation extends AbstractOperation {

   private static final Pattern VALUE_PATTERN = Pattern.compile("(.*?):(.*?)$");

   private final URI vcpUri;
   private final VCastVcp vcastVcp;

   private final Matcher valueMatcher;

   public VCastVcpLoadOperation(URI vcpUri, VCastVcp vcastVcp) {
      super("Load VcastVcp", Activator.PLUGIN_ID);
      this.vcpUri = vcpUri;
      this.vcastVcp = vcastVcp;

      valueMatcher = VALUE_PATTERN.matcher("");
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      Conditions.checkNotNull(vcpUri, "vcpUri");
      Conditions.checkNotNull(vcastVcp, "vcastVcp");

      List<VcpResultsFile> resultsFiles = vcastVcp.getResultsFiles();
      List<VcpSourceFile> sourceFiles = vcastVcp.getSourceFiles();

      BufferedReader br = null;
      try {
         br = new BufferedReader(new InputStreamReader(vcpUri.toURL().openStream(), "UTF-8"));
         String line;
         VcpSourceFile vcpSourceFile = null;
         VcpResultsFile vcpResultsFile = null;
         // Loop through results file and log coverageItem as Test_Unit for each entry
         while ((line = br.readLine()) != null) {
            if (line.startsWith("SOURCE_FILE_BEGIN")) {
               vcpSourceFile = new VcpSourceFile(vcastVcp);
            } else if (line.startsWith("SOURCE_FILE_END")) {
               sourceFiles.add(vcpSourceFile);
               vcpSourceFile = null;
            } else if (vcpSourceFile != null) {
               addToSourceFile(vcpSourceFile, line);
            } else if (line.startsWith("RESULT_FILE_BEGIN")) {
               vcpResultsFile = new VcpResultsFile(vcastVcp);
            } else if (line.startsWith("RESULT_FILE_END")) {
               resultsFiles.add(vcpResultsFile);
               vcpResultsFile = null;
            } else if (vcpResultsFile != null) {
               addToResultFile(vcpResultsFile, line);
            }
         }
      } finally {
         Lib.close(br);
      }
   }

   private Pair<String, String> toKeyValue(String line) {
      Pair<String, String> entry = null;
      valueMatcher.reset(line);
      if (valueMatcher.find()) {
         String key = valueMatcher.group(1);
         String value = valueMatcher.group(2);
         entry = new Pair<String, String>(key, value);
      }
      return entry;
   }

   private void addToSourceFile(VcpSourceFile vcpSourceFile, String line) {
      Pair<String, String> entry = toKeyValue(line);
      if (entry != null) {
         String key = entry.getFirst();
         if ("SOURCE_FILENAME".equals(key)) {
            vcpSourceFile.setFilename(entry.getSecond());
         } else if ("UNIT_NUMBER".equals(key)) {
            vcpSourceFile.setUnitNumber(entry.getSecond());
         }
      } else {
         OseeLog.logf(Activator.class, Level.SEVERE, "Unhandled VcpSourceFile line [%s]", line);
      }
   }

   private void addToResultFile(VcpResultsFile vcpResultsFile, String line) {
      Pair<String, String> entry = toKeyValue(line);
      if (entry != null) {
         String key = entry.getFirst();
         if ("FILENAME".equals(key)) {
            vcpResultsFile.setFilename(entry.getSecond());
         }
      } else {
         OseeLog.logf(Activator.class, Level.SEVERE, "Unhandled VcpResultsFile line [%s]", line);
      }
   }
}

Back to the top