add_stream_attributes Subroutine

private pure subroutine add_stream_attributes(self, attributes_stream, sanitize_values)

Arguments

TypeIntentOptionalAttributesName
class(xml_tag), intent(inout) :: self
character, intent(in) :: attributes_stream
logical, intent(in), optional :: sanitize_values

Contents

Source Code


Source Code

  pure subroutine add_stream_attributes(self, attributes_stream, sanitize_values)
  !< Add list of attributes name/value pairs passed as stream.
  !<
  !< @note The character `=` cannot compare into the attributes names of values.
  class(xml_tag), intent(inout)        :: self              !< XML tag.
  character(*),   intent(in)           :: attributes_stream !< Attribute name/value pair list passed as stream.
  logical,        intent(in), optional :: sanitize_values   !< Sanitize attribute values.
  type(string)                         :: attributes_string !< Attribute name/value pair list as string.
  type(string)                         :: tokens(1:3)       !< Attributes tokenized by `=`.
  type(string)                         :: attribute(1:2)    !< Attribute name/value pair.
  logical                              :: continue_to_parse !< Sentinel to stop attributes stream parsing.
  integer(I4P)                         :: max_chars         !< Counter.

  attributes_string = attributes_stream
  continue_to_parse = .true.
  do while(continue_to_parse)
    tokens = attributes_string%partition(sep='=')
    attribute(1) = trim(adjustl(tokens(1)))
    if (attribute(1)/='') then
      tokens(3) = tokens(3)%slice(istart=tokens(3)%index('"')+1, iend=tokens(3)%len())
      attribute(2) = tokens(3)%slice(istart=1, iend=tokens(3)%index('"')-1)
      tokens(3) = tokens(3)%slice(istart=tokens(3)%index('"')+1, iend=tokens(3)%len())
      max_chars = max(attribute(1)%len(), attribute(2)%len())
      attribute(1) = attribute(1)%fill(width=max_chars, right=.true., filling_char=' ')
      attribute(2) = attribute(2)%fill(width=max_chars, right=.true., filling_char=' ')
      call self%add_single_attribute(attribute=[attribute(1)//'', attribute(2)//''], sanitize_value=sanitize_values)
      if (tokens(3)%index('=')>0) then
        attributes_string = tokens(3)
      else
        continue_to_parse = .false.
      endif
    else
      continue_to_parse = .false.
    endif
  enddo
  endsubroutine add_stream_attributes