Parse device URLs relative to URL base.

This commit is contained in:
John Beisley 2013-09-28 18:47:08 +01:00
parent b36f8b301e
commit 29edeac666

60
xml.go
View File

@ -13,15 +13,15 @@ const (
) )
type RootDevice struct { type RootDevice struct {
URLBase *url.URL `xml:"-"`
Name xml.Name `xml:"root` Name xml.Name `xml:"root`
SpecVersion SpecVersion `xml:"specVersion"` SpecVersion SpecVersion `xml:"specVersion"`
URLBase url.URL `xml:"-"`
URLBaseStr string `xml:"URLBase"` URLBaseStr string `xml:"URLBase"`
Device *Device `xml:"device"` Device *Device `xml:"device"`
} }
func (root *RootDevice) SetURLBase(urlBase *url.URL) { func (root *RootDevice) SetURLBase(urlBase *url.URL) {
root.URLBase = urlBase root.URLBase = *urlBase
root.URLBaseStr = urlBase.String() root.URLBaseStr = urlBase.String()
root.Device.SetURLBase(urlBase) root.Device.SetURLBase(urlBase)
} }
@ -32,15 +32,14 @@ type SpecVersion struct {
} }
type Device struct { type Device struct {
URLBase *url.URL `xml:"-"`
DeviceType string `xml:"deviceType"` DeviceType string `xml:"deviceType"`
FriendlyName string `xml:"friendlyName"` FriendlyName string `xml:"friendlyName"`
Manufacturer string `xml:"manufacturer"` Manufacturer string `xml:"manufacturer"`
ManufacturerURL string `xml:"manufacturerURL"` ManufacturerURL URLField `xml:"manufacturerURL"`
ModelDescription string `xml:"modelDescription"` ModelDescription string `xml:"modelDescription"`
ModelName string `xml:"modelName"` ModelName string `xml:"modelName"`
ModelNumber string `xml:"modelNumber"` ModelNumber string `xml:"modelNumber"`
ModelURL string `xml:"modelURL"` ModelURL URLField `xml:"modelURL"`
SerialNumber string `xml:"serialNumber"` SerialNumber string `xml:"serialNumber"`
UDN string `xml:"UDN"` UDN string `xml:"UDN"`
UPC string `xml:"UPC,omitempty"` UPC string `xml:"UPC,omitempty"`
@ -49,11 +48,15 @@ type Device struct {
Devices []*Device `xml:"deviceList>device,omitempty"` Devices []*Device `xml:"deviceList>device,omitempty"`
// Extra observed elements: // Extra observed elements:
PresentationURL string `xml:"presentationURL"` PresentationURL URLField `xml:"presentationURL"`
} }
func (device *Device) SetURLBase(urlBase *url.URL) { func (device *Device) SetURLBase(urlBase *url.URL) {
device.URLBase = urlBase device.ManufacturerURL.SetURLBase(urlBase)
device.ModelURL.SetURLBase(urlBase)
for _, icon := range device.Icons {
icon.SetURLBase(urlBase)
}
for _, srv := range device.Services { for _, srv := range device.Services {
srv.SetURLBase(urlBase) srv.SetURLBase(urlBase)
} }
@ -67,26 +70,49 @@ func (device *Device) String() string {
} }
type Icon struct { type Icon struct {
Mimetype string `xml:"mimetype"` Mimetype string `xml:"mimetype"`
Width int32 `xml:"width"` Width int32 `xml:"width"`
Height int32 `xml:"height"` Height int32 `xml:"height"`
Depth int32 `xml:"depth"` Depth int32 `xml:"depth"`
URL string `xml:"url"` URL URLField `xml:"url"`
}
func (icon *Icon) SetURLBase(url *url.URL) {
icon.URL.SetURLBase(url)
} }
type Service struct { type Service struct {
URLBase *url.URL `xml:"-"`
ServiceType string `xml:"serviceType"` ServiceType string `xml:"serviceType"`
ServiceId string `xml:"serviceId"` ServiceId string `xml:"serviceId"`
SCPDURL string `xml:"SCPDURL"` SCPD URLField `xml:"SCPDURL"`
ControlURL string `xml:"controlURL"` Control URLField `xml:"controlURL"`
EventSubURL string `xml:"eventSubURL"` EventSub URLField `xml:"eventSubURL"`
} }
func (srv *Service) SetURLBase(urlBase *url.URL) { func (srv *Service) SetURLBase(urlBase *url.URL) {
srv.URLBase = urlBase srv.SCPD.SetURLBase(urlBase)
srv.Control.SetURLBase(urlBase)
srv.EventSub.SetURLBase(urlBase)
} }
func (srv *Service) String() string { func (srv *Service) String() string {
return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType) return fmt.Sprintf("Service ID %s : %s", srv.ServiceId, srv.ServiceType)
} }
type URLField struct {
URL url.URL `xml:"-"`
Ok bool `xml:"-"`
Str string `xml:",chardata"`
}
func (uf *URLField) SetURLBase(urlBase *url.URL) {
refUrl, err := url.Parse(uf.Str)
if err != nil {
uf.URL = url.URL{}
uf.Ok = false
return
}
uf.URL = *urlBase.ResolveReference(refUrl)
uf.Ok = true
}