#!/usr/bin/env mzscheme
#lang scheme
(require xml)
(require (lib "pretty.ss"))



(define (string-whitespace? str)         
  (let rest?
      ((l (string->list str)))
    (or (null? l)
        (and (char-whitespace? (car l))
             (rest? (cdr l))))))

;; convert xexpr to hierarchical dictionary, mapping tags to strings
;; or dictionaries.

(define (xexpr->dict xexpr)
  (define (element->dict kar kdr)
    (if (string? kar)
        (if (string-whitespace? kar)
            kdr
            (cons kar kdr))
        (cons (xexpr->dict kar) kdr)))
  (apply
   (lambda (tag attributes . elements)
     (cons tag
           (append
            ;; don't differentiate between attributes and elements.
            attributes
            (foldr element->dict
                   '() elements))))
   xexpr))


(define (load-xexpr file)
   (xexpr->dict
    (xml->xexpr
     (document-element
      (read-xml
       (open-input-file file))))))

(define (arg n)
  (vector-ref (current-command-line-arguments) n))

(pretty-print
 (load-xexpr (arg 0)))


