src/Pure/General/xml.scala
author wenzelm
Thu Dec 17 23:31:59 2009 +0100 (2009-12-17 ago)
changeset 34108 54d48ca8708f
parent 34047 2af94d45597f
child 34117 1eb8d8e3e40a
permissions -rw-r--r--
cache for partial sharing;
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@34108
     9
import java.util.WeakHashMap
wenzelm@34108
    10
import java.lang.ref.WeakReference
wenzelm@34108
    11
import javax.xml.parsers.DocumentBuilderFactory
wenzelm@34108
    12
wenzelm@27947
    13
import org.w3c.dom.{Node, Document}
wenzelm@27947
    14
wenzelm@27947
    15
wenzelm@29203
    16
object XML
wenzelm@29203
    17
{
wenzelm@27947
    18
  /* datatype representation */
wenzelm@27947
    19
wenzelm@27931
    20
  type Attributes = List[(String, String)]
wenzelm@27931
    21
wenzelm@34046
    22
  sealed abstract class Tree {
wenzelm@29204
    23
    override def toString = {
wenzelm@29204
    24
      val s = new StringBuilder
wenzelm@29204
    25
      append_tree(this, s)
wenzelm@29204
    26
      s.toString
wenzelm@29204
    27
    }
wenzelm@29203
    28
  }
wenzelm@29204
    29
  case class Elem(name: String, attributes: Attributes, body: List[Tree]) extends Tree
wenzelm@29204
    30
  case class Text(content: String) extends Tree
wenzelm@29203
    31
wenzelm@33999
    32
  def elem(name: String, body: List[Tree]) = Elem(name, Nil, body)
wenzelm@33999
    33
  def elem(name: String) = Elem(name, Nil, Nil)
wenzelm@33999
    34
wenzelm@29203
    35
wenzelm@29203
    36
  /* string representation */
wenzelm@29203
    37
wenzelm@29203
    38
  private def append_text(text: String, s: StringBuilder) {
wenzelm@34005
    39
    if (text == null) s ++ text
wenzelm@34005
    40
    else {
wenzelm@34005
    41
      for (c <- text.elements) c match {
wenzelm@34005
    42
        case '<' => s ++ "&lt;"
wenzelm@34005
    43
        case '>' => s ++ "&gt;"
wenzelm@34005
    44
        case '&' => s ++ "&amp;"
wenzelm@34005
    45
        case '"' => s ++ "&quot;"
wenzelm@34005
    46
        case '\'' => s ++ "&apos;"
wenzelm@34005
    47
        case _ => s + c
wenzelm@34005
    48
      }
wenzelm@29203
    49
    }
wenzelm@29203
    50
  }
wenzelm@29203
    51
wenzelm@29203
    52
  private def append_elem(name: String, atts: Attributes, s: StringBuilder) {
wenzelm@34005
    53
    s ++ name
wenzelm@29203
    54
    for ((a, x) <- atts) {
wenzelm@34005
    55
      s ++ " "; s ++ a; s ++ "=\""; append_text(x, s); s ++ "\""
wenzelm@29203
    56
    }
wenzelm@29203
    57
  }
wenzelm@29203
    58
wenzelm@29204
    59
  private def append_tree(tree: Tree, s: StringBuilder) {
wenzelm@29203
    60
    tree match {
wenzelm@29203
    61
      case Elem(name, atts, Nil) =>
wenzelm@34005
    62
        s ++ "<"; append_elem(name, atts, s); s ++ "/>"
wenzelm@29203
    63
      case Elem(name, atts, ts) =>
wenzelm@34005
    64
        s ++ "<"; append_elem(name, atts, s); s ++ ">"
wenzelm@29203
    65
        for (t <- ts) append_tree(t, s)
wenzelm@34005
    66
        s ++ "</"; s ++ name; s ++ ">"
wenzelm@29203
    67
      case Text(text) => append_text(text, s)
wenzelm@29203
    68
    }
wenzelm@29203
    69
  }
wenzelm@27941
    70
wenzelm@27941
    71
wenzelm@27942
    72
  /* iterate over content */
wenzelm@27941
    73
wenzelm@27941
    74
  private type State = Option[(String, List[Tree])]
wenzelm@27941
    75
wenzelm@27941
    76
  private def get_next(tree: Tree): State = tree match {
wenzelm@27941
    77
    case Elem(_, _, body) => get_nexts(body)
wenzelm@27941
    78
    case Text(content) => Some(content, Nil)
wenzelm@27941
    79
  }
wenzelm@27941
    80
  private def get_nexts(trees: List[Tree]): State = trees match {
wenzelm@27941
    81
    case Nil => None
wenzelm@27941
    82
    case t :: ts => get_next(t) match {
wenzelm@27941
    83
      case None => get_nexts(ts)
wenzelm@28007
    84
      case Some((s, r)) => Some((s, r ++ ts))
wenzelm@27941
    85
    }
wenzelm@27941
    86
  }
wenzelm@27941
    87
wenzelm@27941
    88
  def content(tree: Tree) = new Iterator[String] {
wenzelm@27941
    89
    private var state = get_next(tree)
wenzelm@27941
    90
    def hasNext() = state.isDefined
wenzelm@27941
    91
    def next() = state match {
wenzelm@27941
    92
      case Some((s, rest)) => { state = get_nexts(rest); s }
wenzelm@27941
    93
      case None => throw new NoSuchElementException("next on empty iterator")
wenzelm@27941
    94
    }
wenzelm@27941
    95
  }
wenzelm@27941
    96
wenzelm@27947
    97
wenzelm@34108
    98
  /* cache for partial sharing -- NOT THREAD SAFE */
wenzelm@34108
    99
wenzelm@34108
   100
  class Cache(initial_size: Int)
wenzelm@34108
   101
  {
wenzelm@34108
   102
    private val table = new WeakHashMap[Any, WeakReference[Any]](initial_size)
wenzelm@34108
   103
wenzelm@34108
   104
    private def lookup[A](x: A): Option[A] =
wenzelm@34108
   105
    {
wenzelm@34108
   106
      val ref = table.get(x)
wenzelm@34108
   107
      if (ref == null) None
wenzelm@34108
   108
      else {
wenzelm@34108
   109
        val y = ref.asInstanceOf[WeakReference[A]].get
wenzelm@34108
   110
        if (y == null) None
wenzelm@34108
   111
        else Some(y)
wenzelm@34108
   112
      }
wenzelm@34108
   113
    }
wenzelm@34108
   114
    private def store[A](x: A): A =
wenzelm@34108
   115
    {
wenzelm@34108
   116
      table.put(x, new WeakReference[Any](x))
wenzelm@34108
   117
      x
wenzelm@34108
   118
    }
wenzelm@34108
   119
wenzelm@34108
   120
    def cache_string(x: String): String =
wenzelm@34108
   121
      lookup(x) match {
wenzelm@34108
   122
        case Some(y) => y
wenzelm@34108
   123
        case None => store(x)
wenzelm@34108
   124
      }
wenzelm@34108
   125
    def cache_props(x: List[(String, String)]): List[(String, String)] =
wenzelm@34108
   126
      lookup(x) match {
wenzelm@34108
   127
        case Some(y) => y
wenzelm@34108
   128
        case None => store(x.map(p => (cache_string(p._1), cache_string(p._2))))
wenzelm@34108
   129
      }
wenzelm@34108
   130
    def apply(x: XML.Tree): XML.Tree =
wenzelm@34108
   131
      lookup(x) match {
wenzelm@34108
   132
        case Some(y) => y
wenzelm@34108
   133
        case None =>
wenzelm@34108
   134
          x match {
wenzelm@34108
   135
            case XML.Elem(name, props, body) =>
wenzelm@34108
   136
              store(XML.Elem(cache_string(name), cache_props(props), apply(body)))
wenzelm@34108
   137
            case XML.Text(text) => XML.Text(cache_string(text))
wenzelm@34108
   138
          }
wenzelm@34108
   139
      }
wenzelm@34108
   140
    def apply(x: List[XML.Tree]): List[XML.Tree] =
wenzelm@34108
   141
      lookup(x) match {
wenzelm@34108
   142
        case Some(y) => y
wenzelm@34108
   143
        case None => x.map(apply(_))
wenzelm@34108
   144
      }
wenzelm@34108
   145
  }
wenzelm@34108
   146
wenzelm@34108
   147
wenzelm@33953
   148
  /* document object model (W3C DOM) */
wenzelm@27948
   149
wenzelm@34047
   150
  def get_data(node: Node): Option[XML.Tree] =
wenzelm@34047
   151
    node.getUserData(Markup.DATA) match {
wenzelm@34047
   152
      case tree: XML.Tree => Some(tree)
wenzelm@34047
   153
      case _ => None
wenzelm@34047
   154
    }
wenzelm@34047
   155
wenzelm@33953
   156
  def document_node(doc: Document, tree: Tree): Node =
wenzelm@33953
   157
  {
wenzelm@27952
   158
    def DOM(tr: Tree): Node = tr match {
wenzelm@34046
   159
      case Elem(Markup.DATA, Nil, List(data, t)) =>
wenzelm@34046
   160
        val node = DOM(t)
wenzelm@34046
   161
        node.setUserData(Markup.DATA, data, null)
wenzelm@34046
   162
        node
wenzelm@34046
   163
      case Elem(name, atts, ts) =>
wenzelm@34046
   164
        if (name == Markup.DATA)
wenzelm@34046
   165
          error("Malformed data element: " + tr.toString)
wenzelm@27947
   166
        val node = doc.createElement(name)
wenzelm@27947
   167
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
   168
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
   169
        node
wenzelm@27947
   170
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   171
    }
wenzelm@33953
   172
    DOM(tree)
wenzelm@33953
   173
  }
wenzelm@33953
   174
wenzelm@33953
   175
  def document(tree: Tree, styles: String*): Document =
wenzelm@33953
   176
  {
wenzelm@33953
   177
    val doc = DocumentBuilderFactory.newInstance.newDocumentBuilder.newDocument
wenzelm@33953
   178
    doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\""))
wenzelm@33953
   179
wenzelm@33953
   180
    for (style <- styles) {
wenzelm@33953
   181
      doc.appendChild(doc.createProcessingInstruction("xml-stylesheet",
wenzelm@33953
   182
        "href=\"" + style + "\" type=\"text/css\""))
wenzelm@33953
   183
    }
wenzelm@27947
   184
    val root_elem = tree match {
wenzelm@33953
   185
      case Elem(_, _, _) => document_node(doc, tree)
wenzelm@33953
   186
      case Text(_) => document_node(doc, (Elem(Markup.ROOT, Nil, List(tree))))
wenzelm@27947
   187
    }
wenzelm@27947
   188
    doc.appendChild(root_elem)
wenzelm@27947
   189
    doc
wenzelm@27947
   190
  }
wenzelm@27931
   191
}