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

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.define.DefinePlugin;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public class UriResourceContentFinder {

   private final URI source;
   private final HashCollection<IResourceLocator, IResourceHandler> locatorMap;
   private final boolean isRecursionAllowed;
   private final boolean isFileWithMultiplePaths;

   public UriResourceContentFinder(final URI source, final boolean isRecursionAllowed, final boolean isFileWithMultiplePaths) {
      super();
      this.source = source;
      this.isRecursionAllowed = isRecursionAllowed;
      this.isFileWithMultiplePaths = isFileWithMultiplePaths;
      this.locatorMap = new HashCollection<IResourceLocator, IResourceHandler>();
   }

   public void addLocator(IResourceLocator locator, IResourceHandler... handler) {
      if (locator != null && handler != null && handler.length > 0) {
         synchronized (locatorMap) {
            locatorMap.put(locator, Arrays.asList(handler));
         }
      }
   }

   public void removeLocator(IResourceLocator locator) {
      if (locator != null) {
         synchronized (locatorMap) {
            locatorMap.removeValues(locator);
         }
      }
   }

   public void execute(IProgressMonitor monitor) throws OseeCoreException {
      try {
         IFileStore fileStore = EFS.getStore(source);
         if (isFileWithMultiplePaths) {
            processFileWithPaths(monitor, fileStore);
         } else {
            monitor.beginTask("Scanning files", 1);
            processFileStore(monitor, fileStore);
            monitor.worked(1);
         }
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         monitor.done();
      }
   }

   private void processFileWithPaths(IProgressMonitor monitor, IFileStore fileStore) throws Exception {
      IFileInfo info = fileStore.fetchInfo(EFS.NONE, monitor);
      if (info != null && info.exists()) {
         List<String> paths = Lib.readListFromFile(new File(fileStore.toURI()), true);
         monitor.beginTask("Searching for files", paths.size());
         for (String path : paths) {
            if (Strings.isValid(path)) {
               processFileStore(monitor, EFS.getStore(new File(path).toURI()));
            }
            if (monitor.isCanceled()) {
               break;
            }
            monitor.worked(1);
         }
      } else {
         monitor.beginTask("Searching for files", 1);
      }
   }

   private void processFileStore(IProgressMonitor monitor, IFileStore fileStore) throws Exception {
      IFileInfo info = fileStore.fetchInfo(EFS.NONE, monitor);
      if (info != null && info.exists()) {
         if (info.isDirectory()) {
            processDirectory(monitor, fileStore);
         } else {
            processFile(monitor, fileStore);
         }
      }
   }

   private boolean isValidDirectory(IProgressMonitor monitor, IFileStore fileStore) {
      boolean result = false;
      for (IResourceLocator locator : locatorMap.keySet()) {
         if (monitor.isCanceled()) {
            break;
         }
         if (locator.isValidDirectory(fileStore)) {
            result = true;
            break;
         }
      }
      return result;
   }

   private void processDirectory(IProgressMonitor monitor, IFileStore fileStore) throws Exception {
      if (isValidDirectory(monitor, fileStore)) {
         boolean isProcessingAllowed = false;
         for (IFileStore childStore : fileStore.childStores(EFS.NONE, monitor)) {
            isProcessingAllowed = false;
            if (monitor.isCanceled()) {
               break;
            }
            if (!isRecursionAllowed) {
               isProcessingAllowed = !childStore.fetchInfo().isDirectory();
            } else {
               isProcessingAllowed = true;
            }

            if (isProcessingAllowed) {
               processFileStore(monitor, childStore);
            }
         }
      }
      monitor.worked(1);
   }

   private void processFile(IProgressMonitor monitor, IFileStore fileStore) throws Exception {
      if (!monitor.isCanceled()) {
         for (IResourceLocator locator : locatorMap.keySet()) {
            if (locator.isValidFile(fileStore)) {
               CharBuffer fileBuffer = getContents(monitor, fileStore);
               if (locator.hasValidContent(fileBuffer)) {
                  String fileName = locator.getIdentifier(fileStore, fileBuffer);
                  if (!monitor.isCanceled()) {
                     monitor.subTask(String.format("processing [%s]", fileStore.getName()));
                     notifyListeners(locator, fileStore.toURI(), fileName, fileBuffer);
                  }
               }
            }
         }
      }
      monitor.worked(1);
   }

   private CharBuffer getContents(IProgressMonitor monitor, IFileStore fileStore) throws Exception {
      CharBuffer toReturn = null;
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, monitor));
         toReturn = Lib.inputStreamToCharBuffer(inputStream);
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException ex) {
               OseeLog.log(DefinePlugin.class, Level.SEVERE, String.format("Error closing stream for resource: [%s]",
                     fileStore.getName()), ex);
            }
         }
      }
      return toReturn;
   }

   private void notifyListeners(final IResourceLocator locator, final URI uriPath, final String fileName, final CharBuffer fileBuffer) {
      for (IResourceHandler handler : locatorMap.getValues(locator)) {
         handler.onResourceFound(uriPath, fileName, fileBuffer);
      }
   }
}

Back to the top