src/Pure/PIDE/xml.scala
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 46839 f7232c078fa5
child 49416 1053a564dd25
permissions -rw-r--r--
more official command specifications, including source position;
wenzelm@44698
     1
/*  Title:      Pure/PIDE/xml.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@27931
     3
    Author:     Makarius
wenzelm@27931
     4
wenzelm@44698
     5
Untyped XML trees and basic data representation.
wenzelm@27931
     6
*/
wenzelm@27931
     7
wenzelm@27931
     8
package isabelle
wenzelm@27931
     9
wenzelm@43520
    10
import java.lang.System
wenzelm@34108
    11
import java.util.WeakHashMap
wenzelm@34108
    12
import java.lang.ref.WeakReference
wenzelm@34108
    13
import javax.xml.parsers.DocumentBuilderFactory
wenzelm@34108
    14
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@44808
    88
wenzelm@44808
    89
  /** cache for partial sharing (weak table) **/
wenzelm@34108
    90
wenzelm@43745
    91
  class Cache(initial_size: Int = 131071, max_string: Int = 100)
wenzelm@34108
    92
  {
wenzelm@44704
    93
    private var table = new WeakHashMap[Any, WeakReference[Any]](initial_size)
wenzelm@38446
    94
wenzelm@44704
    95
    private def lookup[A](x: A): Option[A] =
wenzelm@44704
    96
    {
wenzelm@44704
    97
      val ref = table.get(x)
wenzelm@44704
    98
      if (ref == null) None
wenzelm@44704
    99
      else {
wenzelm@44704
   100
        val y = ref.asInstanceOf[WeakReference[A]].get
wenzelm@44704
   101
        if (y == null) None
wenzelm@44704
   102
        else Some(y)
wenzelm@38446
   103
      }
wenzelm@44704
   104
    }
wenzelm@44704
   105
    private def store[A](x: A): A =
wenzelm@44704
   106
    {
wenzelm@44704
   107
      table.put(x, new WeakReference[Any](x))
wenzelm@44704
   108
      x
wenzelm@44704
   109
    }
wenzelm@34108
   110
wenzelm@44704
   111
    private def trim_bytes(s: String): String = new String(s.toCharArray)
wenzelm@38869
   112
wenzelm@44704
   113
    private def _cache_string(x: String): String =
wenzelm@44704
   114
      lookup(x) match {
wenzelm@44704
   115
        case Some(y) => y
wenzelm@44704
   116
        case None =>
wenzelm@44704
   117
          val z = trim_bytes(x)
wenzelm@44704
   118
          if (z.length > max_string) z else store(z)
wenzelm@44704
   119
      }
wenzelm@44704
   120
    private def _cache_props(x: Properties.T): Properties.T =
wenzelm@44704
   121
      if (x.isEmpty) x
wenzelm@44704
   122
      else
wenzelm@34133
   123
        lookup(x) match {
wenzelm@34133
   124
          case Some(y) => y
wenzelm@44704
   125
          case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2))))
wenzelm@34133
   126
        }
wenzelm@44704
   127
    private def _cache_markup(x: Markup): Markup =
wenzelm@44704
   128
      lookup(x) match {
wenzelm@44704
   129
        case Some(y) => y
wenzelm@44704
   130
        case None =>
wenzelm@44704
   131
          x match {
wenzelm@44704
   132
            case Markup(name, props) =>
wenzelm@44704
   133
              store(Markup(_cache_string(name), _cache_props(props)))
wenzelm@44704
   134
          }
wenzelm@44704
   135
      }
wenzelm@44704
   136
    private def _cache_tree(x: XML.Tree): XML.Tree =
wenzelm@44704
   137
      lookup(x) match {
wenzelm@44704
   138
        case Some(y) => y
wenzelm@44704
   139
        case None =>
wenzelm@44704
   140
          x match {
wenzelm@44704
   141
            case XML.Elem(markup, body) =>
wenzelm@44704
   142
              store(XML.Elem(_cache_markup(markup), _cache_body(body)))
wenzelm@44704
   143
            case XML.Text(text) => store(XML.Text(_cache_string(text)))
wenzelm@44704
   144
          }
wenzelm@44704
   145
      }
wenzelm@44704
   146
    private def _cache_body(x: XML.Body): XML.Body =
wenzelm@44704
   147
      if (x.isEmpty) x
wenzelm@44704
   148
      else
wenzelm@34133
   149
        lookup(x) match {
wenzelm@34133
   150
          case Some(y) => y
wenzelm@44704
   151
          case None => x.map(_cache_tree(_))
wenzelm@34133
   152
        }
wenzelm@38446
   153
wenzelm@38446
   154
    // main methods
wenzelm@44705
   155
    def cache_string(x: String): String = synchronized { _cache_string(x) }
wenzelm@44705
   156
    def cache_markup(x: Markup): Markup = synchronized { _cache_markup(x) }
wenzelm@44705
   157
    def cache_tree(x: XML.Tree): XML.Tree = synchronized { _cache_tree(x) }
wenzelm@44705
   158
    def cache_body(x: XML.Body): XML.Body = synchronized { _cache_body(x) }
wenzelm@34108
   159
  }
wenzelm@34108
   160
wenzelm@34108
   161
wenzelm@43767
   162
wenzelm@43767
   163
  /** document object model (W3C DOM) **/
wenzelm@27948
   164
wenzelm@34871
   165
  def get_data(node: org.w3c.dom.Node): Option[XML.Tree] =
wenzelm@38231
   166
    node.getUserData(Markup.Data.name) match {
wenzelm@34047
   167
      case tree: XML.Tree => Some(tree)
wenzelm@34047
   168
      case _ => None
wenzelm@34047
   169
    }
wenzelm@34047
   170
wenzelm@34871
   171
  def document_node(doc: org.w3c.dom.Document, tree: Tree): org.w3c.dom.Node =
wenzelm@33953
   172
  {
wenzelm@34871
   173
    def DOM(tr: Tree): org.w3c.dom.Node = tr match {
wenzelm@38231
   174
      case Elem(Markup.Data, List(data, t)) =>
wenzelm@34046
   175
        val node = DOM(t)
wenzelm@38231
   176
        node.setUserData(Markup.Data.name, data, null)
wenzelm@34046
   177
        node
wenzelm@38230
   178
      case Elem(Markup(name, atts), ts) =>
wenzelm@38231
   179
        if (name == Markup.Data.name)
wenzelm@34046
   180
          error("Malformed data element: " + tr.toString)
wenzelm@27947
   181
        val node = doc.createElement(name)
wenzelm@27947
   182
        for ((name, value) <- atts) node.setAttribute(name, value)
wenzelm@27952
   183
        for (t <- ts) node.appendChild(DOM(t))
wenzelm@27947
   184
        node
wenzelm@27947
   185
      case Text(txt) => doc.createTextNode(txt)
wenzelm@27947
   186
    }
wenzelm@33953
   187
    DOM(tree)
wenzelm@33953
   188
  }
wenzelm@43767
   189
wenzelm@43767
   190
wenzelm@43767
   191
wenzelm@43767
   192
  /** XML as data representation language **/
wenzelm@43767
   193
wenzelm@43767
   194
  class XML_Atom(s: String) extends Exception(s)
wenzelm@43767
   195
  class XML_Body(body: XML.Body) extends Exception
wenzelm@43767
   196
wenzelm@43767
   197
  object Encode
wenzelm@43767
   198
  {
wenzelm@43767
   199
    type T[A] = A => XML.Body
wenzelm@43767
   200
wenzelm@43767
   201
wenzelm@43778
   202
    /* atomic values */
wenzelm@43767
   203
wenzelm@43778
   204
    def long_atom(i: Long): String = i.toString
wenzelm@43767
   205
wenzelm@43778
   206
    def int_atom(i: Int): String = i.toString
wenzelm@43767
   207
wenzelm@43778
   208
    def bool_atom(b: Boolean): String = if (b) "1" else "0"
wenzelm@43767
   209
wenzelm@43778
   210
    def unit_atom(u: Unit) = ""
wenzelm@43767
   211
wenzelm@43767
   212
wenzelm@43767
   213
    /* structural nodes */
wenzelm@43767
   214
wenzelm@43767
   215
    private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts)
wenzelm@43767
   216
wenzelm@43781
   217
    private def vector(xs: List[String]): XML.Attributes =
wenzelm@46839
   218
      xs.zipWithIndex.map({ case (x, i) => (int_atom(i), x) })
wenzelm@43778
   219
wenzelm@43778
   220
    private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree =
wenzelm@43778
   221
      XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2)
wenzelm@43767
   222
wenzelm@43767
   223
wenzelm@43767
   224
    /* representation of standard types */
wenzelm@43767
   225
wenzelm@43780
   226
    val properties: T[Properties.T] =
wenzelm@43767
   227
      (props => List(XML.Elem(Markup(":", props), Nil)))
wenzelm@43767
   228
wenzelm@43767
   229
    val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s)))
wenzelm@43767
   230
wenzelm@43767
   231
    val long: T[Long] = (x => string(long_atom(x)))
wenzelm@43767
   232
wenzelm@43767
   233
    val int: T[Int] = (x => string(int_atom(x)))
wenzelm@43767
   234
wenzelm@43767
   235
    val bool: T[Boolean] = (x => string(bool_atom(x)))
wenzelm@43767
   236
wenzelm@43767
   237
    val unit: T[Unit] = (x => string(unit_atom(x)))
wenzelm@43767
   238
wenzelm@43767
   239
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   240
      (x => List(node(f(x._1)), node(g(x._2))))
wenzelm@43767
   241
wenzelm@43767
   242
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   243
      (x => List(node(f(x._1)), node(g(x._2)), node(h(x._3))))
wenzelm@43767
   244
wenzelm@43767
   245
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   246
      (xs => xs.map((x: A) => node(f(x))))
wenzelm@43767
   247
wenzelm@43767
   248
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   249
    {
wenzelm@43767
   250
      case None => Nil
wenzelm@43767
   251
      case Some(x) => List(node(f(x)))
wenzelm@43767
   252
    }
wenzelm@43767
   253
wenzelm@43778
   254
    def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] =
wenzelm@43767
   255
    {
wenzelm@43767
   256
      case x =>
wenzelm@43767
   257
        val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get
wenzelm@43767
   258
        List(tagged(tag, f(x)))
wenzelm@43767
   259
    }
wenzelm@43767
   260
  }
wenzelm@43767
   261
wenzelm@43767
   262
  object Decode
wenzelm@43767
   263
  {
wenzelm@43767
   264
    type T[A] = XML.Body => A
wenzelm@43778
   265
    type V[A] = (List[String], XML.Body) => A
wenzelm@43767
   266
wenzelm@43767
   267
wenzelm@43778
   268
    /* atomic values */
wenzelm@43767
   269
wenzelm@43778
   270
    def long_atom(s: String): Long =
wenzelm@43767
   271
      try { java.lang.Long.parseLong(s) }
wenzelm@43767
   272
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   273
wenzelm@43778
   274
    def int_atom(s: String): Int =
wenzelm@43767
   275
      try { Integer.parseInt(s) }
wenzelm@43767
   276
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   277
wenzelm@43778
   278
    def bool_atom(s: String): Boolean =
wenzelm@43767
   279
      if (s == "1") true
wenzelm@43767
   280
      else if (s == "0") false
wenzelm@43767
   281
      else throw new XML_Atom(s)
wenzelm@43767
   282
wenzelm@43778
   283
    def unit_atom(s: String): Unit =
wenzelm@43767
   284
      if (s == "") () else throw new XML_Atom(s)
wenzelm@43767
   285
wenzelm@43767
   286
wenzelm@43767
   287
    /* structural nodes */
wenzelm@43767
   288
wenzelm@43767
   289
    private def node(t: XML.Tree): XML.Body =
wenzelm@43767
   290
      t match {
wenzelm@43767
   291
        case XML.Elem(Markup(":", Nil), ts) => ts
wenzelm@43767
   292
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   293
      }
wenzelm@43767
   294
wenzelm@43781
   295
    private def vector(atts: XML.Attributes): List[String] =
wenzelm@46839
   296
      atts.iterator.zipWithIndex.map(
wenzelm@46839
   297
        { case ((a, x), i) => if (int_atom(a) == i) x else throw new XML_Atom(a) }).toList
wenzelm@43778
   298
wenzelm@43778
   299
    private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) =
wenzelm@43767
   300
      t match {
wenzelm@43781
   301
        case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts))
wenzelm@43767
   302
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   303
      }
wenzelm@43767
   304
wenzelm@43767
   305
wenzelm@43767
   306
    /* representation of standard types */
wenzelm@43767
   307
wenzelm@43780
   308
    val properties: T[Properties.T] =
wenzelm@43767
   309
    {
wenzelm@43767
   310
      case List(XML.Elem(Markup(":", props), Nil)) => props
wenzelm@43767
   311
      case ts => throw new XML_Body(ts)
wenzelm@43767
   312
    }
wenzelm@43767
   313
wenzelm@43767
   314
    val string: T[String] =
wenzelm@43767
   315
    {
wenzelm@43767
   316
      case Nil => ""
wenzelm@43767
   317
      case List(XML.Text(s)) => s
wenzelm@43767
   318
      case ts => throw new XML_Body(ts)
wenzelm@43767
   319
    }
wenzelm@43767
   320
wenzelm@43767
   321
    val long: T[Long] = (x => long_atom(string(x)))
wenzelm@43767
   322
wenzelm@43767
   323
    val int: T[Int] = (x => int_atom(string(x)))
wenzelm@43767
   324
wenzelm@43767
   325
    val bool: T[Boolean] = (x => bool_atom(string(x)))
wenzelm@43767
   326
wenzelm@43767
   327
    val unit: T[Unit] = (x => unit_atom(string(x)))
wenzelm@43767
   328
wenzelm@43767
   329
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   330
    {
wenzelm@43767
   331
      case List(t1, t2) => (f(node(t1)), g(node(t2)))
wenzelm@43767
   332
      case ts => throw new XML_Body(ts)
wenzelm@43767
   333
    }
wenzelm@43767
   334
wenzelm@43767
   335
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   336
    {
wenzelm@43767
   337
      case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3)))
wenzelm@43767
   338
      case ts => throw new XML_Body(ts)
wenzelm@43767
   339
    }
wenzelm@43767
   340
wenzelm@43767
   341
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   342
      (ts => ts.map(t => f(node(t))))
wenzelm@43767
   343
wenzelm@43767
   344
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   345
    {
wenzelm@43767
   346
      case Nil => None
wenzelm@43767
   347
      case List(t) => Some(f(node(t)))
wenzelm@43767
   348
      case ts => throw new XML_Body(ts)
wenzelm@43767
   349
    }
wenzelm@43767
   350
wenzelm@43778
   351
    def variant[A](fs: List[V[A]]): T[A] =
wenzelm@43767
   352
    {
wenzelm@43767
   353
      case List(t) =>
wenzelm@43778
   354
        val (tag, (xs, ts)) = tagged(t)
wenzelm@43768
   355
        val f =
wenzelm@43768
   356
          try { fs(tag) }
wenzelm@43768
   357
          catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) }
wenzelm@43778
   358
        f(xs, ts)
wenzelm@43767
   359
      case ts => throw new XML_Body(ts)
wenzelm@43767
   360
    }
wenzelm@43767
   361
  }
wenzelm@27931
   362
}