src/Pure/General/xml.scala
author wenzelm
Sun Dec 06 22:22:48 2009 +0100 (2009-12-06 ago)
changeset 33999 d3b200894e21
parent 33953 5e865ed88313
child 34005 ada5098506af
permissions -rw-r--r--
added auxiliary constructors;
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@33999
    29
  def elem(name: String, body: List[Tree]) = Elem(name, Nil, body)
wenzelm@33999
    30
  def elem(name: String) = Elem(name, Nil, Nil)
wenzelm@33999
    31
wenzelm@29203
    32
wenzelm@29203
    33
  /* string representation */
wenzelm@29203
    34
wenzelm@29203
    35
  private def append_text(text: String, s: StringBuilder) {
wenzelm@29203
    36
    for (c <- text.elements) c match {
wenzelm@29203
    37
      case '<' => s.append("&lt;")
wenzelm@29203
    38
      case '>' => s.append("&gt;")
wenzelm@29203
    39
      case '&' => s.append("&amp;")
wenzelm@29203
    40
      case '"' => s.append("&quot;")
wenzelm@29203
    41
      case '\'' => s.append("&apos;")
wenzelm@29204
    42
      case _ => s.append(c)
wenzelm@29203
    43
    }
wenzelm@29203
    44
  }
wenzelm@29203
    45
wenzelm@29203
    46
  private def append_elem(name: String, atts: Attributes, s: StringBuilder) {
wenzelm@29203
    47
    s.append(name)
wenzelm@29203
    48
    for ((a, x) <- atts) {
wenzelm@29203
    49
      s.append(" "); s.append(a); s.append("=\""); append_text(x, s); s.append("\"")
wenzelm@29203
    50
    }
wenzelm@29203
    51
  }
wenzelm@29203
    52
wenzelm@29204
    53
  private def append_tree(tree: Tree, s: StringBuilder) {
wenzelm@29203
    54
    tree match {
wenzelm@29203
    55
      case Elem(name, atts, Nil) =>
wenzelm@29203
    56
        s.append("<"); append_elem(name, atts, s); s.append("/>")
wenzelm@29203
    57
      case Elem(name, atts, ts) =>
wenzelm@29203
    58
        s.append("<"); append_elem(name, atts, s); s.append(">")
wenzelm@29203
    59
        for (t <- ts) append_tree(t, s)
wenzelm@29203
    60
        s.append("</"); s.append(name); s.append(">")
wenzelm@29203
    61
      case Text(text) => append_text(text, s)
wenzelm@29203
    62
    }
wenzelm@29203
    63
  }
wenzelm@27941
    64
wenzelm@27941
    65
wenzelm@27942
    66
  /* iterate over content */
wenzelm@27941
    67
wenzelm@27941
    68
  private type State = Option[(String, List[Tree])]
wenzelm@27941
    69
wenzelm@27941
    70
  private def get_next(tree: Tree): State = tree match {
wenzelm@27941
    71
    case Elem(_, _, body) => get_nexts(body)
wenzelm@27941
    72
    case Text(content) => Some(content, Nil)
wenzelm@27941
    73
  }
wenzelm@27941
    74
  private def get_nexts(trees: List[Tree]): State = trees match {
wenzelm@27941
    75
    case Nil => None
wenzelm@27941
    76
    case t :: ts => get_next(t) match {
wenzelm@27941
    77
      case None => get_nexts(ts)
wenzelm@28007
    78
      case Some((s, r)) => Some((s, r ++ ts))
wenzelm@27941
    79
    }
wenzelm@27941
    80
  }
wenzelm@27941
    81
wenzelm@27941
    82
  def content(tree: Tree) = new Iterator[String] {
wenzelm@27941
    83
    private var state = get_next(tree)
wenzelm@27941
    84
    def hasNext() = state.isDefined
wenzelm@27941
    85
    def next() = state match {
wenzelm@27941
    86
      case Some((s, rest)) => { state = get_nexts(rest); s }
wenzelm@27941
    87
      case None => throw new NoSuchElementException("next on empty iterator")
wenzelm@27941
    88
    }
wenzelm@27941
    89
  }
wenzelm@27941
    90
wenzelm@27947
    91
wenzelm@33953
    92
  /* document object model (W3C DOM) */
wenzelm@27948
    93
wenzelm@33953
    94
  def document_node(doc: Document, tree: Tree): Node =
wenzelm@33953
    95
  {
wenzelm@27952
    96
    def DOM(tr: Tree): Node = tr match {
wenzelm@27947
    97
      case Elem(name, atts, ts) => {
wenzelm@27947
    98
        val node = doc.createElement(name)
wenzelm@27947
    99
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
   100
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
   101
        node
wenzelm@27947
   102
      }
wenzelm@27947
   103
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   104
    }
wenzelm@33953
   105
    DOM(tree)
wenzelm@33953
   106
  }
wenzelm@33953
   107
wenzelm@33953
   108
  def document(tree: Tree, styles: String*): Document =
wenzelm@33953
   109
  {
wenzelm@33953
   110
    val doc = DocumentBuilderFactory.newInstance.newDocumentBuilder.newDocument
wenzelm@33953
   111
    doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\""))
wenzelm@33953
   112
wenzelm@33953
   113
    for (style <- styles) {
wenzelm@33953
   114
      doc.appendChild(doc.createProcessingInstruction("xml-stylesheet",
wenzelm@33953
   115
        "href=\"" + style + "\" type=\"text/css\""))
wenzelm@33953
   116
    }
wenzelm@27947
   117
    val root_elem = tree match {
wenzelm@33953
   118
      case Elem(_, _, _) => document_node(doc, tree)
wenzelm@33953
   119
      case Text(_) => document_node(doc, (Elem(Markup.ROOT, Nil, List(tree))))
wenzelm@27947
   120
    }
wenzelm@27947
   121
    doc.appendChild(root_elem)
wenzelm@27947
   122
    doc
wenzelm@27947
   123
  }
wenzelm@27931
   124
}