loadFromXml method

void loadFromXml(
  1. String? url,
  2. XmlElement element
)

Initializes all fields from the xml element

Implementation

void loadFromXml(String? url, XmlElement element) {
  this.url = url;
  deviceElement = element;

  final uri = Uri.parse(url!);

  urlBase = XmlUtils.getTextSafe(deviceElement, 'URLBase');

  urlBase ??= uri.toString();

  if (deviceElement.findElements('device').isEmpty) {
    throw Exception('ERROR: Invalid Device XML!\n\n$deviceElement');
  }

  final deviceNode = XmlUtils.getElementByName(deviceElement, 'device');

  deviceType = XmlUtils.getTextSafe(deviceNode, 'deviceType');
  friendlyName = XmlUtils.getTextSafe(deviceNode, 'friendlyName');
  modelName = XmlUtils.getTextSafe(deviceNode, 'modelName');
  manufacturer = XmlUtils.getTextSafe(deviceNode, 'manufacturer');
  udn = XmlUtils.getTextSafe(deviceNode, 'UDN');
  presentationUrl = XmlUtils.getTextSafe(deviceNode, 'presentationURL');
  modelType = XmlUtils.getTextSafe(deviceNode, 'modelType');
  modelDescription = XmlUtils.getTextSafe(deviceNode, 'modelDescription');
  manufacturerUrl = XmlUtils.getTextSafe(deviceNode, 'manufacturerURL');

  if (udn != null) {
    uuid = udn!.substring('uuid:'.length);
  }

  if (deviceNode.findElements('iconList').isNotEmpty) {
    final iconList = deviceNode.findElements('iconList').first;
    for (var child in iconList.children) {
      if (child is XmlElement) {
        final icon = Icon();
        icon.mimetype = XmlUtils.getTextSafe(child, 'mimetype');
        final width = XmlUtils.getTextSafe(child, 'width');
        final height = XmlUtils.getTextSafe(child, 'height');
        final depth = XmlUtils.getTextSafe(child, 'depth');
        final url = XmlUtils.getTextSafe(child, 'url');
        if (width != null) {
          icon.width = int.parse(width);
        }

        if (height != null) {
          icon.height = int.parse(height);
        }

        if (depth != null) {
          icon.depth = int.parse(depth);
        }

        icon.url = url;

        icons.add(icon);
      }
    }
  }

  final Uri baseUri = Uri.parse(urlBase!);

  void processDeviceNode(XmlElement e) {
    if (e.findElements('serviceList').isNotEmpty) {
      final list = e.findElements('serviceList').first;
      for (var svc in list.children) {
        if (svc is XmlElement) {
          services.add(ServiceDescription.fromXml(baseUri, svc));
        }
      }
    }

    if (e.findElements('deviceList').isNotEmpty) {
      final list = e.findElements('deviceList').first;
      for (var dvc in list.children) {
        if (dvc is XmlElement) {
          processDeviceNode(dvc);
        }
      }
    }
  }

  processDeviceNode(deviceNode);
}