📄 upnpdevicedescriptionhandler.java
字号:
package no.auc.one.portableplayer.communication;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public abstract class UPnPDeviceDescriptionHandler extends DefaultHandler {
protected UPnPDevice dev;
protected Vector services;
private Stack tagStack;
private String locationURL = null;
public UPnPDeviceDescriptionHandler() {
}
public UPnPDeviceDescriptionHandler(String url) {
locationURL = url;
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startDocument()
*/
public void startDocument() throws SAXException {
dev = new UPnPDevice();
services = new Vector();
tagStack = new Stack();
}
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) throws SAXException
{
if(qName.equals("service")) {
// Should know if the device description contains an URLBase
// element or not at this point
if(dev.urlBase == null || dev.urlBase == "") {
// Use locationURL as URLBase
dev.urlBase = getHostAndPort(locationURL);
}
services.addElement(new UPnPService());
}
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("friendlyName")) {
dev.friendlyName = chars;
} else if (qName.equals("URLBase")) {
dev.urlBase = chars;
} else if (qName.equals("UDN")) {
dev.deviceUDN = chars;
} else if (qName.equals("serviceType")) {
UPnPService currentService = (UPnPService)services.lastElement();
currentService.setServiceType(chars);
} else if (qName.equals("serviceId")) {
UPnPService currentService = (UPnPService)services.lastElement();
currentService.setServiceId(chars);
} else if (qName.equals("controlURL")) {
UPnPService currentService = (UPnPService)services.lastElement();
currentService.setControlURL(dev.urlBase + chars);
} else if (qName.equals("SCPDURL")) {
UPnPService currentService = (UPnPService)services.lastElement();
currentService.setSCPDURL(dev.urlBase + chars);
} else if (qName.equals("eventSubURL")) {
UPnPService currentService = (UPnPService)services.lastElement();
currentService.setEventSubURL(dev.urlBase + chars);
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
tagStack.pop();
}
public void endDocument() throws SAXException {
if (dev != null) {
dev.setDeviceDescriptionLocation(locationURL);
}
}
public UPnPDevice getDevice() {
return dev;
}
public String getLocationUrl() {
return locationURL;
}
public void setLocationUrl(String url) {
locationURL = url;
}
/**
* Analyze the serverLocationUrls to get only the address and port
* @param url URL to find host and port of.
* @return Returns host and port of an URL
*/
private String getHostAndPort(String url) {
System.out.println("getHostAndPort(" + url + ")");
int ss = 0;
for (int i = 8; i < url.length(); i++) {
ss=url.substring(8).indexOf('/');
}
return url.substring(0, ss + 9);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -