1 |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
4 |
* Copyright (C) 2005-2010 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 long size = 0; |
44 |
private boolean drum = false; |
45 |
private String product = ""; |
46 |
private String artists = ""; |
47 |
private String keywords = ""; |
48 |
private boolean showAbsolutePath = false; |
49 |
|
50 |
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
51 |
private static DateFormat dateFormat2 = DateFormat.getInstance(); |
52 |
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
53 |
|
54 |
static { |
55 |
numberFormat.setMinimumFractionDigits(0); |
56 |
numberFormat.setMaximumFractionDigits(1); |
57 |
} |
58 |
|
59 |
/** Creates a new instance of <code>DbInstrumentInfo</code>. */ |
60 |
public |
61 |
DbInstrumentInfo() { } |
62 |
|
63 |
/** |
64 |
* Creates a new instance of <code>DbInstrumentInfo</code>. |
65 |
* @param resultSet An array with information categories about a DB instrument. |
66 |
*/ |
67 |
public |
68 |
DbInstrumentInfo(String[] resultSet) throws LscpException { |
69 |
for(String s : resultSet) |
70 |
if(!parse(s)) Client.getLogger().info(LscpI18n.getLogMsg("unknownLine", s)); |
71 |
} |
72 |
|
73 |
/** |
74 |
* Returns the absolute path name of the directory containing this instrument. |
75 |
**/ |
76 |
public String |
77 |
getDirectoryPath() { return directoryPath; } |
78 |
|
79 |
/** |
80 |
* Sets the absolute path name of the directory containing this instrument. |
81 |
**/ |
82 |
public void |
83 |
setDirectoryPath(String dir) { directoryPath = dir; } |
84 |
|
85 |
/** |
86 |
* Returns the absolute path name of this instrument or |
87 |
* <code>null</code> if the directory path for this instrument is not set. |
88 |
**/ |
89 |
public String |
90 |
getInstrumentPath() { |
91 |
if(getDirectoryPath() == null) return null; |
92 |
if(getDirectoryPath().length() == 1) { |
93 |
if(!getDirectoryPath().equals("/")) return null; |
94 |
return getDirectoryPath() + toEscapedFileName(getName()); |
95 |
} |
96 |
|
97 |
return getDirectoryPath() + "/" + toEscapedFileName(getName()); |
98 |
} |
99 |
|
100 |
/** |
101 |
* Returns the date when the instrument is created. |
102 |
**/ |
103 |
public Date |
104 |
getDateCreated() { return dateCreated; } |
105 |
|
106 |
/** |
107 |
* Returns the date when the instrument is last modified. |
108 |
**/ |
109 |
public Date |
110 |
getDateModified() { return dateModified; } |
111 |
|
112 |
/** |
113 |
* Returns the size of the instrument in bytes. |
114 |
**/ |
115 |
public long |
116 |
getSize() { return size; } |
117 |
|
118 |
/** |
119 |
* Gets a user friendly representation of the instruments size. |
120 |
*/ |
121 |
public String |
122 |
getFormatedSize() { |
123 |
String s; |
124 |
long i = getSize(); |
125 |
if(i > 1024*1024*1024) { |
126 |
double d = i; |
127 |
d /= (1024*1024*1024); |
128 |
s = numberFormat.format(d) + " GB"; |
129 |
} else if(i > 1024*1024) { |
130 |
double d = i; |
131 |
d /= (1024*1024); |
132 |
s = numberFormat.format(d) + " MB"; |
133 |
} else if(i > 1024) { |
134 |
double d = i; |
135 |
d /= (1024); |
136 |
s = numberFormat.format(d) + " KB"; |
137 |
} else { |
138 |
s = numberFormat.format(i) + " bytes"; |
139 |
} |
140 |
|
141 |
return s; |
142 |
} |
143 |
|
144 |
/** |
145 |
* Determines whether the instrument is a drumkit or a chromatic instrument. |
146 |
**/ |
147 |
public boolean |
148 |
isDrum() { return drum; } |
149 |
|
150 |
/** |
151 |
* Returns the product title of the instrument. |
152 |
**/ |
153 |
public String |
154 |
getProduct() { return product; } |
155 |
|
156 |
/** |
157 |
* Lists the artist names. |
158 |
**/ |
159 |
public String |
160 |
getArtists() { return artists; } |
161 |
|
162 |
/** |
163 |
* Provides a list of keywords that refer to the instrument. |
164 |
* Keywords are separated with semicolon and blank. |
165 |
**/ |
166 |
public String |
167 |
getKeywords() { return keywords; } |
168 |
|
169 |
public String |
170 |
getEngine() { |
171 |
// TODO: engine lookup? |
172 |
return getFormatFamily(); |
173 |
} |
174 |
|
175 |
/** |
176 |
* Parses a line of text. |
177 |
* @param s The string to be parsed. |
178 |
* @return <code>true</code> if the line has been processed, <code>false</code> otherwise. |
179 |
* @throws LscpException If some error occurs. |
180 |
*/ |
181 |
public boolean |
182 |
parse(String s) throws LscpException { |
183 |
if (super.parse(s)) return true; |
184 |
|
185 |
if(s.startsWith("CREATED: ")) { |
186 |
s = s.substring("CREATED: ".length()); |
187 |
try { dateCreated.setTime(dateFormat.parse(s).getTime()); } |
188 |
catch(ParseException e) { throw new LscpException(e.getMessage()); } |
189 |
} else if(s.startsWith("MODIFIED: ")) { |
190 |
s = s.substring("MODIFIED: ".length()); |
191 |
try { dateModified.setTime(dateFormat.parse(s).getTime()); } |
192 |
catch(ParseException e) { throw new LscpException(e.getMessage()); } |
193 |
} else if(s.startsWith("SIZE: ")) { |
194 |
try { size = Long.parseLong(s.substring("SIZE: ".length())); } |
195 |
catch(NumberFormatException x) { |
196 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
197 |
} |
198 |
} else if(s.startsWith("IS_DRUM: ")) { |
199 |
drum = Boolean.parseBoolean(s.substring("IS_DRUM: ".length())); |
200 |
} else if(s.startsWith("PRODUCT: ")) { |
201 |
product = s.substring("PRODUCT: ".length()); |
202 |
product = toNonEscapedString(product); |
203 |
} else if(s.startsWith("ARTISTS: ")) { |
204 |
artists = s.substring("ARTISTS: ".length()); |
205 |
artists = toNonEscapedString(artists); |
206 |
} else if(s.startsWith("KEYWORDS: ")) { |
207 |
keywords = s.substring("KEYWORDS: ".length()); |
208 |
keywords = toNonEscapedString(keywords); |
209 |
} else return false; |
210 |
|
211 |
return true; |
212 |
} |
213 |
|
214 |
/** |
215 |
* Determines whether the <code>toString()</code> |
216 |
* method should return the instrument name or |
217 |
* the absolute path name of the instrument. |
218 |
* The default value is <code>false</code>. |
219 |
*/ |
220 |
public boolean |
221 |
getShowAbsolutePath() { return showAbsolutePath; } |
222 |
|
223 |
/** |
224 |
* Sets whether the <code>toString()</code> method |
225 |
* should return the absolute path name of the instrument. |
226 |
* @param b If <code>true</code> the <code>toString()</code> |
227 |
* method will return the absolute path name of the |
228 |
* instrument instead of the instrument name. |
229 |
*/ |
230 |
public void |
231 |
setShowAbsolutePath(boolean b) { showAbsolutePath = b; } |
232 |
|
233 |
/** |
234 |
* Returns the name or the absolute path name of |
235 |
* the instrument as specified by {@link #getShowAbsolutePath}. |
236 |
* @see #setShowAbsolutePath |
237 |
*/ |
238 |
public String |
239 |
toString() { |
240 |
if(getShowAbsolutePath()) return getInstrumentPath(); |
241 |
return getName(); |
242 |
} |
243 |
|
244 |
private class EnhancedDate extends Date { |
245 |
public String |
246 |
toString() { return dateFormat2.format(this); } |
247 |
} |
248 |
} |