Skip to main content
summaryrefslogtreecommitdiffstats
blob: f5fe4b8ef5469c3c71f12e754380a8d34d57b065 (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
/*******************************************************************************
 * 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.operations;

import java.io.File;
import org.eclipse.core.runtime.IProgressMonitor;
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.AbstractOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
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.parsers.IArtifactExtractor;
import org.eclipse.osee.framework.skynet.core.internal.Activator;

/**
 * @author Roberto E. Escobar
 */
public class SourceToRoughArtifactOperation extends AbstractOperation {

   private final OperationLogger logger;
   private final IArtifactExtractor extractor;
   private final File sourceFile;
   private final RoughArtifactCollector collector;

   public SourceToRoughArtifactOperation(OperationLogger logger, IArtifactExtractor extractor, File sourceFile, RoughArtifactCollector collector) {
      super("Extract artifact data from source", Activator.PLUGIN_ID);
      this.extractor = extractor;
      this.sourceFile = sourceFile;
      this.collector = collector;
      this.logger = logger;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      File[] files;
      if (sourceFile.isDirectory()) {
         files = sourceFile.listFiles(extractor.getFileFilter());
      } else {
         files = new File[] {sourceFile};
      }
      double workPercentage = 1.0 / files.length;
      extractArtifacts(monitor, workPercentage, files, collector, collector.getParentRoughArtifact());
   }

   /**
    * used recursively when originally passed a directory, thus an array of files is accepted
    */
   private void extractArtifacts(IProgressMonitor monitor, double workPercentage, File[] files, RoughArtifactCollector collector, RoughArtifact parentArtifact) throws OseeCoreException {
      int workAmount = calculateWork(workPercentage);
      for (File file : files) {
         if (file.isFile()) {
            processFile(file, collector, parentArtifact);
         } else if (file.isDirectory()) {
            RoughArtifact directoryArtifact = new RoughArtifact(RoughArtifactKind.CONTAINER, file.getName());
            collector.addChildRoughArtifact(directoryArtifact);
            File[] subFiles = file.listFiles(extractor.getFileFilter());
            if (files.length > 0) {
               double subPercentage = workPercentage / subFiles.length;
               extractArtifacts(monitor, subPercentage, subFiles, collector, directoryArtifact);
            }
         } else {
            throw new OseeStateException("Source location [%s] is not a valid file or directory", file);
         }
         monitor.worked(workAmount);
      }
   }

   private void processFile(File file, RoughArtifactCollector collector, RoughArtifact parent) throws OseeCoreException {
      RoughArtifactCollector tempCollector = new RoughArtifactCollector(parent);
      try {
         extractor.process(logger, file.toURI(), tempCollector);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      // pass through all the collected items
      collector.addAllRoughArtifacts(tempCollector.getRoughArtifacts());
      collector.addAllRoughRelations(tempCollector.getRoughRelations());
   }
}

Back to the top