Merge branch 'master' into comment-cache
[emacs.git] / lisp / net / soap-client.el
blob5d36cfa89b8a78176d50ae2c4b8c7301d9f1ca3c
1 ;;; soap-client.el --- Access SOAP web services -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009-2017 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.1
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.5"))
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 <http://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 (eval-when-compile (require 'cl))
47 (require 'cl-lib)
49 (require 'xml)
50 (require 'xsd-regexp)
51 (require 'rng-xsd)
52 (require 'rng-dt)
53 (require 'warnings)
54 (require 'url)
55 (require 'url-http)
56 (require 'url-util)
57 (require 'url-vars)
58 (require 'mm-decode)
60 (defsubst soap-warning (message &rest args)
61 "Display a warning MESSAGE with ARGS, using the `soap-client' warning type."
62 ;; Do not use #'format-message, to support older Emacs versions.
63 (display-warning 'soap-client (apply #'format message args) :warning))
65 (defgroup soap-client nil
66 "Access SOAP web services from Emacs."
67 :version "24.1"
68 :group 'tools)
70 ;;;; Support for parsing XML documents with namespaces
72 ;; XML documents with namespaces are difficult to parse because the names of
73 ;; the nodes depend on what "xmlns" aliases have been defined in the document.
74 ;; To work with such documents, we introduce a translation layer between a
75 ;; "well known" namespace tag and the local namespace tag in the document
76 ;; being parsed.
78 (defconst soap-well-known-xmlns
79 '(("apachesoap" . "http://xml.apache.org/xml-soap")
80 ("soapenc" . "http://schemas.xmlsoap.org/soap/encoding/")
81 ("wsdl" . "http://schemas.xmlsoap.org/wsdl/")
82 ("wsdlsoap" . "http://schemas.xmlsoap.org/wsdl/soap/")
83 ("xsd" . "http://www.w3.org/2001/XMLSchema")
84 ("xsi" . "http://www.w3.org/2001/XMLSchema-instance")
85 ("wsa" . "http://www.w3.org/2005/08/addressing")
86 ("wsaw" . "http://www.w3.org/2006/05/addressing/wsdl")
87 ("soap" . "http://schemas.xmlsoap.org/soap/envelope/")
88 ("soap12" . "http://schemas.xmlsoap.org/wsdl/soap12/")
89 ("http" . "http://schemas.xmlsoap.org/wsdl/http/")
90 ("mime" . "http://schemas.xmlsoap.org/wsdl/mime/")
91 ("xml" . "http://www.w3.org/XML/1998/namespace"))
92 "A list of well known xml namespaces and their aliases.")
94 (defvar soap-local-xmlns
95 '(("xml" . "http://www.w3.org/XML/1998/namespace"))
96 "A list of local namespace aliases.
97 This is a dynamically bound variable, controlled by
98 `soap-with-local-xmlns'.")
100 (defvar soap-default-xmlns nil
101 "The default XML namespaces.
102 Names in this namespace will be unqualified. This is a
103 dynamically bound variable, controlled by
104 `soap-with-local-xmlns'")
106 (defvar soap-target-xmlns nil
107 "The target XML namespace.
108 New XSD elements will be defined in this namespace, unless they
109 are fully qualified for a different namespace. This is a
110 dynamically bound variable, controlled by
111 `soap-with-local-xmlns'")
113 (defvar soap-current-wsdl nil
114 "The current WSDL document used when decoding the SOAP response.
115 This is a dynamically bound variable.")
117 (defun soap-wk2l (well-known-name)
118 "Return local variant of WELL-KNOWN-NAME.
119 This is done by looking up the namespace in the
120 `soap-well-known-xmlns' table and resolving the namespace to
121 the local name based on the current local translation table
122 `soap-local-xmlns'. See also `soap-with-local-xmlns'."
123 (let ((wk-name-1 (if (symbolp well-known-name)
124 (symbol-name well-known-name)
125 well-known-name)))
126 (cond
127 ((string-match "^\\(.*\\):\\(.*\\)$" wk-name-1)
128 (let ((ns (match-string 1 wk-name-1))
129 (name (match-string 2 wk-name-1)))
130 (let ((namespace (cdr (assoc ns soap-well-known-xmlns))))
131 (cond ((equal namespace soap-default-xmlns)
132 ;; Name is unqualified in the default namespace
133 (if (symbolp well-known-name)
134 (intern name)
135 name))
137 (let* ((local-ns (car (rassoc namespace soap-local-xmlns)))
138 (local-name (concat local-ns ":" name)))
139 (if (symbolp well-known-name)
140 (intern local-name)
141 local-name)))))))
142 (t well-known-name))))
144 (defun soap-l2wk (local-name)
145 "Convert LOCAL-NAME into a well known name.
146 The namespace of LOCAL-NAME is looked up in the
147 `soap-well-known-xmlns' table and a well known namespace tag is
148 used in the name.
150 nil is returned if there is no well-known namespace for the
151 namespace of LOCAL-NAME."
152 (let ((l-name-1 (if (symbolp local-name)
153 (symbol-name local-name)
154 local-name))
155 namespace name)
156 (cond
157 ((string-match "^\\(.*\\):\\(.*\\)$" l-name-1)
158 (setq name (match-string 2 l-name-1))
159 (let ((ns (match-string 1 l-name-1)))
160 (setq namespace (cdr (assoc ns soap-local-xmlns)))
161 (unless namespace
162 (error "Soap-l2wk(%s): no namespace for alias %s" local-name ns))))
164 (setq name l-name-1)
165 (setq namespace soap-default-xmlns)))
167 (if namespace
168 (let ((well-known-ns (car (rassoc namespace soap-well-known-xmlns))))
169 (if well-known-ns
170 (let ((well-known-name (concat well-known-ns ":" name)))
171 (if (symbolp local-name)
172 (intern well-known-name)
173 well-known-name))
174 nil))
175 ;; if no namespace is defined, just return the unqualified name
176 name)))
179 (defun soap-l2fq (local-name &optional use-tns)
180 "Convert LOCAL-NAME into a fully qualified name.
181 A fully qualified name is a cons of the namespace name and the
182 name of the element itself. For example \"xsd:string\" is
183 converted to (\"http://www.w3.org/2001/XMLSchema\" . \"string\").
185 The USE-TNS argument specifies what to do when LOCAL-NAME has no
186 namespace tag. If USE-TNS is non-nil, the `soap-target-xmlns'
187 will be used as the element's namespace, otherwise
188 `soap-default-xmlns' will be used.
190 This is needed because different parts of a WSDL document can use
191 different namespace aliases for the same element."
192 (let ((local-name-1 (if (symbolp local-name)
193 (symbol-name local-name)
194 local-name)))
195 (cond ((string-match "^\\(.*\\):\\(.*\\)$" local-name-1)
196 (let ((ns (match-string 1 local-name-1))
197 (name (match-string 2 local-name-1)))
198 (let ((namespace (cdr (assoc ns soap-local-xmlns))))
199 (if namespace
200 (cons namespace name)
201 (error "Soap-l2fq(%s): unknown alias %s" local-name ns)))))
203 (cons (if use-tns
204 soap-target-xmlns
205 soap-default-xmlns)
206 local-name-1)))))
208 (defun soap-name-p (name)
209 "Return true if NAME is a valid name for XMLSchema types.
210 A valid name is either a string or a cons of (NAMESPACE . NAME)."
211 (or (stringp name)
212 (and (consp name)
213 (stringp (car name))
214 (stringp (cdr name)))))
216 (defun soap-extract-xmlns (node &optional xmlns-table)
217 "Return a namespace alias table for NODE by extending XMLNS-TABLE."
218 (let (xmlns default-ns target-ns)
219 (dolist (a (xml-node-attributes node))
220 (let ((name (symbol-name (car a)))
221 (value (cdr a)))
222 (cond ((string= name "targetNamespace")
223 (setq target-ns value))
224 ((string= name "xmlns")
225 (setq default-ns value))
226 ((string-match "^xmlns:\\(.*\\)$" name)
227 (push (cons (match-string 1 name) value) xmlns)))))
229 (let ((tns (assoc "tns" xmlns)))
230 (cond ((and tns target-ns)
231 ;; If a tns alias is defined for this node, it must match
232 ;; the target namespace.
233 (unless (equal target-ns (cdr tns))
234 (soap-warning
235 "soap-extract-xmlns(%s): tns alias and targetNamespace mismatch"
236 (xml-node-name node))))
237 ((and tns (not target-ns))
238 (setq target-ns (cdr tns)))))
240 (list default-ns target-ns (append xmlns xmlns-table))))
242 (defmacro soap-with-local-xmlns (node &rest body)
243 "Install a local alias table from NODE and execute BODY."
244 (declare (debug (form &rest form)) (indent 1))
245 (let ((xmlns (make-symbol "xmlns")))
246 `(let ((,xmlns (soap-extract-xmlns ,node soap-local-xmlns)))
247 (let ((soap-default-xmlns (or (nth 0 ,xmlns) soap-default-xmlns))
248 (soap-target-xmlns (or (nth 1 ,xmlns) soap-target-xmlns))
249 (soap-local-xmlns (nth 2 ,xmlns)))
250 ,@body))))
252 (defun soap-get-target-namespace (node)
253 "Return the target namespace of NODE.
254 This is the namespace in which new elements will be defined."
255 (or (xml-get-attribute-or-nil node 'targetNamespace)
256 (cdr (assoc "tns" soap-local-xmlns))
257 soap-target-xmlns))
259 (defun soap-xml-get-children1 (node child-name)
260 "Return the children of NODE named CHILD-NAME.
261 This is the same as `xml-get-children', but CHILD-NAME can have
262 namespace tag."
263 (let (result)
264 (dolist (c (xml-node-children node))
265 (when (and (consp c)
266 (soap-with-local-xmlns c
267 ;; We use `ignore-errors' here because we want to silently
268 ;; skip nodes when we cannot convert them to a well-known
269 ;; name.
270 (eq (ignore-errors (soap-l2wk (xml-node-name c)))
271 child-name)))
272 (push c result)))
273 (nreverse result)))
275 (defun soap-xml-node-find-matching-child (node set)
276 "Return the first child of NODE whose name is a member of SET."
277 (catch 'found
278 (dolist (child (xml-node-children node))
279 (when (and (consp child)
280 (memq (soap-l2wk (xml-node-name child)) set))
281 (throw 'found child)))))
283 (defun soap-xml-get-attribute-or-nil1 (node attribute)
284 "Return the NODE's ATTRIBUTE, or nil if it does not exist.
285 This is the same as `xml-get-attribute-or-nil', but ATTRIBUTE can
286 be tagged with a namespace tag."
287 (catch 'found
288 (soap-with-local-xmlns node
289 (dolist (a (xml-node-attributes node))
290 ;; We use `ignore-errors' here because we want to silently skip
291 ;; attributes for which we cannot convert them to a well-known name.
292 (when (eq (ignore-errors (soap-l2wk (car a))) attribute)
293 (throw 'found (cdr a)))))))
296 ;;;; XML namespaces
298 ;; An element in an XML namespace, "things" stored in soap-xml-namespaces will
299 ;; be derived from this object.
301 (defstruct soap-element
302 name
303 ;; The "well-known" namespace tag for the element. For example, while
304 ;; parsing XML documents, we can have different tags for the XMLSchema
305 ;; namespace, but internally all our XMLSchema elements will have the "xsd"
306 ;; tag.
307 namespace-tag)
309 (defun soap-element-fq-name (element)
310 "Return a fully qualified name for ELEMENT.
311 A fq name is the concatenation of the namespace tag and the
312 element name."
313 (cond ((soap-element-namespace-tag element)
314 (concat (soap-element-namespace-tag element)
315 ":" (soap-element-name element)))
316 ((soap-element-name element)
317 (soap-element-name element))
319 "*unnamed*")))
321 ;; a namespace link stores an alias for an object in once namespace to a
322 ;; "target" object possibly in a different namespace
324 (defstruct (soap-namespace-link (:include soap-element))
325 target)
327 ;; A namespace is a collection of soap-element objects under a name (the name
328 ;; of the namespace).
330 (defstruct soap-namespace
331 (name nil :read-only t) ; e.g "http://xml.apache.org/xml-soap"
332 (elements (make-hash-table :test 'equal) :read-only t))
334 (defun soap-namespace-put (element ns)
335 "Store ELEMENT in NS.
336 Multiple elements with the same name can be stored in a
337 namespace. When retrieving the element you can specify a
338 discriminant predicate to `soap-namespace-get'"
339 (let ((name (soap-element-name element)))
340 (push element (gethash name (soap-namespace-elements ns)))))
342 (defun soap-namespace-put-link (name target ns)
343 "Store a link from NAME to TARGET in NS.
344 TARGET can be either a SOAP-ELEMENT or a string denoting an
345 element name into another namespace.
347 If NAME is nil, an element with the same name as TARGET will be
348 added to the namespace."
350 (unless (and name (not (equal name "")))
351 ;; if name is nil, use TARGET as a name...
352 (cond ((soap-element-p target)
353 (setq name (soap-element-name target)))
354 ((consp target) ; a fq name: (namespace . name)
355 (setq name (cdr target)))
356 ((stringp target)
357 (cond ((string-match "^\\(.*\\):\\(.*\\)$" target)
358 (setq name (match-string 2 target)))
360 (setq name target))))))
362 ;; by now, name should be valid
363 (assert (and name (not (equal name "")))
365 "Cannot determine name for namespace link")
366 (push (make-soap-namespace-link :name name :target target)
367 (gethash name (soap-namespace-elements ns))))
369 (defun soap-namespace-get (name ns &optional discriminant-predicate)
370 "Retrieve an element with NAME from the namespace NS.
371 If multiple elements with the same name exist,
372 DISCRIMINANT-PREDICATE is used to pick one of them. This allows
373 storing elements of different types (like a message type and a
374 binding) but the same name."
375 (assert (stringp name))
376 (let ((elements (gethash name (soap-namespace-elements ns))))
377 (cond (discriminant-predicate
378 (catch 'found
379 (dolist (e elements)
380 (when (funcall discriminant-predicate e)
381 (throw 'found e)))))
382 ((= (length elements) 1) (car elements))
383 ((> (length elements) 1)
384 (error
385 "Soap-namespace-get(%s): multiple elements, discriminant needed"
386 name))
388 nil))))
391 ;;;; XML Schema
393 ;; SOAP WSDL documents use XML Schema to define the types that are part of the
394 ;; message exchange. We include here an XML schema model with a parser and
395 ;; serializer/deserializer.
397 (defstruct (soap-xs-type (:include soap-element))
399 attributes
400 attribute-groups)
402 ;;;;; soap-xs-basic-type
404 (defstruct (soap-xs-basic-type (:include soap-xs-type))
405 ;; Basic types are "built in" and we know how to handle them directly.
406 ;; Other type definitions reference basic types, so we need to create them
407 ;; in a namespace (see `soap-make-xs-basic-types')
409 ;; a symbol of: string, dateTime, long, int, etc
410 kind
413 (defun soap-make-xs-basic-types (namespace-name &optional namespace-tag)
414 "Construct NAMESPACE-NAME containing the XMLSchema basic types.
415 An optional NAMESPACE-TAG can also be specified."
416 (let ((ns (make-soap-namespace :name namespace-name)))
417 (dolist (type '("string" "language" "ID" "IDREF"
418 "dateTime" "time" "date" "boolean"
419 "gYearMonth" "gYear" "gMonthDay" "gDay" "gMonth"
420 "long" "short" "int" "integer" "nonNegativeInteger"
421 "unsignedLong" "unsignedShort" "unsignedInt"
422 "decimal" "duration"
423 "byte" "unsignedByte"
424 "float" "double"
425 "base64Binary" "anyType" "anyURI" "QName" "Array" "byte[]"))
426 (soap-namespace-put
427 (make-soap-xs-basic-type :name type
428 :namespace-tag namespace-tag
429 :kind (intern type))
430 ns))
431 ns))
433 (defun soap-encode-xs-basic-type-attributes (value type)
434 "Encode the XML attributes for VALUE according to TYPE.
435 The xsi:type and an optional xsi:nil attributes are added. The
436 attributes are inserted in the current buffer at the current
437 position.
439 This is a specialization of `soap-encode-attributes' for
440 `soap-xs-basic-type' objects."
441 (let ((xsi-type (soap-element-fq-name type))
442 (basic-type (soap-xs-basic-type-kind type)))
443 ;; try to classify the type based on the value type and use that type when
444 ;; encoding
445 (when (eq basic-type 'anyType)
446 (cond ((stringp value)
447 (setq xsi-type "xsd:string" basic-type 'string))
448 ((integerp value)
449 (setq xsi-type "xsd:int" basic-type 'int))
450 ((memq value '(t nil))
451 (setq xsi-type "xsd:boolean" basic-type 'boolean))
453 (error "Cannot classify anyType value"))))
455 (insert " xsi:type=\"" xsi-type "\"")
456 ;; We have some ambiguity here, as a nil value represents "false" when the
457 ;; type is boolean, we will never have a "nil" boolean type...
458 (unless (or value (eq basic-type 'boolean))
459 (insert " xsi:nil=\"true\""))))
461 (defun soap-encode-xs-basic-type (value type)
462 "Encode the VALUE according to TYPE.
463 The data is inserted in the current buffer at the current
464 position.
466 This is a specialization of `soap-encode-value' for
467 `soap-xs-basic-type' objects."
468 (let ((kind (soap-xs-basic-type-kind type)))
470 (when (eq kind 'anyType)
471 (cond ((stringp value)
472 (setq kind 'string))
473 ((integerp value)
474 (setq kind 'int))
475 ((memq value '(t nil))
476 (setq kind 'boolean))
478 (error "Cannot classify anyType value"))))
480 ;; NOTE: a nil value is not encoded, as an xsi:nil="true" attribute was
481 ;; encoded for it. However, we have some ambiguity here, as a nil value
482 ;; also represents "false" when the type is boolean...
484 (when (or value (eq kind 'boolean))
485 (let ((value-string
486 (case kind
487 ((string anyURI QName ID IDREF language)
488 (unless (stringp value)
489 (error "Not a string value: %s" value))
490 (url-insert-entities-in-string value))
491 ((dateTime time date gYearMonth gYear gMonthDay gDay gMonth)
492 (cond ((consp value)
493 ;; Value is a (current-time) style value,
494 ;; convert to the ISO 8601-inspired XSD
495 ;; string format in UTC.
496 (format-time-string
497 (concat
498 (ecase kind
499 (dateTime "%Y-%m-%dT%H:%M:%S")
500 (time "%H:%M:%S")
501 (date "%Y-%m-%d")
502 (gYearMonth "%Y-%m")
503 (gYear "%Y")
504 (gMonthDay "--%m-%d")
505 (gDay "---%d")
506 (gMonth "--%m"))
507 ;; Internal time is always in UTC.
508 "Z")
509 value t))
510 ((stringp value)
511 ;; Value is a string in the ISO 8601-inspired XSD
512 ;; format. Validate it.
513 (soap-decode-date-time value kind)
514 (url-insert-entities-in-string value))
516 (error "Invalid date-time format"))))
517 (boolean
518 (unless (memq value '(t nil))
519 (error "Not a boolean value"))
520 (if value "true" "false"))
522 ((long short int integer byte unsignedInt unsignedLong
523 unsignedShort nonNegativeInteger decimal duration)
524 (unless (integerp value)
525 (error "Not an integer value"))
526 (when (and (memq kind '(unsignedInt unsignedLong
527 unsignedShort
528 nonNegativeInteger))
529 (< value 0))
530 (error "Not a positive integer"))
531 (number-to-string value))
533 ((float double)
534 (unless (numberp value)
535 (error "Not a number"))
536 (number-to-string value))
538 (base64Binary
539 (unless (stringp value)
540 (error "Not a string value for base64Binary"))
541 (base64-encode-string value))
543 (otherwise
544 (error "Don't know how to encode %s for type %s"
545 value (soap-element-fq-name type))))))
546 (soap-validate-xs-basic-type value-string type)
547 (insert value-string)))))
549 ;; Inspired by rng-xsd-convert-date-time.
550 (defun soap-decode-date-time (date-time-string datatype)
551 "Decode DATE-TIME-STRING as DATATYPE.
552 DATE-TIME-STRING should be in ISO 8601 basic or extended format.
553 DATATYPE is one of dateTime, time, date, gYearMonth, gYear,
554 gMonthDay, gDay or gMonth.
556 Return a list in a format (SEC MINUTE HOUR DAY MONTH YEAR
557 SEC-FRACTION DATATYPE ZONE). This format is meant to be similar
558 to that returned by `decode-time' (and compatible with
559 `encode-time'). The differences are the DOW (day-of-week) field
560 is replaced with SEC-FRACTION, a float representing the
561 fractional seconds, and the DST (daylight savings time) field is
562 replaced with DATATYPE, a symbol representing the XSD primitive
563 datatype. This symbol can be used to determine which fields
564 apply and which don't when it's not already clear from context.
565 For example a datatype of `time' means the year, month and day
566 fields should be ignored.
568 This function will throw an error if DATE-TIME-STRING represents
569 a leap second, since the XML Schema 1.1 standard explicitly
570 disallows them."
571 (let* ((datetime-regexp (cadr (get datatype 'rng-xsd-convert)))
572 (year-sign (progn
573 (string-match datetime-regexp date-time-string)
574 (match-string 1 date-time-string)))
575 (year (match-string 2 date-time-string))
576 (month (match-string 3 date-time-string))
577 (day (match-string 4 date-time-string))
578 (hour (match-string 5 date-time-string))
579 (minute (match-string 6 date-time-string))
580 (second (match-string 7 date-time-string))
581 (second-fraction (match-string 8 date-time-string))
582 (has-time-zone (match-string 9 date-time-string))
583 (time-zone-sign (match-string 10 date-time-string))
584 (time-zone-hour (match-string 11 date-time-string))
585 (time-zone-minute (match-string 12 date-time-string)))
586 (setq year-sign (if year-sign -1 1))
587 (setq year
588 (if year
589 (* year-sign
590 (string-to-number year))
591 ;; By defaulting to the epoch date, a time value can be treated as
592 ;; a relative number of seconds.
593 1970))
594 (setq month
595 (if month (string-to-number month) 1))
596 (setq day
597 (if day (string-to-number day) 1))
598 (setq hour
599 (if hour (string-to-number hour) 0))
600 (setq minute
601 (if minute (string-to-number minute) 0))
602 (setq second
603 (if second (string-to-number second) 0))
604 (setq second-fraction
605 (if second-fraction
606 (float (string-to-number second-fraction))
607 0.0))
608 (setq has-time-zone (and has-time-zone t))
609 (setq time-zone-sign
610 (if (equal time-zone-sign "-") -1 1))
611 (setq time-zone-hour
612 (if time-zone-hour (string-to-number time-zone-hour) 0))
613 (setq time-zone-minute
614 (if time-zone-minute (string-to-number time-zone-minute) 0))
615 (unless (and
616 ;; XSD does not allow year 0.
617 (> year 0)
618 (>= month 1) (<= month 12)
619 (>= day 1) (<= day (rng-xsd-days-in-month year month))
620 (>= hour 0) (<= hour 23)
621 (>= minute 0) (<= minute 59)
622 ;; 60 represents a leap second, but leap seconds are explicitly
623 ;; disallowed by the XML Schema 1.1 specification. This agrees
624 ;; with typical Emacs installations, which don't count leap
625 ;; seconds in time values.
626 (>= second 0) (<= second 59)
627 (>= time-zone-hour 0)
628 (<= time-zone-hour 23)
629 (>= time-zone-minute 0)
630 (<= time-zone-minute 59))
631 (error "Invalid or unsupported time: %s" date-time-string))
632 ;; Return a value in a format similar to that returned by decode-time, and
633 ;; suitable for (apply 'encode-time ...).
634 (list second minute hour day month year second-fraction datatype
635 (if has-time-zone
636 (* (rng-xsd-time-to-seconds
637 time-zone-hour
638 time-zone-minute
640 time-zone-sign)
641 ;; UTC.
642 0))))
644 (defun soap-decode-xs-basic-type (type node)
645 "Use TYPE, a `soap-xs-basic-type', to decode the contents of NODE.
646 A LISP value is returned based on the contents of NODE and the
647 type-info stored in TYPE.
649 This is a specialization of `soap-decode-type' for
650 `soap-xs-basic-type' objects."
651 (let ((contents (xml-node-children node))
652 (kind (soap-xs-basic-type-kind type))
653 (attributes (xml-node-attributes node))
654 (validate-type type)
655 (is-nil nil))
657 (dolist (attribute attributes)
658 (let ((attribute-type (soap-l2fq (car attribute)))
659 (attribute-value (cdr attribute)))
660 ;; xsi:type can override an element's expected type.
661 (when (equal attribute-type (soap-l2fq "xsi:type"))
662 (setq validate-type
663 (soap-wsdl-get attribute-value soap-current-wsdl)))
664 ;; xsi:nil can specify that an element is nil in which case we don't
665 ;; validate it.
666 (when (equal attribute-type (soap-l2fq "xsi:nil"))
667 (setq is-nil (string= (downcase attribute-value) "true")))))
669 (unless is-nil
670 ;; For validation purposes, when xml-node-children returns nil, treat it
671 ;; as the empty string.
672 (soap-validate-xs-basic-type (car (or contents (list ""))) validate-type))
674 (if (null contents)
676 (ecase kind
677 ((string anyURI QName ID IDREF language) (car contents))
678 ((dateTime time date gYearMonth gYear gMonthDay gDay gMonth)
679 (car contents))
680 ((long short int integer
681 unsignedInt unsignedLong unsignedShort nonNegativeInteger
682 decimal byte float double duration)
683 (string-to-number (car contents)))
684 (boolean (string= (downcase (car contents)) "true"))
685 (base64Binary (base64-decode-string (car contents)))
686 (anyType (soap-decode-any-type node))
687 (Array (soap-decode-array node))))))
689 ;; Register methods for `soap-xs-basic-type'
690 (let ((tag (aref (make-soap-xs-basic-type) 0)))
691 (put tag 'soap-attribute-encoder #'soap-encode-xs-basic-type-attributes)
692 (put tag 'soap-encoder #'soap-encode-xs-basic-type)
693 (put tag 'soap-decoder #'soap-decode-xs-basic-type))
695 ;;;;; soap-xs-element
697 (defstruct (soap-xs-element (:include soap-element))
698 ;; NOTE: we don't support exact number of occurrences via minOccurs,
699 ;; maxOccurs. Instead we support optional? and multiple?
702 type^ ; note: use soap-xs-element-type to retrieve this member
703 optional?
704 multiple?
705 reference
706 substitution-group
707 ;; contains a list of elements who point to this one via their
708 ;; substitution-group slot
709 alternatives
710 is-group)
712 (defun soap-xs-element-type (element)
713 "Retrieve the type of ELEMENT.
714 This is normally stored in the TYPE^ slot, but if this element
715 contains a reference, retrieve the type of the reference."
716 (if (soap-xs-element-reference element)
717 (soap-xs-element-type (soap-xs-element-reference element))
718 (soap-xs-element-type^ element)))
720 (defun soap-node-optional (node)
721 "Return t if NODE specifies an optional element."
722 (or (equal (xml-get-attribute-or-nil node 'nillable) "true")
723 (let ((e (xml-get-attribute-or-nil node 'minOccurs)))
724 (and e (equal e "0")))))
726 (defun soap-node-multiple (node)
727 "Return t if NODE permits multiple elements."
728 (let* ((e (xml-get-attribute-or-nil node 'maxOccurs)))
729 (and e (not (equal e "1")))))
731 (defun soap-xs-parse-element (node)
732 "Construct a `soap-xs-element' from NODE."
733 (let ((name (xml-get-attribute-or-nil node 'name))
734 (id (xml-get-attribute-or-nil node 'id))
735 (type (xml-get-attribute-or-nil node 'type))
736 (optional? (soap-node-optional node))
737 (multiple? (soap-node-multiple node))
738 (ref (xml-get-attribute-or-nil node 'ref))
739 (substitution-group (xml-get-attribute-or-nil node 'substitutionGroup))
740 (node-name (soap-l2wk (xml-node-name node))))
741 (assert (memq node-name '(xsd:element xsd:group))
742 "expecting xsd:element or xsd:group, got %s" node-name)
744 (when type
745 (setq type (soap-l2fq type 'tns)))
747 (when ref
748 (setq ref (soap-l2fq ref 'tns)))
750 (when substitution-group
751 (setq substitution-group (soap-l2fq substitution-group 'tns)))
753 (unless (or ref type)
754 ;; no type specified and this is not a reference. Must be a type
755 ;; defined within this node.
756 (let ((simple-type (soap-xml-get-children1 node 'xsd:simpleType)))
757 (if simple-type
758 (setq type (soap-xs-parse-simple-type (car simple-type)))
759 ;; else
760 (let ((complex-type (soap-xml-get-children1 node 'xsd:complexType)))
761 (if complex-type
762 (setq type (soap-xs-parse-complex-type (car complex-type)))
763 ;; else
764 (error "Soap-xs-parse-element: missing type or ref"))))))
766 (make-soap-xs-element :name name
767 ;; Use the full namespace name for now, we will
768 ;; convert it to a nstag in
769 ;; `soap-resolve-references-for-xs-element'
770 :namespace-tag soap-target-xmlns
771 :id id :type^ type
772 :optional? optional? :multiple? multiple?
773 :reference ref
774 :substitution-group substitution-group
775 :is-group (eq node-name 'xsd:group))))
777 (defun soap-resolve-references-for-xs-element (element wsdl)
778 "Replace names in ELEMENT with the referenced objects in the WSDL.
779 This is a specialization of `soap-resolve-references' for
780 `soap-xs-element' objects.
782 See also `soap-wsdl-resolve-references'."
784 (let ((namespace (soap-element-namespace-tag element)))
785 (when namespace
786 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
787 (when nstag
788 (setf (soap-element-namespace-tag element) nstag)))))
790 (let ((type (soap-xs-element-type^ element)))
791 (cond ((soap-name-p type)
792 (setf (soap-xs-element-type^ element)
793 (soap-wsdl-get type wsdl 'soap-xs-type-p)))
794 ((soap-xs-type-p type)
795 ;; an inline defined type, this will not be reached from anywhere
796 ;; else, so we must resolve references now.
797 (soap-resolve-references type wsdl))))
798 (let ((reference (soap-xs-element-reference element)))
799 (when (and (soap-name-p reference)
800 ;; xsd:group reference nodes will be converted to inline types
801 ;; by soap-resolve-references-for-xs-complex-type, so skip them
802 ;; here.
803 (not (soap-xs-element-is-group element)))
804 (setf (soap-xs-element-reference element)
805 (soap-wsdl-get reference wsdl 'soap-xs-element-p))))
807 (let ((subst (soap-xs-element-substitution-group element)))
808 (when (soap-name-p subst)
809 (let ((target (soap-wsdl-get subst wsdl)))
810 (if target
811 (push element (soap-xs-element-alternatives target))
812 (soap-warning "No target found for substitution-group" subst))))))
814 (defun soap-encode-xs-element-attributes (value element)
815 "Encode the XML attributes for VALUE according to ELEMENT.
816 Currently no attributes are needed.
818 This is a specialization of `soap-encode-attributes' for
819 `soap-xs-basic-type' objects."
820 ;; Use the variables to suppress checkdoc and compiler warnings.
821 (list value element)
822 nil)
824 (defun soap-should-encode-value-for-xs-element (value element)
825 "Return t if VALUE should be encoded for ELEMENT, nil otherwise."
826 (cond
827 ;; if value is not nil, attempt to encode it
828 (value)
830 ;; value is nil, but the element's type is a boolean, so nil in this case
831 ;; means "false". We need to encode it.
832 ((let ((type (soap-xs-element-type element)))
833 (and (soap-xs-basic-type-p type)
834 (eq (soap-xs-basic-type-kind type) 'boolean))))
836 ;; This is not an optional element. Force encoding it (although this
837 ;; might fail at the validation step, but this is what we intend.
839 ;; value is nil, but the element's type has some attributes which supply a
840 ;; default value. We need to encode it.
842 ((let ((type (soap-xs-element-type element)))
843 (catch 'found
844 (dolist (a (soap-xs-type-attributes type))
845 (when (soap-xs-attribute-default a)
846 (throw 'found t))))))
848 ;; otherwise, we don't need to encode it
849 (t nil)))
851 (defun soap-type-is-array? (type)
852 "Return t if TYPE defines an ARRAY."
853 (and (soap-xs-complex-type-p type)
854 (eq (soap-xs-complex-type-indicator type) 'array)))
856 (defvar soap-encoded-namespaces nil
857 "A list of namespace tags used during encoding a message.
858 This list is populated by `soap-encode-value' and used by
859 `soap-create-envelope' to add aliases for these namespace to the
860 XML request.
862 This variable is dynamically bound in `soap-create-envelope'.")
864 (defun soap-encode-xs-element (value element)
865 "Encode the VALUE according to ELEMENT.
866 The data is inserted in the current buffer at the current
867 position.
869 This is a specialization of `soap-encode-value' for
870 `soap-xs-basic-type' objects."
871 (let ((fq-name (soap-element-fq-name element))
872 (type (soap-xs-element-type element)))
873 ;; Only encode the element if it has a name. NOTE: soap-element-fq-name
874 ;; will return *unnamed* for such elements
875 (if (soap-element-name element)
876 ;; Don't encode this element if value is nil. However, even if value
877 ;; is nil we still want to encode this element if it has any attributes
878 ;; with default values.
879 (when (soap-should-encode-value-for-xs-element value element)
880 (progn
881 (insert "<" fq-name)
882 (soap-encode-attributes value type)
883 ;; If value is nil and type is boolean encode the value as "false".
884 ;; Otherwise don't encode the value.
885 (if (or value (and (soap-xs-basic-type-p type)
886 (eq (soap-xs-basic-type-kind type) 'boolean)))
887 (progn (insert ">")
888 ;; ARRAY's need special treatment, as each element of
889 ;; the array is encoded with the same tag as the
890 ;; current element...
891 (if (soap-type-is-array? type)
892 (let ((new-element (copy-soap-xs-element element)))
893 (when (soap-element-namespace-tag type)
894 (add-to-list 'soap-encoded-namespaces
895 (soap-element-namespace-tag type)))
896 (setf (soap-xs-element-type^ new-element)
897 (soap-xs-complex-type-base type))
898 (loop for i below (length value)
899 do (progn
900 (soap-encode-xs-element (aref value i) new-element)
902 (soap-encode-value value type))
903 (insert "</" fq-name ">\n"))
904 ;; else
905 (insert "/>\n"))))
906 (when (soap-should-encode-value-for-xs-element value element)
907 (soap-encode-value value type)))))
909 (defun soap-decode-xs-element (element node)
910 "Use ELEMENT, a `soap-xs-element', to decode the contents of NODE.
911 A LISP value is returned based on the contents of NODE and the
912 type-info stored in ELEMENT.
914 This is a specialization of `soap-decode-type' for
915 `soap-xs-basic-type' objects."
916 (let ((type (soap-xs-element-type element)))
917 (soap-decode-type type node)))
919 ;; Register methods for `soap-xs-element'
920 (let ((tag (aref (make-soap-xs-element) 0)))
921 (put tag 'soap-resolve-references #'soap-resolve-references-for-xs-element)
922 (put tag 'soap-attribute-encoder #'soap-encode-xs-element-attributes)
923 (put tag 'soap-encoder #'soap-encode-xs-element)
924 (put tag 'soap-decoder #'soap-decode-xs-element))
926 ;;;;; soap-xs-attribute
928 (defstruct (soap-xs-attribute (:include soap-element))
929 type ; a simple type or basic type
930 default ; the default value, if any
931 reference)
933 (defstruct (soap-xs-attribute-group (:include soap-xs-type))
934 reference)
936 (defun soap-xs-parse-attribute (node)
937 "Construct a `soap-xs-attribute' from NODE."
938 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:attribute)
939 "expecting xsd:attribute, got %s" (soap-l2wk (xml-node-name node)))
940 (let* ((name (xml-get-attribute-or-nil node 'name))
941 (type (soap-l2fq (xml-get-attribute-or-nil node 'type)))
942 (default (xml-get-attribute-or-nil node 'fixed))
943 (attribute (xml-get-attribute-or-nil node 'ref))
944 (ref (when attribute (soap-l2fq attribute))))
945 (unless (or type ref)
946 (setq type (soap-xs-parse-simple-type
947 (soap-xml-node-find-matching-child
948 node '(xsd:restriction xsd:list xsd:union)))))
949 (make-soap-xs-attribute
950 :name name :type type :default default :reference ref)))
952 (defun soap-xs-parse-attribute-group (node)
953 "Construct a `soap-xs-attribute-group' from NODE."
954 (let ((node-name (soap-l2wk (xml-node-name node))))
955 (assert (eq node-name 'xsd:attributeGroup)
956 "expecting xsd:attributeGroup, got %s" node-name)
957 (let ((name (xml-get-attribute-or-nil node 'name))
958 (id (xml-get-attribute-or-nil node 'id))
959 (ref (xml-get-attribute-or-nil node 'ref))
960 attribute-group)
961 (when (and name ref)
962 (soap-warning "name and ref set for attribute group %s" node-name))
963 (setq attribute-group
964 (make-soap-xs-attribute-group :id id
965 :name name
966 :reference (and ref (soap-l2fq ref))))
967 (when (not ref)
968 (dolist (child (xml-node-children node))
969 ;; Ignore whitespace.
970 (unless (stringp child)
971 ;; Ignore optional annotation.
972 ;; Ignore anyAttribute nodes.
973 (case (soap-l2wk (xml-node-name child))
974 (xsd:attribute
975 (push (soap-xs-parse-attribute child)
976 (soap-xs-type-attributes attribute-group)))
977 (xsd:attributeGroup
978 (push (soap-xs-parse-attribute-group child)
979 (soap-xs-attribute-group-attribute-groups
980 attribute-group)))))))
981 attribute-group)))
983 (defun soap-resolve-references-for-xs-attribute (attribute wsdl)
984 "Replace names in ATTRIBUTE with the referenced objects in the WSDL.
985 This is a specialization of `soap-resolve-references' for
986 `soap-xs-attribute' objects.
988 See also `soap-wsdl-resolve-references'."
989 (let* ((type (soap-xs-attribute-type attribute))
990 (reference (soap-xs-attribute-reference attribute))
991 (predicate 'soap-xs-element-p)
992 (xml-reference
993 (and (soap-name-p reference)
994 (equal (car reference) "http://www.w3.org/XML/1998/namespace"))))
995 (cond (xml-reference
996 ;; Convert references to attributes defined by the XML
997 ;; schema (xml:base, xml:lang, xml:space and xml:id) to
998 ;; xsd:string, to avoid needing to bundle and parse
999 ;; xml.xsd.
1000 (setq reference '("http://www.w3.org/2001/XMLSchema" . "string"))
1001 (setq predicate 'soap-xs-basic-type-p))
1002 ((soap-name-p type)
1003 (setf (soap-xs-attribute-type attribute)
1004 (soap-wsdl-get type wsdl
1005 (lambda (type)
1006 (or (soap-xs-basic-type-p type)
1007 (soap-xs-simple-type-p type))))))
1008 ((soap-xs-type-p type)
1009 ;; an inline defined type, this will not be reached from anywhere
1010 ;; else, so we must resolve references now.
1011 (soap-resolve-references type wsdl)))
1012 (when (soap-name-p reference)
1013 (setf (soap-xs-attribute-reference attribute)
1014 (soap-wsdl-get reference wsdl predicate)))))
1016 (put (aref (make-soap-xs-attribute) 0)
1017 'soap-resolve-references #'soap-resolve-references-for-xs-attribute)
1019 (defun soap-resolve-references-for-xs-attribute-group (attribute-group wsdl)
1020 "Set slots in ATTRIBUTE-GROUP to the referenced objects in the WSDL.
1021 This is a specialization of `soap-resolve-references' for
1022 `soap-xs-attribute-group' objects.
1024 See also `soap-wsdl-resolve-references'."
1025 (let ((reference (soap-xs-attribute-group-reference attribute-group)))
1026 (when (soap-name-p reference)
1027 (let ((resolved (soap-wsdl-get reference wsdl
1028 'soap-xs-attribute-group-p)))
1029 (dolist (attribute (soap-xs-attribute-group-attributes resolved))
1030 (soap-resolve-references attribute wsdl))
1031 (setf (soap-xs-attribute-group-name attribute-group)
1032 (soap-xs-attribute-group-name resolved))
1033 (setf (soap-xs-attribute-group-id attribute-group)
1034 (soap-xs-attribute-group-id resolved))
1035 (setf (soap-xs-attribute-group-reference attribute-group) nil)
1036 (setf (soap-xs-attribute-group-attributes attribute-group)
1037 (soap-xs-attribute-group-attributes resolved))
1038 (setf (soap-xs-attribute-group-attribute-groups attribute-group)
1039 (soap-xs-attribute-group-attribute-groups resolved))))))
1041 (put (aref (make-soap-xs-attribute-group) 0)
1042 'soap-resolve-references #'soap-resolve-references-for-xs-attribute-group)
1044 ;;;;; soap-xs-simple-type
1046 (defstruct (soap-xs-simple-type (:include soap-xs-type))
1047 ;; A simple type is an extension on the basic type to which some
1048 ;; restrictions can be added. For example we can define a simple type based
1049 ;; off "string" with the restrictions that only the strings "one", "two" and
1050 ;; "three" are valid values (this is an enumeration).
1052 base ; can be a single type, or a list of types for union types
1053 enumeration ; nil, or list of permitted values for the type
1054 pattern ; nil, or value must match this pattern
1055 length-range ; a cons of (min . max) length, inclusive range.
1056 ; For exact length, use (l, l).
1057 ; nil means no range,
1058 ; (nil . l) means no min range,
1059 ; (l . nil) means no max range.
1060 integer-range ; a pair of (min, max) integer values, inclusive range,
1061 ; same meaning as `length-range'
1062 is-list ; t if this is an xs:list, nil otherwise
1065 (defun soap-xs-parse-simple-type (node)
1066 "Construct an `soap-xs-simple-type' object from the XML NODE."
1067 (assert (memq (soap-l2wk (xml-node-name node))
1068 '(xsd:simpleType xsd:simpleContent))
1070 "expecting xsd:simpleType or xsd:simpleContent node, got %s"
1071 (soap-l2wk (xml-node-name node)))
1073 ;; NOTE: name can be nil for inline types. Such types cannot be added to a
1074 ;; namespace.
1075 (let ((name (xml-get-attribute-or-nil node 'name))
1076 (id (xml-get-attribute-or-nil node 'id)))
1078 (let ((type (make-soap-xs-simple-type
1079 :name name :namespace-tag soap-target-xmlns :id id))
1080 (def (soap-xml-node-find-matching-child
1081 node '(xsd:restriction xsd:extension xsd:union xsd:list))))
1082 (ecase (soap-l2wk (xml-node-name def))
1083 (xsd:restriction (soap-xs-add-restriction def type))
1084 (xsd:extension (soap-xs-add-extension def type))
1085 (xsd:union (soap-xs-add-union def type))
1086 (xsd:list (soap-xs-add-list def type)))
1088 type)))
1090 (defun soap-xs-add-restriction (node type)
1091 "Add restrictions defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1093 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:restriction)
1095 "expecting xsd:restriction node, got %s"
1096 (soap-l2wk (xml-node-name node)))
1098 (setf (soap-xs-simple-type-base type)
1099 (soap-l2fq (xml-get-attribute node 'base)))
1101 (dolist (r (xml-node-children node))
1102 (unless (stringp r) ; skip the white space
1103 (let ((value (xml-get-attribute r 'value)))
1104 (case (soap-l2wk (xml-node-name r))
1105 (xsd:enumeration
1106 (push value (soap-xs-simple-type-enumeration type)))
1107 (xsd:pattern
1108 (setf (soap-xs-simple-type-pattern type)
1109 (concat "\\`" (xsdre-translate value) "\\'")))
1110 (xsd:length
1111 (let ((value (string-to-number value)))
1112 (setf (soap-xs-simple-type-length-range type)
1113 (cons value value))))
1114 (xsd:minLength
1115 (let ((value (string-to-number value)))
1116 (setf (soap-xs-simple-type-length-range type)
1117 (if (soap-xs-simple-type-length-range type)
1118 (cons value
1119 (cdr (soap-xs-simple-type-length-range type)))
1120 ;; else
1121 (cons value nil)))))
1122 (xsd:maxLength
1123 (let ((value (string-to-number value)))
1124 (setf (soap-xs-simple-type-length-range type)
1125 (if (soap-xs-simple-type-length-range type)
1126 (cons (car (soap-xs-simple-type-length-range type))
1127 value)
1128 ;; else
1129 (cons nil value)))))
1130 (xsd:minExclusive
1131 (let ((value (string-to-number value)))
1132 (setf (soap-xs-simple-type-integer-range type)
1133 (if (soap-xs-simple-type-integer-range type)
1134 (cons (1+ value)
1135 (cdr (soap-xs-simple-type-integer-range type)))
1136 ;; else
1137 (cons (1+ value) nil)))))
1138 (xsd:maxExclusive
1139 (let ((value (string-to-number value)))
1140 (setf (soap-xs-simple-type-integer-range type)
1141 (if (soap-xs-simple-type-integer-range type)
1142 (cons (car (soap-xs-simple-type-integer-range type))
1143 (1- value))
1144 ;; else
1145 (cons nil (1- value))))))
1146 (xsd:minInclusive
1147 (let ((value (string-to-number value)))
1148 (setf (soap-xs-simple-type-integer-range type)
1149 (if (soap-xs-simple-type-integer-range type)
1150 (cons value
1151 (cdr (soap-xs-simple-type-integer-range type)))
1152 ;; else
1153 (cons value nil)))))
1154 (xsd:maxInclusive
1155 (let ((value (string-to-number value)))
1156 (setf (soap-xs-simple-type-integer-range type)
1157 (if (soap-xs-simple-type-integer-range type)
1158 (cons (car (soap-xs-simple-type-integer-range type))
1159 value)
1160 ;; else
1161 (cons nil value))))))))))
1163 (defun soap-xs-add-union (node type)
1164 "Add union members defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1165 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:union)
1167 "expecting xsd:union node, got %s" (soap-l2wk (xml-node-name node)))
1169 (setf (soap-xs-simple-type-base type)
1170 (mapcar 'soap-l2fq
1171 (split-string
1172 (or (xml-get-attribute-or-nil node 'memberTypes) ""))))
1174 ;; Additional simple types can be defined inside the union node. Add them
1175 ;; to the base list. The "memberTypes" members will have to be resolved by
1176 ;; the "resolve-references" method, the inline types will not.
1177 (let (result)
1178 (dolist (simple-type (soap-xml-get-children1 node 'xsd:simpleType))
1179 (push (soap-xs-parse-simple-type simple-type) result))
1180 (setf (soap-xs-simple-type-base type)
1181 (append (soap-xs-simple-type-base type) (nreverse result)))))
1183 (defun soap-xs-add-list (node type)
1184 "Add list defined in XML NODE to TYPE, a `soap-xs-simple-type'."
1185 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:list)
1187 "expecting xsd:list node, got %s" (soap-l2wk (xml-node-name node)))
1189 ;; A simple type can be defined inline inside the list node or referenced by
1190 ;; the itemType attribute, in which case it will be resolved by the
1191 ;; resolve-references method.
1192 (let* ((item-type (xml-get-attribute-or-nil node 'itemType))
1193 (children (soap-xml-get-children1 node 'xsd:simpleType)))
1194 (if item-type
1195 (if (= (length children) 0)
1196 (setf (soap-xs-simple-type-base type) (soap-l2fq item-type))
1197 (soap-warning
1198 "xsd:list node with itemType has more than zero children: %s"
1199 (soap-xs-type-name type)))
1200 (if (= (length children) 1)
1201 (setf (soap-xs-simple-type-base type)
1202 (soap-xs-parse-simple-type
1203 (car (soap-xml-get-children1 node 'xsd:simpleType))))
1204 (soap-warning "xsd:list node has more than one child %s"
1205 (soap-xs-type-name type))))
1206 (setf (soap-xs-simple-type-is-list type) t)))
1208 (defun soap-xs-add-extension (node type)
1209 "Add the extended type defined in XML NODE to TYPE, an `soap-xs-simple-type'."
1210 (setf (soap-xs-simple-type-base type)
1211 (soap-l2fq (xml-get-attribute node 'base)))
1212 (dolist (attribute (soap-xml-get-children1 node 'xsd:attribute))
1213 (push (soap-xs-parse-attribute attribute)
1214 (soap-xs-type-attributes type)))
1215 (dolist (attribute-group (soap-xml-get-children1 node 'xsd:attributeGroup))
1216 (push (soap-xs-parse-attribute-group attribute-group)
1217 (soap-xs-type-attribute-groups type))))
1219 (defun soap-validate-xs-basic-type (value type)
1220 "Validate VALUE against the basic type TYPE."
1221 (let* ((kind (soap-xs-basic-type-kind type)))
1222 (case kind
1223 ((anyType Array byte[])
1224 value)
1226 (let ((convert (get kind 'rng-xsd-convert)))
1227 (if convert
1228 (if (rng-dt-make-value convert value)
1229 value
1230 (error "Invalid %s: %s" (symbol-name kind) value))
1231 (error "Don't know how to convert %s" kind)))))))
1233 (defun soap-validate-xs-simple-type (value type)
1234 "Validate VALUE against the restrictions of TYPE."
1236 (let* ((base-type (soap-xs-simple-type-base type))
1237 (messages nil))
1238 (if (listp base-type)
1239 (catch 'valid
1240 (dolist (base base-type)
1241 (condition-case error-object
1242 (cond ((soap-xs-simple-type-p base)
1243 (throw 'valid
1244 (soap-validate-xs-simple-type value base)))
1245 ((soap-xs-basic-type-p base)
1246 (throw 'valid
1247 (soap-validate-xs-basic-type value base))))
1248 (error (push (cadr error-object) messages))))
1249 (when messages
1250 (error (mapconcat 'identity (nreverse messages) "; and: "))))
1251 (cl-labels ((fail-with-message (format value)
1252 (push (format format value) messages)
1253 (throw 'invalid nil)))
1254 (catch 'invalid
1255 (let ((enumeration (soap-xs-simple-type-enumeration type)))
1256 (when (and (> (length enumeration) 1)
1257 (not (member value enumeration)))
1258 (fail-with-message "bad value, should be one of %s" enumeration)))
1260 (let ((pattern (soap-xs-simple-type-pattern type)))
1261 (when (and pattern (not (string-match-p pattern value)))
1262 (fail-with-message "bad value, should match pattern %s" pattern)))
1264 (let ((length-range (soap-xs-simple-type-length-range type)))
1265 (when length-range
1266 (unless (stringp value)
1267 (fail-with-message
1268 "bad value, should be a string with length range %s"
1269 length-range))
1270 (when (car length-range)
1271 (unless (>= (length value) (car length-range))
1272 (fail-with-message "short string, should be at least %s chars"
1273 (car length-range))))
1274 (when (cdr length-range)
1275 (unless (<= (length value) (cdr length-range))
1276 (fail-with-message "long string, should be at most %s chars"
1277 (cdr length-range))))))
1279 (let ((integer-range (soap-xs-simple-type-integer-range type)))
1280 (when integer-range
1281 (unless (numberp value)
1282 (fail-with-message "bad value, should be a number with range %s"
1283 integer-range))
1284 (when (car integer-range)
1285 (unless (>= value (car integer-range))
1286 (fail-with-message "small value, should be at least %s"
1287 (car integer-range))))
1288 (when (cdr integer-range)
1289 (unless (<= value (cdr integer-range))
1290 (fail-with-message "big value, should be at most %s"
1291 (cdr integer-range))))))))
1292 (when messages
1293 (error "Xs-simple-type(%s, %s): %s"
1294 value (or (soap-xs-type-name type) (soap-xs-type-id type))
1295 (car messages)))))
1296 ;; Return the validated value.
1297 value)
1299 (defun soap-resolve-references-for-xs-simple-type (type wsdl)
1300 "Replace names in TYPE with the referenced objects in the WSDL.
1301 This is a specialization of `soap-resolve-references' for
1302 `soap-xs-simple-type' objects.
1304 See also `soap-wsdl-resolve-references'."
1306 (let ((namespace (soap-element-namespace-tag type)))
1307 (when namespace
1308 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
1309 (when nstag
1310 (setf (soap-element-namespace-tag type) nstag)))))
1312 (let ((base (soap-xs-simple-type-base type)))
1313 (cond
1314 ((soap-name-p base)
1315 (setf (soap-xs-simple-type-base type)
1316 (soap-wsdl-get base wsdl 'soap-xs-type-p)))
1317 ((soap-xs-type-p base)
1318 (soap-resolve-references base wsdl))
1319 ((listp base)
1320 (setf (soap-xs-simple-type-base type)
1321 (mapcar (lambda (type)
1322 (cond ((soap-name-p type)
1323 (soap-wsdl-get type wsdl 'soap-xs-type-p))
1324 ((soap-xs-type-p type)
1325 (soap-resolve-references type wsdl)
1326 type)
1327 (t ; signal an error?
1328 type)))
1329 base)))
1330 (t (error "Oops"))))
1331 (dolist (attribute (soap-xs-type-attributes type))
1332 (soap-resolve-references attribute wsdl))
1333 (dolist (attribute-group (soap-xs-type-attribute-groups type))
1334 (soap-resolve-references attribute-group wsdl)))
1336 (defun soap-encode-xs-simple-type-attributes (value type)
1337 "Encode the XML attributes for VALUE according to TYPE.
1338 The xsi:type and an optional xsi:nil attributes are added. The
1339 attributes are inserted in the current buffer at the current
1340 position.
1342 This is a specialization of `soap-encode-attributes' for
1343 `soap-xs-simple-type' objects."
1344 (insert " xsi:type=\"" (soap-element-fq-name type) "\"")
1345 (unless value (insert " xsi:nil=\"true\"")))
1347 (defun soap-encode-xs-simple-type (value type)
1348 "Encode the VALUE according to TYPE.
1349 The data is inserted in the current buffer at the current
1350 position.
1352 This is a specialization of `soap-encode-value' for
1353 `soap-xs-simple-type' objects."
1354 (soap-validate-xs-simple-type value type)
1355 (if (soap-xs-simple-type-is-list type)
1356 (progn
1357 (dolist (v (butlast value))
1358 (soap-encode-value v (soap-xs-simple-type-base type))
1359 (insert " "))
1360 (soap-encode-value (car (last value)) (soap-xs-simple-type-base type)))
1361 (soap-encode-value value (soap-xs-simple-type-base type))))
1363 (defun soap-decode-xs-simple-type (type node)
1364 "Use TYPE, a `soap-xs-simple-type', to decode the contents of NODE.
1365 A LISP value is returned based on the contents of NODE and the
1366 type-info stored in TYPE.
1368 This is a specialization of `soap-decode-type' for
1369 `soap-xs-simple-type' objects."
1370 (if (soap-xs-simple-type-is-list type)
1371 ;; Technically, we could construct fake XML NODEs and pass them to
1372 ;; soap-decode-value...
1373 (split-string (car (xml-node-children node)))
1374 (let ((value (soap-decode-type (soap-xs-simple-type-base type) node)))
1375 (soap-validate-xs-simple-type value type))))
1377 ;; Register methods for `soap-xs-simple-type'
1378 (let ((tag (aref (make-soap-xs-simple-type) 0)))
1379 (put tag 'soap-resolve-references
1380 #'soap-resolve-references-for-xs-simple-type)
1381 (put tag 'soap-attribute-encoder #'soap-encode-xs-simple-type-attributes)
1382 (put tag 'soap-encoder #'soap-encode-xs-simple-type)
1383 (put tag 'soap-decoder #'soap-decode-xs-simple-type))
1385 ;;;;; soap-xs-complex-type
1387 (defstruct (soap-xs-complex-type (:include soap-xs-type))
1388 indicator ; sequence, choice, all, array
1389 base
1390 elements
1391 optional?
1392 multiple?
1393 is-group)
1395 (defun soap-xs-parse-complex-type (node)
1396 "Construct a `soap-xs-complex-type' by parsing the XML NODE."
1397 (let ((name (xml-get-attribute-or-nil node 'name))
1398 (id (xml-get-attribute-or-nil node 'id))
1399 (node-name (soap-l2wk (xml-node-name node)))
1400 type
1401 attributes
1402 attribute-groups)
1403 (assert (memq node-name '(xsd:complexType xsd:complexContent xsd:group))
1404 nil "unexpected node: %s" node-name)
1406 (dolist (def (xml-node-children node))
1407 (when (consp def) ; skip text nodes
1408 (case (soap-l2wk (xml-node-name def))
1409 (xsd:attribute (push (soap-xs-parse-attribute def) attributes))
1410 (xsd:attributeGroup
1411 (push (soap-xs-parse-attribute-group def)
1412 attribute-groups))
1413 (xsd:simpleContent (setq type (soap-xs-parse-simple-type def)))
1414 ((xsd:sequence xsd:all xsd:choice)
1415 (setq type (soap-xs-parse-sequence def)))
1416 (xsd:complexContent
1417 (dolist (def (xml-node-children def))
1418 (when (consp def)
1419 (case (soap-l2wk (xml-node-name def))
1420 (xsd:attribute
1421 (push (soap-xs-parse-attribute def) attributes))
1422 (xsd:attributeGroup
1423 (push (soap-xs-parse-attribute-group def)
1424 attribute-groups))
1425 ((xsd:extension xsd:restriction)
1426 (setq type
1427 (soap-xs-parse-extension-or-restriction def)))
1428 ((xsd:sequence xsd:all xsd:choice)
1429 (soap-xs-parse-sequence def)))))))))
1430 (unless type
1431 ;; the type has not been built, this is a shortcut for a simpleContent
1432 ;; node
1433 (setq type (make-soap-xs-complex-type)))
1435 (setf (soap-xs-type-name type) name)
1436 (setf (soap-xs-type-namespace-tag type) soap-target-xmlns)
1437 (setf (soap-xs-type-id type) id)
1438 (setf (soap-xs-type-attributes type)
1439 (append attributes (soap-xs-type-attributes type)))
1440 (setf (soap-xs-type-attribute-groups type)
1441 (append attribute-groups (soap-xs-type-attribute-groups type)))
1442 (when (soap-xs-complex-type-p type)
1443 (setf (soap-xs-complex-type-is-group type)
1444 (eq node-name 'xsd:group)))
1445 type))
1447 (defun soap-xs-parse-sequence (node)
1448 "Parse a sequence definition from XML NODE.
1449 Returns a `soap-xs-complex-type'"
1450 (assert (memq (soap-l2wk (xml-node-name node))
1451 '(xsd:sequence xsd:choice xsd:all))
1453 "unexpected node: %s" (soap-l2wk (xml-node-name node)))
1455 (let ((type (make-soap-xs-complex-type)))
1457 (setf (soap-xs-complex-type-indicator type)
1458 (ecase (soap-l2wk (xml-node-name node))
1459 (xsd:sequence 'sequence)
1460 (xsd:all 'all)
1461 (xsd:choice 'choice)))
1463 (setf (soap-xs-complex-type-optional? type) (soap-node-optional node))
1464 (setf (soap-xs-complex-type-multiple? type) (soap-node-multiple node))
1466 (dolist (r (xml-node-children node))
1467 (unless (stringp r) ; skip the white space
1468 (case (soap-l2wk (xml-node-name r))
1469 ((xsd:element xsd:group)
1470 (push (soap-xs-parse-element r)
1471 (soap-xs-complex-type-elements type)))
1472 ((xsd:sequence xsd:choice xsd:all)
1473 ;; an inline sequence, choice or all node
1474 (let ((choice (soap-xs-parse-sequence r)))
1475 (push (make-soap-xs-element :name nil :type^ choice)
1476 (soap-xs-complex-type-elements type))))
1477 (xsd:attribute
1478 (push (soap-xs-parse-attribute r)
1479 (soap-xs-type-attributes type)))
1480 (xsd:attributeGroup
1481 (push (soap-xs-parse-attribute-group r)
1482 (soap-xs-type-attribute-groups type))))))
1484 (setf (soap-xs-complex-type-elements type)
1485 (nreverse (soap-xs-complex-type-elements type)))
1487 type))
1489 (defun soap-xs-parse-extension-or-restriction (node)
1490 "Parse an extension or restriction definition from XML NODE.
1491 Return a `soap-xs-complex-type'."
1492 (assert (memq (soap-l2wk (xml-node-name node))
1493 '(xsd:extension xsd:restriction))
1495 "unexpected node: %s" (soap-l2wk (xml-node-name node)))
1496 (let (type
1497 attributes
1498 attribute-groups
1499 array?
1500 (base (xml-get-attribute-or-nil node 'base)))
1502 ;; Array declarations are recognized specially, it is unclear to me how
1503 ;; they could be treated generally...
1504 (setq array?
1505 (and (eq (soap-l2wk (xml-node-name node)) 'xsd:restriction)
1506 (equal base (soap-wk2l "soapenc:Array"))))
1508 (dolist (def (xml-node-children node))
1509 (when (consp def) ; skip text nodes
1510 (case (soap-l2wk (xml-node-name def))
1511 ((xsd:sequence xsd:choice xsd:all)
1512 (setq type (soap-xs-parse-sequence def)))
1513 (xsd:attribute
1514 (if array?
1515 (let ((array-type
1516 (soap-xml-get-attribute-or-nil1 def 'wsdl:arrayType)))
1517 (when (and array-type
1518 (string-match "^\\(.*\\)\\[\\]$" array-type))
1519 ;; Override
1520 (setq base (match-string 1 array-type))))
1521 ;; else
1522 (push (soap-xs-parse-attribute def) attributes)))
1523 (xsd:attributeGroup
1524 (push (soap-xs-parse-attribute-group def) attribute-groups)))))
1526 (unless type
1527 (setq type (make-soap-xs-complex-type))
1528 (when array?
1529 (setf (soap-xs-complex-type-indicator type) 'array)))
1531 (setf (soap-xs-complex-type-base type) (soap-l2fq base))
1532 (setf (soap-xs-complex-type-attributes type) attributes)
1533 (setf (soap-xs-complex-type-attribute-groups type) attribute-groups)
1534 type))
1536 (defun soap-resolve-references-for-xs-complex-type (type wsdl)
1537 "Replace names in TYPE with the referenced objects in the WSDL.
1538 This is a specialization of `soap-resolve-references' for
1539 `soap-xs-complex-type' objects.
1541 See also `soap-wsdl-resolve-references'."
1543 (let ((namespace (soap-element-namespace-tag type)))
1544 (when namespace
1545 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
1546 (when nstag
1547 (setf (soap-element-namespace-tag type) nstag)))))
1549 (let ((base (soap-xs-complex-type-base type)))
1550 (cond ((soap-name-p base)
1551 (setf (soap-xs-complex-type-base type)
1552 (soap-wsdl-get base wsdl 'soap-xs-type-p)))
1553 ((soap-xs-type-p base)
1554 (soap-resolve-references base wsdl))))
1555 (let (all-elements)
1556 (dolist (element (soap-xs-complex-type-elements type))
1557 (if (soap-xs-element-is-group element)
1558 ;; This is an xsd:group element that references an xsd:group node,
1559 ;; which we treat as a complex type. We replace the reference
1560 ;; element by inlining the elements of the referenced xsd:group
1561 ;; (complex type) node.
1562 (let ((type (soap-wsdl-get
1563 (soap-xs-element-reference element)
1564 wsdl (lambda (type)
1565 (and
1566 (soap-xs-complex-type-p type)
1567 (soap-xs-complex-type-is-group type))))))
1568 (dolist (element (soap-xs-complex-type-elements type))
1569 (soap-resolve-references element wsdl)
1570 (push element all-elements)))
1571 ;; This is a non-xsd:group node so just add it directly.
1572 (soap-resolve-references element wsdl)
1573 (push element all-elements)))
1574 (setf (soap-xs-complex-type-elements type) (nreverse all-elements)))
1575 (dolist (attribute (soap-xs-type-attributes type))
1576 (soap-resolve-references attribute wsdl))
1577 (dolist (attribute-group (soap-xs-type-attribute-groups type))
1578 (soap-resolve-references attribute-group wsdl)))
1580 (defun soap-encode-xs-complex-type-attributes (value type)
1581 "Encode the XML attributes for encoding VALUE according to TYPE.
1582 The xsi:type and optional xsi:nil attributes are added, plus
1583 additional attributes needed for arrays types, if applicable. The
1584 attributes are inserted in the current buffer at the current
1585 position.
1587 This is a specialization of `soap-encode-attributes' for
1588 `soap-xs-complex-type' objects."
1589 (if (eq (soap-xs-complex-type-indicator type) 'array)
1590 (let ((element-type (soap-xs-complex-type-base type)))
1591 (insert " xsi:type=\"soapenc:Array\"")
1592 (insert " soapenc:arrayType=\""
1593 (soap-element-fq-name element-type)
1594 "[" (format "%s" (length value)) "]" "\""))
1595 ;; else
1596 (progn
1597 (dolist (a (soap-get-xs-attributes type))
1598 (let ((element-name (soap-element-name a)))
1599 (if (soap-xs-attribute-default a)
1600 (insert " " element-name
1601 "=\"" (soap-xs-attribute-default a) "\"")
1602 (dolist (value-pair value)
1603 (when (equal element-name (symbol-name (car value-pair)))
1604 (insert " " element-name
1605 "=\"" (cdr value-pair) "\""))))))
1606 ;; If this is not an empty type, and we have no value, mark it as nil
1607 (when (and (soap-xs-complex-type-indicator type) (null value))
1608 (insert " xsi:nil=\"true\"")))))
1610 (defun soap-get-candidate-elements (element)
1611 "Return a list of elements that are compatible with ELEMENT.
1612 The returned list includes ELEMENT's references and
1613 alternatives."
1614 (let ((reference (soap-xs-element-reference element)))
1615 ;; If the element is a reference, append the reference and its
1616 ;; alternatives...
1617 (if reference
1618 (append (list reference)
1619 (soap-xs-element-alternatives reference))
1620 ;; ...otherwise append the element itself and its alternatives.
1621 (append (list element)
1622 (soap-xs-element-alternatives element)))))
1624 (defun soap-encode-xs-complex-type (value type)
1625 "Encode the VALUE according to TYPE.
1626 The data is inserted in the current buffer at the current
1627 position.
1629 This is a specialization of `soap-encode-value' for
1630 `soap-xs-complex-type' objects."
1631 (case (soap-xs-complex-type-indicator type)
1632 (array
1633 (error "Arrays of type soap-encode-xs-complex-type are handled elsewhere"))
1634 ((sequence choice all nil)
1635 (let ((type-list (list type)))
1637 ;; Collect all base types
1638 (let ((base (soap-xs-complex-type-base type)))
1639 (while base
1640 (push base type-list)
1641 (setq base (soap-xs-complex-type-base base))))
1643 (dolist (type type-list)
1644 (dolist (element (soap-xs-complex-type-elements type))
1645 (catch 'done
1646 (let ((instance-count 0))
1647 (dolist (candidate (soap-get-candidate-elements element))
1648 (let ((e-name (soap-xs-element-name candidate)))
1649 (if e-name
1650 (let ((e-name (intern e-name)))
1651 (dolist (v value)
1652 (when (equal (car v) e-name)
1653 (incf instance-count)
1654 (soap-encode-value (cdr v) candidate))))
1655 (if (soap-xs-complex-type-indicator type)
1656 (let ((current-point (point)))
1657 ;; Check if encoding happened by checking if
1658 ;; characters were inserted in the buffer.
1659 (soap-encode-value value candidate)
1660 (when (not (equal current-point (point)))
1661 (incf instance-count)))
1662 (dolist (v value)
1663 (let ((current-point (point)))
1664 (soap-encode-value v candidate)
1665 (when (not (equal current-point (point)))
1666 (incf instance-count))))))))
1667 ;; Do some sanity checking
1668 (let* ((indicator (soap-xs-complex-type-indicator type))
1669 (element-type (soap-xs-element-type element))
1670 (reference (soap-xs-element-reference element))
1671 (e-name (or (soap-xs-element-name element)
1672 (and reference
1673 (soap-xs-element-name reference)))))
1674 (cond ((and (eq indicator 'choice)
1675 (> instance-count 0))
1676 ;; This was a choice node and we encoded
1677 ;; one instance.
1678 (throw 'done t))
1679 ((and (not (eq indicator 'choice))
1680 (= instance-count 0)
1681 (not (soap-xs-element-optional? element))
1682 (and (soap-xs-complex-type-p element-type)
1683 (not (soap-xs-complex-type-optional-p
1684 element-type))))
1685 (soap-warning
1686 "While encoding %s: missing non-nillable slot %s"
1687 value e-name))
1688 ((and (> instance-count 1)
1689 (not (soap-xs-element-multiple? element))
1690 (and (soap-xs-complex-type-p element-type)
1691 (not (soap-xs-complex-type-multiple-p
1692 element-type))))
1693 (soap-warning
1694 (concat "While encoding %s: expected single,"
1695 " found multiple elements for slot %s")
1696 value e-name))))))))))
1698 (error "Don't know how to encode complex type: %s"
1699 (soap-xs-complex-type-indicator type)))))
1701 (defun soap-xml-get-children-fq (node child-name)
1702 "Return the children of NODE named CHILD-NAME.
1703 This is the same as `xml-get-children1', but NODE's local
1704 namespace is used to resolve the children's namespace tags."
1705 (let (result)
1706 (dolist (c (xml-node-children node))
1707 (when (and (consp c)
1708 (soap-with-local-xmlns node
1709 ;; We use `ignore-errors' here because we want to silently
1710 ;; skip nodes for which we cannot convert them to a
1711 ;; well-known name.
1712 (equal (ignore-errors
1713 (soap-l2fq (xml-node-name c)))
1714 child-name)))
1715 (push c result)))
1716 (nreverse result)))
1718 (defun soap-xs-element-get-fq-name (element wsdl)
1719 "Return ELEMENT's fully-qualified name using WSDL's alias table.
1720 Return nil if ELEMENT does not have a name."
1721 (let* ((ns-aliases (soap-wsdl-alias-table wsdl))
1722 (ns-name (cdr (assoc
1723 (soap-element-namespace-tag element)
1724 ns-aliases))))
1725 (when ns-name
1726 (cons ns-name (soap-element-name element)))))
1728 (defun soap-xs-complex-type-optional-p (type)
1729 "Return t if TYPE or any of TYPE's ancestor types is optional.
1730 Return nil otherwise."
1731 (when type
1732 (or (soap-xs-complex-type-optional? type)
1733 (and (soap-xs-complex-type-p type)
1734 (soap-xs-complex-type-optional-p
1735 (soap-xs-complex-type-base type))))))
1737 (defun soap-xs-complex-type-multiple-p (type)
1738 "Return t if TYPE or any of TYPE's ancestor types permits multiple elements.
1739 Return nil otherwise."
1740 (when type
1741 (or (soap-xs-complex-type-multiple? type)
1742 (and (soap-xs-complex-type-p type)
1743 (soap-xs-complex-type-multiple-p
1744 (soap-xs-complex-type-base type))))))
1746 (defun soap-get-xs-attributes-from-groups (attribute-groups)
1747 "Return a list of attributes from all ATTRIBUTE-GROUPS."
1748 (let (attributes)
1749 (dolist (group attribute-groups)
1750 (let ((sub-groups (soap-xs-attribute-group-attribute-groups group)))
1751 (setq attributes (append attributes
1752 (soap-get-xs-attributes-from-groups sub-groups)
1753 (soap-xs-attribute-group-attributes group)))))
1754 attributes))
1756 (defun soap-get-xs-attributes (type)
1757 "Return a list of all of TYPE's and TYPE's ancestors' attributes."
1758 (let* ((base (and (soap-xs-complex-type-p type)
1759 (soap-xs-complex-type-base type)))
1760 (attributes (append (soap-xs-type-attributes type)
1761 (soap-get-xs-attributes-from-groups
1762 (soap-xs-type-attribute-groups type)))))
1763 (if base
1764 (append attributes (soap-get-xs-attributes base))
1765 attributes)))
1767 (defun soap-decode-xs-attributes (type node)
1768 "Use TYPE, a `soap-xs-complex-type', to decode the attributes of NODE."
1769 (let (result)
1770 (dolist (attribute (soap-get-xs-attributes type))
1771 (let* ((name (soap-xs-attribute-name attribute))
1772 (attribute-type (soap-xs-attribute-type attribute))
1773 (symbol (intern name))
1774 (value (xml-get-attribute-or-nil node symbol)))
1775 ;; We don't support attribute uses: required, optional, prohibited.
1776 (cond
1777 ((soap-xs-basic-type-p attribute-type)
1778 ;; Basic type values are validated by xml.el.
1779 (when value
1780 (push (cons symbol
1781 ;; Create a fake XML node to satisfy the
1782 ;; soap-decode-xs-basic-type API.
1783 (soap-decode-xs-basic-type attribute-type
1784 (list symbol nil value)))
1785 result)))
1786 ((soap-xs-simple-type-p attribute-type)
1787 (when value
1788 (push (cons symbol
1789 (soap-validate-xs-simple-type value attribute-type))
1790 result)))
1792 (error (concat "Attribute %s is of type %s which is"
1793 " not a basic or simple type")
1794 name (soap-name-p attribute))))))
1795 result))
1797 (defun soap-decode-xs-complex-type (type node)
1798 "Use TYPE, a `soap-xs-complex-type', to decode the contents of NODE.
1799 A LISP value is returned based on the contents of NODE and the
1800 type-info stored in TYPE.
1802 This is a specialization of `soap-decode-type' for
1803 `soap-xs-basic-type' objects."
1804 (case (soap-xs-complex-type-indicator type)
1805 (array
1806 (let ((result nil)
1807 (element-type (soap-xs-complex-type-base type)))
1808 (dolist (node (xml-node-children node))
1809 (when (consp node)
1810 (push (soap-decode-type element-type node) result)))
1811 (nreverse result)))
1812 ((sequence choice all nil)
1813 (let ((result nil)
1814 (base (soap-xs-complex-type-base type)))
1815 (when base
1816 (setq result (nreverse (soap-decode-type base node))))
1817 (catch 'done
1818 (dolist (element (soap-xs-complex-type-elements type))
1819 (let* ((instance-count 0)
1820 (e-name (soap-xs-element-name element))
1821 ;; Heuristic: guess if we need to decode using local
1822 ;; namespaces.
1823 (use-fq-names (string-match ":" (symbol-name (car node))))
1824 (children (if e-name
1825 (if use-fq-names
1826 ;; Find relevant children
1827 ;; using local namespaces by
1828 ;; searching for the element's
1829 ;; fully-qualified name.
1830 (soap-xml-get-children-fq
1831 node
1832 (soap-xs-element-get-fq-name
1833 element soap-current-wsdl))
1834 ;; No local namespace resolution
1835 ;; needed so use the element's
1836 ;; name unqualified.
1837 (xml-get-children node (intern e-name)))
1838 ;; e-name is nil so a) we don't know which
1839 ;; children to operate on, and b) we want to
1840 ;; re-use soap-decode-xs-complex-type, which
1841 ;; expects a node argument with a complex
1842 ;; type; therefore we need to operate on the
1843 ;; entire node. We wrap node in a list so
1844 ;; that it will carry through as "node" in the
1845 ;; loop below.
1847 ;; For example:
1849 ;; Element Type:
1850 ;; <xs:complexType name="A">
1851 ;; <xs:sequence>
1852 ;; <xs:element name="B" type="t:BType"/>
1853 ;; <xs:choice>
1854 ;; <xs:element name="C" type="xs:string"/>
1855 ;; <xs:element name="D" type="t:DType"/>
1856 ;; </xs:choice>
1857 ;; </xs:sequence>
1858 ;; </xs:complexType>
1860 ;; Node:
1861 ;; <t:A>
1862 ;; <t:B tag="b"/>
1863 ;; <t:C>1</C>
1864 ;; </t:A>
1866 ;; soap-decode-type will be called below with:
1868 ;; element =
1869 ;; <xs:choice>
1870 ;; <xs:element name="C" type="xs:string"/>
1871 ;; <xs:element name="D" type="t:DType"/>
1872 ;; </xs:choice>
1873 ;; node =
1874 ;; <t:A>
1875 ;; <t:B tag="b"/>
1876 ;; <t:C>1</C>
1877 ;; </t:A>
1878 (list node)))
1879 (element-type (soap-xs-element-type element)))
1880 (dolist (node children)
1881 (incf instance-count)
1882 (let* ((attributes
1883 (soap-decode-xs-attributes element-type node))
1884 ;; Attributes may specify xsi:type override.
1885 (element-type
1886 (if (soap-xml-get-attribute-or-nil1 node 'xsi:type)
1887 (soap-wsdl-get
1888 (soap-l2fq
1889 (soap-xml-get-attribute-or-nil1 node
1890 'xsi:type))
1891 soap-current-wsdl 'soap-xs-type-p t)
1892 element-type))
1893 (decoded-child (soap-decode-type element-type node)))
1894 (if e-name
1895 (push (cons (intern e-name)
1896 (append attributes decoded-child)) result)
1897 ;; When e-name is nil we don't want to introduce an extra
1898 ;; level of nesting, so we splice the decoding into
1899 ;; result.
1900 (setq result (append decoded-child result)))))
1901 (cond ((and (eq (soap-xs-complex-type-indicator type) 'choice)
1902 ;; Choices can allow multiple values.
1903 (not (soap-xs-complex-type-multiple-p type))
1904 (> instance-count 0))
1905 ;; This was a choice node, and we decoded one value.
1906 (throw 'done t))
1908 ;; Do some sanity checking
1909 ((and (not (eq (soap-xs-complex-type-indicator type)
1910 'choice))
1911 (= instance-count 0)
1912 (not (soap-xs-element-optional? element))
1913 (and (soap-xs-complex-type-p element-type)
1914 (not (soap-xs-complex-type-optional-p
1915 element-type))))
1916 (soap-warning "missing non-nillable slot %s" e-name))
1917 ((and (> instance-count 1)
1918 (not (soap-xs-complex-type-multiple-p type))
1919 (not (soap-xs-element-multiple? element))
1920 (and (soap-xs-complex-type-p element-type)
1921 (not (soap-xs-complex-type-multiple-p
1922 element-type))))
1923 (soap-warning "expected single %s slot, found multiple"
1924 e-name))))))
1925 (nreverse result)))
1927 (error "Don't know how to decode complex type: %s"
1928 (soap-xs-complex-type-indicator type)))))
1930 ;; Register methods for `soap-xs-complex-type'
1931 (let ((tag (aref (make-soap-xs-complex-type) 0)))
1932 (put tag 'soap-resolve-references
1933 #'soap-resolve-references-for-xs-complex-type)
1934 (put tag 'soap-attribute-encoder #'soap-encode-xs-complex-type-attributes)
1935 (put tag 'soap-encoder #'soap-encode-xs-complex-type)
1936 (put tag 'soap-decoder #'soap-decode-xs-complex-type))
1938 ;;;; WSDL documents
1939 ;;;;; WSDL document elements
1942 (defstruct (soap-message (:include soap-element))
1943 parts ; ALIST of NAME => WSDL-TYPE name
1946 (defstruct (soap-operation (:include soap-element))
1947 parameter-order
1948 input ; (NAME . MESSAGE)
1949 output ; (NAME . MESSAGE)
1950 faults ; a list of (NAME . MESSAGE)
1951 input-action ; WS-addressing action string
1952 output-action) ; WS-addressing action string
1954 (defstruct (soap-port-type (:include soap-element))
1955 operations) ; a namespace of operations
1957 ;; A bound operation is an operation which has a soap action and a use
1958 ;; method attached -- these are attached as part of a binding and we
1959 ;; can have different bindings for the same operations.
1960 (defstruct soap-bound-operation
1961 operation ; SOAP-OPERATION
1962 soap-action ; value for SOAPAction HTTP header
1963 soap-headers ; list of (message part use)
1964 soap-body ; message parts present in the body
1965 use ; 'literal or 'encoded, see
1966 ; http://www.w3.org/TR/wsdl#_soap:body
1969 (defstruct (soap-binding (:include soap-element))
1970 port-type
1971 (operations (make-hash-table :test 'equal) :readonly t))
1973 (defstruct (soap-port (:include soap-element))
1974 service-url
1975 binding)
1978 ;;;;; The WSDL document
1980 ;; The WSDL data structure used for encoding/decoding SOAP messages
1981 (defstruct (soap-wsdl
1982 ;; NOTE: don't call this constructor, see `soap-make-wsdl'
1983 (:constructor soap-make-wsdl^)
1984 (:copier soap-copy-wsdl))
1985 origin ; file or URL from which this wsdl was loaded
1986 current-file ; most-recently fetched file or URL
1987 xmlschema-imports ; a list of schema imports
1988 ports ; a list of SOAP-PORT instances
1989 alias-table ; a list of namespace aliases
1990 namespaces ; a list of namespaces
1993 (defun soap-make-wsdl (origin)
1994 "Create a new WSDL document, loaded from ORIGIN, and initialize it."
1995 (let ((wsdl (soap-make-wsdl^ :origin origin)))
1997 ;; Add the XSD types to the wsdl document
1998 (let ((ns (soap-make-xs-basic-types
1999 "http://www.w3.org/2001/XMLSchema" "xsd")))
2000 (soap-wsdl-add-namespace ns wsdl)
2001 (soap-wsdl-add-alias "xsd" (soap-namespace-name ns) wsdl))
2003 ;; Add the soapenc types to the wsdl document
2004 (let ((ns (soap-make-xs-basic-types
2005 "http://schemas.xmlsoap.org/soap/encoding/" "soapenc")))
2006 (soap-wsdl-add-namespace ns wsdl)
2007 (soap-wsdl-add-alias "soapenc" (soap-namespace-name ns) wsdl))
2009 wsdl))
2011 (defun soap-wsdl-add-alias (alias name wsdl)
2012 "Add a namespace ALIAS for NAME to the WSDL document."
2013 (let ((existing (assoc alias (soap-wsdl-alias-table wsdl))))
2014 (if existing
2015 (unless (equal (cdr existing) name)
2016 (warn "Redefining alias %s from %s to %s"
2017 alias (cdr existing) name)
2018 (push (cons alias name) (soap-wsdl-alias-table wsdl)))
2019 (push (cons alias name) (soap-wsdl-alias-table wsdl)))))
2021 (defun soap-wsdl-find-namespace (name wsdl)
2022 "Find a namespace by NAME in the WSDL document."
2023 (catch 'found
2024 (dolist (ns (soap-wsdl-namespaces wsdl))
2025 (when (equal name (soap-namespace-name ns))
2026 (throw 'found ns)))))
2028 (defun soap-wsdl-add-namespace (ns wsdl)
2029 "Add the namespace NS to the WSDL document.
2030 If a namespace by this name already exists in WSDL, individual
2031 elements will be added to it."
2032 (let ((existing (soap-wsdl-find-namespace (soap-namespace-name ns) wsdl)))
2033 (if existing
2034 ;; Add elements from NS to EXISTING, replacing existing values.
2035 (maphash (lambda (_key value)
2036 (dolist (v value)
2037 (soap-namespace-put v existing)))
2038 (soap-namespace-elements ns))
2039 (push ns (soap-wsdl-namespaces wsdl)))))
2041 (defun soap-wsdl-get (name wsdl &optional predicate use-local-alias-table)
2042 "Retrieve element NAME from the WSDL document.
2044 PREDICATE is used to differentiate between elements when NAME
2045 refers to multiple elements. A typical value for this would be a
2046 structure predicate for the type of element you want to retrieve.
2047 For example, to retrieve a message named \"foo\" when other
2048 elements named \"foo\" exist in the WSDL you could use:
2050 (soap-wsdl-get \"foo\" WSDL \\='soap-message-p)
2052 If USE-LOCAL-ALIAS-TABLE is not nil, `soap-local-xmlns' will be
2053 used to resolve the namespace alias."
2054 (let ((alias-table (soap-wsdl-alias-table wsdl))
2055 namespace element-name element)
2057 (when (symbolp name)
2058 (setq name (symbol-name name)))
2060 (when use-local-alias-table
2061 (setq alias-table (append soap-local-xmlns alias-table)))
2063 (cond ((consp name) ; a fully qualified name, as returned by `soap-l2fq'
2064 (setq element-name (cdr name))
2065 (when (symbolp element-name)
2066 (setq element-name (symbol-name element-name)))
2067 (setq namespace (soap-wsdl-find-namespace (car name) wsdl))
2068 (unless namespace
2069 (error "Soap-wsdl-get(%s): unknown namespace: %s" name namespace)))
2071 ((string-match "^\\(.*\\):\\(.*\\)$" name)
2072 (setq element-name (match-string 2 name))
2074 (let* ((ns-alias (match-string 1 name))
2075 (ns-name (cdr (assoc ns-alias alias-table))))
2076 (unless ns-name
2077 (error "Soap-wsdl-get(%s): cannot find namespace alias %s"
2078 name ns-alias))
2080 (setq namespace (soap-wsdl-find-namespace ns-name wsdl))
2081 (unless namespace
2082 (error
2083 "Soap-wsdl-get(%s): unknown namespace %s, referenced as %s"
2084 name ns-name ns-alias))))
2086 (error "Soap-wsdl-get(%s): bad name" name)))
2088 (setq element (soap-namespace-get
2089 element-name namespace
2090 (if predicate
2091 (lambda (e)
2092 (or (funcall 'soap-namespace-link-p e)
2093 (funcall predicate e)))
2094 nil)))
2096 (unless element
2097 (error "Soap-wsdl-get(%s): cannot find element" name))
2099 (if (soap-namespace-link-p element)
2100 ;; NOTE: don't use the local alias table here
2101 (soap-wsdl-get (soap-namespace-link-target element) wsdl predicate)
2102 element)))
2104 ;;;;; soap-parse-schema
2106 (defun soap-parse-schema (node wsdl)
2107 "Parse a schema NODE, placing the results in WSDL.
2108 Return a SOAP-NAMESPACE containing the elements."
2109 (soap-with-local-xmlns node
2110 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
2112 "expecting an xsd:schema node, got %s"
2113 (soap-l2wk (xml-node-name node)))
2115 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
2117 (dolist (def (xml-node-children node))
2118 (unless (stringp def) ; skip text nodes
2119 (case (soap-l2wk (xml-node-name def))
2120 (xsd:import
2121 ;; Imports will be processed later
2122 ;; NOTE: we should expand the location now!
2123 (let ((location (or
2124 (xml-get-attribute-or-nil def 'schemaLocation)
2125 (xml-get-attribute-or-nil def 'location))))
2126 (when location
2127 (push location (soap-wsdl-xmlschema-imports wsdl)))))
2128 (xsd:element
2129 (soap-namespace-put (soap-xs-parse-element def) ns))
2130 (xsd:attribute
2131 (soap-namespace-put (soap-xs-parse-attribute def) ns))
2132 (xsd:attributeGroup
2133 (soap-namespace-put (soap-xs-parse-attribute-group def) ns))
2134 (xsd:simpleType
2135 (soap-namespace-put (soap-xs-parse-simple-type def) ns))
2136 ((xsd:complexType xsd:group)
2137 (soap-namespace-put (soap-xs-parse-complex-type def) ns)))))
2138 ns)))
2140 ;;;;; Resolving references for wsdl types
2142 ;; See `soap-wsdl-resolve-references', which is the main entry point for
2143 ;; resolving references
2145 (defun soap-resolve-references (element wsdl)
2146 "Replace names in ELEMENT with the referenced objects in the WSDL.
2147 This is a generic function which invokes a specific resolver
2148 function depending on the type of the ELEMENT.
2150 If ELEMENT has no resolver function, it is silently ignored."
2151 (let ((resolver (get (aref element 0) 'soap-resolve-references)))
2152 (when resolver
2153 (funcall resolver element wsdl))))
2155 (defun soap-resolve-references-for-message (message wsdl)
2156 "Replace names in MESSAGE with the referenced objects in the WSDL.
2157 This is a generic function, called by `soap-resolve-references',
2158 you should use that function instead.
2160 See also `soap-wsdl-resolve-references'."
2161 (let (resolved-parts)
2162 (dolist (part (soap-message-parts message))
2163 (let ((name (car part))
2164 (element (cdr part)))
2165 (when (stringp name)
2166 (setq name (intern name)))
2167 (if (soap-name-p element)
2168 (setq element (soap-wsdl-get
2169 element wsdl
2170 (lambda (x)
2171 (or (soap-xs-type-p x) (soap-xs-element-p x)))))
2172 ;; else, inline element, resolve recursively, as the element
2173 ;; won't be reached.
2174 (soap-resolve-references element wsdl)
2175 (unless (soap-element-namespace-tag element)
2176 (setf (soap-element-namespace-tag element)
2177 (soap-element-namespace-tag message))))
2178 (push (cons name element) resolved-parts)))
2179 (setf (soap-message-parts message) (nreverse resolved-parts))))
2181 (defun soap-resolve-references-for-operation (operation wsdl)
2182 "Resolve references for an OPERATION type using the WSDL document.
2183 See also `soap-resolve-references' and
2184 `soap-wsdl-resolve-references'"
2186 (let ((namespace (soap-element-namespace-tag operation)))
2187 (when namespace
2188 (let ((nstag (car (rassoc namespace (soap-wsdl-alias-table wsdl)))))
2189 (when nstag
2190 (setf (soap-element-namespace-tag operation) nstag)))))
2192 (let ((input (soap-operation-input operation))
2193 (counter 0))
2194 (let ((name (car input))
2195 (message (cdr input)))
2196 ;; Name this part if it was not named
2197 (when (or (null name) (equal name ""))
2198 (setq name (format "in%d" (incf counter))))
2199 (when (soap-name-p message)
2200 (setf (soap-operation-input operation)
2201 (cons (intern name)
2202 (soap-wsdl-get message wsdl 'soap-message-p))))))
2204 (let ((output (soap-operation-output operation))
2205 (counter 0))
2206 (let ((name (car output))
2207 (message (cdr output)))
2208 (when (or (null name) (equal name ""))
2209 (setq name (format "out%d" (incf counter))))
2210 (when (soap-name-p message)
2211 (setf (soap-operation-output operation)
2212 (cons (intern name)
2213 (soap-wsdl-get message wsdl 'soap-message-p))))))
2215 (let ((resolved-faults nil)
2216 (counter 0))
2217 (dolist (fault (soap-operation-faults operation))
2218 (let ((name (car fault))
2219 (message (cdr fault)))
2220 (when (or (null name) (equal name ""))
2221 (setq name (format "fault%d" (incf counter))))
2222 (if (soap-name-p message)
2223 (push (cons (intern name)
2224 (soap-wsdl-get message wsdl 'soap-message-p))
2225 resolved-faults)
2226 (push fault resolved-faults))))
2227 (setf (soap-operation-faults operation) resolved-faults))
2229 (when (= (length (soap-operation-parameter-order operation)) 0)
2230 (setf (soap-operation-parameter-order operation)
2231 (mapcar 'car (soap-message-parts
2232 (cdr (soap-operation-input operation))))))
2234 (setf (soap-operation-parameter-order operation)
2235 (mapcar (lambda (p)
2236 (if (stringp p)
2237 (intern p)
2239 (soap-operation-parameter-order operation))))
2241 (defun soap-resolve-references-for-binding (binding wsdl)
2242 "Resolve references for a BINDING type using the WSDL document.
2243 See also `soap-resolve-references' and
2244 `soap-wsdl-resolve-references'"
2245 (when (soap-name-p (soap-binding-port-type binding))
2246 (setf (soap-binding-port-type binding)
2247 (soap-wsdl-get (soap-binding-port-type binding)
2248 wsdl 'soap-port-type-p)))
2250 (let ((port-ops (soap-port-type-operations (soap-binding-port-type binding))))
2251 (maphash (lambda (k v)
2252 (setf (soap-bound-operation-operation v)
2253 (soap-namespace-get k port-ops 'soap-operation-p))
2254 (let (resolved-headers)
2255 (dolist (h (soap-bound-operation-soap-headers v))
2256 (push (list (soap-wsdl-get (nth 0 h) wsdl)
2257 (intern (nth 1 h))
2258 (nth 2 h))
2259 resolved-headers))
2260 (setf (soap-bound-operation-soap-headers v)
2261 (nreverse resolved-headers))))
2262 (soap-binding-operations binding))))
2264 (defun soap-resolve-references-for-port (port wsdl)
2265 "Replace names in PORT with the referenced objects in the WSDL.
2266 This is a generic function, called by `soap-resolve-references',
2267 you should use that function instead.
2269 See also `soap-wsdl-resolve-references'."
2270 (when (soap-name-p (soap-port-binding port))
2271 (setf (soap-port-binding port)
2272 (soap-wsdl-get (soap-port-binding port) wsdl 'soap-binding-p))))
2274 ;; Install resolvers for our types
2275 (progn
2276 (put (aref (make-soap-message) 0) 'soap-resolve-references
2277 'soap-resolve-references-for-message)
2278 (put (aref (make-soap-operation) 0) 'soap-resolve-references
2279 'soap-resolve-references-for-operation)
2280 (put (aref (make-soap-binding) 0) 'soap-resolve-references
2281 'soap-resolve-references-for-binding)
2282 (put (aref (make-soap-port) 0) 'soap-resolve-references
2283 'soap-resolve-references-for-port))
2285 (defun soap-wsdl-resolve-references (wsdl)
2286 "Resolve all references inside the WSDL structure.
2288 When the WSDL elements are created from the XML document, they
2289 refer to each other by name. For example, the ELEMENT-TYPE slot
2290 of an SOAP-ARRAY-TYPE will contain the name of the element and
2291 the user would have to call `soap-wsdl-get' to obtain the actual
2292 element.
2294 After the entire document is loaded, we resolve all these
2295 references to the actual elements they refer to so that at
2296 runtime, we don't have to call `soap-wsdl-get' each time we
2297 traverse an element tree."
2298 (let ((nprocessed 0)
2299 (nstag-id 0)
2300 (alias-table (soap-wsdl-alias-table wsdl)))
2301 (dolist (ns (soap-wsdl-namespaces wsdl))
2302 (let ((nstag (car-safe (rassoc (soap-namespace-name ns) alias-table))))
2303 (unless nstag
2304 ;; If this namespace does not have an alias, create one for it.
2305 (catch 'done
2306 (while t
2307 (setq nstag (format "ns%d" (incf nstag-id)))
2308 (unless (assoc nstag alias-table)
2309 (soap-wsdl-add-alias nstag (soap-namespace-name ns) wsdl)
2310 (throw 'done t)))))
2312 (maphash (lambda (_name element)
2313 (cond ((soap-element-p element) ; skip links
2314 (incf nprocessed)
2315 (soap-resolve-references element wsdl))
2316 ((listp element)
2317 (dolist (e element)
2318 (when (soap-element-p e)
2319 (incf nprocessed)
2320 (soap-resolve-references e wsdl))))))
2321 (soap-namespace-elements ns)))))
2322 wsdl)
2324 ;;;;; Loading WSDL from XML documents
2326 (defun soap-parse-server-response ()
2327 "Error-check and parse the XML contents of the current buffer."
2328 (let ((mime-part (mm-dissect-buffer t t)))
2329 (unless mime-part
2330 (error "Failed to decode response from server"))
2331 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
2332 (error "Server response is not an XML document"))
2333 (with-temp-buffer
2334 (mm-insert-part mime-part)
2335 (prog1
2336 (car (xml-parse-region (point-min) (point-max)))
2337 (kill-buffer)
2338 (mm-destroy-part mime-part)))))
2340 (defun soap-fetch-xml-from-url (url wsdl)
2341 "Load an XML document from URL and return it.
2342 The previously parsed URL is read from WSDL."
2343 (message "Fetching from %s" url)
2344 (let ((current-file (url-expand-file-name url (soap-wsdl-current-file wsdl)))
2345 (url-request-method "GET")
2346 (url-package-name "soap-client.el")
2347 (url-package-version "1.0")
2348 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
2349 (url-http-attempt-keepalives t))
2350 (setf (soap-wsdl-current-file wsdl) current-file)
2351 (let ((buffer (url-retrieve-synchronously current-file)))
2352 (with-current-buffer buffer
2353 (declare (special url-http-response-status))
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 (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 (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 (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 (destructuring-bind (name . message) (soap-operation-input o)
2526 (soap-namespace-put-link name message ns))
2528 (destructuring-bind (name . message) (soap-operation-output o)
2529 (soap-namespace-put-link name message ns))
2531 (dolist (fault (soap-operation-faults o))
2532 (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 (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 (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 (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 (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 (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 (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 (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 "\"" (soap-bound-operation-soap-action
3053 operation) "\""))
3054 (cons "Content-Type"
3055 "text/xml; charset=utf-8"))))
3056 (if callback
3057 (url-retrieve
3058 (soap-port-service-url port)
3059 (lambda (status)
3060 (let ((data-buffer (current-buffer)))
3061 (unwind-protect
3062 (let ((error-status (plist-get status :error)))
3063 (if error-status
3064 (signal (car error-status) (cdr error-status))
3065 (apply callback
3066 (soap-parse-envelope
3067 (soap-parse-server-response)
3068 operation wsdl)
3069 cbargs)))
3070 ;; Ensure the url-retrieve buffer is not leaked.
3071 (and (buffer-live-p data-buffer)
3072 (kill-buffer data-buffer))))))
3073 (let ((buffer (url-retrieve-synchronously
3074 (soap-port-service-url port))))
3075 (condition-case err
3076 (with-current-buffer buffer
3077 (declare (special url-http-response-status))
3078 (if (null url-http-response-status)
3079 (error "No HTTP response from server"))
3080 (if (and soap-debug (> url-http-response-status 299))
3081 ;; This is a warning because some SOAP errors come
3082 ;; back with a HTTP response 500 (internal server
3083 ;; error)
3084 (warn "Error in SOAP response: HTTP code %s"
3085 url-http-response-status))
3086 (soap-parse-envelope (soap-parse-server-response)
3087 operation wsdl))
3088 (soap-error
3089 ;; Propagate soap-errors -- they are error replies of the
3090 ;; SOAP protocol and don't indicate a communication
3091 ;; problem or a bug in this code.
3092 (signal (car err) (cdr err)))
3093 (error
3094 (when soap-debug
3095 (pop-to-buffer buffer))
3096 (error (error-message-string err)))))))))
3098 (defun soap-invoke (wsdl service operation-name &rest parameters)
3099 "Invoke a SOAP operation and return the result.
3101 WSDL is used for encoding the request and decoding the response.
3102 It also contains information about the WEB server address that
3103 will service the request.
3105 SERVICE is the SOAP service to invoke.
3107 OPERATION-NAME is the operation to invoke.
3109 PARAMETERS -- the remaining parameters are used as parameters for
3110 the SOAP request.
3112 NOTE: The SOAP service provider should document the available
3113 operations and their parameters for the service. You can also
3114 use the `soap-inspect' function to browse the available
3115 operations in a WSDL document.
3117 NOTE: `soap-invoke' base64-decodes xsd:base64Binary return values
3118 into unibyte strings; these byte-strings require further
3119 interpretation by the caller."
3120 (apply #'soap-invoke-internal nil nil wsdl service operation-name parameters))
3122 (defun soap-invoke-async (callback cbargs wsdl service operation-name
3123 &rest parameters)
3124 "Like `soap-invoke', but call CALLBACK asynchronously with response.
3125 CALLBACK is called as (apply CALLBACK RESPONSE CBARGS), where
3126 RESPONSE is the SOAP invocation result. WSDL, SERVICE,
3127 OPERATION-NAME and PARAMETERS are as described in `soap-invoke'."
3128 (unless callback
3129 (error "Callback argument is nil"))
3130 (apply #'soap-invoke-internal callback cbargs wsdl service operation-name
3131 parameters))
3133 (provide 'soap-client)
3136 ;; Local Variables:
3137 ;; eval: (outline-minor-mode 1)
3138 ;; outline-regexp: ";;;;+"
3139 ;; End:
3141 ;;; soap-client.el ends here