Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6920a000b86eaef1f44c9decc8d6492d7a6bdb6a (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
/*******************************************************************************
 * 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.database.schema.internal.util;

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// TODO: this class has some overlap with methods provided in org.eclipse.osee.framework.jdk.core.Lib
public final class FileUtility {

   public static String SCHEMA_EXTENSION = ".SCHEMA.xml";
   public static String DB_DATA_EXTENSION = ".DATA.xml";

   private FileUtility() {
      // Utility class
   }

   public static boolean isValidDirectory(File directory) {
      if (directory != null && directory.exists() && directory.canRead()) {
         File[] listOfFiles = directory.listFiles();
         if (listOfFiles.length != 0) {
            return true;
         }
      }
      return false;
   }

   public static void setupDirectoryForWrite(File directory) {
      if (directory.exists() && directory.canWrite()) {
         if (!directory.isDirectory()) {
            directory.mkdirs();
         }
      } else {
         directory.mkdirs();
      }
   }

   public static List<File> getFileList(File sourceDirectory, final String extension) {
      File[] listOfFiles = sourceDirectory.listFiles(new FilenameFilter() {
         @Override
         public boolean accept(File directoryName, String filename) {
            return filename.endsWith(extension) && new File(directoryName + File.separator + filename).canRead();
         }
      });
      return Arrays.asList(listOfFiles);
   }

   public static List<URL> getSchemaFileList(File sourceDirectory) {
      List<File> files = getFileList(sourceDirectory, SCHEMA_EXTENSION);
      List<URL> streams = new ArrayList<URL>();
      for (File file : files) {
         try {
            streams.add(file.toURI().toURL());
         } catch (MalformedURLException e) {
            e.printStackTrace();
         }
      }
      return streams;
   }

   public static List<File> getDBDataFileList(File sourceDirectory) {
      return getFileList(sourceDirectory, DB_DATA_EXTENSION);
   }

   public static List<URL> getDBDataFileListInputStream(File sourceDirectory) {
      List<File> files = getFileList(sourceDirectory, DB_DATA_EXTENSION);
      List<URL> streams = new ArrayList<URL>();
      for (File file : files) {
         try {
            streams.add(file.toURI().toURL());
         } catch (MalformedURLException e) {
            e.printStackTrace();
         }
      }
      return streams;
   }
}

Back to the top