Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8432f7b3581d7c599b27e8a52f37bfcc3c015f4d (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
/*******************************************************************************
 * 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.plugin.core.util;

import java.io.File;
import java.io.InputStream;
import java.util.logging.Level;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.internal.PluginCoreActivator;

/**
 * This class provides a front end to writing files to a common osee.data directory in the workspace. This dir is
 * invisible to Eclipse Navigator and Package Explorer. It is provided as a common repository for files that need to be
 * created and retained by any plugin, but don't need to be visible to the user. This class does nothing more than
 * ensure the directory is created and provide a way to get the path for other plugins to use.
 * 
 * @author Donald G. Dunne
 */
public class OseeData {
   private static final IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation();
   private static final String oseeDataPathName = ".osee.data";
   private static final IPath oseeDataPath = workspacePath.append(oseeDataPathName);
   private static final File oseeDir = oseeDataPath.toFile();
   private static IProject project;

   static {
      if (!oseeDir.exists()) {
         if (!oseeDir.mkdir()) {
            OseeLog.log(PluginCoreActivator.class, Level.SEVERE, "Can't create " + oseeDataPathName + " dir.");
         }
      }
      createProject();
   }

   public static IPath getPath() {
      return oseeDataPath;
   }

   public static File getFile(String filename) {
      return new File(oseeDir, filename);
   }

   public static IFile getIFile(String fileName) {
      return project.getFile(fileName);
   }

   public static IFile getIFile(String fileName, InputStream in) throws OseeCoreException {
      return getIFile(fileName, in, false);
   }

   public static IFile getIFile(String fileName, InputStream in, boolean overwrite) throws OseeCoreException {
      IFile iFile = project.getFile(fileName);
      if (!iFile.exists() || overwrite) {
         AIFile.writeToFile(iFile, in);
      }
      return iFile;
   }

   public static File getWorkspaceFile(String path) {
      IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
      return new File(workspaceRoot.getFile(new Path(path)).getLocation().toString());
   }

   private static boolean createProject() {
      IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
      project = workspaceRoot.getProject(oseeDataPathName);
      if (!project.exists()) {
         try {
            project.create(null);
         } catch (CoreException ex) {
            ex.printStackTrace();
            return false;
         }
      }
      try {
         project.open(null);
      } catch (CoreException e) {
         e.printStackTrace();
         return false;
      }
      return true;
   }

   /**
    * @return Returns the project.
    */
   public static IProject getProject() {
      return project;
   }

   public static IFolder getFolder(String name) throws OseeCoreException {
      try {
         IFolder folder = project.getFolder(name);

         if (!folder.exists()) {
            folder.create(true, true, null);
         }
         return folder;
      } catch (CoreException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }
}

Back to the top