001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Jean-Marc Orliaguet, Chalmers 018 */ 019package org.nuxeo.theme.resources; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.Arrays; 024import java.util.Comparator; 025import java.util.regex.Matcher; 026import java.util.regex.Pattern; 027 028import org.nuxeo.common.utils.FileUtils; 029 030public class BankUtils { 031 032 final static Pattern filenamePattern = Pattern.compile("^\\p{IsAlnum}+[a-z0-9_\\-\\. ]*\\p{IsAlnum}+$|^\\p{IsAlnum}$"); 033 034 public static String getFileContent(File file) throws IOException { 035 if (!file.exists()) { 036 throw new IOException("File not found: " + file.getAbsolutePath()); 037 } 038 return FileUtils.readFile(file); 039 } 040 041 public static String getDomId(String id) { 042 return id.replaceAll("[\\s\\.]+", "-"); 043 } 044 045 public static File[] listFilesSorted(File folder) { 046 if (!folder.isDirectory()) { 047 return null; 048 } 049 File files[] = folder.listFiles(); 050 Arrays.sort(files, new Comparator() { 051 @Override 052 public int compare(final Object o1, final Object o2) { 053 return new Long(((File) o1).lastModified()).compareTo(new Long(((File) o2).lastModified())); 054 } 055 }); 056 return files; 057 } 058 059 public static boolean checkFilePath(String path) { 060 for (String f : path.split("/")) { 061 if ("".equals(f)) { 062 continue; 063 } 064 Matcher m = filenamePattern.matcher(f); 065 if (!m.find()) { 066 return false; 067 } 068 } 069 return true; 070 } 071}