KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > structure > ResourceDefinition


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.structure;
31
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35 import java.text.ParseException JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.Date JavaDoc;
38
39 import com.genimen.djeneric.util.DjBase64;
40 import com.genimen.djeneric.util.DjStringReplacer;
41
42 public class ResourceDefinition
43 {
44   String JavaDoc _type = null;
45   byte[] _bytes = new byte[0];
46   String JavaDoc _path;
47   String JavaDoc _name;
48   String JavaDoc _timeStamp;
49
50   public static String JavaDoc TIMESTAMP_FORMAT_MASK = "dd-MM-yyyy HH:mm:ss";
51
52   boolean _isDeleted = false;
53
54   public ResourceDefinition()
55   {
56     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(TIMESTAMP_FORMAT_MASK);
57     _timeStamp = sdf.format(new Date JavaDoc());
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 JavaDoc getBase64() throws UnsupportedEncodingException JavaDoc
70   {
71     StringBuffer JavaDoc result = new StringBuffer JavaDoc(10000);
72
73     String JavaDoc encoded = DjBase64.encode(getBytes());
74
75     // Sanity check: make sure that the encoded string is multiline.
76
// If it is not; make sure it is.
77
if (encoded.trim().indexOf("\n") != -1) return encoded;
78
79     // We have to break it up ourselves
80
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 JavaDoc base64) throws UnsupportedEncodingException JavaDoc
95   {
96     _bytes = DjBase64.decode(base64);
97   }
98
99   public byte[] getBytes()
100   {
101     return _bytes;
102   }
103
104   public String JavaDoc getName()
105   {
106     return _name;
107   }
108
109   public String JavaDoc getPath()
110   {
111     return _path;
112   }
113
114   public String JavaDoc getType()
115   {
116     return _type;
117   }
118
119   public void setBytes(byte[] bs)
120   {
121     _bytes = bs;
122   }
123
124   public void setName(String JavaDoc string)
125   {
126     _name = string;
127   }
128
129   public void setPath(String JavaDoc 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 JavaDoc string)
139   {
140     _type = string;
141   }
142
143   public void setAbsolutePath(String JavaDoc filename)
144   {
145
146     String JavaDoc ext = "";
147     String JavaDoc name = filename;
148     String JavaDoc 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 JavaDoc getAbsolutePath()
175   {
176     return getPath() + getName() + "." + getType();
177   }
178
179   public String JavaDoc 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 JavaDoc getTimeStamp()
197   {
198     return _timeStamp;
199   }
200
201   public long getTime() throws ParseException JavaDoc
202   {
203     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(TIMESTAMP_FORMAT_MASK);
204     return sdf.parse(getTimeStamp()).getTime();
205   }
206
207   public void setTimeStamp(String JavaDoc 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 JavaDoc asStream()
219   {
220     return new ByteArrayInputStream JavaDoc(getBytes());
221   }
222
223   public boolean isJar()
224   {
225     return "jar".equalsIgnoreCase(getType()) || "zip".equalsIgnoreCase(getType());
226   }
227 }
Popular Tags