Function: soap-encode-xs-basic-type

soap-encode-xs-basic-type is a byte-compiled function defined in soap-client.el.gz.

Signature

(soap-encode-xs-basic-type VALUE TYPE)

Documentation

Encode the VALUE according to TYPE.

The data is inserted in the current buffer at the current position.

This is a specialization of soap-encode-value for soap-xs-basic-type objects.

Source Code

;; Defined in /usr/src/emacs/lisp/net/soap-client.el.gz
(defun soap-encode-xs-basic-type (value type)
  "Encode the VALUE according to TYPE.
The data is inserted in the current buffer at the current
position.

This is a specialization of `soap-encode-value' for
`soap-xs-basic-type' objects."
  (let ((kind (soap-xs-basic-type-kind type))
        ;; Handle conversions of this form:
        ;; (Element (AttrA . "A") (AttrB . "B") "Value here")
        ;; to:
        ;; <ns:Element AttrA="A" AttrB="B">Value here</ns:Element>
        ;; by assuming that if this is a list, it must have attributes
        ;; preceding the basic value.
        (value (if (listp value) (progn (car (last value))) value)))
    (when (eq kind 'anyType)
      (cond ((stringp value)
             (setq kind 'string))
            ((integerp value)
             (setq kind 'int))
            ((memq value '(t nil))
             (setq kind 'boolean))
            (t
             (error "Cannot classify anyType value"))))

    ;; NOTE: a nil value is not encoded, as an xsi:nil="true" attribute was
    ;; encoded for it.  However, we have some ambiguity here, as a nil value
    ;; also represents "false" when the type is boolean...

    (when (or value (eq kind 'boolean))
      (let ((value-string
             (cl-case kind
               ((string anyURI QName ID IDREF language)
                (unless (stringp value)
                  (error "Not a string value: %s" value))
                (url-insert-entities-in-string value))
               ((dateTime time date gYearMonth gYear gMonthDay gDay gMonth)
                (cond ((consp value)
                       ;; Value is a (current-time) style value,
                       ;; convert to the ISO 8601-inspired XSD
                       ;; string format in UTC.
                       (format-time-string
                        (concat
                         (cl-ecase kind
                           (dateTime "%Y-%m-%dT%H:%M:%S")
                           (time "%H:%M:%S")
                           (date "%Y-%m-%d")
                           (gYearMonth "%Y-%m")
                           (gYear "%Y")
                           (gMonthDay "--%m-%d")
                           (gDay "---%d")
                           (gMonth "--%m"))
                         ;; Internal time is always in UTC.
                         "Z")
                        value t))
                      ((stringp value)
                       ;; Value is a string in the ISO 8601-inspired XSD
                       ;; format.  Validate it.
                       (soap-decode-date-time value kind)
                       (url-insert-entities-in-string value))
                      (t
                       (error "Invalid date-time format"))))
               (boolean
                (unless (memq value '(t nil))
                  (error "Not a boolean value"))
                (if value "true" "false"))

               ((long short int integer byte unsignedInt unsignedLong
                      unsignedShort nonNegativeInteger decimal duration)
                (unless (integerp value)
                  (error "Not an integer value"))
                (when (and (memq kind '(unsignedInt unsignedLong
                                                    unsignedShort
                                                    nonNegativeInteger))
                           (< value 0))
                  (error "Not a positive integer"))
                (number-to-string value))

               ((float double)
                (unless (numberp value)
                  (error "Not a number"))
                (number-to-string value))

               (base64Binary
                (unless (stringp value)
                  (error "Not a string value for base64Binary"))
                (base64-encode-string value))

               (otherwise
                (error "Don't know how to encode %s for type %s"
                       value (soap-element-fq-name type))))))
        (soap-validate-xs-basic-type value-string type)
        (insert value-string)))))