(compose-mail): Give a better error message for `mail-user-agent'
[emacs.git] / lisp / net / soap-client.el
blob3996da0b554b409875b04c0b9f0f8f497083b63e
1 ;;; soap-client.el --- Access SOAP web services -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Alexandru Harsanyi <AlexHarsanyi@gmail.com>
6 ;; Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
7 ;; Created: December, 2009
8 ;; Version: 3.1.3
9 ;; Keywords: soap, web-services, comm, hypermedia
10 ;; Package: soap-client
11 ;; Homepage: https://github.com/alex-hhh/emacs-soap-client
12 ;; Package-Requires: ((cl-lib "0.6.1"))
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
29 ;;; Commentary:
31 ;; To use the SOAP client, you first need to load the WSDL document for the
32 ;; service you want to access, using `soap-load-wsdl-from-url'. A WSDL
33 ;; document describes the available operations of the SOAP service, how their
34 ;; parameters and responses are encoded. To invoke operations, you use the
35 ;; `soap-invoke' method passing it the WSDL, the service name, the operation
36 ;; you wish to invoke and any required parameters.
38 ;; Ideally, the service you want to access will have some documentation about
39 ;; the operations it supports. If it does not, you can try using
40 ;; `soap-inspect' to browse the WSDL document and see the available operations
41 ;; and their parameters.
44 ;;; Code:
46 (require 'cl-lib)
48 (require 'xml)
49 (require 'xsd-regexp)
50 (require 'rng-xsd)
51 (require 'rng-dt)
52 (require 'warnings)
53 (require 'url)
54 (require 'url-http)
55 (require 'url-util)
56 (require 'url-vars)
57 (require 'mm-decode)
59 (defsubst soap-warning (message &rest args)
60 "Display a warning MESSAGE with ARGS, using the `soap-client' warning type."
61 ;; Do not use #'format-message, to support older Emacs versions.
62 (display-warning 'soap-client (apply #'format message args) :warning))
64 (defgroup soap-client nil
65 "Access SOAP web services from Emacs."
66 :version "24.1"
67 :group 'tools)
69 ;;;; Support for parsing XML documents with namespaces
71 ;; XML documents with namespaces are difficult to parse because the names of
72 ;; the nodes depend on what "xmlns" aliases have been defined in the document.
73 ;; To work with such documents, we introduce a translation layer between a
74 ;; "well known" namespace tag and the local namespace tag in the document
75 ;; being parsed.
77 (defconst soap-well-known-xmlns
78 '(("apachesoap" . "http://xml.apache.org/xml-soap")
79 ("soapenc" . "http://schemas.xmlsoap.org/soap/encoding/")
80 ("wsdl" . "http://schemas.xmlsoap.org/wsdl/")
81 ("wsdlsoap" . "http://schemas.xmlsoap.org/wsdl/soap/")
82 ("xsd" . "http://www.w3.org/2001/XMLSchema")
83 ("xsi" . "http://www.w3.org/2001/XMLSchema-instance")
84 ("wsa" . "http://www.w3.org/2005/08/addressing")
85 ("wsaw" . "http://www.w3.org/2006/05/addressing/wsdl")
86 ("soap" . "http://schemas.xmlsoap.org/soap/envelope/")
87 ("soap12" . "http://schemas.xmlsoap.org/wsdl/soap12/")
88 ("http" . "http://schemas.xmlsoap.org/wsdl/http/")
89 ("mime" . "http://schemas.xmlsoap.org/wsdl/mime/")
90 ("xml" . "http://www.w3.org/XML/1998/namespace"))
91 "A list of well known xml namespaces and their aliases.")
93 (defvar soap-local-xmlns
94 '(("xml" . "http://www.w3.org/XML/1998/namespace"))
95 "A list of local namespace aliases.
96 This is a dynamically bound variable, controlled by
97 `soap-with-local-xmlns'.")
99 (defvar soap-default-xmlns nil
100 "The default XML namespaces.
101 Names in this namespace will be unqualified. This is a
102 dynamically bound variable, controlled by
103 `soap-with-local-xmlns'")
105 (defvar soap-target-xmlns nil
106 "The target XML namespace.
107 New XSD elements will be defined in this namespace, unless they
108 are fully qualified for a different namespace. This is a
109 dynamically bound variable, controlled by
110 `soap-with-local-xmlns'")
112 (defvar soap-current-wsdl nil
113 "The current WSDL document used when decoding the SOAP response.
114 This is a dynamically bound variable.")
116 (defun soap-wk2l (well-known-name)
117 "Return local variant of WELL-KNOWN-NAME.
118 This is done by looking up the namespace in the
119 `soap-well-known-xmlns' table and resolving the namespace to
120 the local name based on the current local translation table
121 `soap-local-xmlns'. See also `soap-with-local-xmlns'."
122 (let ((wk-name-1 (if (symbolp well-known-name)
123 (symbol-name well-known-name)
124 well-known-name)))
125 (cond
126 ((string-match "^\\(.*\\):\\(.*\\)$" wk-name-1)
127 (let ((ns (match-string 1 wk-name-1))
128 (name (match-string 2 wk-name-1)))
129 (let ((namespace (cdr (assoc ns soap-well-known-xmlns))))
130 (cond ((equal namespace soap-default-xmlns)
131 ;; Name is unqualified in the default namespace
132 (if (symbolp well-known-name)
133 (intern name)
134 name))
136 (let* ((local-ns (car (rassoc namespace soap-local-xmlns)))
137 (local-name (concat local-ns ":" name)))
138 (if (symbolp well-known-name)
139 (intern local-name)
140 local-name)))))))
141 (t well-known-name))))
143 (defun soap-l2wk (local-name)
144 "Convert LOCAL-NAME into a well known name.
145 The namespace of LOCAL-NAME is looked up in the
146 `soap-well-known-xmlns' table and a well known namespace tag is
147 used in the name.
149 nil is returned if there is no well-known namespace for the
150 namespace of LOCAL-NAME."
151 (let ((l-name-1 (if (symbolp local-name)
152 (symbol-name local-name)
153 local-name))
154 namespace name)
155 (cond
156 ((string-match "^\\(.*\\):\\(.*\\)$" l-name-1)
157 (setq name (match-string 2 l-name-1))
158 (let ((ns (match-string 1 l-name-1)))
159 (setq namespace (cdr (assoc ns soap-local-xmlns)))
160 (unless namespace
161 (error "Soap-l2wk(%s): no namespace for alias %s" local-name ns))))
163 (setq name l-name-1)
164 (setq namespace soap-default-xmlns)))
166 (if namespace
167 (let ((well-known-ns (car (rassoc namespace soap-well-known-xmlns))))
168 (if well-known-ns
169 (let ((well-known-name (concat well-known-ns ":" name)))
170 (if (symbolp local-name)
171 (intern well-known-name)
172 well-known-name))
173 nil))
174 ;; if no namespace is defined, just return the unqualified name
175 name)))
178 (defun soap-l2fq (local-name &optional use-tns)
179 "Convert LOCAL-NAME into a fully qualified name.
180 A fully qualified name is a cons of the namespace name and the
181 name of the element itself. For example \"xsd:string\" is
182 converted to (\"http://www.w3.org/2001/XMLSchema\" . \"string\").
184 The USE-TNS argument specifies what to do when LOCAL-NAME has no
185 namespace tag. If USE-TNS is non-nil, the `soap-target-xmlns'
186 will be used as the element's namespace, otherwise
187 `soap-default-xmlns' will be used.
189 This is needed because different parts of a WSDL document can use
190 different namespace aliases for the same element."
191 (let ((local-name-1 (if (symbolp local-name)
192 (symbol-name local-name)
193 local-name)))
194 (cond ((string-match "^\\(.*\\):\\(.*\\)$" local-name-1)
195 (let ((ns (match-string 1 local-name-1))
196 (name (match-string 2 local-name-1)))
197 (let ((namespace (cdr (assoc ns soap-local-xmlns))))
198 (if namespace
199 (cons namespace name)
200 (error "Soap-l2fq(%s): unknown alias %s" local-name ns)))))
202 (cons (if use-tns
203 soap-target-xmlns
204 soap-default-xmlns)
205 local-name-1)))))
207 (defun soap-name-p (name)
208 "Return t if NAME is a valid name for XMLSchema types.
209 A valid name is either a string or a cons of (NAMESPACE . NAME)."
210 (or (stringp name)
211 (and (consp name)
212 (stringp (car name))
213 (stringp (cdr name)))))
215 (defun soap-extract-xmlns (node &optional xmlns-table)
216 "Return a namespace alias table for NODE by extending XMLNS-TABLE."
217 (let (xmlns default-ns target-ns)
218 (dolist (a (xml-node-attributes node))
219 (let ((name (symbol-name (car a)))
220 (value (cdr a)))
221 (cond ((string= name "targetNamespace")
222 (setq target-ns value))
223 ((string= name "xmlns")
224 (setq default-ns value))
225 ((string-match "^xmlns:\\(.*\\)$" name)
226 (push (cons (match-string 1 name) value) xmlns)))))
228 (let ((tns (assoc "tns" xmlns)))
229 (cond ((and tns target-ns)
230 ;; If a tns alias is defined for this node, it must match
231 ;; the target namespace.
232 (unless (equal target-ns (cdr tns))
233 (soap-warning
234 "soap-extract-xmlns(%s): tns alias and targetNamespace mismatch"
235 (xml-node-name node))))
236 ((and tns (not target-ns))
237 (setq target-ns (cdr tns)))))
239 (list default-ns target-ns (append xmlns xmlns-table))))
241 (defmacro soap-with-local-xmlns (node &rest body)
242 "Install a local alias table from NODE and execute BODY."
243 (declare (debug (form &rest form)) (indent 1))
244 (let ((xmlns (make-symbol "xmlns")))
245 `(let ((,xmlns (soap-extract-xmlns ,node soap-local-xmlns)))
246 (let ((soap-default-xmlns (or (nth 0 ,xmlns) soap-default-xmlns))
247 (soap-target-xmlns (or (nth 1 ,xmlns) soap-target-xmlns))
248 (soap-local-xmlns (nth 2 ,xmlns)))
249 ,@body))))
251 (defun soap-get-target-namespace (node)
252 "Return the target namespace of NODE.
253 This is the namespace in which new elements will be defined."
254 (or (xml-get-attribute-or-nil node 'targetNamespace)
255 (cdr (assoc "tns" soap-local-xmlns))
256 soap-target-xmlns))
258 (defun soap-xml-get-children1 (node child-name)
259 "Return the children of NODE named CHILD-NAME.
260 This is the same as `xml-get-children', but CHILD-NAME can have
261 namespace tag."
262 (let (result)
263 (dolist (c (xml-node-children node))
264 (when (and (consp c)
265 (soap-with-local-xmlns c
266 ;; We use `ignore-errors' here because we want to silently
267 ;; skip nodes when we cannot convert them to a well-known
268 ;; name.
269 (eq (ignore-errors (soap-l2wk (xml-node-name c)))
270 child-name)))
271 (push c result)))
272 (nreverse result)))
274 (defun soap-xml-node-find-matching-child (node set)
275 "Return the first child of NODE whose name is a member of SET."
276 (catch 'found
277 (dolist (child (xml-node-children node))
278 (when (and (consp child)
279 (memq (soap-l2wk (xml-node-name child)) set))
280 (throw 'found child)))))
282 (defun soap-xml-get-attribute-or-nil1 (node attribute)
283 "Return the NODE's ATTRIBUTE, or nil if it does not exist.
284 This is the same as `xml-get-attribute-or-nil', but ATTRIBUTE can
285 be tagged with a namespace tag."
286 (catch 'found
287 (soap-with-local-xmlns node
288 (dolist (a (xml-node-attributes node))
289 ;; We use `ignore-errors' here because we want to silently skip
290 ;; attributes for which we cannot convert them to a well-known name.
291 (when (eq (ignore-errors (soap-l2wk (car a))) attribute)
292 (throw 'found (cdr a)))))))
295 ;;;; XML namespaces
297 ;; An element in an XML namespace, "things" stored in soap-xml-namespaces will
298 ;; be derived from this object.
300 (cl-defstruct soap-element
301 name
302 ;; The "well-known" namespace tag for the element. For example, while
303 ;; parsing XML documents, we can have different tags for the XMLSchema
304 ;; namespace, but internally all our XMLSchema elements will have the "xsd"
305 ;; tag.
306 namespace-tag)
308 (defun soap-element-fq-name (element)
309 "Return a fully qualified name for ELEMENT.
310 A fq name is the concatenation of the namespace tag and the
311 element name."
312 (cond ((soap-element-namespace-tag element)
313 (concat (soap-element-namespace-tag element)
314 ":" (soap-element-name element)))
315 ((soap-element-name element)
316 (soap-element-name element))
318 "*unnamed*")))
320 ;; a namespace link stores an alias for an object in once namespace to a
321 ;; "target" object possibly in a different namespace
323 (cl-defstruct (soap-namespace-link (:include soap-element))
324 target)
326 ;; A namespace is a collection of soap-element objects under a name (the name
327 ;; of the namespace).
329 (cl-defstruct soap-namespace
330 (name nil :read-only t) ; e.g "http://xml.apache.org/xml-soap"
331 (elements (make-hash-table :test 'equal) :read-only t))
333 (defun soap-namespace-put (element ns)
334 "Store ELEMENT in NS.
335 Multiple elements with the same name can be stored in a
336 namespace. When retrieving the element you can specify a
337 discriminant predicate to `soap-namespace-get'"
338 (let ((name (soap-element-name element)))
339 (push element (gethash name (soap-namespace-elements ns)))))
341 (defun soap-namespace-put-link (name target ns)
342 "Store a link from NAME to TARGET in NS.
343 TARGET can be either a SOAP-ELEMENT or a string denoting an
344 element name into another namespace.
346 If NAME is nil, an element with the same name as TARGET will be
347 added to the namespace."
349 (unless (and name (not (equal name "")))
350 ;; if name is nil, use TARGET as a name...
351 (cond ((soap-element-p target)
352 (setq name (soap-element-name target)))
353 ((consp target) ; a fq name: (namespace . name)
354 (setq name (cdr target)))
355 ((stringp target)
356 (cond ((string-match "^\\(.*\\):\\(.*\\)$" target)
357 (setq name (match-string 2 target)))
359 (setq name target))))))
361 ;; by now, name should be valid
362 (cl-assert (and name (not (equal name "")))
364 "Cannot determine name for namespace link")
365 (push (make-soap-namespace-link :name name :target target)
366 (gethash name (soap-namespace-elements ns))))
368 (defun soap-namespace-get (name ns &optional discriminant-predicate)
369 "Retrieve an element with NAME from the namespace NS.
370 If multiple elements with the same name exist,
371 DISCRIMINANT-PREDICATE is used to pick one of them. This allows
372 storing elements of different types (like a message type and a
373 binding) but the same name."
374 (cl-assert (stringp name))
375 (let ((elements (gethash name (soap-namespace-elements ns))))
376 (cond (discriminant-predicate
377 (catch 'found
378 (dolist (e elements)
379 (when (funcall discriminant-predicate e)
380 (throw 'found e)))))
381 ((= (length elements) 1) (car elements))
382 ((> (length elements) 1)
383 (error
384 "Soap-namespace-get(%s): multiple elements, discriminant needed"
385 name))
387 nil))))
390 ;;;; XML Schema
392 ;; SOAP WSDL documents use XML Schema to define the types that are part of the
393 ;; message exchange. We include here an XML schema model with a parser and
394 ;; serializer/deserializer.
396 (cl-defstruct (soap-xs-type (:include soap-element))
398 attributes
399 attribute-groups)
401 ;;;;; soap-xs-basic-type
403 (cl-defstruct (soap-xs-basic-type (:include soap-xs-type))
404 ;; Basic types are "built in" and we know how to handle them directly.
405 ;; Other type definitions reference basic types, so we need to create them
406 ;; in a namespace (see `soap-make-xs-basic-types')
408 ;; a symbol of: string, dateTime, long, int, etc
409 kind
412 (defun soap-make-xs-basic-types (namespace-name &optional namespace-tag)
413 "Construct NAMESPACE-NAME containing the XMLSchema basic types.
414 An optional NAMESPACE-TAG can also be specified."
415 (let ((ns (make-soap-namespace :name namespace-name)))
416 (dolist (type '("string" "language" "ID" "IDREF"
417 "dateTime" "time" "date" "boolean"
418 "gYearMonth" "gYear" "gMonthDay" "gDay" "gMonth"
419 "long" "short" "int" "integer" "nonNegativeInteger"
420 "unsignedLong" "unsignedShort" "unsignedInt"
421 "decimal" "duration"
422 "byte" "unsignedByte"
423 "float" "double"
424 "base64Binary" "anyType" "anyURI" "QName" "Array" "byte[]"))
425 (soap-namespace-put
426 (make-soap-xs-basic-type :name type
427 :namespace-tag namespace-tag
428 :kind (intern type))
429 ns))
430 ns))
432 (defun soap-encode-xs-basic-type-attributes (value type)
433 "Encode the XML attributes for VALUE according to TYPE.
434 The xsi:type and an optional xsi:nil attributes are added. The
435 attributes are inserted in the current buffer at the current
436 position.
438 This is a specialization of `soap-encode-attributes' for
439 `soap-xs-basic-type' objects."
440 (let ((xsi-type (soap-element-fq-name type))
441 (basic-type (soap-xs-basic-type-kind type)))
442 ;; try to classify the type based on the value type and use that type when
443 ;; encoding
444 (when (eq basic-type 'anyType)
445 (cond ((stringp value)
446 (setq xsi-type "xsd:string" basic-type 'string))
447 ((integerp value)
448 (setq xsi-type "xsd:int" basic-type 'int))
449 ((memq value '(t nil))
450 (setq xsi-type "xsd:boolean" basic-type 'boolean))
452 (error "Cannot classify anyType value"))))
454 (insert " xsi:type=\"" xsi-type "\"")
455 ;; We have some ambiguity here, as a nil value represents "false" when the
456 ;; type is boolean, we will never have a "nil" boolean type...
457 (unless (or value (eq basic-type 'boolean))
458 (insert " xsi:nil=\"true\""))))
460 (defun soap-encode-xs-basic-type (value type)
461 "Encode the VALUE according to TYPE.
462 The data is inserted in the current buffer at the current
463 position.
465 This is a specialization of `soap-encode-value' for
466 `soap-xs-basic-type' objects."
467 (let ((kind (soap-xs-basic-type-kind type)))
469 (when (eq kind 'anyType)
470 (cond ((stringp value)
471 (setq kind 'string))
472 ((integerp value)
473 (setq kind 'int))
474 ((memq value '(t nil))
475 (setq kind 'boolean))
477 (error "Cannot classify anyType value"))))
479 ;; NOTE: a nil value is not encoded, as an xsi:nil="true" attribute was
480 ;; encoded for it. However, we have some ambiguity here, as a nil value
481 ;; also represents "false" when the type is boolean...
483 (when (or value (eq kind 'boolean))
484 (let ((value-string
485 (cl-case kind
486 ((string anyURI QName ID IDREF language)
487 (unless (stringp value)
488 (error "Not a string value: %s" value))
489 (url-insert-entities-in-string value))
490 ((dateTime time date gYearMonth gYear gMonthDay gDay gMonth)
491 (cond ((consp value)
492 ;; Value is a (current-time) style value,
493 ;; convert to the ISO 8601-inspired XSD
494 ;; string format in UTC.
495 (format-time-string
496 (concat
497 (cl-ecase kind
498 (dateTime "%Y-%m-%dT%H:%M:%S")
499 (time "%H:%M:%S")
500 (date "%Y-%m-%d")
501 (gYearMonth "%Y-%m")
502 (gYear "%Y")
503 (gMonthDay "--%m-%d")
504 (gDay "---%d")
505 (gMonth "--%m"))
506 ;; Internal time is always in UTC.
507 "Z")
508 value t))
509 ((stringp value)
510 ;; Value is a string in the ISO 8601-inspired XSD
511 ;; format. Validate it.
512 (soap-decode-date-time value kind)
513 (url-insert-entities-in-string value))
515 (error "Invalid date-time format"))))
516 (boolean
517 (unless (memq value '(t nil))
518 (error "Not a boolean value"))
519 (if value "true" "false"))
521 ((long short int integer byte unsignedInt unsignedLong
522 unsignedShort nonNegativeInteger decimal duration)
523 (unless (integerp value)
524 (error "Not an integer value"))
525 (when (and (memq kind '(unsignedInt unsignedLong
526 unsignedShort
527 nonNegativeInteger))
528 (< value 0))
529 (error "Not a positive integer"))
530 (number-to-string value))
532 ((float double)
533 (unless (numberp value)
534 (error "Not a number"))
535 (number-to-string value))
537 (base64Binary
538 (unless (stringp value)
539 (error "Not a string value for base64Binary"))
540 (base64-encode-string value))
542 (otherwise
543 (error "Don't know how to encode %s for type %s"
544 value (soap-element-fq-name type))))))
545 (soap-validate-xs-basic-type value-string type)
546 (insert value-string)))))
548 ;; Inspired by rng-xsd-convert-date-time.
549 (defun soap-decode-date-time (date-time-string datatype)
550 "Decode DATE-TIME-STRING as DATATYPE.
551 DATE-TIME-STRING should be in ISO 8601 basic or extended format.
552 DATATYPE is one of dateTime, time, date, gYearMonth, gYear,
553 gMonthDay, gDay or gMonth.
555 Return a list in a format (SEC MINUTE HOUR DAY MONTH YEAR
556 SEC-FRACTION DATATYPE ZONE). This format is meant to be similar
557 to that returned by `decode-time' (and compatible with
558 `encode-time'). The differences are the DOW (day-of-week) field
559 is replaced with SEC-FRACTION, a float representing the
560 fractional seconds, and the DST (daylight savings time) field is
561 replaced with DATATYPE, a symbol representing the XSD primitive
562 datatype. This symbol can be used to determine which fields
563 apply and which don't when it's not already clear from context.
564 For example a datatype of `time' means the year, month and day
565 fields should be ignored.
567 This function will throw an error if DATE-TIME-STRING represents
568 a leap second, since the XML Schema 1.1 standard explicitly
569 disallows them."
570 (let* ((datetime-regexp (cadr (get datatype 'rng-xsd-convert)))
571 (year-sign (progn
572 (string-match datetime-regexp date-time-string)
573 (match-string 1 date-time-string)))
574 (year (match-string 2 date-time-string))
575 (month (match-string 3 date-time-string))
576 (day (match-string 4 date-time-string))
577 (hour (match-string 5 date-time-string))
578 (minute (match-string 6 date-time-string))
579 (second (match-string 7 date-time-string))
580 (second-fraction (match-string 8 date-time-string))
581 (has-time-zone (match-string 9 date-time-string))
582 (time-zone-sign (match-string 10 date-time-string))
583 (time-zone-hour (match-string 11 date-time-string))
584 (time-zone-minute (match-string 12 date-time-string)))
585 (setq year-sign (if year-sign -1 1))
586 (setq year
587 (if year
588 (* year-sign
589 (string-to-number year))
590 ;; By defaulting to the epoch date, a time value can be treated as
591 ;; a relative number of seconds.
592 1970))
593 (setq month
594 (if month (string-to-number month) 1))
595 (setq day
596 (if day (string-to-number day) 1))
597 (setq hour
598 (if hour (string-to-number hour) 0))
599 (setq minute
600 (if minute (string-to-number minute) 0))
601 (setq second
602 (if second (string-to-number second) 0))
603 (setq second-fraction
604 (if second-fraction
605 (float (string-to-number second-fraction))
606 0.0))
607 (setq has-time-zone (and has-time-zone t))
608 (setq time-zone-sign
609 (if (equal time-zone-sign "-") -1 1))
610 (setq time-zone-hour
611 (if time-zone-hour (string-to-number time-zone-hour) 0))
612 (setq time-zone-minute
613 (if time-zone-minute (string-to-number time-zone-minute) 0))
614 (unless (and
615 ;; XSD does not allow year 0.
616 (> year 0)
617 (>= month 1) (<= month 12)
618 (>= day 1) (<= day (rng-xsd-days-in-month year month))
619 (>= hour 0) (<= hour 23)
620 (>= minute 0) (<= minute 59)
621 ;; 60 represents a leap second, but leap seconds are explicitly
622 ;; disallowed by the XML Schema 1.1 specification. This agrees
623 ;; with typical Emacs installations, which don't count leap
624 ;; seconds in time values.
625 (>= second 0) (<= second 59)
626 (>= time-zone-hour 0)
627 (<= time-zone-hour 23)
628 (>= time-zone-minute 0)
629 (<= time-zone-minute 59))
630 (error "Invalid or unsupported time: %s" date-time-string))
631 ;; Return a value in a format similar to that returned by decode-time, and
632 ;; suitable for (apply 'encode-time ...).
633 (list second minute hour day month year second-fraction datatype
634 (if has-time-zone
635 (* (rng-xsd-time-to-seconds
636 time-zone-hour
637 time-zone-minute
639 time-zone-sign)
640 ;; UTC.
641 0))))
643 (defun soap-decode-xs-basic-type (type node)
644 "Use TYPE, a `soap-xs-basic-type', to decode the contents of NODE.
645 A LISP value is returned based on the contents of NODE and the
646 type-info stored in TYPE.
648 This is a specialization of `soap-decode-type' for
649 `soap-xs-basic-type' objects."
650 (let ((contents (xml-node-children node))
651 (kind (soap-xs-basic-type-kind type))
652 (attributes (xml-node-attributes node))
653 (validate-type type)
654 (is-nil nil))
656 (dolist (attribute attributes)
657 (let ((attribute-type (soap-l2fq (car attribute)))
658 (attribute-value (cdr attribute)))
659 ;; xsi:type can override an element's expected type.
660 (when (equal attribute-type (soap-l2fq "xsi:type"))
661 (setq validate-type
662 (soap-wsdl-get attribute-value soap-current-wsdl)))
663 ;; xsi:nil can specify that an element is nil in which case we don't
664 ;; validate it.
665 (when (equal attribute-type (soap-l2fq "xsi:nil"))
666 (setq is-nil (string= (downcase attribute-value) "true")))))
668 (unless is-nil
669 ;; For validation purposes, when xml-node-children returns nil, treat it
670 ;; as the empty string.
671 (soap-validate-xs-basic-type (car (or contents (list ""))) validate-type))
673 (if (null contents)
675 (cl-ecase kind
676 ((string anyURI QName ID IDREF language) (car contents))
677 ((dateTime time date gYearMonth gYear gMonthDay gDay gMonth)
678 (car contents))
679 ((long short int integer
680 unsignedInt unsignedLong unsignedShort nonNegativeInteger
681 decimal byte float double duration)
682 (string-to-number (car contents)))
683 (boolean (string= (downcase (car contents)) "true"))
684 (base64Binary (base64-decode-string (car contents)))
685 (anyType (soap-decode-any-type node))
686 (Array (soap-decode-array node))))))
688 ;; Register methods for `soap-xs-basic-type'
689 (let ((tag (aref (make-soap-xs-basic-type) 0)))
690 (put tag 'soap-attribute-encoder #'soap-encode-xs-basic-type-attributes)
691 (put tag 'soap-encoder #'soap-encode-xs-basic-type)
692 (put tag 'soap-decoder #'soap-decode-xs-basic-type))
694 ;;;;; soap-xs-element
696 (cl-defstruct (soap-xs-element (:include soap-element))
697 ;; NOTE: we don't support exact number of occurrences via minOccurs,
698 ;; maxOccurs. Instead we support optional? and multiple?
701 type^ ; note: use soap-xs-element-type to retrieve this member
702 optional?
703 multiple?
704 reference
705 substitution-group
706 ;; contains a list of elements who point to this one via their
707 ;; substitution-group slot
708 alternatives
709 is-group)
711 (defun soap-xs-element-type (element)
712 "Retrieve the type of ELEMENT.
713 This is normally stored in the TYPE^ slot, but if this element
714 contains a reference, retrieve the type of the reference."
715 (if (soap-xs-element-reference element)
716 (soap-xs-element-type (soap-xs-element-reference element))
717 (soap-xs-element-type^ element)))
719 (defun soap-node-optional (node)
720 "Return t if NODE specifies an optional element."
721 (or (equal (xml-get-attribute-or-nil node 'nillable) "true")
722 (let ((e (xml-get-attribute-or-nil node 'minOccurs)))
723 (and e (equal e "0")))))
725 (defun soap-node-multiple (node)
726 "Return t if NODE permits multiple elements."
727 (let* ((e (xml-get-attribute-or-nil node 'maxOccurs)))
728 (and e (not (equal e "1")))))
730 (defun soap-xs-parse-element (node)
731 "Construct a `soap-xs-element' from NODE."
732 (let ((name (xml-get-attribute-or-nil node 'name))
733 (id (xml-get-attribute-or-nil node 'id))
734 (type (xml-get-attribute-or-nil node 'type))
735 (optional? (soap-node-optional node))
736 (multiple? (soap-node-multiple node))
737 (ref (xml-get-attribute-or-nil node 'ref))
738 (substitution-group (xml-get-attribute-or-nil node 'substitutionGroup))
739 (node-name (soap-l2wk (xml-node-name node))))
740 (cl-assert (memq node-name '(xsd:element xsd:group))
741 "expecting xsd:element or xsd:group, got %s" node-name)
743 (when type
744 (setq type (soap-l2fq type 'tns)))
746 (when ref
747 (setq ref (soap-l2fq ref 'tns)))
749 (when substitution-group
750 (setq substitution-group (soap-l2fq substitution-group 'tns)))
752 (unless (or ref type)
753 ;; no type specified and this is not a reference. Must be a type
754 ;; defined within this node.
755 (let ((simple-type (soap-xml-get-children1 node 'xsd:simpleType)))
756 (if simple-type
757 (setq type (soap-xs-parse-simple-type (car simple-type)))
758 ;; else
759 (let ((complex-type (soap-xml-get-children1 node 'xsd:complexType)))
760 (if complex-type
761 (setq type (soap-xs-parse-complex-type (car complex-type)))
762 ;; else
763 (error "Soap-xs-parse-element: missing type or ref"))))))
765 (make-soap-xs-element :name name
766 ;; Use the full namespace name for now, we will
767 ;; convert it to a nstag in
768 ;; `soap-resolve-references-for-xs-element'
769 :namespace-tag soap-target-xmlns
770 :id id :type^ type
771 :optional? optional? :multiple? multiple?
772 :reference ref
773 :substitution-group substitution-group
774 :is-group (eq node-name 'xsd:group))))
776 (defun soap-resolve-references-for-xs-element (element wsdl)
777 "Replace names in ELEMENT with the referenced objects in the WSDL.
778 This is a specialization of `soap-resolve-references' for
779 `soap-xs-element' objects.
781 See also `soap-wsdl-resolve-references'."
783 (let ((namespace (soap-element-namespace-tag element)))
784 (when namespace
785 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
786 (when nstag
787 (setf (soap-element-namespace-tag element) nstag)))))
789 (let ((type (soap-xs-element-type^ element)))
790 (cond ((soap-name-p type)
791 (setf (soap-xs-element-type^ element)
792 (soap-wsdl-get type wsdl 'soap-xs-type-p)))
793 ((soap-xs-type-p type)
794 ;; an inline defined type, this will not be reached from anywhere
795 ;; else, so we must resolve references now.
796 (soap-resolve-references type wsdl))))
797 (let ((reference (soap-xs-element-reference element)))
798 (when (and (soap-name-p reference)
799 ;; xsd:group reference nodes will be converted to inline types
800 ;; by soap-resolve-references-for-xs-complex-type, so skip them
801 ;; here.
802 (not (soap-xs-element-is-group element)))
803 (setf (soap-xs-element-reference element)
804 (soap-wsdl-get reference wsdl 'soap-xs-element-p))))
806 (let ((subst (soap-xs-element-substitution-group element)))
807 (when (soap-name-p subst)
808 (let ((target (soap-wsdl-get subst wsdl)))
809 (if target
810 (push element (soap-xs-element-alternatives target))
811 (soap-warning "No target found for substitution-group" subst))))))
813 (defun soap-encode-xs-element-attributes (value element)
814 "Encode the XML attributes for VALUE according to ELEMENT.
815 Currently no attributes are needed.
817 This is a specialization of `soap-encode-attributes' for
818 `soap-xs-basic-type' objects."
819 ;; Use the variables to suppress checkdoc and compiler warnings.
820 (list value element)
821 nil)
823 (defun soap-should-encode-value-for-xs-element (value element)
824 "Return t if VALUE should be encoded for ELEMENT, nil otherwise."
825 (cond
826 ;; if value is not nil, attempt to encode it
827 (value)
829 ;; value is nil, but the element's type is a boolean, so nil in this case
830 ;; means "false". We need to encode it.
831 ((let ((type (soap-xs-element-type element)))
832 (and (soap-xs-basic-type-p type)
833 (eq (soap-xs-basic-type-kind type) 'boolean))))
835 ;; This is not an optional element. Force encoding it (although this
836 ;; might fail at the validation step, but this is what we intend.
838 ;; value is nil, but the element's type has some attributes which supply a
839 ;; default value. We need to encode it.
841 ((let ((type (soap-xs-element-type element)))
842 (catch 'found
843 (dolist (a (soap-xs-type-attributes type))
844 (when (soap-xs-attribute-default a)
845 (throw 'found t))))))
847 ;; otherwise, we don't need to encode it
848 (t nil)))
850 (defun soap-type-is-array? (type)
851 "Return t if TYPE defines an ARRAY."
852 (and (soap-xs-complex-type-p type)
853 (eq (soap-xs-complex-type-indicator type) 'array)))
855 (defvar soap-encoded-namespaces nil
856 "A list of namespace tags used during encoding a message.
857 This list is populated by `soap-encode-value' and used by
858 `soap-create-envelope' to add aliases for these namespace to the
859 XML request.
861 This variable is dynamically bound in `soap-create-envelope'.")
863 (defun soap-encode-xs-element (value element)
864 "Encode the VALUE according to ELEMENT.
865 The data is inserted in the current buffer at the current
866 position.
868 This is a specialization of `soap-encode-value' for
869 `soap-xs-basic-type' objects."
870 (let ((fq-name (soap-element-fq-name element))
871 (type (soap-xs-element-type element)))
872 ;; Only encode the element if it has a name. NOTE: soap-element-fq-name
873 ;; will return *unnamed* for such elements
874 (if (soap-element-name element)
875 ;; Don't encode this element if value is nil. However, even if value
876 ;; is nil we still want to encode this element if it has any attributes
877 ;; with default values.
878 (when (soap-should-encode-value-for-xs-element value element)
879 (progn
880 (insert "<" fq-name)
881 (soap-encode-attributes value type)
882 ;; If value is nil and type is boolean encode the value as "false".
883 ;; Otherwise don't encode the value.
884 (if (or value (and (soap-xs-basic-type-p type)
885 (eq (soap-xs-basic-type-kind type) 'boolean)))
886 (progn (insert ">")
887 ;; ARRAY's need special treatment, as each element of
888 ;; the array is encoded with the same tag as the
889 ;; current element...
890 (if (soap-type-is-array? type)
891 (let ((new-element (copy-soap-xs-element element)))
892 (when (soap-element-namespace-tag type)
893 (add-to-list 'soap-encoded-namespaces
894 (soap-element-namespace-tag type)))
895 (setf (soap-xs-element-type^ new-element)
896 (soap-xs-complex-type-base type))
897 (cl-loop for i below (length value)
898 do (soap-encode-xs-element
899 (aref value i) new-element)))
900 (soap-encode-value value type))
901 (insert "</" fq-name ">\n"))
902 ;; else
903 (insert "/>\n"))))
904 (when (soap-should-encode-value-for-xs-element value element)
905 (soap-encode-value value type)))))
907 (defun soap-decode-xs-element (element node)
908 "Use ELEMENT, a `soap-xs-element', to decode the contents of NODE.
909 A LISP value is returned based on the contents of NODE and the
910 type-info stored in ELEMENT.
912 This is a specialization of `soap-decode-type' for
913 `soap-xs-basic-type' objects."
914 (let ((type (soap-xs-element-type element)))
915 (soap-decode-type type node)))
917 ;; Register methods for `soap-xs-element'
918 (let ((tag (aref (make-soap-xs-element) 0)))
919 (put tag 'soap-resolve-references #'soap-resolve-references-for-xs-element)
920 (put tag 'soap-attribute-encoder #'soap-encode-xs-element-attributes)
921 (put tag 'soap-encoder #'soap-encode-xs-element)
922 (put tag 'soap-decoder #'soap-decode-xs-element))
924 ;;;;; soap-xs-attribute
926 (cl-defstruct (soap-xs-attribute (:include soap-element))
927 type ; a simple type or basic type
928 default ; the default value, if any
929 reference)
931 (cl-defstruct (soap-xs-attribute-group (:include soap-xs-type))
932 reference)
934 (defun soap-xs-parse-attribute (node)
935 "Construct a `soap-xs-attribute' from NODE."
936 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'xsd:attribute)
937 "expecting xsd:attribute, got %s" (soap-l2wk (xml-node-name node)))
938 (let* ((name (xml-get-attribute-or-nil node 'name))
939 (type (soap-l2fq (xml-get-attribute-or-nil node 'type)))
940 (default (xml-get-attribute-or-nil node 'fixed))
941 (attribute (xml-get-attribute-or-nil node 'ref))
942 (ref (when attribute (soap-l2fq attribute))))
943 (unless (or type ref)
944 (setq type (soap-xs-parse-simple-type
945 (soap-xml-node-find-matching-child
946 node '(xsd:restriction xsd:list xsd:union)))))
947 (make-soap-xs-attribute
948 :name name :type type :default default :reference ref)))
950 (defun soap-xs-parse-attribute-group (node)
951 "Construct a `soap-xs-attribute-group' from NODE."
952 (let ((node-name (soap-l2wk (xml-node-name node))))
953 (cl-assert (eq node-name 'xsd:attributeGroup)
954 "expecting xsd:attributeGroup, got %s" node-name)
955 (let ((name (xml-get-attribute-or-nil node 'name))
956 (id (xml-get-attribute-or-nil node 'id))
957 (ref (xml-get-attribute-or-nil node 'ref))
958 attribute-group)
959 (when (and name ref)
960 (soap-warning "name and ref set for attribute group %s" node-name))
961 (setq attribute-group
962 (make-soap-xs-attribute-group :id id
963 :name name
964 :reference (and ref (soap-l2fq ref))))
965 (when (not ref)
966 (dolist (child (xml-node-children node))
967 ;; Ignore whitespace.
968 (unless (stringp child)
969 ;; Ignore optional annotation.
970 ;; Ignore anyAttribute nodes.
971 (cl-case (soap-l2wk (xml-node-name child))
972 (xsd:attribute
973 (push (soap-xs-parse-attribute child)
974 (soap-xs-type-attributes attribute-group)))
975 (xsd:attributeGroup
976 (push (soap-xs-parse-attribute-group child)
977 (soap-xs-attribute-group-attribute-groups
978 attribute-group)))))))
979 attribute-group)))
981 (defun soap-resolve-references-for-xs-attribute (attribute wsdl)
982 "Replace names in ATTRIBUTE with the referenced objects in the WSDL.
983 This is a specialization of `soap-resolve-references' for
984 `soap-xs-attribute' objects.
986 See also `soap-wsdl-resolve-references'."
987 (let* ((type (soap-xs-attribute-type attribute))
988 (reference (soap-xs-attribute-reference attribute))
989 (predicate 'soap-xs-element-p)
990 (xml-reference
991 (and (soap-name-p reference)
992 (equal (car reference) "http://www.w3.org/XML/1998/namespace"))))
993 (cond (xml-reference
994 ;; Convert references to attributes defined by the XML
995 ;; schema (xml:base, xml:lang, xml:space and xml:id) to
996 ;; xsd:string, to avoid needing to bundle and parse
997 ;; xml.xsd.
998 (setq reference '("http://www.w3.org/2001/XMLSchema" . "string"))
999 (setq predicate 'soap-xs-basic-type-p))
1000 ((soap-name-p type)
1001 (setf (soap-xs-attribute-type attribute)
1002 (soap-wsdl-get type wsdl
1003 (lambda (type)
1004 (or (soap-xs-basic-type-p type)
1005 (soap-xs-simple-type-p type))))))
1006 ((soap-xs-type-p type)
1007 ;; an inline defined type, this will not be reached from anywhere
1008 ;; else, so we must resolve references now.
1009 (soap-resolve-references type wsdl)))
1010 (when (soap-name-p reference)
1011 (setf (soap-xs-attribute-reference attribute)
1012 (soap-wsdl-get reference wsdl predicate)))))
1014 (put (aref (make-soap-xs-attribute) 0)
1015 'soap-resolve-references #'soap-resolve-references-for-xs-attribute)
1017 (defun soap-resolve-references-for-xs-attribute-group (attribute-group wsdl)
1018 "Set slots in ATTRIBUTE-GROUP to the referenced objects in the WSDL.
1019 This is a specialization of `soap-resolve-references' for
1020 `soap-xs-attribute-group' objects.
1022 See also `soap-wsdl-resolve-references'."
1023 (let ((reference (soap-xs-attribute-group-reference attribute-group)))
1024 (when (soap-name-p reference)
1025 (let ((resolved (soap-wsdl-get reference wsdl
1026 'soap-xs-attribute-group-p)))
1027 (dolist (attribute (soap-xs-attribute-group-attributes resolved))
1028 (soap-resolve-references attribute wsdl))
1029 (setf (soap-xs-attribute-group-name attribute-group)
1030 (soap-xs-attribute-group-name resolved))
1031 (setf (soap-xs-attribute-group-id attribute-group)
1032 (soap-xs-attribute-group-id resolved))
1033 (setf (soap-xs-attribute-group-reference attribute-group) nil)
1034 (setf (soap-xs-attribute-group-attributes attribute-group)
1035 (soap-xs-attribute-group-attributes resolved))
1036 (setf (soap-xs-attribute-group-attribute-groups attribute-group)
1037 (soap-xs-attribute-group-attribute-groups resolved))))))
1039 (put (aref (make-soap-xs-attribute-group) 0)
1040 'soap-resolve-references #'soap-resolve-references-for-xs-attribute-group)
1042 ;;;;; soap-xs-simple-type
1044 (cl-defstruct (soap-xs-simple-type (:include soap-xs-type))
1045 ;; A simple type is an extension on the basic type to which some
1046 ;; restrictions can be added. For example we can define a simple type based
1047 ;; off "string" with the restrictions that only the strings "one", "two" and
1048 ;; "three" are valid values (this is an enumeration).
1050 base ; can be a single type, or a list of types for union types
1051 enumeration ; nil, or list of permitted values for the type
1052 pattern ; nil, or value must match this pattern
1053 length-range ; a cons of (min . max) length, inclusive range.
1054 ; For exact length, use (l, l).
1055 ; nil means no range,
1056 ; (nil . l) means no min range,
1057 ; (l . nil) means no max range.
1058 integer-range ; a pair of (min, max) integer values, inclusive range,
1059 ; same meaning as `length-range'
1060 is-list ; t if this is an xs:list, nil otherwise
1063 (defun soap-xs-parse-simple-type (node)
1064 "Construct an `soap-xs-simple-type' object from the XML NODE."
1065 (cl-assert (memq (soap-l2wk (xml-node-name node))
1066 '(xsd:simpleType xsd:simpleContent))
1068 "expecting xsd:simpleType or xsd:simpleContent node, got %s"
1069 (soap-l2wk (xml-node-name node)))
1071 ;; NOTE: name can be nil for inline types. Such types cannot be added to a
1072 ;; namespace.
1073 (let ((name (xml-get-attribute-or-nil node 'name))
1074 (id (xml-get-attribute-or-nil node 'id)))
1076 (let ((type (make-soap-xs-simple-type
1077 :name name :namespace-tag soap-target-xmlns :id id))
1078 (def (soap-xml-node-find-matching-child
1079 node '(xsd:restriction xsd:extension xsd:union xsd:list))))
1080 (cl-ecase (soap-l2wk (xml-node-name def))
1081 (xsd:restriction (soap-xs-add-restriction def type))
1082 (xsd:extension (soap-xs-add-extension def type))
1083 (xsd:union (soap-xs-add-union def type))
1084 (xsd:list (soap-xs-add-list def type)))
1086 type)))
1088 (defun soap-xs-add-restriction (node type)
1089 "Add restrictions defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1091 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'xsd:restriction)
1093 "expecting xsd:restriction node, got %s"
1094 (soap-l2wk (xml-node-name node)))
1096 (setf (soap-xs-simple-type-base type)
1097 (soap-l2fq (xml-get-attribute node 'base)))
1099 (dolist (r (xml-node-children node))
1100 (unless (stringp r) ; skip the white space
1101 (let ((value (xml-get-attribute r 'value)))
1102 (cl-case (soap-l2wk (xml-node-name r))
1103 (xsd:enumeration
1104 (push value (soap-xs-simple-type-enumeration type)))
1105 (xsd:pattern
1106 (setf (soap-xs-simple-type-pattern type)
1107 (concat "\\`" (xsdre-translate value) "\\'")))
1108 (xsd:length
1109 (let ((value (string-to-number value)))
1110 (setf (soap-xs-simple-type-length-range type)
1111 (cons value value))))
1112 (xsd:minLength
1113 (let ((value (string-to-number value)))
1114 (setf (soap-xs-simple-type-length-range type)
1115 (if (soap-xs-simple-type-length-range type)
1116 (cons value
1117 (cdr (soap-xs-simple-type-length-range type)))
1118 ;; else
1119 (cons value nil)))))
1120 (xsd:maxLength
1121 (let ((value (string-to-number value)))
1122 (setf (soap-xs-simple-type-length-range type)
1123 (if (soap-xs-simple-type-length-range type)
1124 (cons (car (soap-xs-simple-type-length-range type))
1125 value)
1126 ;; else
1127 (cons nil value)))))
1128 (xsd:minExclusive
1129 (let ((value (string-to-number value)))
1130 (setf (soap-xs-simple-type-integer-range type)
1131 (if (soap-xs-simple-type-integer-range type)
1132 (cons (1+ value)
1133 (cdr (soap-xs-simple-type-integer-range type)))
1134 ;; else
1135 (cons (1+ value) nil)))))
1136 (xsd:maxExclusive
1137 (let ((value (string-to-number value)))
1138 (setf (soap-xs-simple-type-integer-range type)
1139 (if (soap-xs-simple-type-integer-range type)
1140 (cons (car (soap-xs-simple-type-integer-range type))
1141 (1- value))
1142 ;; else
1143 (cons nil (1- value))))))
1144 (xsd:minInclusive
1145 (let ((value (string-to-number value)))
1146 (setf (soap-xs-simple-type-integer-range type)
1147 (if (soap-xs-simple-type-integer-range type)
1148 (cons value
1149 (cdr (soap-xs-simple-type-integer-range type)))
1150 ;; else
1151 (cons value nil)))))
1152 (xsd:maxInclusive
1153 (let ((value (string-to-number value)))
1154 (setf (soap-xs-simple-type-integer-range type)
1155 (if (soap-xs-simple-type-integer-range type)
1156 (cons (car (soap-xs-simple-type-integer-range type))
1157 value)
1158 ;; else
1159 (cons nil value))))))))))
1161 (defun soap-xs-add-union (node type)
1162 "Add union members defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1163 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'xsd:union)
1165 "expecting xsd:union node, got %s"
1166 (soap-l2wk (xml-node-name node)))
1168 (setf (soap-xs-simple-type-base type)
1169 (mapcar 'soap-l2fq
1170 (split-string
1171 (or (xml-get-attribute-or-nil node 'memberTypes) ""))))
1173 ;; Additional simple types can be defined inside the union node. Add them
1174 ;; to the base list. The "memberTypes" members will have to be resolved by
1175 ;; the "resolve-references" method, the inline types will not.
1176 (let (result)
1177 (dolist (simple-type (soap-xml-get-children1 node 'xsd:simpleType))
1178 (push (soap-xs-parse-simple-type simple-type) result))
1179 (setf (soap-xs-simple-type-base type)
1180 (append (soap-xs-simple-type-base type) (nreverse result)))))
1182 (defun soap-xs-add-list (node type)
1183 "Add list defined in XML NODE to TYPE, a `soap-xs-simple-type'."
1184 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'xsd:list)
1186 "expecting xsd:list node, got %s" (soap-l2wk (xml-node-name node)))
1188 ;; A simple type can be defined inline inside the list node or referenced by
1189 ;; the itemType attribute, in which case it will be resolved by the
1190 ;; resolve-references method.
1191 (let* ((item-type (xml-get-attribute-or-nil node 'itemType))
1192 (children (soap-xml-get-children1 node 'xsd:simpleType)))
1193 (if item-type
1194 (if (= (length children) 0)
1195 (setf (soap-xs-simple-type-base type) (soap-l2fq item-type))
1196 (soap-warning
1197 "xsd:list node with itemType has more than zero children: %s"
1198 (soap-xs-type-name type)))
1199 (if (= (length children) 1)
1200 (setf (soap-xs-simple-type-base type)
1201 (soap-xs-parse-simple-type
1202 (car (soap-xml-get-children1 node 'xsd:simpleType))))
1203 (soap-warning "xsd:list node has more than one child %s"
1204 (soap-xs-type-name type))))
1205 (setf (soap-xs-simple-type-is-list type) t)))
1207 (defun soap-xs-add-extension (node type)
1208 "Add the extended type defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1209 (setf (soap-xs-simple-type-base type)
1210 (soap-l2fq (xml-get-attribute node 'base)))
1211 (dolist (attribute (soap-xml-get-children1 node 'xsd:attribute))
1212 (push (soap-xs-parse-attribute attribute)
1213 (soap-xs-type-attributes type)))
1214 (dolist (attribute-group (soap-xml-get-children1 node 'xsd:attributeGroup))
1215 (push (soap-xs-parse-attribute-group attribute-group)
1216 (soap-xs-type-attribute-groups type))))
1218 (defun soap-validate-xs-basic-type (value type)
1219 "Validate VALUE against the basic type TYPE."
1220 (let* ((kind (soap-xs-basic-type-kind type)))
1221 (cl-case kind
1222 ((anyType Array byte[])
1223 value)
1225 (let ((convert (get kind 'rng-xsd-convert)))
1226 (if convert
1227 (if (rng-dt-make-value convert value)
1228 value
1229 (error "Invalid %s: %s" (symbol-name kind) value))
1230 (error "Don't know how to convert %s" kind)))))))
1232 (defun soap-validate-xs-simple-type (value type)
1233 "Validate VALUE against the restrictions of TYPE."
1235 (let* ((base-type (soap-xs-simple-type-base type))
1236 (messages nil))
1237 (if (listp base-type)
1238 (catch 'valid
1239 (dolist (base base-type)
1240 (condition-case error-object
1241 (cond ((soap-xs-simple-type-p base)
1242 (throw 'valid
1243 (soap-validate-xs-simple-type value base)))
1244 ((soap-xs-basic-type-p base)
1245 (throw 'valid
1246 (soap-validate-xs-basic-type value base))))
1247 (error (push (cadr error-object) messages))))
1248 (when messages
1249 (error (mapconcat 'identity (nreverse messages) "; and: "))))
1250 (cl-labels ((fail-with-message (format value)
1251 (push (format format value) messages)
1252 (throw 'invalid nil)))
1253 (catch 'invalid
1254 (let ((enumeration (soap-xs-simple-type-enumeration type)))
1255 (when (and (> (length enumeration) 1)
1256 (not (member value enumeration)))
1257 (fail-with-message "bad value, should be one of %s" enumeration)))
1259 (let ((pattern (soap-xs-simple-type-pattern type)))
1260 (when (and pattern (not (string-match-p pattern value)))
1261 (fail-with-message "bad value, should match pattern %s" pattern)))
1263 (let ((length-range (soap-xs-simple-type-length-range type)))
1264 (when length-range
1265 (unless (stringp value)
1266 (fail-with-message
1267 "bad value, should be a string with length range %s"
1268 length-range))
1269 (when (car length-range)
1270 (unless (>= (length value) (car length-range))
1271 (fail-with-message "short string, should be at least %s chars"
1272 (car length-range))))
1273 (when (cdr length-range)
1274 (unless (<= (length value) (cdr length-range))
1275 (fail-with-message "long string, should be at most %s chars"
1276 (cdr length-range))))))
1278 (let ((integer-range (soap-xs-simple-type-integer-range type)))
1279 (when integer-range
1280 (unless (numberp value)
1281 (fail-with-message "bad value, should be a number with range %s"
1282 integer-range))
1283 (when (car integer-range)
1284 (unless (>= value (car integer-range))
1285 (fail-with-message "small value, should be at least %s"
1286 (car integer-range))))
1287 (when (cdr integer-range)
1288 (unless (<= value (cdr integer-range))
1289 (fail-with-message "big value, should be at most %s"
1290 (cdr integer-range))))))))
1291 (when messages
1292 (error "Xs-simple-type(%s, %s): %s"
1293 value (or (soap-xs-type-name type) (soap-xs-type-id type))
1294 (car messages)))))
1295 ;; Return the validated value.
1296 value)
1298 (defun soap-resolve-references-for-xs-simple-type (type wsdl)
1299 "Replace names in TYPE with the referenced objects in the WSDL.
1300 This is a specialization of `soap-resolve-references' for
1301 `soap-xs-simple-type' objects.
1303 See also `soap-wsdl-resolve-references'."
1305 (let ((namespace (soap-element-namespace-tag type)))
1306 (when namespace
1307 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
1308 (when nstag
1309 (setf (soap-element-namespace-tag type) nstag)))))
1311 (let ((base (soap-xs-simple-type-base type)))
1312 (cond
1313 ((soap-name-p base)
1314 (setf (soap-xs-simple-type-base type)
1315 (soap-wsdl-get base wsdl 'soap-xs-type-p)))
1316 ((soap-xs-type-p base)
1317 (soap-resolve-references base wsdl))
1318 ((listp base)
1319 (setf (soap-xs-simple-type-base type)
1320 (mapcar (lambda (type)
1321 (cond ((soap-name-p type)
1322 (soap-wsdl-get type wsdl 'soap-xs-type-p))
1323 ((soap-xs-type-p type)
1324 (soap-resolve-references type wsdl)
1325 type)
1326 (t ; signal an error?
1327 type)))
1328 base)))
1329 (t (error "Oops"))))
1330 (dolist (attribute (soap-xs-type-attributes type))
1331 (soap-resolve-references attribute wsdl))
1332 (dolist (attribute-group (soap-xs-type-attribute-groups type))
1333 (soap-resolve-references attribute-group wsdl)))
1335 (defun soap-encode-xs-simple-type-attributes (value type)
1336 "Encode the XML attributes for VALUE according to TYPE.
1337 The xsi:type and an optional xsi:nil attributes are added. The
1338 attributes are inserted in the current buffer at the current
1339 position.
1341 This is a specialization of `soap-encode-attributes' for
1342 `soap-xs-simple-type' objects."
1343 (insert " xsi:type=\"" (soap-element-fq-name type) "\"")
1344 (unless value (insert " xsi:nil=\"true\"")))
1346 (defun soap-encode-xs-simple-type (value type)
1347 "Encode the VALUE according to TYPE.
1348 The data is inserted in the current buffer at the current
1349 position.
1351 This is a specialization of `soap-encode-value' for
1352 `soap-xs-simple-type' objects."
1353 (soap-validate-xs-simple-type value type)
1354 (if (soap-xs-simple-type-is-list type)
1355 (progn
1356 (dolist (v (butlast value))
1357 (soap-encode-value v (soap-xs-simple-type-base type))
1358 (insert " "))
1359 (soap-encode-value (car (last value)) (soap-xs-simple-type-base type)))
1360 (soap-encode-value value (soap-xs-simple-type-base type))))
1362 (defun soap-decode-xs-simple-type (type node)
1363 "Use TYPE, a `soap-xs-simple-type', to decode the contents of NODE.
1364 A LISP value is returned based on the contents of NODE and the
1365 type-info stored in TYPE.
1367 This is a specialization of `soap-decode-type' for
1368 `soap-xs-simple-type' objects."
1369 (if (soap-xs-simple-type-is-list type)
1370 ;; Technically, we could construct fake XML NODEs and pass them to
1371 ;; soap-decode-value...
1372 (split-string (car (xml-node-children node)))
1373 (let ((value (soap-decode-type (soap-xs-simple-type-base type) node)))
1374 (soap-validate-xs-simple-type value type))))
1376 ;; Register methods for `soap-xs-simple-type'
1377 (let ((tag (aref (make-soap-xs-simple-type) 0)))
1378 (put tag 'soap-resolve-references
1379 #'soap-resolve-references-for-xs-simple-type)
1380 (put tag 'soap-attribute-encoder #'soap-encode-xs-simple-type-attributes)
1381 (put tag 'soap-encoder #'soap-encode-xs-simple-type)
1382 (put tag 'soap-decoder #'soap-decode-xs-simple-type))
1384 ;;;;; soap-xs-complex-type
1386 (cl-defstruct (soap-xs-complex-type (:include soap-xs-type))
1387 indicator ; sequence, choice, all, array
1388 base
1389 elements
1390 optional?
1391 multiple?
1392 is-group)
1394 (defun soap-xs-parse-complex-type (node)
1395 "Construct a `soap-xs-complex-type' by parsing the XML NODE."
1396 (let ((name (xml-get-attribute-or-nil node 'name))
1397 (id (xml-get-attribute-or-nil node 'id))
1398 (node-name (soap-l2wk (xml-node-name node)))
1399 type
1400 attributes
1401 attribute-groups)
1402 (cl-assert (memq node-name '(xsd:complexType xsd:complexContent xsd:group))
1403 nil "unexpected node: %s" node-name)
1405 (dolist (def (xml-node-children node))
1406 (when (consp def) ; skip text nodes
1407 (cl-case (soap-l2wk (xml-node-name def))
1408 (xsd:attribute (push (soap-xs-parse-attribute def) attributes))
1409 (xsd:attributeGroup
1410 (push (soap-xs-parse-attribute-group def)
1411 attribute-groups))
1412 (xsd:simpleContent (setq type (soap-xs-parse-simple-type def)))
1413 ((xsd:sequence xsd:all xsd:choice)
1414 (setq type (soap-xs-parse-sequence def)))
1415 (xsd:complexContent
1416 (dolist (def (xml-node-children def))
1417 (when (consp def)
1418 (cl-case (soap-l2wk (xml-node-name def))
1419 (xsd:attribute
1420 (push (soap-xs-parse-attribute def) attributes))
1421 (xsd:attributeGroup
1422 (push (soap-xs-parse-attribute-group def)
1423 attribute-groups))
1424 ((xsd:extension xsd:restriction)
1425 (setq type
1426 (soap-xs-parse-extension-or-restriction def)))
1427 ((xsd:sequence xsd:all xsd:choice)
1428 (soap-xs-parse-sequence def)))))))))
1429 (unless type
1430 ;; the type has not been built, this is a shortcut for a simpleContent
1431 ;; node
1432 (setq type (make-soap-xs-complex-type)))
1434 (setf (soap-xs-type-name type) name)
1435 (setf (soap-xs-type-namespace-tag type) soap-target-xmlns)
1436 (setf (soap-xs-type-id type) id)
1437 (setf (soap-xs-type-attributes type)
1438 (append attributes (soap-xs-type-attributes type)))
1439 (setf (soap-xs-type-attribute-groups type)
1440 (append attribute-groups (soap-xs-type-attribute-groups type)))
1441 (when (soap-xs-complex-type-p type)
1442 (setf (soap-xs-complex-type-is-group type)
1443 (eq node-name 'xsd:group)))
1444 type))
1446 (defun soap-xs-parse-sequence (node)
1447 "Parse a sequence definition from XML NODE.
1448 Returns a `soap-xs-complex-type'"
1449 (cl-assert (memq (soap-l2wk (xml-node-name node))
1450 '(xsd:sequence xsd:choice xsd:all))
1452 "unexpected node: %s" (soap-l2wk (xml-node-name node)))
1454 (let ((type (make-soap-xs-complex-type)))
1456 (setf (soap-xs-complex-type-indicator type)
1457 (cl-ecase (soap-l2wk (xml-node-name node))
1458 (xsd:sequence 'sequence)
1459 (xsd:all 'all)
1460 (xsd:choice 'choice)))
1462 (setf (soap-xs-complex-type-optional? type) (soap-node-optional node))
1463 (setf (soap-xs-complex-type-multiple? type) (soap-node-multiple node))
1465 (dolist (r (xml-node-children node))
1466 (unless (stringp r) ; skip the white space
1467 (cl-case (soap-l2wk (xml-node-name r))
1468 ((xsd:element xsd:group)
1469 (push (soap-xs-parse-element r)
1470 (soap-xs-complex-type-elements type)))
1471 ((xsd:sequence xsd:choice xsd:all)
1472 ;; an inline sequence, choice or all node
1473 (let ((choice (soap-xs-parse-sequence r)))
1474 (push (make-soap-xs-element :name nil :type^ choice)
1475 (soap-xs-complex-type-elements type))))
1476 (xsd:attribute
1477 (push (soap-xs-parse-attribute r)
1478 (soap-xs-type-attributes type)))
1479 (xsd:attributeGroup
1480 (push (soap-xs-parse-attribute-group r)
1481 (soap-xs-type-attribute-groups type))))))
1483 (setf (soap-xs-complex-type-elements type)
1484 (nreverse (soap-xs-complex-type-elements type)))
1486 type))
1488 (defun soap-xs-parse-extension-or-restriction (node)
1489 "Parse an extension or restriction definition from XML NODE.
1490 Return a `soap-xs-complex-type'."
1491 (cl-assert (memq (soap-l2wk (xml-node-name node))
1492 '(xsd:extension xsd:restriction))
1494 "unexpected node: %s" (soap-l2wk (xml-node-name node)))
1495 (let (type
1496 attributes
1497 attribute-groups
1498 array?
1499 (base (xml-get-attribute-or-nil node 'base)))
1501 ;; Array declarations are recognized specially, it is unclear to me how
1502 ;; they could be treated generally...
1503 (setq array?
1504 (and (eq (soap-l2wk (xml-node-name node)) 'xsd:restriction)
1505 (equal base (soap-wk2l "soapenc:Array"))))
1507 (dolist (def (xml-node-children node))
1508 (when (consp def) ; skip text nodes
1509 (cl-case (soap-l2wk (xml-node-name def))
1510 ((xsd:sequence xsd:choice xsd:all)
1511 (setq type (soap-xs-parse-sequence def)))
1512 (xsd:attribute
1513 (if array?
1514 (let ((array-type
1515 (soap-xml-get-attribute-or-nil1 def 'wsdl:arrayType)))
1516 (when (and array-type
1517 (string-match "^\\(.*\\)\\[\\]$" array-type))
1518 ;; Override
1519 (setq base (match-string 1 array-type))))
1520 ;; else
1521 (push (soap-xs-parse-attribute def) attributes)))
1522 (xsd:attributeGroup
1523 (push (soap-xs-parse-attribute-group def) attribute-groups)))))
1525 (unless type
1526 (setq type (make-soap-xs-complex-type))
1527 (when array?
1528 (setf (soap-xs-complex-type-indicator type) 'array)))
1530 (setf (soap-xs-complex-type-base type) (soap-l2fq base))
1531 (setf (soap-xs-complex-type-attributes type) attributes)
1532 (setf (soap-xs-complex-type-attribute-groups type) attribute-groups)
1533 type))
1535 (defun soap-resolve-references-for-xs-complex-type (type wsdl)
1536 "Replace names in TYPE with the referenced objects in the WSDL.
1537 This is a specialization of `soap-resolve-references' for
1538 `soap-xs-complex-type' objects.
1540 See also `soap-wsdl-resolve-references'."
1542 (let ((namespace (soap-element-namespace-tag type)))
1543 (when namespace
1544 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
1545 (when nstag
1546 (setf (soap-element-namespace-tag type) nstag)))))
1548 (let ((base (soap-xs-complex-type-base type)))
1549 (cond ((soap-name-p base)
1550 (setf (soap-xs-complex-type-base type)
1551 (soap-wsdl-get base wsdl 'soap-xs-type-p)))
1552 ((soap-xs-type-p base)
1553 (soap-resolve-references base wsdl))))
1554 (let (all-elements)
1555 (dolist (element (soap-xs-complex-type-elements type))
1556 (if (soap-xs-element-is-group element)
1557 ;; This is an xsd:group element that references an xsd:group node,
1558 ;; which we treat as a complex type. We replace the reference
1559 ;; element by inlining the elements of the referenced xsd:group
1560 ;; (complex type) node.
1561 (let ((type (soap-wsdl-get
1562 (soap-xs-element-reference element)
1563 wsdl (lambda (type)
1564 (and
1565 (soap-xs-complex-type-p type)
1566 (soap-xs-complex-type-is-group type))))))
1567 (dolist (element (soap-xs-complex-type-elements type))
1568 (soap-resolve-references element wsdl)
1569 (push element all-elements)))
1570 ;; This is a non-xsd:group node so just add it directly.
1571 (soap-resolve-references element wsdl)
1572 (push element all-elements)))
1573 (setf (soap-xs-complex-type-elements type) (nreverse all-elements)))
1574 (dolist (attribute (soap-xs-type-attributes type))
1575 (soap-resolve-references attribute wsdl))
1576 (dolist (attribute-group (soap-xs-type-attribute-groups type))
1577 (soap-resolve-references attribute-group wsdl)))
1579 (defun soap-encode-xs-complex-type-attributes (value type)
1580 "Encode the XML attributes for encoding VALUE according to TYPE.
1581 The xsi:type and optional xsi:nil attributes are added, plus
1582 additional attributes needed for arrays types, if applicable. The
1583 attributes are inserted in the current buffer at the current
1584 position.
1586 This is a specialization of `soap-encode-attributes' for
1587 `soap-xs-complex-type' objects."
1588 (if (eq (soap-xs-complex-type-indicator type) 'array)
1589 (let ((element-type (soap-xs-complex-type-base type)))
1590 (insert " xsi:type=\"soapenc:Array\"")
1591 (insert " soapenc:arrayType=\""
1592 (soap-element-fq-name element-type)
1593 "[" (format "%s" (length value)) "]" "\""))
1594 ;; else
1595 (progn
1596 (dolist (a (soap-get-xs-attributes type))
1597 (let ((element-name (soap-element-name a)))
1598 (if (soap-xs-attribute-default a)
1599 (insert " " element-name
1600 "=\"" (soap-xs-attribute-default a) "\"")
1601 (dolist (value-pair value)
1602 (when (equal element-name (symbol-name (car value-pair)))
1603 (insert " " element-name
1604 "=\"" (cdr value-pair) "\""))))))
1605 ;; If this is not an empty type, and we have no value, mark it as nil
1606 (when (and (soap-xs-complex-type-indicator type) (null value))
1607 (insert " xsi:nil=\"true\"")))))
1609 (defun soap-get-candidate-elements (element)
1610 "Return a list of elements that are compatible with ELEMENT.
1611 The returned list includes ELEMENT's references and
1612 alternatives."
1613 (let ((reference (soap-xs-element-reference element)))
1614 ;; If the element is a reference, append the reference and its
1615 ;; alternatives...
1616 (if reference
1617 (append (list reference)
1618 (soap-xs-element-alternatives reference))
1619 ;; ...otherwise append the element itself and its alternatives.
1620 (append (list element)
1621 (soap-xs-element-alternatives element)))))
1623 (defun soap-encode-xs-complex-type (value type)
1624 "Encode the VALUE according to TYPE.
1625 The data is inserted in the current buffer at the current
1626 position.
1628 This is a specialization of `soap-encode-value' for
1629 `soap-xs-complex-type' objects."
1630 (cl-case (soap-xs-complex-type-indicator type)
1631 (array
1632 (error "Arrays of type soap-encode-xs-complex-type are handled elsewhere"))
1633 ((sequence choice all nil)
1634 (let ((type-list (list type)))
1636 ;; Collect all base types
1637 (let ((base (soap-xs-complex-type-base type)))
1638 (while base
1639 (push base type-list)
1640 (setq base (soap-xs-complex-type-base base))))
1642 (dolist (type type-list)
1643 (dolist (element (soap-xs-complex-type-elements type))
1644 (catch 'done
1645 (let ((instance-count 0))
1646 (dolist (candidate (soap-get-candidate-elements element))
1647 (let ((e-name (soap-xs-element-name candidate)))
1648 (if e-name
1649 (let ((e-name (intern e-name)))
1650 (dolist (v value)
1651 (when (equal (car v) e-name)
1652 (cl-incf instance-count)
1653 (soap-encode-value (cdr v) candidate))))
1654 (if (soap-xs-complex-type-indicator type)
1655 (let ((current-point (point)))
1656 ;; Check if encoding happened by checking if
1657 ;; characters were inserted in the buffer.
1658 (soap-encode-value value candidate)
1659 (when (not (equal current-point (point)))
1660 (cl-incf instance-count)))
1661 (dolist (v value)
1662 (let ((current-point (point)))
1663 (soap-encode-value v candidate)
1664 (when (not (equal current-point (point)))
1665 (cl-incf instance-count))))))))
1666 ;; Do some sanity checking
1667 (let* ((indicator (soap-xs-complex-type-indicator type))
1668 (element-type (soap-xs-element-type element))
1669 (reference (soap-xs-element-reference element))
1670 (e-name (or (soap-xs-element-name element)
1671 (and reference
1672 (soap-xs-element-name reference)))))
1673 (cond ((and (eq indicator 'choice)
1674 (> instance-count 0))
1675 ;; This was a choice node and we encoded
1676 ;; one instance.
1677 (throw 'done t))
1678 ((and (not (eq indicator 'choice))
1679 (= instance-count 0)
1680 (not (soap-xs-element-optional? element))
1681 (and (soap-xs-complex-type-p element-type)
1682 (not (soap-xs-complex-type-optional-p
1683 element-type))))
1684 (soap-warning
1685 "While encoding %s: missing non-nillable slot %s"
1686 value e-name))
1687 ((and (> instance-count 1)
1688 (not (soap-xs-element-multiple? element))
1689 (and (soap-xs-complex-type-p element-type)
1690 (not (soap-xs-complex-type-multiple-p
1691 element-type))))
1692 (soap-warning
1693 (concat "While encoding %s: expected single,"
1694 " found multiple elements for slot %s")
1695 value e-name))))))))))
1697 (error "Don't know how to encode complex type: %s"
1698 (soap-xs-complex-type-indicator type)))))
1700 (defun soap-xml-get-children-fq (node child-name)
1701 "Return the children of NODE named CHILD-NAME.
1702 This is the same as `xml-get-children1', but NODE's local
1703 namespace is used to resolve the children's namespace tags."
1704 (let (result)
1705 (dolist (c (xml-node-children node))
1706 (when (and (consp c)
1707 (soap-with-local-xmlns node
1708 ;; We use `ignore-errors' here because we want to silently
1709 ;; skip nodes for which we cannot convert them to a
1710 ;; well-known name.
1711 (equal (ignore-errors
1712 (soap-l2fq (xml-node-name c)))
1713 child-name)))
1714 (push c result)))
1715 (nreverse result)))
1717 (defun soap-xs-element-get-fq-name (element wsdl)
1718 "Return ELEMENT's fully-qualified name using WSDL's alias table.
1719 Return nil if ELEMENT does not have a name."
1720 (let* ((ns-aliases (soap-wsdl-alias-table wsdl))
1721 (ns-name (cdr (assoc
1722 (soap-element-namespace-tag element)
1723 ns-aliases))))
1724 (when ns-name
1725 (cons ns-name (soap-element-name element)))))
1727 (defun soap-xs-complex-type-optional-p (type)
1728 "Return t if TYPE or any of TYPE's ancestor types is optional.
1729 Return nil otherwise."
1730 (when type
1731 (or (soap-xs-complex-type-optional? type)
1732 (and (soap-xs-complex-type-p type)
1733 (soap-xs-complex-type-optional-p
1734 (soap-xs-complex-type-base type))))))
1736 (defun soap-xs-complex-type-multiple-p (type)
1737 "Return t if TYPE or any of TYPE's ancestor types permits multiple elements.
1738 Return nil otherwise."
1739 (when type
1740 (or (soap-xs-complex-type-multiple? type)
1741 (and (soap-xs-complex-type-p type)
1742 (soap-xs-complex-type-multiple-p
1743 (soap-xs-complex-type-base type))))))
1745 (defun soap-get-xs-attributes-from-groups (attribute-groups)
1746 "Return a list of attributes from all ATTRIBUTE-GROUPS."
1747 (let (attributes)
1748 (dolist (group attribute-groups)
1749 (let ((sub-groups (soap-xs-attribute-group-attribute-groups group)))
1750 (setq attributes (append attributes
1751 (soap-get-xs-attributes-from-groups sub-groups)
1752 (soap-xs-attribute-group-attributes group)))))
1753 attributes))
1755 (defun soap-get-xs-attributes (type)
1756 "Return a list of all of TYPE's and TYPE's ancestors' attributes."
1757 (let* ((base (and (soap-xs-complex-type-p type)
1758 (soap-xs-complex-type-base type)))
1759 (attributes (append (soap-xs-type-attributes type)
1760 (soap-get-xs-attributes-from-groups
1761 (soap-xs-type-attribute-groups type)))))
1762 (if base
1763 (append attributes (soap-get-xs-attributes base))
1764 attributes)))
1766 (defun soap-decode-xs-attributes (type node)
1767 "Use TYPE, a `soap-xs-complex-type', to decode the attributes of NODE."
1768 (let (result)
1769 (dolist (attribute (soap-get-xs-attributes type))
1770 (let* ((name (soap-xs-attribute-name attribute))
1771 (attribute-type (soap-xs-attribute-type attribute))
1772 (symbol (intern name))
1773 (value (xml-get-attribute-or-nil node symbol)))
1774 ;; We don't support attribute uses: required, optional, prohibited.
1775 (cond
1776 ((soap-xs-basic-type-p attribute-type)
1777 ;; Basic type values are validated by xml.el.
1778 (when value
1779 (push (cons symbol
1780 ;; Create a fake XML node to satisfy the
1781 ;; soap-decode-xs-basic-type API.
1782 (soap-decode-xs-basic-type attribute-type
1783 (list symbol nil value)))
1784 result)))
1785 ((soap-xs-simple-type-p attribute-type)
1786 (when value
1787 (push (cons symbol
1788 (soap-validate-xs-simple-type value attribute-type))
1789 result)))
1791 (error (concat "Attribute %s is of type %s which is"
1792 " not a basic or simple type")
1793 name (soap-name-p attribute))))))
1794 result))
1796 (defun soap-decode-xs-complex-type (type node)
1797 "Use TYPE, a `soap-xs-complex-type', to decode the contents of NODE.
1798 A LISP value is returned based on the contents of NODE and the
1799 type-info stored in TYPE.
1801 This is a specialization of `soap-decode-type' for
1802 `soap-xs-basic-type' objects."
1803 (cl-case (soap-xs-complex-type-indicator type)
1804 (array
1805 (let ((result nil)
1806 (element-type (soap-xs-complex-type-base type)))
1807 (dolist (node (xml-node-children node))
1808 (when (consp node)
1809 (push (soap-decode-type element-type node) result)))
1810 (nreverse result)))
1811 ((sequence choice all nil)
1812 (let ((result nil)
1813 (base (soap-xs-complex-type-base type)))
1814 (when base
1815 (setq result (nreverse (soap-decode-type base node))))
1816 (catch 'done
1817 (dolist (element (soap-xs-complex-type-elements type))
1818 (let* ((instance-count 0)
1819 (e-name (soap-xs-element-name element))
1820 ;; Heuristic: guess if we need to decode using local
1821 ;; namespaces.
1822 (use-fq-names (string-match ":" (symbol-name (car node))))
1823 (children (if e-name
1824 (if use-fq-names
1825 ;; Find relevant children
1826 ;; using local namespaces by
1827 ;; searching for the element's
1828 ;; fully-qualified name.
1829 (soap-xml-get-children-fq
1830 node
1831 (soap-xs-element-get-fq-name
1832 element soap-current-wsdl))
1833 ;; No local namespace resolution
1834 ;; needed so use the element's
1835 ;; name unqualified.
1836 (xml-get-children node (intern e-name)))
1837 ;; e-name is nil so a) we don't know which
1838 ;; children to operate on, and b) we want to
1839 ;; re-use soap-decode-xs-complex-type, which
1840 ;; expects a node argument with a complex
1841 ;; type; therefore we need to operate on the
1842 ;; entire node. We wrap node in a list so
1843 ;; that it will carry through as "node" in the
1844 ;; loop below.
1846 ;; For example:
1848 ;; Element Type:
1849 ;; <xs:complexType name="A">
1850 ;; <xs:sequence>
1851 ;; <xs:element name="B" type="t:BType"/>
1852 ;; <xs:choice>
1853 ;; <xs:element name="C" type="xs:string"/>
1854 ;; <xs:element name="D" type="t:DType"/>
1855 ;; </xs:choice>
1856 ;; </xs:sequence>
1857 ;; </xs:complexType>
1859 ;; Node:
1860 ;; <t:A>
1861 ;; <t:B tag="b"/>
1862 ;; <t:C>1</C>
1863 ;; </t:A>
1865 ;; soap-decode-type will be called below with:
1867 ;; element =
1868 ;; <xs:choice>
1869 ;; <xs:element name="C" type="xs:string"/>
1870 ;; <xs:element name="D" type="t:DType"/>
1871 ;; </xs:choice>
1872 ;; node =
1873 ;; <t:A>
1874 ;; <t:B tag="b"/>
1875 ;; <t:C>1</C>
1876 ;; </t:A>
1877 (list node)))
1878 (element-type (soap-xs-element-type element)))
1879 (dolist (node children)
1880 (cl-incf instance-count)
1881 (let* ((attributes
1882 (soap-decode-xs-attributes element-type node))
1883 ;; Attributes may specify xsi:type override.
1884 (element-type
1885 (if (soap-xml-get-attribute-or-nil1 node 'xsi:type)
1886 (soap-wsdl-get
1887 (soap-l2fq
1888 (soap-xml-get-attribute-or-nil1 node
1889 'xsi:type))
1890 soap-current-wsdl 'soap-xs-type-p t)
1891 element-type))
1892 (decoded-child (soap-decode-type element-type node)))
1893 (if e-name
1894 (push (cons (intern e-name)
1895 (append attributes decoded-child)) result)
1896 ;; When e-name is nil we don't want to introduce an extra
1897 ;; level of nesting, so we splice the decoding into
1898 ;; result.
1899 (setq result (append decoded-child result)))))
1900 (cond ((and (eq (soap-xs-complex-type-indicator type) 'choice)
1901 ;; Choices can allow multiple values.
1902 (not (soap-xs-complex-type-multiple-p type))
1903 (> instance-count 0))
1904 ;; This was a choice node, and we decoded one value.
1905 (throw 'done t))
1907 ;; Do some sanity checking
1908 ((and (not (eq (soap-xs-complex-type-indicator type)
1909 'choice))
1910 (= instance-count 0)
1911 (not (soap-xs-element-optional? element))
1912 (and (soap-xs-complex-type-p element-type)
1913 (not (soap-xs-complex-type-optional-p
1914 element-type))))
1915 (soap-warning "missing non-nillable slot %s" e-name))
1916 ((and (> instance-count 1)
1917 (not (soap-xs-complex-type-multiple-p type))
1918 (not (soap-xs-element-multiple? element))
1919 (and (soap-xs-complex-type-p element-type)
1920 (not (soap-xs-complex-type-multiple-p
1921 element-type))))
1922 (soap-warning "expected single %s slot, found multiple"
1923 e-name))))))
1924 (nreverse result)))
1926 (error "Don't know how to decode complex type: %s"
1927 (soap-xs-complex-type-indicator type)))))
1929 ;; Register methods for `soap-xs-complex-type'
1930 (let ((tag (aref (make-soap-xs-complex-type) 0)))
1931 (put tag 'soap-resolve-references
1932 #'soap-resolve-references-for-xs-complex-type)
1933 (put tag 'soap-attribute-encoder #'soap-encode-xs-complex-type-attributes)
1934 (put tag 'soap-encoder #'soap-encode-xs-complex-type)
1935 (put tag 'soap-decoder #'soap-decode-xs-complex-type))
1937 ;;;; WSDL documents
1938 ;;;;; WSDL document elements
1941 (cl-defstruct (soap-message (:include soap-element))
1942 parts ; ALIST of NAME => WSDL-TYPE name
1945 (cl-defstruct (soap-operation (:include soap-element))
1946 parameter-order
1947 input ; (NAME . MESSAGE)
1948 output ; (NAME . MESSAGE)
1949 faults ; a list of (NAME . MESSAGE)
1950 input-action ; WS-addressing action string
1951 output-action) ; WS-addressing action string
1953 (cl-defstruct (soap-port-type (:include soap-element))
1954 operations) ; a namespace of operations
1956 ;; A bound operation is an operation which has a soap action and a use
1957 ;; method attached -- these are attached as part of a binding and we
1958 ;; can have different bindings for the same operations.
1959 (cl-defstruct soap-bound-operation
1960 operation ; SOAP-OPERATION
1961 soap-action ; value for SOAPAction HTTP header
1962 soap-headers ; list of (message part use)
1963 soap-body ; message parts present in the body
1964 use ; 'literal or 'encoded, see
1965 ; http://www.w3.org/TR/wsdl#_soap:body
1968 (cl-defstruct (soap-binding (:include soap-element))
1969 port-type
1970 (operations (make-hash-table :test 'equal) :readonly t))
1972 (cl-defstruct (soap-port (:include soap-element))
1973 service-url
1974 binding)
1977 ;;;;; The WSDL document
1979 ;; The WSDL data structure used for encoding/decoding SOAP messages
1980 (cl-defstruct (soap-wsdl
1981 ;; NOTE: don't call this constructor, see `soap-make-wsdl'
1982 (:constructor soap-make-wsdl^)
1983 (:copier soap-copy-wsdl))
1984 origin ; file or URL from which this wsdl was loaded
1985 current-file ; most-recently fetched file or URL
1986 xmlschema-imports ; a list of schema imports
1987 ports ; a list of SOAP-PORT instances
1988 alias-table ; a list of namespace aliases
1989 namespaces ; a list of namespaces
1992 (defun soap-make-wsdl (origin)
1993 "Create a new WSDL document, loaded from ORIGIN, and initialize it."
1994 (let ((wsdl (soap-make-wsdl^ :origin origin)))
1996 ;; Add the XSD types to the wsdl document
1997 (let ((ns (soap-make-xs-basic-types
1998 "http://www.w3.org/2001/XMLSchema" "xsd")))
1999 (soap-wsdl-add-namespace ns wsdl)
2000 (soap-wsdl-add-alias "xsd" (soap-namespace-name ns) wsdl))
2002 ;; Add the soapenc types to the wsdl document
2003 (let ((ns (soap-make-xs-basic-types
2004 "http://schemas.xmlsoap.org/soap/encoding/" "soapenc")))
2005 (soap-wsdl-add-namespace ns wsdl)
2006 (soap-wsdl-add-alias "soapenc" (soap-namespace-name ns) wsdl))
2008 wsdl))
2010 (defun soap-wsdl-add-alias (alias name wsdl)
2011 "Add a namespace ALIAS for NAME to the WSDL document."
2012 (let ((existing (assoc alias (soap-wsdl-alias-table wsdl))))
2013 (if existing
2014 (unless (equal (cdr existing) name)
2015 (warn "Redefining alias %s from %s to %s"
2016 alias (cdr existing) name)
2017 (push (cons alias name) (soap-wsdl-alias-table wsdl)))
2018 (push (cons alias name) (soap-wsdl-alias-table wsdl)))))
2020 (defun soap-wsdl-find-namespace (name wsdl)
2021 "Find a namespace by NAME in the WSDL document."
2022 (catch 'found
2023 (dolist (ns (soap-wsdl-namespaces wsdl))
2024 (when (equal name (soap-namespace-name ns))
2025 (throw 'found ns)))))
2027 (defun soap-wsdl-add-namespace (ns wsdl)
2028 "Add the namespace NS to the WSDL document.
2029 If a namespace by this name already exists in WSDL, individual
2030 elements will be added to it."
2031 (let ((existing (soap-wsdl-find-namespace (soap-namespace-name ns) wsdl)))
2032 (if existing
2033 ;; Add elements from NS to EXISTING, replacing existing values.
2034 (maphash (lambda (_key value)
2035 (dolist (v value)
2036 (soap-namespace-put v existing)))
2037 (soap-namespace-elements ns))
2038 (push ns (soap-wsdl-namespaces wsdl)))))
2040 (defun soap-wsdl-get (name wsdl &optional predicate use-local-alias-table)
2041 "Retrieve element NAME from the WSDL document.
2043 PREDICATE is used to differentiate between elements when NAME
2044 refers to multiple elements. A typical value for this would be a
2045 structure predicate for the type of element you want to retrieve.
2046 For example, to retrieve a message named \"foo\" when other
2047 elements named \"foo\" exist in the WSDL you could use:
2049 (soap-wsdl-get \"foo\" WSDL \\='soap-message-p)
2051 If USE-LOCAL-ALIAS-TABLE is not nil, `soap-local-xmlns' will be
2052 used to resolve the namespace alias."
2053 (let ((alias-table (soap-wsdl-alias-table wsdl))
2054 namespace element-name element)
2056 (when (symbolp name)
2057 (setq name (symbol-name name)))
2059 (when use-local-alias-table
2060 (setq alias-table (append soap-local-xmlns alias-table)))
2062 (cond ((consp name) ; a fully qualified name, as returned by `soap-l2fq'
2063 (setq element-name (cdr name))
2064 (when (symbolp element-name)
2065 (setq element-name (symbol-name element-name)))
2066 (setq namespace (soap-wsdl-find-namespace (car name) wsdl))
2067 (unless namespace
2068 (error "Soap-wsdl-get(%s): unknown namespace: %s" name namespace)))
2070 ((string-match "^\\(.*\\):\\(.*\\)$" name)
2071 (setq element-name (match-string 2 name))
2073 (let* ((ns-alias (match-string 1 name))
2074 (ns-name (cdr (assoc ns-alias alias-table))))
2075 (unless ns-name
2076 (error "Soap-wsdl-get(%s): cannot find namespace alias %s"
2077 name ns-alias))
2079 (setq namespace (soap-wsdl-find-namespace ns-name wsdl))
2080 (unless namespace
2081 (error
2082 "Soap-wsdl-get(%s): unknown namespace %s, referenced as %s"
2083 name ns-name ns-alias))))
2085 (error "Soap-wsdl-get(%s): bad name" name)))
2087 (setq element (soap-namespace-get
2088 element-name namespace
2089 (if predicate
2090 (lambda (e)
2091 (or (funcall 'soap-namespace-link-p e)
2092 (funcall predicate e)))
2093 nil)))
2095 (unless element
2096 (error "Soap-wsdl-get(%s): cannot find element" name))
2098 (if (soap-namespace-link-p element)
2099 ;; NOTE: don't use the local alias table here
2100 (soap-wsdl-get (soap-namespace-link-target element) wsdl predicate)
2101 element)))
2103 ;;;;; soap-parse-schema
2105 (defun soap-parse-schema (node wsdl)
2106 "Parse a schema NODE, placing the results in WSDL.
2107 Return a SOAP-NAMESPACE containing the elements."
2108 (soap-with-local-xmlns node
2109 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
2111 "expecting an xsd:schema node, got %s"
2112 (soap-l2wk (xml-node-name node)))
2114 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
2116 (dolist (def (xml-node-children node))
2117 (unless (stringp def) ; skip text nodes
2118 (cl-case (soap-l2wk (xml-node-name def))
2119 (xsd:import
2120 ;; Imports will be processed later
2121 ;; NOTE: we should expand the location now!
2122 (let ((location (or
2123 (xml-get-attribute-or-nil def 'schemaLocation)
2124 (xml-get-attribute-or-nil def 'location))))
2125 (when location
2126 (push location (soap-wsdl-xmlschema-imports wsdl)))))
2127 (xsd:element
2128 (soap-namespace-put (soap-xs-parse-element def) ns))
2129 (xsd:attribute
2130 (soap-namespace-put (soap-xs-parse-attribute def) ns))
2131 (xsd:attributeGroup
2132 (soap-namespace-put (soap-xs-parse-attribute-group def) ns))
2133 (xsd:simpleType
2134 (soap-namespace-put (soap-xs-parse-simple-type def) ns))
2135 ((xsd:complexType xsd:group)
2136 (soap-namespace-put (soap-xs-parse-complex-type def) ns)))))
2137 ns)))
2139 ;;;;; Resolving references for wsdl types
2141 ;; See `soap-wsdl-resolve-references', which is the main entry point for
2142 ;; resolving references
2144 (defun soap-resolve-references (element wsdl)
2145 "Replace names in ELEMENT with the referenced objects in the WSDL.
2146 This is a generic function which invokes a specific resolver
2147 function depending on the type of the ELEMENT.
2149 If ELEMENT has no resolver function, it is silently ignored."
2150 (let ((resolver (get (aref element 0) 'soap-resolve-references)))
2151 (when resolver
2152 (funcall resolver element wsdl))))
2154 (defun soap-resolve-references-for-message (message wsdl)
2155 "Replace names in MESSAGE with the referenced objects in the WSDL.
2156 This is a generic function, called by `soap-resolve-references',
2157 you should use that function instead.
2159 See also `soap-wsdl-resolve-references'."
2160 (let (resolved-parts)
2161 (dolist (part (soap-message-parts message))
2162 (let ((name (car part))
2163 (element (cdr part)))
2164 (when (stringp name)
2165 (setq name (intern name)))
2166 (if (soap-name-p element)
2167 (setq element (soap-wsdl-get
2168 element wsdl
2169 (lambda (x)
2170 (or (soap-xs-type-p x) (soap-xs-element-p x)))))
2171 ;; else, inline element, resolve recursively, as the element
2172 ;; won't be reached.
2173 (soap-resolve-references element wsdl)
2174 (unless (soap-element-namespace-tag element)
2175 (setf (soap-element-namespace-tag element)
2176 (soap-element-namespace-tag message))))
2177 (push (cons name element) resolved-parts)))
2178 (setf (soap-message-parts message) (nreverse resolved-parts))))
2180 (defun soap-resolve-references-for-operation (operation wsdl)
2181 "Resolve references for an OPERATION type using the WSDL document.
2182 See also `soap-resolve-references' and
2183 `soap-wsdl-resolve-references'"
2185 (let ((namespace (soap-element-namespace-tag operation)))
2186 (when namespace
2187 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
2188 (when nstag
2189 (setf (soap-element-namespace-tag operation) nstag)))))
2191 (let ((input (soap-operation-input operation))
2192 (counter 0))
2193 (let ((name (car input))
2194 (message (cdr input)))
2195 ;; Name this part if it was not named
2196 (when (or (null name) (equal name ""))
2197 (setq name (format "in%d" (cl-incf counter))))
2198 (when (soap-name-p message)
2199 (setf (soap-operation-input operation)
2200 (cons (intern name)
2201 (soap-wsdl-get message wsdl 'soap-message-p))))))
2203 (let ((output (soap-operation-output operation))
2204 (counter 0))
2205 (let ((name (car output))
2206 (message (cdr output)))
2207 (when (or (null name) (equal name ""))
2208 (setq name (format "out%d" (cl-incf counter))))
2209 (when (soap-name-p message)
2210 (setf (soap-operation-output operation)
2211 (cons (intern name)
2212 (soap-wsdl-get message wsdl 'soap-message-p))))))
2214 (let ((resolved-faults nil)
2215 (counter 0))
2216 (dolist (fault (soap-operation-faults operation))
2217 (let ((name (car fault))
2218 (message (cdr fault)))
2219 (when (or (null name) (equal name ""))
2220 (setq name (format "fault%d" (cl-incf counter))))
2221 (if (soap-name-p message)
2222 (push (cons (intern name)
2223 (soap-wsdl-get message wsdl 'soap-message-p))
2224 resolved-faults)
2225 (push fault resolved-faults))))
2226 (setf (soap-operation-faults operation) resolved-faults))
2228 (when (= (length (soap-operation-parameter-order operation)) 0)
2229 (setf (soap-operation-parameter-order operation)
2230 (mapcar 'car (soap-message-parts
2231 (cdr (soap-operation-input operation))))))
2233 (setf (soap-operation-parameter-order operation)
2234 (mapcar (lambda (p)
2235 (if (stringp p)
2236 (intern p)
2238 (soap-operation-parameter-order operation))))
2240 (defun soap-resolve-references-for-binding (binding wsdl)
2241 "Resolve references for a BINDING type using the WSDL document.
2242 See also `soap-resolve-references' and
2243 `soap-wsdl-resolve-references'"
2244 (when (soap-name-p (soap-binding-port-type binding))
2245 (setf (soap-binding-port-type binding)
2246 (soap-wsdl-get (soap-binding-port-type binding)
2247 wsdl 'soap-port-type-p)))
2249 (let ((port-ops (soap-port-type-operations (soap-binding-port-type binding))))
2250 (maphash (lambda (k v)
2251 (setf (soap-bound-operation-operation v)
2252 (soap-namespace-get k port-ops 'soap-operation-p))
2253 (let (resolved-headers)
2254 (dolist (h (soap-bound-operation-soap-headers v))
2255 (push (list (soap-wsdl-get (nth 0 h) wsdl)
2256 (intern (nth 1 h))
2257 (nth 2 h))
2258 resolved-headers))
2259 (setf (soap-bound-operation-soap-headers v)
2260 (nreverse resolved-headers))))
2261 (soap-binding-operations binding))))
2263 (defun soap-resolve-references-for-port (port wsdl)
2264 "Replace names in PORT with the referenced objects in the WSDL.
2265 This is a generic function, called by `soap-resolve-references',
2266 you should use that function instead.
2268 See also `soap-wsdl-resolve-references'."
2269 (when (soap-name-p (soap-port-binding port))
2270 (setf (soap-port-binding port)
2271 (soap-wsdl-get (soap-port-binding port) wsdl 'soap-binding-p))))
2273 ;; Install resolvers for our types
2274 (progn
2275 (put (aref (make-soap-message) 0) 'soap-resolve-references
2276 'soap-resolve-references-for-message)
2277 (put (aref (make-soap-operation) 0) 'soap-resolve-references
2278 'soap-resolve-references-for-operation)
2279 (put (aref (make-soap-binding) 0) 'soap-resolve-references
2280 'soap-resolve-references-for-binding)
2281 (put (aref (make-soap-port) 0) 'soap-resolve-references
2282 'soap-resolve-references-for-port))
2284 (defun soap-wsdl-resolve-references (wsdl)
2285 "Resolve all references inside the WSDL structure.
2287 When the WSDL elements are created from the XML document, they
2288 refer to each other by name. For example, the ELEMENT-TYPE slot
2289 of an SOAP-ARRAY-TYPE will contain the name of the element and
2290 the user would have to call `soap-wsdl-get' to obtain the actual
2291 element.
2293 After the entire document is loaded, we resolve all these
2294 references to the actual elements they refer to so that at
2295 runtime, we don't have to call `soap-wsdl-get' each time we
2296 traverse an element tree."
2297 (let ((nprocessed 0)
2298 (nstag-id 0)
2299 (alias-table (soap-wsdl-alias-table wsdl)))
2300 (dolist (ns (soap-wsdl-namespaces wsdl))
2301 (let ((nstag (car-safe (rassoc (soap-namespace-name ns) alias-table))))
2302 (unless nstag
2303 ;; If this namespace does not have an alias, create one for it.
2304 (catch 'done
2305 (while t
2306 (setq nstag (format "ns%d" (cl-incf nstag-id)))
2307 (unless (assoc nstag alias-table)
2308 (soap-wsdl-add-alias nstag (soap-namespace-name ns) wsdl)
2309 (throw 'done t)))))
2311 (maphash (lambda (_name element)
2312 (cond ((soap-element-p element) ; skip links
2313 (cl-incf nprocessed)
2314 (soap-resolve-references element wsdl))
2315 ((listp element)
2316 (dolist (e element)
2317 (when (soap-element-p e)
2318 (cl-incf nprocessed)
2319 (soap-resolve-references e wsdl))))))
2320 (soap-namespace-elements ns)))))
2321 wsdl)
2323 ;;;;; Loading WSDL from XML documents
2325 (defun soap-parse-server-response ()
2326 "Error-check and parse the XML contents of the current buffer."
2327 (let ((mime-part (mm-dissect-buffer t t)))
2328 (unless mime-part
2329 (error "Failed to decode response from server"))
2330 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
2331 (error "Server response is not an XML document"))
2332 (with-temp-buffer
2333 (mm-insert-part mime-part)
2334 (prog1
2335 (car (xml-parse-region (point-min) (point-max)))
2336 (kill-buffer)
2337 (mm-destroy-part mime-part)))))
2339 (defvar url-http-response-status)
2341 (defun soap-fetch-xml-from-url (url wsdl)
2342 "Load an XML document from URL and return it.
2343 The previously parsed URL is read from WSDL."
2344 (message "Fetching from %s" url)
2345 (let ((current-file (url-expand-file-name url (soap-wsdl-current-file wsdl)))
2346 (url-request-method "GET")
2347 (url-package-name "soap-client.el")
2348 (url-package-version "1.0")
2349 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
2350 (url-http-attempt-keepalives t))
2351 (setf (soap-wsdl-current-file wsdl) current-file)
2352 (let ((buffer (url-retrieve-synchronously current-file)))
2353 (with-current-buffer buffer
2354 (if (> url-http-response-status 299)
2355 (error "Error retrieving WSDL: %s" url-http-response-status))
2356 (soap-parse-server-response)))))
2358 (defun soap-fetch-xml-from-file (file wsdl)
2359 "Load an XML document from FILE and return it.
2360 The previously parsed file is read from WSDL."
2361 (let* ((current-file (soap-wsdl-current-file wsdl))
2362 (expanded-file (expand-file-name file
2363 (if current-file
2364 (file-name-directory current-file)
2365 default-directory))))
2366 (setf (soap-wsdl-current-file wsdl) expanded-file)
2367 (with-temp-buffer
2368 (insert-file-contents expanded-file)
2369 (car (xml-parse-region (point-min) (point-max))))))
2371 (defun soap-fetch-xml (file-or-url wsdl)
2372 "Load an XML document from FILE-OR-URL and return it.
2373 The previously parsed file or URL is read from WSDL."
2374 (let ((current-file (or (soap-wsdl-current-file wsdl) file-or-url)))
2375 (if (or (and current-file (file-exists-p current-file))
2376 (file-exists-p file-or-url))
2377 (soap-fetch-xml-from-file file-or-url wsdl)
2378 (soap-fetch-xml-from-url file-or-url wsdl))))
2380 (defun soap-load-wsdl (file-or-url &optional wsdl)
2381 "Load a document from FILE-OR-URL and return it.
2382 Build on WSDL if it is provided."
2383 (let* ((wsdl (or wsdl (soap-make-wsdl file-or-url)))
2384 (xml (soap-fetch-xml file-or-url wsdl)))
2385 (soap-wsdl-resolve-references (soap-parse-wsdl xml wsdl))
2386 wsdl))
2388 (defalias 'soap-load-wsdl-from-url 'soap-load-wsdl)
2390 (defun soap-parse-wsdl-phase-validate-node (node)
2391 "Assert that NODE is valid."
2392 (soap-with-local-xmlns node
2393 (let ((node-name (soap-l2wk (xml-node-name node))))
2394 (cl-assert (eq node-name 'wsdl:definitions)
2396 "expecting wsdl:definitions node, got %s" node-name))))
2398 (defun soap-parse-wsdl-phase-fetch-imports (node wsdl)
2399 "Fetch and load files imported by NODE into WSDL."
2400 (soap-with-local-xmlns node
2401 (dolist (node (soap-xml-get-children1 node 'wsdl:import))
2402 (let ((location (xml-get-attribute-or-nil node 'location)))
2403 (when location
2404 (soap-load-wsdl location wsdl))))))
2406 (defun soap-parse-wsdl-phase-parse-schema (node wsdl)
2407 "Load types found in NODE into WSDL."
2408 (soap-with-local-xmlns node
2409 ;; Find all the 'xsd:schema nodes which are children of wsdl:types nodes and
2410 ;; build our type-library.
2411 (let ((types (car (soap-xml-get-children1 node 'wsdl:types))))
2412 (dolist (node (xml-node-children types))
2413 ;; We cannot use (xml-get-children node (soap-wk2l 'xsd:schema)) because
2414 ;; each node can install its own alias type so the schema nodes might
2415 ;; have a different prefix.
2416 (when (consp node)
2417 (soap-with-local-xmlns
2418 node
2419 (when (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
2420 (soap-wsdl-add-namespace (soap-parse-schema node wsdl)
2421 wsdl))))))))
2423 (defun soap-parse-wsdl-phase-fetch-schema (node wsdl)
2424 "Fetch and load schema imports defined by NODE into WSDL."
2425 (soap-with-local-xmlns node
2426 (while (soap-wsdl-xmlschema-imports wsdl)
2427 (let* ((import (pop (soap-wsdl-xmlschema-imports wsdl)))
2428 (xml (soap-fetch-xml import wsdl)))
2429 (soap-wsdl-add-namespace (soap-parse-schema xml wsdl) wsdl)))))
2431 (defun soap-parse-wsdl-phase-finish-parsing (node wsdl)
2432 "Finish parsing NODE into WSDL."
2433 (soap-with-local-xmlns node
2434 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
2435 (dolist (node (soap-xml-get-children1 node 'wsdl:message))
2436 (soap-namespace-put (soap-parse-message node) ns))
2438 (dolist (node (soap-xml-get-children1 node 'wsdl:portType))
2439 (let ((port-type (soap-parse-port-type node)))
2440 (soap-namespace-put port-type ns)
2441 (soap-wsdl-add-namespace
2442 (soap-port-type-operations port-type) wsdl)))
2444 (dolist (node (soap-xml-get-children1 node 'wsdl:binding))
2445 (soap-namespace-put (soap-parse-binding node) ns))
2447 (dolist (node (soap-xml-get-children1 node 'wsdl:service))
2448 (dolist (node (soap-xml-get-children1 node 'wsdl:port))
2449 (let ((name (xml-get-attribute node 'name))
2450 (binding (xml-get-attribute node 'binding))
2451 (url (let ((n (car (soap-xml-get-children1
2452 node 'wsdlsoap:address))))
2453 (xml-get-attribute n 'location))))
2454 (let ((port (make-soap-port
2455 :name name :binding (soap-l2fq binding 'tns)
2456 :service-url url)))
2457 (soap-namespace-put port ns)
2458 (push port (soap-wsdl-ports wsdl))))))
2460 (soap-wsdl-add-namespace ns wsdl))))
2462 (defun soap-parse-wsdl (node wsdl)
2463 "Construct from NODE a WSDL structure, which is an XML document."
2464 ;; Break this into phases to allow for asynchronous parsing.
2465 (soap-parse-wsdl-phase-validate-node node)
2466 ;; Makes synchronous calls.
2467 (soap-parse-wsdl-phase-fetch-imports node wsdl)
2468 (soap-parse-wsdl-phase-parse-schema node wsdl)
2469 ;; Makes synchronous calls.
2470 (soap-parse-wsdl-phase-fetch-schema node wsdl)
2471 (soap-parse-wsdl-phase-finish-parsing node wsdl)
2472 wsdl)
2474 (defun soap-parse-message (node)
2475 "Parse NODE as a wsdl:message and return the corresponding type."
2476 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:message)
2478 "expecting wsdl:message node, got %s"
2479 (soap-l2wk (xml-node-name node)))
2480 (let ((name (xml-get-attribute-or-nil node 'name))
2481 parts)
2482 (dolist (p (soap-xml-get-children1 node 'wsdl:part))
2483 (let ((name (xml-get-attribute-or-nil p 'name))
2484 (type (xml-get-attribute-or-nil p 'type))
2485 (element (xml-get-attribute-or-nil p 'element)))
2487 (when type
2488 (setq type (soap-l2fq type 'tns)))
2490 (if element
2491 (setq element (soap-l2fq element 'tns))
2492 ;; else
2493 (setq element (make-soap-xs-element
2494 :name name
2495 :namespace-tag soap-target-xmlns
2496 :type^ type)))
2498 (push (cons name element) parts)))
2499 (make-soap-message :name name :parts (nreverse parts))))
2501 (defun soap-parse-port-type (node)
2502 "Parse NODE as a wsdl:portType and return the corresponding port."
2503 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:portType)
2505 "expecting wsdl:portType node got %s"
2506 (soap-l2wk (xml-node-name node)))
2507 (let* ((soap-target-xmlns (concat "urn:" (xml-get-attribute node 'name)))
2508 (ns (make-soap-namespace :name soap-target-xmlns)))
2509 (dolist (node (soap-xml-get-children1 node 'wsdl:operation))
2510 (let ((o (soap-parse-operation node)))
2512 (let ((other-operation (soap-namespace-get
2513 (soap-element-name o) ns 'soap-operation-p)))
2514 (if other-operation
2515 ;; Unfortunately, the Confluence WSDL defines two operations
2516 ;; named "search" which differ only in parameter names...
2517 (soap-warning "Discarding duplicate operation: %s"
2518 (soap-element-name o))
2520 (progn
2521 (soap-namespace-put o ns)
2523 ;; link all messages from this namespace, as this namespace
2524 ;; will be used for decoding the response.
2525 (cl-destructuring-bind (name . message) (soap-operation-input o)
2526 (soap-namespace-put-link name message ns))
2528 (cl-destructuring-bind (name . message) (soap-operation-output o)
2529 (soap-namespace-put-link name message ns))
2531 (dolist (fault (soap-operation-faults o))
2532 (cl-destructuring-bind (name . message) fault
2533 (soap-namespace-put-link name message ns)))
2535 )))))
2537 (make-soap-port-type :name (xml-get-attribute node 'name)
2538 :operations ns)))
2540 (defun soap-parse-operation (node)
2541 "Parse NODE as a wsdl:operation and return the corresponding type."
2542 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:operation)
2544 "expecting wsdl:operation node, got %s"
2545 (soap-l2wk (xml-node-name node)))
2546 (let ((name (xml-get-attribute node 'name))
2547 (parameter-order (split-string
2548 (xml-get-attribute node 'parameterOrder)))
2549 input output faults input-action output-action)
2550 (dolist (n (xml-node-children node))
2551 (when (consp n) ; skip string nodes which are whitespace
2552 (let ((node-name (soap-l2wk (xml-node-name n))))
2553 (cond
2554 ((eq node-name 'wsdl:input)
2555 (let ((message (xml-get-attribute n 'message))
2556 (name (xml-get-attribute n 'name))
2557 (action (soap-xml-get-attribute-or-nil1 n 'wsaw:Action)))
2558 (setq input (cons name (soap-l2fq message 'tns)))
2559 (setq input-action action)))
2560 ((eq node-name 'wsdl:output)
2561 (let ((message (xml-get-attribute n 'message))
2562 (name (xml-get-attribute n 'name))
2563 (action (soap-xml-get-attribute-or-nil1 n 'wsaw:Action)))
2564 (setq output (cons name (soap-l2fq message 'tns)))
2565 (setq output-action action)))
2566 ((eq node-name 'wsdl:fault)
2567 (let ((message (xml-get-attribute n 'message))
2568 (name (xml-get-attribute n 'name)))
2569 (push (cons name (soap-l2fq message 'tns)) faults)))))))
2570 (make-soap-operation
2571 :name name
2572 :namespace-tag soap-target-xmlns
2573 :parameter-order parameter-order
2574 :input input
2575 :output output
2576 :faults (nreverse faults)
2577 :input-action input-action
2578 :output-action output-action)))
2580 (defun soap-parse-binding (node)
2581 "Parse NODE as a wsdl:binding and return the corresponding type."
2582 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:binding)
2584 "expecting wsdl:binding node, got %s"
2585 (soap-l2wk (xml-node-name node)))
2586 (let ((name (xml-get-attribute node 'name))
2587 (type (xml-get-attribute node 'type)))
2588 (let ((binding (make-soap-binding :name name
2589 :port-type (soap-l2fq type 'tns))))
2590 (dolist (wo (soap-xml-get-children1 node 'wsdl:operation))
2591 (let ((name (xml-get-attribute wo 'name))
2592 soap-action
2593 soap-headers
2594 soap-body
2595 use)
2596 (dolist (so (soap-xml-get-children1 wo 'wsdlsoap:operation))
2597 (setq soap-action (xml-get-attribute-or-nil so 'soapAction)))
2599 ;; Search a wsdlsoap:body node and find a "use" tag. The
2600 ;; same use tag is assumed to be present for both input and
2601 ;; output types (although the WDSL spec allows separate
2602 ;; "use"-s for each of them...
2604 (dolist (i (soap-xml-get-children1 wo 'wsdl:input))
2606 ;; There can be multiple headers ...
2607 (dolist (h (soap-xml-get-children1 i 'wsdlsoap:header))
2608 (let ((message (soap-l2fq (xml-get-attribute-or-nil h 'message)))
2609 (part (xml-get-attribute-or-nil h 'part))
2610 (use (xml-get-attribute-or-nil h 'use)))
2611 (when (and message part)
2612 (push (list message part use) soap-headers))))
2614 ;; ... but only one body
2615 (let ((body (car (soap-xml-get-children1 i 'wsdlsoap:body))))
2616 (setq soap-body (xml-get-attribute-or-nil body 'parts))
2617 (when soap-body
2618 (setq soap-body
2619 (mapcar #'intern (split-string soap-body
2621 'omit-nulls))))
2622 (setq use (xml-get-attribute-or-nil body 'use))))
2624 (unless use
2625 (dolist (i (soap-xml-get-children1 wo 'wsdl:output))
2626 (dolist (b (soap-xml-get-children1 i 'wsdlsoap:body))
2627 (setq use (or use
2628 (xml-get-attribute-or-nil b 'use))))))
2630 (puthash name (make-soap-bound-operation
2631 :operation name
2632 :soap-action soap-action
2633 :soap-headers (nreverse soap-headers)
2634 :soap-body soap-body
2635 :use (and use (intern use)))
2636 (soap-binding-operations binding))))
2637 binding)))
2639 ;;;; SOAP type decoding
2641 (defvar soap-multi-refs nil
2642 "The list of multi-ref nodes in the current SOAP response.
2643 This is a dynamically bound variable used during decoding the
2644 SOAP response.")
2646 (defvar soap-decoded-multi-refs nil
2647 "List of decoded multi-ref nodes in the current SOAP response.
2648 This is a dynamically bound variable used during decoding the
2649 SOAP response.")
2651 (defun soap-decode-type (type node)
2652 "Use TYPE (an xsd type) to decode the contents of NODE.
2654 NODE is an XML node, representing some SOAP encoded value or a
2655 reference to another XML node (a multiRef). This function will
2656 resolve the multiRef reference, if any, than call a TYPE specific
2657 decode function to perform the actual decoding."
2658 (let ((href (xml-get-attribute-or-nil node 'href)))
2659 (cond (href
2660 (catch 'done
2661 ;; NODE is actually a HREF, find the target and decode that.
2662 ;; Check first if we already decoded this multiref.
2664 (let ((decoded (cdr (assoc href soap-decoded-multi-refs))))
2665 (when decoded
2666 (throw 'done decoded)))
2668 (unless (string-match "^#\\(.*\\)$" href)
2669 (error "Invalid multiRef: %s" href))
2671 (let ((id (match-string 1 href)))
2672 (dolist (mr soap-multi-refs)
2673 (let ((mrid (xml-get-attribute mr 'id)))
2674 (when (equal id mrid)
2675 ;; recurse here, in case there are multiple HREF's
2676 (let ((decoded (soap-decode-type type mr)))
2677 (push (cons href decoded) soap-decoded-multi-refs)
2678 (throw 'done decoded)))))
2679 (error "Cannot find href %s" href))))
2681 (soap-with-local-xmlns node
2682 (if (equal (soap-xml-get-attribute-or-nil1 node 'xsi:nil) "true")
2684 ;; Handle union types.
2685 (cond ((listp type)
2686 (catch 'done
2687 (dolist (union-member type)
2688 (let* ((decoder (get (aref union-member 0)
2689 'soap-decoder))
2690 (result (ignore-errors
2691 (funcall decoder
2692 union-member node))))
2693 (when result (throw 'done result))))))
2695 (let ((decoder (get (aref type 0) 'soap-decoder)))
2696 (cl-assert decoder nil
2697 "no soap-decoder for %s type" (aref type 0))
2698 (funcall decoder type node))))))))))
2700 (defun soap-decode-any-type (node)
2701 "Decode NODE using type information inside it."
2702 ;; If the NODE has type information, we use that...
2703 (let ((type (soap-xml-get-attribute-or-nil1 node 'xsi:type)))
2704 (when type
2705 (setq type (soap-l2fq type)))
2706 (if type
2707 (let ((wtype (soap-wsdl-get type soap-current-wsdl 'soap-xs-type-p)))
2708 (if wtype
2709 (soap-decode-type wtype node)
2710 ;; The node has type info encoded in it, but we don't know how
2711 ;; to decode it...
2712 (error "Node has unknown type: %s" type)))
2714 ;; No type info in the node...
2716 (let ((contents (xml-node-children node)))
2717 (if (and (= (length contents) 1) (stringp (car contents)))
2718 ;; contents is just a string
2719 (car contents)
2721 ;; we assume the NODE is a sequence with every element a
2722 ;; structure name
2723 (let (result)
2724 (dolist (element contents)
2725 ;; skip any string contents, assume they are whitespace
2726 (unless (stringp element)
2727 (let ((key (xml-node-name element))
2728 (value (soap-decode-any-type element)))
2729 (push (cons key value) result))))
2730 (nreverse result)))))))
2732 (defun soap-decode-array (node)
2733 "Decode NODE as an Array using type information inside it."
2734 (let ((type (soap-xml-get-attribute-or-nil1 node 'soapenc:arrayType))
2735 (wtype nil)
2736 (contents (xml-node-children node))
2737 result)
2738 (when type
2739 ;; Type is in the format "someType[NUM]" where NUM is the number of
2740 ;; elements in the array. We discard the [NUM] part.
2741 (setq type (replace-regexp-in-string "\\[[0-9]+\\]\\'" "" type))
2742 (setq wtype (soap-wsdl-get (soap-l2fq type)
2743 soap-current-wsdl 'soap-xs-type-p))
2744 (unless wtype
2745 ;; The node has type info encoded in it, but we don't know how to
2746 ;; decode it...
2747 (error "Soap-decode-array: node has unknown type: %s" type)))
2748 (dolist (e contents)
2749 (when (consp e)
2750 (push (if wtype
2751 (soap-decode-type wtype e)
2752 (soap-decode-any-type e))
2753 result)))
2754 (nreverse result)))
2756 ;;;; Soap Envelope parsing
2758 (if (fboundp 'define-error)
2759 (define-error 'soap-error "SOAP error")
2760 ;; Support older Emacs versions that do not have define-error, so
2761 ;; that soap-client can remain unchanged in GNU ELPA.
2762 (put 'soap-error
2763 'error-conditions
2764 '(error soap-error))
2765 (put 'soap-error 'error-message "SOAP error"))
2767 (defun soap-parse-envelope (node operation wsdl)
2768 "Parse the SOAP envelope in NODE and return the response.
2769 OPERATION is the WSDL operation for which we expect the response,
2770 WSDL is used to decode the NODE"
2771 (soap-with-local-xmlns node
2772 (cl-assert (eq (soap-l2wk (xml-node-name node)) 'soap:Envelope)
2774 "expecting soap:Envelope node, got %s"
2775 (soap-l2wk (xml-node-name node)))
2776 (let ((headers (soap-xml-get-children1 node 'soap:Header))
2777 (body (car (soap-xml-get-children1 node 'soap:Body))))
2779 (let ((fault (car (soap-xml-get-children1 body 'soap:Fault))))
2780 (when fault
2781 (let ((fault-code (let ((n (car (xml-get-children
2782 fault 'faultcode))))
2783 (car-safe (xml-node-children n))))
2784 (fault-string (let ((n (car (xml-get-children
2785 fault 'faultstring))))
2786 (car-safe (xml-node-children n))))
2787 (detail (xml-get-children fault 'detail)))
2788 (while t
2789 (signal 'soap-error (list fault-code fault-string detail))))))
2791 ;; First (non string) element of the body is the root node of he
2792 ;; response
2793 (let ((response (if (eq (soap-bound-operation-use operation) 'literal)
2794 ;; For 'literal uses, the response is the actual body
2795 body
2796 ;; ...otherwise the first non string element
2797 ;; of the body is the response
2798 (catch 'found
2799 (dolist (n (xml-node-children body))
2800 (when (consp n)
2801 (throw 'found n)))))))
2802 (soap-parse-response response operation wsdl headers body)))))
2804 (defun soap-parse-response (response-node operation wsdl soap-headers soap-body)
2805 "Parse RESPONSE-NODE and return the result as a LISP value.
2806 OPERATION is the WSDL operation for which we expect the response,
2807 WSDL is used to decode the NODE.
2809 SOAP-HEADERS is a list of the headers of the SOAP envelope or nil
2810 if there are no headers.
2812 SOAP-BODY is the body of the SOAP envelope (of which
2813 RESPONSE-NODE is a sub-node). It is used in case RESPONSE-NODE
2814 reference multiRef parts which are external to RESPONSE-NODE."
2815 (let* ((soap-current-wsdl wsdl)
2816 (op (soap-bound-operation-operation operation))
2817 (use (soap-bound-operation-use operation))
2818 (message (cdr (soap-operation-output op))))
2820 (soap-with-local-xmlns response-node
2822 (when (eq use 'encoded)
2823 (let* ((received-message-name (soap-l2fq (xml-node-name response-node)))
2824 (received-message (soap-wsdl-get
2825 received-message-name wsdl 'soap-message-p)))
2826 (unless (eq received-message message)
2827 (error "Unexpected message: got %s, expecting %s"
2828 received-message-name
2829 (soap-element-name message)))))
2831 (let ((decoded-parts nil)
2832 (soap-multi-refs (xml-get-children soap-body 'multiRef))
2833 (soap-decoded-multi-refs nil))
2835 (dolist (part (soap-message-parts message))
2836 (let ((tag (car part))
2837 (type (cdr part))
2838 node)
2840 (setq node
2841 (cond
2842 ((eq use 'encoded)
2843 (car (xml-get-children response-node tag)))
2845 ((eq use 'literal)
2846 (catch 'found
2847 (let* ((ns-aliases (soap-wsdl-alias-table wsdl))
2848 (ns-name (cdr (assoc
2849 (soap-element-namespace-tag type)
2850 ns-aliases)))
2851 (fqname (cons ns-name (soap-element-name type))))
2852 (dolist (c (append (mapcar (lambda (header)
2853 (car (xml-node-children
2854 header)))
2855 soap-headers)
2856 (xml-node-children response-node)))
2857 (when (consp c)
2858 (soap-with-local-xmlns c
2859 (when (equal (soap-l2fq (xml-node-name c))
2860 fqname)
2861 (throw 'found c))))))))))
2863 (unless node
2864 (error "Soap-parse-response(%s): cannot find message part %s"
2865 (soap-element-name op) tag))
2866 (let ((decoded-value (soap-decode-type type node)))
2867 (when decoded-value
2868 (push decoded-value decoded-parts)))))
2870 decoded-parts))))
2872 ;;;; SOAP type encoding
2874 (defun soap-encode-attributes (value type)
2875 "Encode XML attributes for VALUE according to TYPE.
2876 This is a generic function which determines the attribute encoder
2877 for the type and calls that specialized function to do the work.
2879 Attributes are inserted in the current buffer at the current
2880 position."
2881 (let ((attribute-encoder (get (aref type 0) 'soap-attribute-encoder)))
2882 (cl-assert attribute-encoder nil
2883 "no soap-attribute-encoder for %s type" (aref type 0))
2884 (funcall attribute-encoder value type)))
2886 (defun soap-encode-value (value type)
2887 "Encode the VALUE using TYPE.
2888 The resulting XML data is inserted in the current buffer
2889 at (point)/
2891 TYPE is one of the soap-*-type structures which defines how VALUE
2892 is to be encoded. This is a generic function which finds an
2893 encoder function based on TYPE and calls that encoder to do the
2894 work."
2895 (let ((encoder (get (aref type 0) 'soap-encoder)))
2896 (cl-assert encoder nil "no soap-encoder for %s type" (aref type 0))
2897 (funcall encoder value type))
2898 (when (soap-element-namespace-tag type)
2899 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag type))))
2901 (defun soap-encode-body (operation parameters &optional service-url)
2902 "Create the body of a SOAP request for OPERATION in the current buffer.
2903 PARAMETERS is a list of parameters supplied to the OPERATION.
2905 The OPERATION and PARAMETERS are encoded according to the WSDL
2906 document. SERVICE-URL should be provided when WS-Addressing is
2907 being used."
2908 (let* ((op (soap-bound-operation-operation operation))
2909 (use (soap-bound-operation-use operation))
2910 (message (cdr (soap-operation-input op)))
2911 (parameter-order (soap-operation-parameter-order op))
2912 (param-table (cl-loop for formal in parameter-order
2913 for value in parameters
2914 collect (cons formal value))))
2916 (unless (= (length parameter-order) (length parameters))
2917 (error "Wrong number of parameters for %s: expected %d, got %s"
2918 (soap-element-name op)
2919 (length parameter-order)
2920 (length parameters)))
2922 (let ((headers (soap-bound-operation-soap-headers operation))
2923 (input-action (soap-operation-input-action op)))
2924 (when headers
2925 (insert "<soap:Header>\n")
2926 (when input-action
2927 (add-to-list 'soap-encoded-namespaces "wsa")
2928 (insert "<wsa:Action>" input-action "</wsa:Action>\n")
2929 (insert "<wsa:To>" service-url "</wsa:To>\n"))
2930 (dolist (h headers)
2931 (let* ((message (nth 0 h))
2932 (part (assq (nth 1 h) (soap-message-parts message)))
2933 (value (cdr (assoc (car part) (car parameters))))
2934 (use (nth 2 h))
2935 (element (cdr part)))
2936 (when (eq use 'encoded)
2937 (when (soap-element-namespace-tag element)
2938 (add-to-list 'soap-encoded-namespaces
2939 (soap-element-namespace-tag element)))
2940 (insert "<" (soap-element-fq-name element) ">\n"))
2941 (soap-encode-value value element)
2942 (when (eq use 'encoded)
2943 (insert "</" (soap-element-fq-name element) ">\n"))))
2944 (insert "</soap:Header>\n")))
2946 (insert "<soap:Body>\n")
2947 (when (eq use 'encoded)
2948 (when (soap-element-namespace-tag op)
2949 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag op)))
2950 (insert "<" (soap-element-fq-name op) ">\n"))
2952 (dolist (part (soap-message-parts message))
2953 (let* ((param-name (car part))
2954 (element (cdr part))
2955 (value (cdr (assoc param-name param-table))))
2956 (when (or (null (soap-bound-operation-soap-body operation))
2957 (member param-name
2958 (soap-bound-operation-soap-body operation)))
2959 (soap-encode-value value element))))
2961 (when (eq use 'encoded)
2962 (insert "</" (soap-element-fq-name op) ">\n"))
2963 (insert "</soap:Body>\n")))
2965 (defun soap-create-envelope (operation parameters wsdl &optional service-url)
2966 "Create a SOAP request envelope for OPERATION using PARAMETERS.
2967 WSDL is the wsdl document used to encode the PARAMETERS.
2968 SERVICE-URL should be provided when WS-Addressing is being used."
2969 (with-temp-buffer
2970 (let ((soap-encoded-namespaces '("xsi" "soap" "soapenc"))
2971 (use (soap-bound-operation-use operation)))
2973 ;; Create the request body
2974 (soap-encode-body operation parameters service-url)
2976 ;; Put the envelope around the body
2977 (goto-char (point-min))
2978 (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope\n")
2979 (when (eq use 'encoded)
2980 (insert " soapenc:encodingStyle=\"\
2981 http://schemas.xmlsoap.org/soap/encoding/\"\n"))
2982 (dolist (nstag soap-encoded-namespaces)
2983 (insert " xmlns:" nstag "=\"")
2984 (let ((nsname (cdr (assoc nstag soap-well-known-xmlns))))
2985 (unless nsname
2986 (setq nsname (cdr (assoc nstag (soap-wsdl-alias-table wsdl)))))
2987 (insert nsname)
2988 (insert "\"\n")))
2989 (insert ">\n")
2990 (goto-char (point-max))
2991 (insert "</soap:Envelope>\n"))
2993 (buffer-string)))
2995 ;;;; invoking soap methods
2997 (defcustom soap-debug nil
2998 "When t, enable some debugging facilities."
2999 :type 'boolean
3000 :group 'soap-client)
3002 (defun soap-find-port (wsdl service)
3003 "Return the WSDL port having SERVICE name.
3004 Signal an error if not found."
3005 (or (catch 'found
3006 (dolist (p (soap-wsdl-ports wsdl))
3007 (when (equal service (soap-element-name p))
3008 (throw 'found p))))
3009 (error "Unknown SOAP service: %s" service)))
3011 (defun soap-find-operation (port operation-name)
3012 "Inside PORT, find OPERATION-NAME, a `soap-port-type'.
3013 Signal an error if not found."
3014 (let* ((binding (soap-port-binding port))
3015 (op (gethash operation-name (soap-binding-operations binding))))
3016 (or op
3017 (error "No operation %s for SOAP service %s"
3018 operation-name (soap-element-name port)))))
3020 (defun soap-operation-arity (wsdl service operation-name)
3021 "Return the number of arguments required by a soap operation.
3022 WSDL, SERVICE, OPERATION-NAME and PARAMETERS are as described in
3023 `soap-invoke'."
3024 (let* ((port (soap-find-port wsdl service))
3025 (op (soap-find-operation port operation-name))
3026 (bop (soap-bound-operation-operation op)))
3027 (length (soap-operation-parameter-order bop))))
3029 (defun soap-invoke-internal (callback cbargs wsdl service operation-name
3030 &rest parameters)
3031 "Implement `soap-invoke' and `soap-invoke-async'.
3032 If CALLBACK is non-nil, operate asynchronously, then call CALLBACK as (apply
3033 CALLBACK RESPONSE CBARGS), where RESPONSE is the SOAP invocation result.
3034 If CALLBACK is nil, operate synchronously. WSDL, SERVICE,
3035 OPERATION-NAME and PARAMETERS are as described in `soap-invoke'."
3036 (let* ((port (soap-find-port wsdl service))
3037 (operation (soap-find-operation port operation-name)))
3038 (let ((url-request-method "POST")
3039 (url-package-name "soap-client.el")
3040 (url-package-version "1.0")
3041 (url-request-data
3042 ;; url-request-data expects a unibyte string already encoded...
3043 (encode-coding-string
3044 (soap-create-envelope operation parameters wsdl
3045 (soap-port-service-url port))
3046 'utf-8))
3047 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
3048 (url-http-attempt-keepalives t)
3049 (url-request-extra-headers
3050 (list
3051 (cons "SOAPAction"
3052 (concat "\"" (encode-coding-string
3053 (soap-bound-operation-soap-action
3054 operation)
3055 'utf-8)
3056 "\""))
3057 (cons "Content-Type"
3058 "text/xml; charset=utf-8"))))
3059 (if callback
3060 (url-retrieve
3061 (soap-port-service-url port)
3062 (lambda (status)
3063 (let ((data-buffer (current-buffer)))
3064 (unwind-protect
3065 (let ((error-status (plist-get status :error)))
3066 (if error-status
3067 (signal (car error-status) (cdr error-status))
3068 (apply callback
3069 (soap-parse-envelope
3070 (soap-parse-server-response)
3071 operation wsdl)
3072 cbargs)))
3073 ;; Ensure the url-retrieve buffer is not leaked.
3074 (and (buffer-live-p data-buffer)
3075 (kill-buffer data-buffer))))))
3076 (let ((buffer (url-retrieve-synchronously
3077 (soap-port-service-url port))))
3078 (condition-case err
3079 (with-current-buffer buffer
3080 (if (null url-http-response-status)
3081 (error "No HTTP response from server"))
3082 (if (and soap-debug (> url-http-response-status 299))
3083 ;; This is a warning because some SOAP errors come
3084 ;; back with a HTTP response 500 (internal server
3085 ;; error)
3086 (warn "Error in SOAP response: HTTP code %s"
3087 url-http-response-status))
3088 (soap-parse-envelope (soap-parse-server-response)
3089 operation wsdl))
3090 (soap-error
3091 ;; Propagate soap-errors -- they are error replies of the
3092 ;; SOAP protocol and don't indicate a communication
3093 ;; problem or a bug in this code.
3094 (signal (car err) (cdr err)))
3095 (error
3096 (when soap-debug
3097 (pop-to-buffer buffer))
3098 (error (error-message-string err)))))))))
3100 (defun soap-invoke (wsdl service operation-name &rest parameters)
3101 "Invoke a SOAP operation and return the result.
3103 WSDL is used for encoding the request and decoding the response.
3104 It also contains information about the WEB server address that
3105 will service the request.
3107 SERVICE is the SOAP service to invoke.
3109 OPERATION-NAME is the operation to invoke.
3111 PARAMETERS -- the remaining parameters are used as parameters for
3112 the SOAP request.
3114 NOTE: The SOAP service provider should document the available
3115 operations and their parameters for the service. You can also
3116 use the `soap-inspect' function to browse the available
3117 operations in a WSDL document.
3119 NOTE: `soap-invoke' base64-decodes xsd:base64Binary return values
3120 into unibyte strings; these byte-strings require further
3121 interpretation by the caller."
3122 (apply #'soap-invoke-internal nil nil wsdl service operation-name parameters))
3124 (defun soap-invoke-async (callback cbargs wsdl service operation-name
3125 &rest parameters)
3126 "Like `soap-invoke', but call CALLBACK asynchronously with response.
3127 CALLBACK is called as (apply CALLBACK RESPONSE CBARGS), where
3128 RESPONSE is the SOAP invocation result. WSDL, SERVICE,
3129 OPERATION-NAME and PARAMETERS are as described in `soap-invoke'."
3130 (unless callback
3131 (error "Callback argument is nil"))
3132 (apply #'soap-invoke-internal callback cbargs wsdl service operation-name
3133 parameters))
3135 (provide 'soap-client)
3138 ;; Local Variables:
3139 ;; eval: (outline-minor-mode 1)
3140 ;; outline-regexp: ";;;;+"
3141 ;; End:
3143 ;;; soap-client.el ends here