src/Pure/General/xml.scala
author wenzelm
Fri Dec 18 16:52:36 2009 +0100 (2009-12-18 ago)
changeset 34133 17554065f3be
parent 34119 ae92efb48784
child 34871 e596a0b71f3c
permissions -rw-r--r--
tuned;
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@34133
   126
      if (x.isEmpty) x
wenzelm@34133
   127
      else
wenzelm@34133
   128
        lookup(x) match {
wenzelm@34133
   129
          case Some(y) => y
wenzelm@34133
   130
          case None => store(x.map(p => (cache_string(p._1), cache_string(p._2))))
wenzelm@34133
   131
        }
wenzelm@34117
   132
    def cache_tree(x: XML.Tree): XML.Tree =
wenzelm@34108
   133
      lookup(x) match {
wenzelm@34108
   134
        case Some(y) => y
wenzelm@34108
   135
        case None =>
wenzelm@34108
   136
          x match {
wenzelm@34108
   137
            case XML.Elem(name, props, body) =>
wenzelm@34117
   138
              store(XML.Elem(cache_string(name), cache_props(props), cache_trees(body)))
wenzelm@34108
   139
            case XML.Text(text) => XML.Text(cache_string(text))
wenzelm@34108
   140
          }
wenzelm@34108
   141
      }
wenzelm@34117
   142
    def cache_trees(x: List[XML.Tree]): List[XML.Tree] =
wenzelm@34133
   143
      if (x.isEmpty) x
wenzelm@34133
   144
      else
wenzelm@34133
   145
        lookup(x) match {
wenzelm@34133
   146
          case Some(y) => y
wenzelm@34133
   147
          case None => x.map(cache_tree(_))
wenzelm@34133
   148
        }
wenzelm@34108
   149
  }
wenzelm@34108
   150
wenzelm@34108
   151
wenzelm@33953
   152
  /* document object model (W3C DOM) */
wenzelm@27948
   153
wenzelm@34047
   154
  def get_data(node: Node): Option[XML.Tree] =
wenzelm@34047
   155
    node.getUserData(Markup.DATA) match {
wenzelm@34047
   156
      case tree: XML.Tree => Some(tree)
wenzelm@34047
   157
      case _ => None
wenzelm@34047
   158
    }
wenzelm@34047
   159
wenzelm@33953
   160
  def document_node(doc: Document, tree: Tree): Node =
wenzelm@33953
   161
  {
wenzelm@27952
   162
    def DOM(tr: Tree): Node = tr match {
wenzelm@34046
   163
      case Elem(Markup.DATA, Nil, List(data, t)) =>
wenzelm@34046
   164
        val node = DOM(t)
wenzelm@34046
   165
        node.setUserData(Markup.DATA, data, null)
wenzelm@34046
   166
        node
wenzelm@34046
   167
      case Elem(name, atts, ts) =>
wenzelm@34046
   168
        if (name == Markup.DATA)
wenzelm@34046
   169
          error("Malformed data element: " + tr.toString)
wenzelm@27947
   170
        val node = doc.createElement(name)
wenzelm@27947
   171
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
   172
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
   173
        node
wenzelm@27947
   174
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   175
    }
wenzelm@33953
   176
    DOM(tree)
wenzelm@33953
   177
  }
wenzelm@27931
   178
}