1 30 package com.genimen.djeneric.structure; 31 32 import java.io.ByteArrayInputStream ; 33 import java.io.InputStream ; 34 import java.io.UnsupportedEncodingException ; 35 import java.text.ParseException ; 36 import java.text.SimpleDateFormat ; 37 import java.util.Date ; 38 39 import com.genimen.djeneric.util.DjBase64; 40 import com.genimen.djeneric.util.DjStringReplacer; 41 42 public class ResourceDefinition 43 { 44 String _type = null; 45 byte[] _bytes = new byte[0]; 46 String _path; 47 String _name; 48 String _timeStamp; 49 50 public static String TIMESTAMP_FORMAT_MASK = "dd-MM-yyyy HH:mm:ss"; 51 52 boolean _isDeleted = false; 53 54 public ResourceDefinition() 55 { 56 SimpleDateFormat sdf = new SimpleDateFormat (TIMESTAMP_FORMAT_MASK); 57 _timeStamp = sdf.format(new Date ()); 58 } 59 60 public void copyFrom(ResourceDefinition def) 61 { 62 _type = def._type; 63 _bytes = def._bytes; 64 _path = def._path; 65 _name = def._name; 66 _timeStamp = def._timeStamp; 67 } 68 69 public String getBase64() throws UnsupportedEncodingException 70 { 71 StringBuffer result = new StringBuffer (10000); 72 73 String encoded = DjBase64.encode(getBytes()); 74 75 if (encoded.trim().indexOf("\n") != -1) return encoded; 78 79 int lineLength = 80; 81 82 int idx = 0; 83 while (idx < encoded.length()) 84 { 85 int length = lineLength; 86 if (idx + length > encoded.length()) length = encoded.length() - idx; 87 result.append(encoded.subSequence(idx, idx + length)); 88 result.append("\n"); 89 idx += length; 90 } 91 return result.toString(); 92 } 93 94 public void setBase64(String base64) throws UnsupportedEncodingException 95 { 96 _bytes = DjBase64.decode(base64); 97 } 98 99 public byte[] getBytes() 100 { 101 return _bytes; 102 } 103 104 public String getName() 105 { 106 return _name; 107 } 108 109 public String getPath() 110 { 111 return _path; 112 } 113 114 public String getType() 115 { 116 return _type; 117 } 118 119 public void setBytes(byte[] bs) 120 { 121 _bytes = bs; 122 } 123 124 public void setName(String string) 125 { 126 _name = string; 127 } 128 129 public void setPath(String path) 130 { 131 DjStringReplacer sr = new DjStringReplacer(path); 132 sr.replace("\\", "/"); 133 _path = sr.toString(); 134 if (!_path.endsWith("/")) _path += "/"; 135 if (!_path.startsWith("/")) _path = "/" + _path; 136 } 137 138 public void setType(String string) 139 { 140 _type = string; 141 } 142 143 public void setAbsolutePath(String filename) 144 { 145 146 String ext = ""; 147 String name = filename; 148 String path = ""; 149 150 int dotIdx = filename.lastIndexOf("."); 151 if (dotIdx != -1) 152 { 153 ext = filename.substring(dotIdx + 1); 154 filename = filename.substring(0, dotIdx); 155 } 156 157 int slashIdx1 = filename.lastIndexOf("/"); 158 int slashIdx2 = filename.lastIndexOf("\\"); 159 160 if (slashIdx1 == -1) slashIdx1 = slashIdx2; 161 slashIdx1 = Math.max(slashIdx1, slashIdx2); 162 163 if (slashIdx1 != -1) 164 { 165 name = filename.substring(slashIdx1 + 1); 166 path = filename.substring(0, slashIdx1); 167 } 168 169 setType(ext); 170 setPath(path); 171 setName(name); 172 } 173 174 public String getAbsolutePath() 175 { 176 return getPath() + getName() + "." + getType(); 177 } 178 179 public String toString() 180 { 181 if (_type == null) return _name; 182 183 return getAbsolutePath(); 184 } 185 186 public void markDeleted(boolean b) 187 { 188 _isDeleted = b; 189 } 190 191 public boolean isMarkedForDelete() 192 { 193 return _isDeleted; 194 } 195 196 public String getTimeStamp() 197 { 198 return _timeStamp; 199 } 200 201 public long getTime() throws ParseException 202 { 203 SimpleDateFormat sdf = new SimpleDateFormat (TIMESTAMP_FORMAT_MASK); 204 return sdf.parse(getTimeStamp()).getTime(); 205 } 206 207 public void setTimeStamp(String timestamp) 208 { 209 if (timestamp != null && timestamp.trim().length() > 0) _timeStamp = timestamp; 210 } 211 212 public boolean isImageIcon() 213 { 214 return "gif".equalsIgnoreCase(getType()) || "png".equalsIgnoreCase(getType()) || "jpg".equalsIgnoreCase(getType()) 215 || "jpeg".equalsIgnoreCase(getType()); 216 } 217 218 public InputStream asStream() 219 { 220 return new ByteArrayInputStream (getBytes()); 221 } 222 223 public boolean isJar() 224 { 225 return "jar".equalsIgnoreCase(getType()) || "zip".equalsIgnoreCase(getType()); 226 } 227 } | Popular Tags |