1 |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of jlscp. |
7 |
* |
8 |
* jlscp is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* jlscp is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with jlscp; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.linuxsampler.lscp; |
24 |
|
25 |
import java.text.DateFormat; |
26 |
import java.text.NumberFormat; |
27 |
import java.text.ParseException; |
28 |
import java.text.SimpleDateFormat; |
29 |
|
30 |
import java.util.Date; |
31 |
|
32 |
import static org.linuxsampler.lscp.Parser.*; |
33 |
|
34 |
|
35 |
/** |
36 |
* Provides information about a database instrument. |
37 |
* @author Grigor Iliev |
38 |
*/ |
39 |
public class DbInstrumentInfo extends AbstractInstrument implements Parseable { |
40 |
private String directoryPath = null; |
41 |
private final Date dateCreated = new EnhancedDate(); |
42 |
private final Date dateModified = new EnhancedDate(); |
43 |
private String formatFamily = ""; |
44 |
private String formatVersion = ""; |
45 |
private long size = 0; |
46 |
private boolean drum = false; |
47 |
private String product = ""; |
48 |
private String artists = ""; |
49 |
private String keywords = ""; |
50 |
private boolean showAbsolutePath = false; |
51 |
|
52 |
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
53 |
private static DateFormat dateFormat2 = DateFormat.getInstance(); |
54 |
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
55 |
|
56 |
static { |
57 |
numberFormat.setMinimumFractionDigits(0); |
58 |
numberFormat.setMaximumFractionDigits(1); |
59 |
} |
60 |
|
61 |
/** |
62 |
* Creates a new instance of <code>DbInstrumentInfo</code>. |
63 |
* @param resultSet An array with information categories about a DB instrument. |
64 |
*/ |
65 |
public |
66 |
DbInstrumentInfo(String[] resultSet) throws LscpException { |
67 |
for(String s : resultSet) |
68 |
if(!parse(s)) Client.getLogger().info(LscpI18n.getLogMsg("unknownLine", s)); |
69 |
} |
70 |
|
71 |
/** |
72 |
* Returns the absolute path name of the directory containing this instrument. |
73 |
**/ |
74 |
public String |
75 |
getDirectoryPath() { return directoryPath; } |
76 |
|
77 |
/** |
78 |
* Sets the absolute path name of the directory containing this instrument. |
79 |
**/ |
80 |
public void |
81 |
setDirectoryPath(String dir) { directoryPath = dir; } |
82 |
|
83 |
/** |
84 |
* Returns the absolute path name of this instrument or |
85 |
* <code>null</code> if the directory path for this instrument is not set. |
86 |
**/ |
87 |
public String |
88 |
getInstrumentPath() { |
89 |
if(getDirectoryPath() == null) return null; |
90 |
if(getDirectoryPath().length() == 1) { |
91 |
if(!getDirectoryPath().equals("/")) return null; |
92 |
return getDirectoryPath() + toEscapedFileName(getName()); |
93 |
} |
94 |
|
95 |
return getDirectoryPath() + "/" + toEscapedFileName(getName()); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Returns the date when the instrument is created. |
100 |
**/ |
101 |
public Date |
102 |
getDateCreated() { return dateCreated; } |
103 |
|
104 |
/** |
105 |
* Returns the date when the instrument is last modified. |
106 |
**/ |
107 |
public Date |
108 |
getDateModified() { return dateModified; } |
109 |
|
110 |
/** |
111 |
* Returns the format family of the instrument. |
112 |
**/ |
113 |
public String |
114 |
getFormatFamily() { return formatFamily; } |
115 |
|
116 |
/** |
117 |
* Returns the format version of the instrument. |
118 |
**/ |
119 |
public String |
120 |
getFormatVersion() { return formatVersion; } |
121 |
|
122 |
/** |
123 |
* Returns the size of the instrument in bytes. |
124 |
**/ |
125 |
public long |
126 |
getSize() { return size; } |
127 |
|
128 |
/** |
129 |
* Gets a user friendly representation of the instruments size. |
130 |
*/ |
131 |
public String |
132 |
getFormatedSize() { |
133 |
String s; |
134 |
long i = getSize(); |
135 |
if(i > 1024*1024*1024) { |
136 |
double d = i; |
137 |
d /= (1024*1024*1024); |
138 |
s = numberFormat.format(d) + " GB"; |
139 |
} else if(i > 1024*1024) { |
140 |
double d = i; |
141 |
d /= (1024*1024); |
142 |
s = numberFormat.format(d) + " MB"; |
143 |
} else if(i > 1024) { |
144 |
double d = i; |
145 |
d /= (1024); |
146 |
s = numberFormat.format(d) + " KB"; |
147 |
} else { |
148 |
s = numberFormat.format(i) + " bytes"; |
149 |
} |
150 |
|
151 |
return s; |
152 |
} |
153 |
|
154 |
/** |
155 |
* Determines whether the instrument is a drumkit or a chromatic instrument. |
156 |
**/ |
157 |
public boolean |
158 |
isDrum() { return drum; } |
159 |
|
160 |
/** |
161 |
* Returns the product title of the instrument. |
162 |
**/ |
163 |
public String |
164 |
getProduct() { return product; } |
165 |
|
166 |
/** |
167 |
* Lists the artist names. |
168 |
**/ |
169 |
public String |
170 |
getArtists() { return artists; } |
171 |
|
172 |
/** |
173 |
* Provides a list of keywords that refer to the instrument. |
174 |
* Keywords are separated with semicolon and blank. |
175 |
**/ |
176 |
public String |
177 |
getKeywords() { return keywords; } |
178 |
|
179 |
/** |
180 |
* Parses a line of text. |
181 |
* @param s The string to be parsed. |
182 |
* @return <code>true</code> if the line has been processed, <code>false</code> otherwise. |
183 |
* @throws LscpException If some error occurs. |
184 |
*/ |
185 |
public boolean |
186 |
parse(String s) throws LscpException { |
187 |
if (super.parse(s)) return true; |
188 |
|
189 |
if(s.startsWith("CREATED: ")) { |
190 |
s = s.substring("CREATED: ".length()); |
191 |
try { dateCreated.setTime(dateFormat.parse(s).getTime()); } |
192 |
catch(ParseException e) { throw new LscpException(e.getMessage()); } |
193 |
} else if(s.startsWith("MODIFIED: ")) { |
194 |
s = s.substring("MODIFIED: ".length()); |
195 |
try { dateModified.setTime(dateFormat.parse(s).getTime()); } |
196 |
catch(ParseException e) { throw new LscpException(e.getMessage()); } |
197 |
} else if(s.startsWith("FORMAT_FAMILY: ")) { |
198 |
formatFamily = s.substring("FORMAT_FAMILY: ".length()); |
199 |
} else if(s.startsWith("FORMAT_VERSION: ")) { |
200 |
formatVersion = s.substring("FORMAT_VERSION: ".length()); |
201 |
} else if(s.startsWith("SIZE: ")) { |
202 |
try { size = Long.parseLong(s.substring("SIZE: ".length())); } |
203 |
catch(NumberFormatException x) { |
204 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
205 |
} |
206 |
} else if(s.startsWith("IS_DRUM: ")) { |
207 |
drum = Boolean.parseBoolean(s.substring("IS_DRUM: ".length())); |
208 |
} else if(s.startsWith("PRODUCT: ")) { |
209 |
product = s.substring("PRODUCT: ".length()); |
210 |
product = toNonEscapedText(product); |
211 |
} else if(s.startsWith("ARTISTS: ")) { |
212 |
artists = s.substring("ARTISTS: ".length()); |
213 |
artists = toNonEscapedText(artists); |
214 |
} else if(s.startsWith("KEYWORDS: ")) { |
215 |
keywords = s.substring("KEYWORDS: ".length()); |
216 |
keywords = toNonEscapedText(keywords); |
217 |
} else return false; |
218 |
|
219 |
return true; |
220 |
} |
221 |
|
222 |
/** |
223 |
* Determines whether the <code>toString()</code> |
224 |
* method should return the instrument name or |
225 |
* the absolute path name of the instrument. |
226 |
* The default value is <code>false</code>. |
227 |
*/ |
228 |
public boolean |
229 |
getShowAbsolutePath() { return showAbsolutePath; } |
230 |
|
231 |
/** |
232 |
* Sets whether the <code>toString()</code> method |
233 |
* should return the absolute path name of the instrument. |
234 |
* @param b If <code>true</code> the <code>toString()</code> |
235 |
* method will return the absolute path name of the |
236 |
* instrument instead of the instrument name. |
237 |
*/ |
238 |
public void |
239 |
setShowAbsolutePath(boolean b) { showAbsolutePath = b; } |
240 |
|
241 |
/** |
242 |
* Returns the name or the absolute path name of |
243 |
* the instrument as specified by {@link #getShowAbsolutePath}. |
244 |
* @see #setShowAbsolutePath |
245 |
*/ |
246 |
public String |
247 |
toString() { |
248 |
if(getShowAbsolutePath()) return getInstrumentPath(); |
249 |
return getName(); |
250 |
} |
251 |
|
252 |
private class EnhancedDate extends Date { |
253 |
public String |
254 |
toString() { return dateFormat2.format(this); } |
255 |
} |
256 |
} |