📄 musicprofile.java
字号:
package edu.uiuc.cs.cs327.linuxwifi.services;
import java.util.Vector.*;
import java.util.Vector;
import java.awt.*;
import de.ueberdosis.mp3info.*;
import de.ueberdosis.util.*;
import java.util.Vector.*;
import java.util.Vector;
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.TextImpl;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.DOMImplementationImpl;
import org.w3c.dom.Document;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;
/**
* Constructs a configuration information class that holds all
* parameters relating to the users' preferences.
*/
public class MusicProfile implements Serializable{
private static final boolean DEBUG = false;
// File downloading restriction preferences
private int maxFileSize = 0;
private String artist = null;
private String song = null;
private String genre = null;
private String fileFormat = null;
private id3 myFile = null;
public MusicProfile() {
// Read all configuration information from a stored file and populate the above fields
}
/* Read all configuration information from a stored file and populate the above fields
* @param fileURL path to mp3 file
*/
public MusicProfile(String fileURL) {
try{
myFile = new id3(fileURL);
}catch (Exception e) {
// could not instanciate the group, print the stack and exit
System.out.println("fatal error : can not open mp3 file");
e.printStackTrace();
System.exit(-1);
}
song = myFile.getTitle().toLowerCase();
artist = myFile.getArtist().toLowerCase();
genre = myFile.getGenre().toLowerCase();
}
public String Display() {
String str;
String sep = " | ";
str = artist + sep + song + sep + genre + sep + fileFormat + sep + Integer.toString(maxFileSize/1024) + "k";
return str;
}
/* User inputs all configuration information for search purposes
* @param
*/
public MusicProfile(String aArtist, String aSong, String aGenre) {
setArtist(aArtist);
setSong(aSong);
setGenre(aGenre);
}
/**
* Gets a string String containing all the users' preferred file formats.
* @return a string String of fileformats (specifying filename extensions) such as:
* mp3 or wav
*/
public String getFileFormat()
{
return(fileFormat);
}
/**
* Gets the maximum file size of the files to be downloaded
* @return an integer representing the file size in bytes
*/
public int getMaxFileSize()
{
return(maxFileSize);
}
/**
* Gets a string String containing all the users' preferred file artists.
* @return a string String of artist names such as:
* "Jay Z" and "jennifer lopez"
*/
public String getArtist()
{
return(artist);
}
/**
* Gets a string String containing all the users' preferred songs.
* @return a string String of song names such as:
* "I do" and "U Got It Bad"
*/
public String getSong()
{
return(song);
}
/**
* Gets a string String containing all the users' preferred genre's.
* @return a string String of genre names
* "pop" and "rock"
*/
public String getGenre()
{
return(genre);
}
/**
* Sets a string String containing all the users' preferred file formats.
* @param a string of fileformat (specifying filename extensions) such as:
* mp3 or wav
*/
public void setFileFormat(String a)
{
fileFormat = a;
}
/**
* Sets the maximum file size of the files to be downloaded
* @param an integer representing the file size in bytes
*/
public void setMaxFileSize(int a)
{
maxFileSize=a;
}
public void setMaxFileSize(String a)
{
int nSize = 0;
try
{
nSize = Integer.parseInt(a);
}
catch (NumberFormatException ex) {
;
}
maxFileSize= nSize;
}
/**
* Sets a string String containing all the users' preferred file artists.
* @param a string String of artist names such as:
* "Jay Z" and "jennifer lopez"
*/
public void setArtist(String a)
{
artist = a;
}
/**
* Sets a string String containing all the users' preferred songs.
* @param a string String of song names such as:
* "I do" and "U Got It Bad"
*/
public void setSong(String a)
{
song = a;
}
/**
* Sets a string String containing all the users' preferred genre's.
* @param a string String of genre names
* "pop" and "rock"
*/
public void setGenre(String a)
{
genre =a;
}
/**
* Sets the maximum uploads per hour allowed by this user
* @param an integer representing the number of uploads in one hour
*/
public Element xmlNode(Document doc)
{
try {
int i;
Element root = doc.createElement("musicprofile");
Element elem = null;
elem = doc.createElement("maxfilesize");
elem.appendChild(doc.createTextNode(Integer.toString(maxFileSize)));
root.appendChild(elem);
elem = doc.createElement("artist");
elem.appendChild(doc.createTextNode(artist));
root.appendChild(elem);
elem = doc.createElement("song");
elem.appendChild(doc.createTextNode(song));
root.appendChild(elem);
elem = doc.createElement("genre");
elem.appendChild(doc.createTextNode(genre));
root.appendChild(elem);
elem = doc.createElement("fileformat");
elem.appendChild(doc.createTextNode(fileFormat));
root.appendChild(elem);
return root;
}
catch(Exception ex) {
DebugOut("ERROR: creating XML");
ex.printStackTrace();
}
return null;
}
public void setFromXmlNode(Element root) {
setSong(getElementValue(root, "song"));
setArtist(getElementValue(root, "artist"));
setGenre(getElementValue(root, "genre"));
setFileFormat(getElementValue(root, "fileformat"));
setMaxFileSize(getElementValue(root, "maxfilesize"));
}
private String getElementValue(Element root, String sTag) {
String sVal = "";
NodeList elements = root.getElementsByTagName(sTag);
if (elements != null) {
if (elements.getLength() > 0) {
Element element = (Element)elements.item(0);
Node textNode = element.getFirstChild();
if (textNode != null)
sVal = textNode.getNodeValue();
}
}
return sVal;
}
private void DebugOut(String sStr)
{
if (DEBUG)
System.out.println(sStr);
}
/**
* Sets the maximum uploads per hour allowed by this user
* @param an integer representing the number of uploads in one hour
*/
/**Copies one MusicProfile object to another
*@param a MusicProfile object
*/
public void copy(MusicProfile mp){
artist = mp.getArtist();
song = mp.getSong();
genre = mp.getGenre();
}
public void print(){
System.out.println("Song: " + song);
System.out.println("Artist: " + artist);
System.out.println("Genre: " + genre);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -