001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.objectmatrix.impl; 025 026 import java.io.File; 027 import java.io.IOException; 028 import java.io.Serializable; 029 import java.lang.ref.SoftReference; 030 import java.util.Collection; 031 import java.util.HashMap; 032 import java.util.Map; 033 import java.util.Set; 034 035 import org.ujmp.core.Matrix; 036 import org.ujmp.core.MatrixFactory; 037 import org.ujmp.core.enums.FileFormat; 038 import org.ujmp.core.exceptions.MatrixException; 039 import org.ujmp.core.mapmatrix.AbstractMapMatrix; 040 import org.ujmp.core.mapmatrix.MapMatrix; 041 import org.ujmp.core.util.MathUtil; 042 043 public class FileMatrix extends AbstractMapMatrix<String, Object> { 044 private static final long serialVersionUID = 7869997158743678080L; 045 046 public static final String CONTENT = "Content"; 047 048 public static final String TEXT = "Text"; 049 050 public static final String ID = "Id"; 051 052 public static final String BYTES = "Bytes"; 053 054 public static final String SIZE = "Size"; 055 056 public static final String CANREAD = "CanRead"; 057 058 public static final String CANWRITE = "CanWrite"; 059 060 public static final String ISHIDDEN = "IsHidden"; 061 062 public static final String ISFILE = "IsFile"; 063 064 public static final String ISDIRECTORY = "IsDirectory"; 065 066 public static final String LASTMODIFIED = "LastModified"; 067 068 public static final String FILENAME = "FileName"; 069 070 public static final String CANEXECUTE = "CanExecute"; 071 072 public static final String PATH = "Path"; 073 074 public static final String EXTENSION = "Extension"; 075 076 public static final String FILEFORMAT = "FileFormat"; 077 078 public static final String MD5 = "MD5"; 079 080 private FileMap map = null; 081 082 public FileMatrix(File file, Object... parameters) throws IOException { 083 map = new FileMap(file, parameters); 084 } 085 086 public FileMatrix(FileFormat fileFormat, File file, Object... parameters) throws IOException { 087 map = new FileMap(fileFormat, file, parameters); 088 } 089 090 public Map<String, Object> getMap() { 091 return map; 092 } 093 094 class FileMap implements Map<String, Object>, Serializable { 095 private static final long serialVersionUID = -4946966403241068247L; 096 097 private File file = null; 098 099 private Map<String, Object> map = null; 100 101 private transient SoftReference<Matrix> content = null; 102 103 private transient SoftReference<Matrix> bytes = null; 104 105 private FileFormat fileformat = null; 106 107 private Object[] parameters = null; 108 109 public FileMap(File file, Object... paramegters) throws IOException { 110 this(null, file, paramegters); 111 } 112 113 public FileMap(FileFormat fileFormat, File file, Object... paramegters) throws IOException { 114 if (fileFormat == null) { 115 this.fileformat = FileFormat.guess(file); 116 } else { 117 this.fileformat = fileFormat; 118 } 119 this.parameters = paramegters; 120 this.file = file; 121 this.map = new HashMap<String, Object>(); 122 this.content = new SoftReference<Matrix>(null); 123 map.put(CONTENT, null); 124 map.put(MD5, null); 125 map.put(ID, file.getAbsolutePath()); 126 map.put(PATH, file.getPath()); 127 map.put(FILENAME, file.getName()); 128 map.put(BYTES, null); 129 String[] components = file.getName().split("\\."); 130 if (components.length > 1) { 131 map.put(EXTENSION, components[components.length - 1]); 132 } else { 133 map.put(EXTENSION, null); 134 } 135 map.put(CANREAD, file.canRead()); 136 map.put(CANWRITE, file.canWrite()); 137 map.put(ISHIDDEN, file.isHidden()); 138 map.put(ISDIRECTORY, file.isDirectory()); 139 map.put(ISFILE, file.isFile()); 140 map.put(LASTMODIFIED, file.lastModified()); 141 map.put(SIZE, file.length()); 142 map.put(FILEFORMAT, this.fileformat); 143 } 144 145 public File getFile() { 146 return file; 147 } 148 149 public void clear() { 150 map.clear(); 151 } 152 153 public boolean containsKey(Object key) { 154 return map.containsKey(key); 155 } 156 157 public boolean containsValue(Object value) { 158 return map.containsValue(value); 159 } 160 161 public Set<java.util.Map.Entry<String, Object>> entrySet() { 162 throw new MatrixException("not implemented"); 163 } 164 165 public Object get(Object key) { 166 if (CONTENT.equals(key)) { 167 if (fileformat == null) { 168 return null; 169 } 170 Matrix m = null; 171 if (content == null || content.get() == null) { 172 try { 173 m = MatrixFactory.linkToFile(fileformat, file, parameters); 174 content = new SoftReference<Matrix>(m); 175 } catch (Exception e) { 176 throw new MatrixException(e); 177 } 178 } 179 return content.get(); 180 } else if (MD5.equals(key)) { 181 String md5 = (String) map.get(MD5); 182 if (md5 == null) { 183 try { 184 md5 = MathUtil.md5(file); 185 map.put((String) key, md5); 186 } catch (Exception e) { 187 e.printStackTrace(); 188 } 189 } 190 return md5; 191 } else if (BYTES.equals(key)) { 192 if (bytes == null || bytes.get() == null) { 193 try { 194 bytes = new SoftReference<Matrix>(MatrixFactory.linkToFile(FileFormat.RAW, 195 file)); 196 } catch (Exception e) { 197 e.printStackTrace(); 198 } 199 } 200 return bytes.get(); 201 } 202 return map.get(key); 203 } 204 205 public boolean isEmpty() { 206 return map.isEmpty(); 207 } 208 209 public Set<String> keySet() { 210 return map.keySet(); 211 } 212 213 public Object put(String key, Object value) { 214 return map.put(key, value); 215 } 216 217 public void putAll(Map<? extends String, ? extends Object> m) { 218 map.putAll(m); 219 } 220 221 public Object remove(Object key) { 222 return map.remove(key); 223 } 224 225 public int size() { 226 return map.size(); 227 } 228 229 public Collection<Object> values() { 230 throw new MatrixException("not implemented"); 231 } 232 233 } 234 235 public MapMatrix<String, Object> copy() { 236 try { 237 MapMatrix<String, Object> ret; 238 ret = new FileMatrix(map.getFile()); 239 return ret; 240 } catch (IOException e) { 241 throw new MatrixException(e); 242 } 243 } 244 }