src/Pure/General/xml.scala
author wenzelm
Fri Dec 04 14:21:07 2009 +0100 (2009-12-04 ago)
changeset 33953 5e865ed88313
parent 29204 e002f7b63e3c
child 33999 d3b200894e21
permissions -rw-r--r--
added document_node;
tuned comments;
wenzelm@27931
     1
/*  Title:      Pure/General/xml.scala
wenzelm@27931
     2
    Author:     Makarius
wenzelm@27931
     3
wenzelm@27947
     4
Simple XML tree values.
wenzelm@27931
     5
*/
wenzelm@27931
     6
wenzelm@27931
     7
package isabelle
wenzelm@27931
     8
wenzelm@27947
     9
import org.w3c.dom.{Node, Document}
wenzelm@27947
    10
import javax.xml.parsers.DocumentBuilderFactory
wenzelm@27947
    11
wenzelm@27947
    12
wenzelm@29203
    13
object XML
wenzelm@29203
    14
{
wenzelm@27947
    15
  /* datatype representation */
wenzelm@27947
    16
wenzelm@27931
    17
  type Attributes = List[(String, String)]
wenzelm@27931
    18
wenzelm@29204
    19
  abstract class Tree {
wenzelm@29204
    20
    override def toString = {
wenzelm@29204
    21
      val s = new StringBuilder
wenzelm@29204
    22
      append_tree(this, s)
wenzelm@29204
    23
      s.toString
wenzelm@29204
    24
    }
wenzelm@29203
    25
  }
wenzelm@29204
    26
  case class Elem(name: String, attributes: Attributes, body: List[Tree]) extends Tree
wenzelm@29204
    27
  case class Text(content: String) extends Tree
wenzelm@29203
    28
wenzelm@29203
    29
wenzelm@29203
    30
  /* string representation */
wenzelm@29203
    31
wenzelm@29203
    32
  private def append_text(text: String, s: StringBuilder) {
wenzelm@29203
    33
    for (c <- text.elements) c match {
wenzelm@29203
    34
      case '<' => s.append("&lt;")
wenzelm@29203
    35
      case '>' => s.append("&gt;")
wenzelm@29203
    36
      case '&' => s.append("&amp;")
wenzelm@29203
    37
      case '"' => s.append("&quot;")
wenzelm@29203
    38
      case '\'' => s.append("&apos;")
wenzelm@29204
    39
      case _ => s.append(c)
wenzelm@29203
    40
    }
wenzelm@29203
    41
  }
wenzelm@29203
    42
wenzelm@29203
    43
  private def append_elem(name: String, atts: Attributes, s: StringBuilder) {
wenzelm@29203
    44
    s.append(name)
wenzelm@29203
    45
    for ((a, x) <- atts) {
wenzelm@29203
    46
      s.append(" "); s.append(a); s.append("=\""); append_text(x, s); s.append("\"")
wenzelm@29203
    47
    }
wenzelm@29203
    48
  }
wenzelm@29203
    49
wenzelm@29204
    50
  private def append_tree(tree: Tree, s: StringBuilder) {
wenzelm@29203
    51
    tree match {
wenzelm@29203
    52
      case Elem(name, atts, Nil) =>
wenzelm@29203
    53
        s.append("<"); append_elem(name, atts, s); s.append("/>")
wenzelm@29203
    54
      case Elem(name, atts, ts) =>
wenzelm@29203
    55
        s.append("<"); append_elem(name, atts, s); s.append(">")
wenzelm@29203
    56
        for (t <- ts) append_tree(t, s)
wenzelm@29203
    57
        s.append("</"); s.append(name); s.append(">")
wenzelm@29203
    58
      case Text(text) => append_text(text, s)
wenzelm@29203
    59
    }
wenzelm@29203
    60
  }
wenzelm@27941
    61
wenzelm@27941
    62
wenzelm@27942
    63
  /* iterate over content */
wenzelm@27941
    64
wenzelm@27941
    65
  private type State = Option[(String, List[Tree])]
wenzelm@27941
    66
wenzelm@27941
    67
  private def get_next(tree: Tree): State = tree match {
wenzelm@27941
    68
    case Elem(_, _, body) => get_nexts(body)
wenzelm@27941
    69
    case Text(content) => Some(content, Nil)
wenzelm@27941
    70
  }
wenzelm@27941
    71
  private def get_nexts(trees: List[Tree]): State = trees match {
wenzelm@27941
    72
    case Nil => None
wenzelm@27941
    73
    case t :: ts => get_next(t) match {
wenzelm@27941
    74
      case None => get_nexts(ts)
wenzelm@28007
    75
      case Some((s, r)) => Some((s, r ++ ts))
wenzelm@27941
    76
    }
wenzelm@27941
    77
  }
wenzelm@27941
    78
wenzelm@27941
    79
  def content(tree: Tree) = new Iterator[String] {
wenzelm@27941
    80
    private var state = get_next(tree)
wenzelm@27941
    81
    def hasNext() = state.isDefined
wenzelm@27941
    82
    def next() = state match {
wenzelm@27941
    83
      case Some((s, rest)) => { state = get_nexts(rest); s }
wenzelm@27941
    84
      case None => throw new NoSuchElementException("next on empty iterator")
wenzelm@27941
    85
    }
wenzelm@27941
    86
  }
wenzelm@27941
    87
wenzelm@27947
    88
wenzelm@33953
    89
  /* document object model (W3C DOM) */
wenzelm@27948
    90
wenzelm@33953
    91
  def document_node(doc: Document, tree: Tree): Node =
wenzelm@33953
    92
  {
wenzelm@27952
    93
    def DOM(tr: Tree): Node = tr match {
wenzelm@27947
    94
      case Elem(name, atts, ts) => {
wenzelm@27947
    95
        val node = doc.createElement(name)
wenzelm@27947
    96
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
    97
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
    98
        node
wenzelm@27947
    99
      }
wenzelm@27947
   100
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   101
    }
wenzelm@33953
   102
    DOM(tree)
wenzelm@33953
   103
  }
wenzelm@33953
   104
wenzelm@33953
   105
  def document(tree: Tree, styles: String*): Document =
wenzelm@33953
   106
  {
wenzelm@33953
   107
    val doc = DocumentBuilderFactory.newInstance.newDocumentBuilder.newDocument
wenzelm@33953
   108
    doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\""))
wenzelm@33953
   109
wenzelm@33953
   110
    for (style <- styles) {
wenzelm@33953
   111
      doc.appendChild(doc.createProcessingInstruction("xml-stylesheet",
wenzelm@33953
   112
        "href=\"" + style + "\" type=\"text/css\""))
wenzelm@33953
   113
    }
wenzelm@27947
   114
    val root_elem = tree match {
wenzelm@33953
   115
      case Elem(_, _, _) => document_node(doc, tree)
wenzelm@33953
   116
      case Text(_) => document_node(doc, (Elem(Markup.ROOT, Nil, List(tree))))
wenzelm@27947
   117
    }
wenzelm@27947
   118
    doc.appendChild(root_elem)
wenzelm@27947
   119
    doc
wenzelm@27947
   120
  }
wenzelm@27931
   121
}