incompatible Kawa change: symbol vs. java.lang.String
Hoehle, Joerg-Cyril
Joerg-Cyril.Hoehle@t-systems.com
Tue Apr 3 16:27:00 GMT 2007
Hi,
Per Bothner writes:
http://www.gnu.org/software/kawa/news.html
>Changes in SVN repository since 1.9.1
> A major incompatible (but long-sought) change: Java strings (i.e.
java.lang.String values) are now Scheme
> strings, rather than Scheme symbols.
I'm surprised this change did not so far generate more traffic in the
mailing list
http://sources.redhat.com/ml/kawa/2007-q1/
I've put the Java-String <-> Scheme Symbol mapping to great (IMHO) use
in a XML to Scheme SXML converter. The core issue is that as Scheme
symbols, all Java strings would be uniq'ified. EQ and MEMQ are useable
to match XML element names.
If I understand the implications of this incompatible change in Kawa
right, code depending on this property of Scheme symbols originating
from <String> would break.
The following code shows how I'm used to convert and filter in few lines
of code a complex XML schema based document into a nice Lisp structure
that contains only the elements (and possibly attributes) and values I'm
interested in.
The code depends on the implicit <java.lang.String> to Scheme symbol
conversion
<String> org.w3c.dom.Node:getLocalName() -> Scheme symbol to be compared
with EQ and MEMQ.
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html
(define-namespace Node "class:org.w3c.dom.Node")
(define-namespace xml "class:org.apache.xml.security.utils.XMLUtils")
;; Presupposition: correctly schema-validated input
; The nodes whose values I'm interested in:
(define nodes-b64 '(CRI TAID ESN KeyMod KeyExp ID Identifier
CRSignature Cert Certificate))
(define nodes-int '(ET Reason MC Status))
(define (load-dom filename :: <string>) ;;:: <org.w3c.dom.Document>
(invoke (invoke dbf 'newDocumentBuilder)
'parse (make <java.io.File> filename)))
;(define doc (load-dom "foobar.xml"))
(define (dom-data parts :: <org.w3c.dom.NodeList>)
(let* ((len (invoke parts 'getLength)))
(let accumulate ((acc '()) (i 0))
(if (= i len) (reverse! acc)
(let* ((node :: <org.w3c.dom.Node> (invoke parts 'item i))
(elem (Node:getLocalName node)))
(accumulate
(cons
(cond
((memq elem nodes-b64)
;;TODO Bug: xml:getBytesFromElement barfs on comments
;; while getfulltext ignores them
`(,elem (b64-bytes
;; ,(xml:getBytesFromElement node)
,(Base64:decode
(xml:getFullTextChildrenFromElement node)))))
((memq elem nodes-int)
(list elem
(string->number
(symbol->string
(xml:getFullTextChildrenFromElement node)))))
(else
(cons elem
(dom-data (xml:getDirectChildrenElements node)))))
acc) (+ i 1)))))))
;(dom-data (xml:getDirectChildrenElements (select-single-node doc
"Core")))
Output is something like (Cert (CRI 123) (KeyExp (base64-bytes
#<byte[]>)))
Some other variant of that simple code that I could not locate right now
generates SXML-compatible output and is able to extract (known in
advance) attributes.
I'm happy to see Kawa development going on.
Regards,
Jorg Hohle.
More information about the Kawa
mailing list