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