View Javadoc
1   /*
2    *  Copyright 2012 Chris Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.scanner.core.helper;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.util.WeakHashMap;
26  
27  import org.jadira.scanner.core.exception.FileAccessException;
28  
29  import de.schlichtherle.io.ArchiveDetector;
30  import de.schlichtherle.io.ArchiveException;
31  
32  public final class FileUtils {
33  
34      private static final WeakHashMap<String, de.schlichtherle.io.File> FILE_CACHE = new WeakHashMap<String, de.schlichtherle.io.File>();
35  	
36  	private FileUtils() {
37  	}
38  
39  	public static File getFileForPathName(String pathName, File parentFile) throws FileAccessException {
40  
41  		final String directoryPathName;
42  		if (!pathName.contains("/")) {
43  			directoryPathName = pathName.replace('.', de.schlichtherle.io.File.separatorChar);
44  		} else {
45  			directoryPathName = pathName.replace('/', de.schlichtherle.io.File.separatorChar);
46  		}
47  
48  		String filePath = parentFile.getPath() + de.schlichtherle.io.File.separatorChar + directoryPathName;
49  		de.schlichtherle.io.File cachedFile = FILE_CACHE.get(filePath);
50  		if (cachedFile != null) {
51  			return cachedFile;
52  		}
53  
54  		de.schlichtherle.io.File resolvedFile = new de.schlichtherle.io.File(filePath, ArchiveDetector.ALL);
55  		if (resolvedFile.exists()) {
56  			FILE_CACHE.put(filePath, resolvedFile);
57  			return resolvedFile;
58  		} else {
59  			return null;
60  		}
61  	}
62  	
63  	public static File getFileForPathName(String pathName, URL url) throws FileAccessException {
64  
65  		File parentFile = getFileFromURL(url);
66  
67  		return getFileForPathName(pathName, parentFile);
68  	}
69  
70  	public static File getFileFromURL(URL url) throws FileAccessException {
71  
72  		String pathString = url.toString();
73  		if (pathString.endsWith("!/")) {
74  			pathString = pathString.substring(4);
75  			pathString = pathString.substring(0, pathString.length() - 2);
76  		}
77  
78  		File retVal;
79  
80  		try {
81  			if (pathString.endsWith("/")) {
82  				retVal = new File(url.toURI());
83  			} else {
84  				retVal = new de.schlichtherle.io.File(new File(url.toURI()), ArchiveDetector.ALL);
85  			}
86  		} catch (URISyntaxException e) {
87  			throw new FileAccessException("Could not derive file from URL: " + url, e);
88  		}
89  
90  		return retVal;
91  	}
92  
93      public static <T> T doWithFile(File file, FileInputStreamOperation<T> callback) {
94      	
95      	InputStream fiStream = null;
96  		
97  		try {
98  			if (file instanceof de.schlichtherle.io.File) {
99  				fiStream = new de.schlichtherle.io.FileInputStream(file);
100 			} else {
101 				fiStream = new FileInputStream(file);
102 			}
103 			return callback.execute(file.getPath(), fiStream);
104 		} catch (FileNotFoundException e) {
105 			throw new FileAccessException("Could not find referenced file: " + file.getPath(), e);
106 		} finally {
107 			
108 			try {
109 				if (fiStream != null) {
110 					fiStream.close();
111 					
112 				}
113 			} catch (IOException e) {
114 			} finally {
115 			
116 				if (file instanceof de.schlichtherle.io.File) {
117 					try {
118 					    de.schlichtherle.io.File.umount(); //TVFS.umount((de.schlichtherle.io.File)file);
119 					} catch (ArchiveException e) { // FsSyncException e) {
120 					}
121 				}
122 			}
123 		}
124     }
125 }