jaxb-annotations3.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (define-alias JAXBContext javax.xml.bind.JAXBContext)
  2. (define-alias StringReader java.io.StringReader)
  3. (define-alias XmlRegistry javax.xml.bind.annotation.XmlRegistry)
  4. (define-alias XmlRootElement javax.xml.bind.annotation.XmlRootElement)
  5. (define-alias XmlElement javax.xml.bind.annotation.XmlElement)
  6. (define-alias XmlAttribute javax.xml.bind.annotation.XmlAttribute)
  7. (define-alias BigDecimal java.math.BigDecimal)
  8. (define-alias ArrayList java.util.ArrayList)
  9. ;; Enable stronger compile-time error-checking:
  10. (module-compile-options warn-undefined-variable: #t
  11. warn-unknown-member: #t
  12. warn-as-error: #t)
  13. ;; See http://www.w3.org/TR/xquery-use-cases/#xmp-dtd
  14. (define-simple-class Bib ( ) (@XmlRootElement name: "bib")
  15. (books (@XmlElement name: "book" type: Book) ::ArrayList))
  16. (define-simple-class Book ()
  17. (year (@XmlAttribute required: #t) ::String)
  18. (title (@XmlElement) ::String)
  19. (authors (@XmlElement name: "author" type: Author) ::ArrayList)
  20. (editors (@XmlElement name: "editor" type: Editor) ::ArrayList)
  21. (publisher (@XmlElement) ::String)
  22. (price (@XmlElement) ::BigDecimal))
  23. (define-simple-class Person ()
  24. (last (@XmlElement) ::String)
  25. (first (@XmlElement) ::String))
  26. (define-simple-class Author (Person))
  27. (define-simple-class Editor (Person)
  28. (affiliation (@XmlElement) ::String))
  29. (define jaxb-context ::JAXBContext
  30. (JAXBContext:newInstance Bib Book Person Editor))
  31. ;; Alternatively use an ObjectFactory, as in the following.
  32. ;; (Replace "testsuite" by the name of the package this is compiled to.)
  33. ;(define jaxb-context ::JAXBContext (JAXBContext:newInstance "testsuite"))
  34. ;(define-simple-class ObjectFactory () (@XmlRegistry)
  35. ; ((createBib) ::Bib (Bib)))
  36. ;; Read in an XML-formatted <bib> document.
  37. (define (parse-xml (in ::java.io.Reader)) ::Bib
  38. ((jaxb-context:createUnmarshaller):unmarshal in))
  39. ;; Multiply old by ratio to yield an updated 2-decimal-digit BigDecimal.
  40. (define (adjust-price old::BigDecimal ratio::double)::BigDecimal
  41. (BigDecimal (round (* (old:doubleValue) ratio 100)) 2))
  42. ;; Multiply the price of all the books in bb by ratio.
  43. (define (adjust-prices (bb::Bib) (ratio::double))
  44. (let* ((books bb:books)
  45. (nbooks (books:size)))
  46. (do ((i :: int 0 (+ i 1))) ((>= i nbooks))
  47. (let* ((book ::Book (books i)))
  48. (set! book:price (adjust-price book:price ratio))))))
  49. ;; Write bb as pretty-printed XML to an output port.
  50. (define (write-bib (bb ::Bib) (out ::output-port))::void
  51. (let ((m (jaxb-context:createMarshaller)))
  52. (fluid-let ((*print-xml-indent* 'pretty))
  53. ;; We could marshal directly to 'out' (which extends java.io.Writer).
  54. ;; However, XMLPrinter can pretty-print the output more readably.
  55. ;; We use XMLFilter as glue that implements org.xml.sax.ContentHandler.
  56. (m:marshal bb (gnu.xml.XMLFilter (gnu.xml.XMLPrinter out))))))
  57. (define in (current-input-port))
  58. (define bb (parse-xml in))
  59. (adjust-prices bb 1.1)
  60. (bb:books:add
  61. (Book year: "2006"
  62. title: "JavaScript: The Definitive Guide (5th edition)"
  63. authors: [(Author last: "Flanagan" first: "David")]
  64. publisher: "O'Reilly"
  65. price: (BigDecimal "49.99")))
  66. (write-bib bb (current-output-port))