XmlName.parse constructor

XmlName.parse(
  1. String name, {
  2. String? namespaceUri,
  3. Map<String, String>? namespaceUris,
})

Creates an XmlName from a parsed string.

This code also parsed extended qualified names such as Q{uri}local or Q{uri}prefix:local. If the name is not in the extended qualified form, the function tries to resolve the namespace URI from the given prefix-to-uri namespaceUris map. If the namespace URI is still not defined, namespaceUri is used instead (null by default).

Throws a XmlParserException if the name is in an invalid extended form.

Implementation

factory XmlName.parse(
  String name, {
  String? namespaceUri,
  Map<String, String>? namespaceUris,
}) {
  String? uri;
  // Handle the extended qualified name first.
  if (name.startsWith(XmlToken.openQualifiedUrl)) {
    final end = name.indexOf(XmlToken.closeQualifiedUrl);
    if (end == -1) {
      throw XmlParserException('Invalid extended qualified name: $name');
    } else if (end > XmlToken.openQualifiedUrl.length) {
      uri = name.substring(XmlToken.openQualifiedUrl.length, end);
    }
    name = name.substring(end + XmlToken.closeQualifiedUrl.length);
  }
  // Handle the prefix name lookup.
  if (uri == null && namespaceUris != null) {
    final index = name.indexOf(XmlToken.namespace);
    if (index > 0) {
      final prefix = name.substring(0, index);
      uri = namespaceUris[prefix];
    }
  }
  // Handle the default namespace.
  return XmlName.qualified(name, namespaceUri: uri ?? namespaceUri);
}