📄 contentcontainerhandler.java
字号:
package no.auc.one.portableplayer.communication.mediaserver;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.util.*;
import no.auc.one.portableplayer.librarymanager.*;
public class ContentContainerHandler extends DefaultHandler
implements BrowseResultParser
{
private Vector content = new Vector();
private Stack tagStack = new Stack();
private int totalMatches = 0;
private int numberReturned = 0;
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) throws SAXException
{
if(qName.equals("item")) {
String trackID = attributes.getValue("id");
String parentID = attributes.getValue("parentID");
TrackInformation ti = new TrackInformation();
ti.setObjectID(trackID);
ti.setParentID(parentID);
content.addElement(ti);
} else if (qName.equals("res")) {
if (content.lastElement() instanceof TrackInformation) {
TrackInformation ti = (TrackInformation)content.lastElement();
// XXX ERROR ti.setResourceSize(Long.parseLong(attributes.getValue("size")));
ti.setDuration(attributes.getValue("duration"));
}
} else if(qName.equals("container")) {
String containerID = attributes.getValue("id");
String parentID = attributes.getValue("parentID");
int numberOfElements = Integer.parseInt(
attributes.getValue("childCount"));
ContentContainer cc = new ContentContainer();
cc.setObjectID(containerID);
cc.setParentID(parentID);
// XXX cc.setNumberReturned(numberOfElements);
content.addElement(cc);
}
tagStack.push(qName);
}
public void characters(char[] ch, int start, int length) throws SAXException {
String chars = new String(ch, start, length).trim();
if(chars.length() > 0) {
String qName = (String)tagStack.peek();
if (qName.equals("dc:title")) {
MediaContent current = (MediaContent)content.lastElement();
current.setName(chars);
} else if (qName.equals("res")) {
MediaContent current = (MediaContent)content.lastElement();
current.setResourceUri(chars);
} else if (content.lastElement() instanceof TrackInformation) {
TrackInformation current = (TrackInformation)content.lastElement();
if (qName.equals("upnp:artist")){
current.setArtist(chars);
} else if (qName.equals("upnp:album")){
current.setAlbum(chars);
} else if (qName.equals("upnp:genre")){
current.setGenre(chars);
} else if (qName.equals("upnp:genre")){
current.setGenre(chars);
}
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
tagStack.pop();
}
public MediaContent[] getMediaContent(){
MediaContent[] mc = new MediaContent[content.size()];
content.copyInto(mc);
return mc;
}
public void setNumberReturned(int number) {
numberReturned = number;
}
public int getNumberReturned() {
return numberReturned;
}
public void setTotalMatches(int number) {
totalMatches = number;
}
public int getTotalMatches() {
return totalMatches;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -