src/Pure/PIDE/xml.scala
author wenzelm
Sun Sep 04 19:06:45 2011 +0200 (2011-09-04 ago)
changeset 44704 528d635ef6f0
parent 44698 0385292321a0
child 44705 089fcaf94c00
permissions -rw-r--r--
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm@44698
     1
/*  Title:      Pure/PIDE/xml.scala
wenzelm@27931
     2
    Author:     Makarius
wenzelm@27931
     3
wenzelm@44698
     4
Untyped XML trees and basic data representation.
wenzelm@27931
     5
*/
wenzelm@27931
     6
wenzelm@27931
     7
package isabelle
wenzelm@27931
     8
wenzelm@43520
     9
import java.lang.System
wenzelm@34108
    10
import java.util.WeakHashMap
wenzelm@34108
    11
import java.lang.ref.WeakReference
wenzelm@34108
    12
import javax.xml.parsers.DocumentBuilderFactory
wenzelm@34108
    13
wenzelm@38446
    14
import scala.actors.Actor._
wenzelm@43778
    15
import scala.collection.mutable
wenzelm@38446
    16
wenzelm@27947
    17
wenzelm@29203
    18
object XML
wenzelm@29203
    19
{
wenzelm@43767
    20
  /** XML trees **/
wenzelm@43767
    21
wenzelm@27947
    22
  /* datatype representation */
wenzelm@27947
    23
wenzelm@43780
    24
  type Attributes = Properties.T
wenzelm@27931
    25
wenzelm@38268
    26
  sealed abstract class Tree { override def toString = string_of_tree(this) }
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@38267
    33
  type Body = List[Tree]
wenzelm@38267
    34
wenzelm@29203
    35
wenzelm@29203
    36
  /* string representation */
wenzelm@29203
    37
wenzelm@38268
    38
  def string_of_body(body: Body): String =
wenzelm@38268
    39
  {
wenzelm@38268
    40
    val s = new StringBuilder
wenzelm@38268
    41
wenzelm@38268
    42
    def text(txt: String) {
wenzelm@38268
    43
      if (txt == null) s ++= txt
wenzelm@38268
    44
      else {
wenzelm@38268
    45
        for (c <- txt.iterator) c match {
wenzelm@38268
    46
          case '<' => s ++= "&lt;"
wenzelm@38268
    47
          case '>' => s ++= "&gt;"
wenzelm@38268
    48
          case '&' => s ++= "&amp;"
wenzelm@38268
    49
          case '"' => s ++= "&quot;"
wenzelm@38268
    50
          case '\'' => s ++= "&apos;"
wenzelm@38268
    51
          case _ => s += c
wenzelm@38268
    52
        }
wenzelm@34005
    53
      }
wenzelm@29203
    54
    }
wenzelm@38268
    55
    def attrib(p: (String, String)) { s ++= " "; s ++= p._1; s ++= "=\""; text(p._2); s ++= "\"" }
wenzelm@38268
    56
    def elem(markup: Markup) { s ++= markup.name; markup.properties.foreach(attrib) }
wenzelm@38268
    57
    def tree(t: Tree): Unit =
wenzelm@38268
    58
      t match {
wenzelm@38268
    59
        case Elem(markup, Nil) =>
wenzelm@38268
    60
          s ++= "<"; elem(markup); s ++= "/>"
wenzelm@38268
    61
        case Elem(markup, ts) =>
wenzelm@38268
    62
          s ++= "<"; elem(markup); s ++= ">"
wenzelm@38268
    63
          ts.foreach(tree)
wenzelm@38268
    64
          s ++= "</"; s ++= markup.name; s ++= ">"
wenzelm@38268
    65
        case Text(txt) => text(txt)
wenzelm@38268
    66
      }
wenzelm@38268
    67
    body.foreach(tree)
wenzelm@38268
    68
    s.toString
wenzelm@29203
    69
  }
wenzelm@29203
    70
wenzelm@38268
    71
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
wenzelm@27941
    72
wenzelm@27941
    73
wenzelm@38484
    74
  /* text content */
wenzelm@27941
    75
wenzelm@38484
    76
  def content_stream(tree: Tree): Stream[String] =
wenzelm@38484
    77
    tree match {
wenzelm@43747
    78
      case Elem(_, body) => content_stream(body)
wenzelm@38484
    79
      case Text(content) => Stream(content)
wenzelm@27941
    80
    }
wenzelm@43747
    81
  def content_stream(body: Body): Stream[String] =
wenzelm@43747
    82
    body.toStream.flatten(content_stream(_))
wenzelm@27941
    83
wenzelm@38484
    84
  def content(tree: Tree): Iterator[String] = content_stream(tree).iterator
wenzelm@43747
    85
  def content(body: Body): Iterator[String] = content_stream(body).iterator
wenzelm@27941
    86
wenzelm@27947
    87
wenzelm@38446
    88
  /* pipe-lined cache for partial sharing */
wenzelm@34108
    89
wenzelm@43745
    90
  class Cache(initial_size: Int = 131071, max_string: Int = 100)
wenzelm@34108
    91
  {
wenzelm@44704
    92
    private var table = new WeakHashMap[Any, WeakReference[Any]](initial_size)
wenzelm@38446
    93
wenzelm@44704
    94
    private def lookup[A](x: A): Option[A] =
wenzelm@44704
    95
    {
wenzelm@44704
    96
      val ref = table.get(x)
wenzelm@44704
    97
      if (ref == null) None
wenzelm@44704
    98
      else {
wenzelm@44704
    99
        val y = ref.asInstanceOf[WeakReference[A]].get
wenzelm@44704
   100
        if (y == null) None
wenzelm@44704
   101
        else Some(y)
wenzelm@38446
   102
      }
wenzelm@44704
   103
    }
wenzelm@44704
   104
    private def store[A](x: A): A =
wenzelm@44704
   105
    {
wenzelm@44704
   106
      table.put(x, new WeakReference[Any](x))
wenzelm@44704
   107
      x
wenzelm@44704
   108
    }
wenzelm@34108
   109
wenzelm@44704
   110
    private def trim_bytes(s: String): String = new String(s.toCharArray)
wenzelm@38869
   111
wenzelm@44704
   112
    private def _cache_string(x: String): String =
wenzelm@44704
   113
      lookup(x) match {
wenzelm@44704
   114
        case Some(y) => y
wenzelm@44704
   115
        case None =>
wenzelm@44704
   116
          val z = trim_bytes(x)
wenzelm@44704
   117
          if (z.length > max_string) z else store(z)
wenzelm@44704
   118
      }
wenzelm@44704
   119
    private def _cache_props(x: Properties.T): Properties.T =
wenzelm@44704
   120
      if (x.isEmpty) x
wenzelm@44704
   121
      else
wenzelm@34133
   122
        lookup(x) match {
wenzelm@34133
   123
          case Some(y) => y
wenzelm@44704
   124
          case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2))))
wenzelm@34133
   125
        }
wenzelm@44704
   126
    private def _cache_markup(x: Markup): Markup =
wenzelm@44704
   127
      lookup(x) match {
wenzelm@44704
   128
        case Some(y) => y
wenzelm@44704
   129
        case None =>
wenzelm@44704
   130
          x match {
wenzelm@44704
   131
            case Markup(name, props) =>
wenzelm@44704
   132
              store(Markup(_cache_string(name), _cache_props(props)))
wenzelm@44704
   133
          }
wenzelm@44704
   134
      }
wenzelm@44704
   135
    private def _cache_tree(x: XML.Tree): XML.Tree =
wenzelm@44704
   136
      lookup(x) match {
wenzelm@44704
   137
        case Some(y) => y
wenzelm@44704
   138
        case None =>
wenzelm@44704
   139
          x match {
wenzelm@44704
   140
            case XML.Elem(markup, body) =>
wenzelm@44704
   141
              store(XML.Elem(_cache_markup(markup), _cache_body(body)))
wenzelm@44704
   142
            case XML.Text(text) => store(XML.Text(_cache_string(text)))
wenzelm@44704
   143
          }
wenzelm@44704
   144
      }
wenzelm@44704
   145
    private def _cache_body(x: XML.Body): XML.Body =
wenzelm@44704
   146
      if (x.isEmpty) x
wenzelm@44704
   147
      else
wenzelm@34133
   148
        lookup(x) match {
wenzelm@34133
   149
          case Some(y) => y
wenzelm@44704
   150
          case None => x.map(_cache_tree(_))
wenzelm@34133
   151
        }
wenzelm@38446
   152
wenzelm@38446
   153
    // main methods
wenzelm@44704
   154
    // FIXME simplify signatures
wenzelm@44704
   155
    def cache_string(x: String)(f: String => Unit): Unit = f(synchronized { _cache_string(x) })
wenzelm@44704
   156
    def cache_markup(x: Markup)(f: Markup => Unit): Unit = f(synchronized { _cache_markup(x) })
wenzelm@44704
   157
    def cache_tree(x: XML.Tree)(f: XML.Tree => Unit): Unit = f(synchronized { _cache_tree(x) })
wenzelm@44704
   158
    def cache_body(x: XML.Body)(f: XML.Body => Unit): Unit = f(synchronized { _cache_body(x) })
wenzelm@44704
   159
    def cache_ignore[A](x: A)(f: A => Unit): Unit = f(x)
wenzelm@34108
   160
  }
wenzelm@34108
   161
wenzelm@34108
   162
wenzelm@43767
   163
wenzelm@43767
   164
  /** document object model (W3C DOM) **/
wenzelm@27948
   165
wenzelm@34871
   166
  def get_data(node: org.w3c.dom.Node): Option[XML.Tree] =
wenzelm@38231
   167
    node.getUserData(Markup.Data.name) match {
wenzelm@34047
   168
      case tree: XML.Tree => Some(tree)
wenzelm@34047
   169
      case _ => None
wenzelm@34047
   170
    }
wenzelm@34047
   171
wenzelm@34871
   172
  def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node =
wenzelm@33953
   173
  {
wenzelm@34871
   174
    def DOM(tr: Tree): org.w3c.dom.Node = tr match {
wenzelm@38231
   175
      case Elem(Markup.Data, List(data, t)) =>
wenzelm@34046
   176
        val node = DOM(t)
wenzelm@38231
   177
        node.setUserData(Markup.Data.name, data, null)
wenzelm@34046
   178
        node
wenzelm@38230
   179
      case Elem(Markup(name, atts), ts) =>
wenzelm@38231
   180
        if (name == Markup.Data.name)
wenzelm@34046
   181
          error("Malformed data element: " + tr.toString)
wenzelm@27947
   182
        val node = doc.createElement(name)
wenzelm@27947
   183
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
   184
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
   185
        node
wenzelm@27947
   186
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   187
    }
wenzelm@33953
   188
    DOM(tree)
wenzelm@33953
   189
  }
wenzelm@43767
   190
wenzelm@43767
   191
wenzelm@43767
   192
wenzelm@43767
   193
  /** XML as data representation language **/
wenzelm@43767
   194
wenzelm@43767
   195
  class XML_Atom(s: String) extends Exception(s)
wenzelm@43767
   196
  class XML_Body(body: XML.Body) extends Exception
wenzelm@43767
   197
wenzelm@43767
   198
  object Encode
wenzelm@43767
   199
  {
wenzelm@43767
   200
    type T[A] = A => XML.Body
wenzelm@43767
   201
wenzelm@43767
   202
wenzelm@43778
   203
    /* atomic values */
wenzelm@43767
   204
wenzelm@43778
   205
    def long_atom(i: Long): String = i.toString
wenzelm@43767
   206
wenzelm@43778
   207
    def int_atom(i: Int): String = i.toString
wenzelm@43767
   208
wenzelm@43778
   209
    def bool_atom(b: Boolean): String = if (b) "1" else "0"
wenzelm@43767
   210
wenzelm@43778
   211
    def unit_atom(u: Unit) = ""
wenzelm@43767
   212
wenzelm@43767
   213
wenzelm@43767
   214
    /* structural nodes */
wenzelm@43767
   215
wenzelm@43767
   216
    private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts)
wenzelm@43767
   217
wenzelm@43781
   218
    private def vector(xs: List[String]): XML.Attributes =
wenzelm@43778
   219
      xs.zipWithIndex.map(p => (int_atom(p._2), p._1))
wenzelm@43778
   220
wenzelm@43778
   221
    private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree =
wenzelm@43778
   222
      XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2)
wenzelm@43767
   223
wenzelm@43767
   224
wenzelm@43767
   225
    /* representation of standard types */
wenzelm@43767
   226
wenzelm@43780
   227
    val properties: T[Properties.T] =
wenzelm@43767
   228
      (props => List(XML.Elem(Markup(":", props), Nil)))
wenzelm@43767
   229
wenzelm@43767
   230
    val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s)))
wenzelm@43767
   231
wenzelm@43767
   232
    val long: T[Long] = (x => string(long_atom(x)))
wenzelm@43767
   233
wenzelm@43767
   234
    val int: T[Int] = (x => string(int_atom(x)))
wenzelm@43767
   235
wenzelm@43767
   236
    val bool: T[Boolean] = (x => string(bool_atom(x)))
wenzelm@43767
   237
wenzelm@43767
   238
    val unit: T[Unit] = (x => string(unit_atom(x)))
wenzelm@43767
   239
wenzelm@43767
   240
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   241
      (x => List(node(f(x._1)), node(g(x._2))))
wenzelm@43767
   242
wenzelm@43767
   243
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   244
      (x => List(node(f(x._1)), node(g(x._2)), node(h(x._3))))
wenzelm@43767
   245
wenzelm@43767
   246
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   247
      (xs => xs.map((x: A) => node(f(x))))
wenzelm@43767
   248
wenzelm@43767
   249
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   250
    {
wenzelm@43767
   251
      case None => Nil
wenzelm@43767
   252
      case Some(x) => List(node(f(x)))
wenzelm@43767
   253
    }
wenzelm@43767
   254
wenzelm@43778
   255
    def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] =
wenzelm@43767
   256
    {
wenzelm@43767
   257
      case x =>
wenzelm@43767
   258
        val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get
wenzelm@43767
   259
        List(tagged(tag, f(x)))
wenzelm@43767
   260
    }
wenzelm@43767
   261
  }
wenzelm@43767
   262
wenzelm@43767
   263
  object Decode
wenzelm@43767
   264
  {
wenzelm@43767
   265
    type T[A] = XML.Body => A
wenzelm@43778
   266
    type V[A] = (List[String], XML.Body) => A
wenzelm@43767
   267
wenzelm@43767
   268
wenzelm@43778
   269
    /* atomic values */
wenzelm@43767
   270
wenzelm@43778
   271
    def long_atom(s: String): Long =
wenzelm@43767
   272
      try { java.lang.Long.parseLong(s) }
wenzelm@43767
   273
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   274
wenzelm@43778
   275
    def int_atom(s: String): Int =
wenzelm@43767
   276
      try { Integer.parseInt(s) }
wenzelm@43767
   277
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   278
wenzelm@43778
   279
    def bool_atom(s: String): Boolean =
wenzelm@43767
   280
      if (s == "1") true
wenzelm@43767
   281
      else if (s == "0") false
wenzelm@43767
   282
      else throw new XML_Atom(s)
wenzelm@43767
   283
wenzelm@43778
   284
    def unit_atom(s: String): Unit =
wenzelm@43767
   285
      if (s == "") () else throw new XML_Atom(s)
wenzelm@43767
   286
wenzelm@43767
   287
wenzelm@43767
   288
    /* structural nodes */
wenzelm@43767
   289
wenzelm@43767
   290
    private def node(t: XML.Tree): XML.Body =
wenzelm@43767
   291
      t match {
wenzelm@43767
   292
        case XML.Elem(Markup(":", Nil), ts) => ts
wenzelm@43767
   293
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   294
      }
wenzelm@43767
   295
wenzelm@43781
   296
    private def vector(atts: XML.Attributes): List[String] =
wenzelm@43778
   297
    {
wenzelm@43778
   298
      val xs = new mutable.ListBuffer[String]
wenzelm@43778
   299
      var i = 0
wenzelm@43781
   300
      for ((a, x) <- atts) {
wenzelm@43778
   301
        if (int_atom(a) == i) { xs += x; i = i + 1 }
wenzelm@43778
   302
        else throw new XML_Atom(a)
wenzelm@43778
   303
      }
wenzelm@43778
   304
      xs.toList
wenzelm@43778
   305
    }
wenzelm@43778
   306
wenzelm@43778
   307
    private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) =
wenzelm@43767
   308
      t match {
wenzelm@43781
   309
        case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts))
wenzelm@43767
   310
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   311
      }
wenzelm@43767
   312
wenzelm@43767
   313
wenzelm@43767
   314
    /* representation of standard types */
wenzelm@43767
   315
wenzelm@43780
   316
    val properties: T[Properties.T] =
wenzelm@43767
   317
    {
wenzelm@43767
   318
      case List(XML.Elem(Markup(":", props), Nil)) => props
wenzelm@43767
   319
      case ts => throw new XML_Body(ts)
wenzelm@43767
   320
    }
wenzelm@43767
   321
wenzelm@43767
   322
    val string: T[String] =
wenzelm@43767
   323
    {
wenzelm@43767
   324
      case Nil => ""
wenzelm@43767
   325
      case List(XML.Text(s)) => s
wenzelm@43767
   326
      case ts => throw new XML_Body(ts)
wenzelm@43767
   327
    }
wenzelm@43767
   328
wenzelm@43767
   329
    val long: T[Long] = (x => long_atom(string(x)))
wenzelm@43767
   330
wenzelm@43767
   331
    val int: T[Int] = (x => int_atom(string(x)))
wenzelm@43767
   332
wenzelm@43767
   333
    val bool: T[Boolean] = (x => bool_atom(string(x)))
wenzelm@43767
   334
wenzelm@43767
   335
    val unit: T[Unit] = (x => unit_atom(string(x)))
wenzelm@43767
   336
wenzelm@43767
   337
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   338
    {
wenzelm@43767
   339
      case List(t1, t2) => (f(node(t1)), g(node(t2)))
wenzelm@43767
   340
      case ts => throw new XML_Body(ts)
wenzelm@43767
   341
    }
wenzelm@43767
   342
wenzelm@43767
   343
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   344
    {
wenzelm@43767
   345
      case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3)))
wenzelm@43767
   346
      case ts => throw new XML_Body(ts)
wenzelm@43767
   347
    }
wenzelm@43767
   348
wenzelm@43767
   349
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   350
      (ts => ts.map(t => f(node(t))))
wenzelm@43767
   351
wenzelm@43767
   352
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   353
    {
wenzelm@43767
   354
      case Nil => None
wenzelm@43767
   355
      case List(t) => Some(f(node(t)))
wenzelm@43767
   356
      case ts => throw new XML_Body(ts)
wenzelm@43767
   357
    }
wenzelm@43767
   358
wenzelm@43778
   359
    def variant[A](fs: List[V[A]]): T[A] =
wenzelm@43767
   360
    {
wenzelm@43767
   361
      case List(t) =>
wenzelm@43778
   362
        val (tag, (xs, ts)) = tagged(t)
wenzelm@43768
   363
        val f =
wenzelm@43768
   364
          try { fs(tag) }
wenzelm@43768
   365
          catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) }
wenzelm@43778
   366
        f(xs, ts)
wenzelm@43767
   367
      case ts => throw new XML_Body(ts)
wenzelm@43767
   368
    }
wenzelm@43767
   369
  }
wenzelm@27931
   370
}