/[svn]/jlscp/trunk/src/org/linuxsampler/lscp/DbInstrumentInfo.java
ViewVC logotype

Contents of /jlscp/trunk/src/org/linuxsampler/lscp/DbInstrumentInfo.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1539 - (show annotations) (download)
Mon Dec 3 22:59:39 2007 UTC (16 years, 4 months ago) by iliev
File size: 6997 byte(s)
* Added new interface - Instrument
* Client: added new methods - getFileInstrumentCount,
  getFileInstrumentInfo,  getFileInstruments

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

  ViewVC Help
Powered by ViewVC