BrightSide Workbench Full Report + Source Code
Files.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.impl.util;
20 
21 import com.google.gson.Gson;
22 import java.io.File;
23 import java.io.FilenameFilter;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import org.turro.string.Strings;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 
35 public class Files {
36 
37  public static String getJsonFiles(final String path, final String pattern) {
38  return new Gson().toJson(getFiles(path, pattern));
39  }
40 
41  public static String getJsonSortedFiles(final String path, final String pattern) {
42  List<String> files = getFiles(path, pattern);
43  Collections.sort(files);
44  return new Gson().toJson(files);
45  }
46 
47  public static String getJsonShuffledFiles(final String path, final String pattern) {
48  List<String> files = getFiles(path, pattern);
49  Collections.shuffle(files);
50  return new Gson().toJson(files);
51  }
52 
53  public static List<String> getFiles(final String path, final String pattern) {
54  ArrayList<String> files = new ArrayList<>();
55  File folder = new File(ElephantContext.getRealPath(path));
56  if(folder.exists()) {
57  for(File file : folder.listFiles(new FilenameFilter() {
58  @Override
59  public boolean accept(File dir, String name) {
60  return name.matches(Strings.convertToRegEx(pattern));
61  }
62  })) {
63  if(file.isFile()) {
64  files.add(ElephantContext.getRootWebPath() + path + "/" + file.getName());
65  }
66  }
67  }
68  return files;
69  }
70 
71  public static File file(IConstructor constructor, String file) {
72  return file(file.replaceAll("\\*", constructor.getRenderingContext().getPath() + "/_internal/repository"));
73  }
74 
75  public static File file(String file) {
77  return new File(file);
78  }
79 
80  public static boolean exists(String path) {
81  path = ElephantContext.getRealPath(path);
82  return path == null ? false : new File(path).exists();
83  }
84 
85  public static String toExisting(String path) {
86  while(!exists(path)) {
87  int p = path.lastIndexOf("/");
88  if(p > 0) {
89  path = path.substring(0, p);
90  } else {
91  break;
92  }
93  }
94  return path;
95  }
96 
97  private Files() {
98  }
99 
100 }
static String getJsonFiles(final String path, final String pattern)
Definition: Files.java:37
static String getJsonSortedFiles(final String path, final String pattern)
Definition: Files.java:41
static String getJsonShuffledFiles(final String path, final String pattern)
Definition: Files.java:47
static boolean exists(String path)
Definition: Files.java:80
static File file(IConstructor constructor, String file)
Definition: Files.java:71
static File file(String file)
Definition: Files.java:75
static List< String > getFiles(final String path, final String pattern)
Definition: Files.java:53
static String toExisting(String path)
Definition: Files.java:85