src/Pure/PIDE/xml.scala
author wenzelm
Thu Sep 27 15:55:38 2012 +0200 (2012-09-27 ago)
changeset 49613 2f6986e2ef06
parent 49466 99ed1f422635
child 49650 9fad6480300d
permissions -rw-r--r--
removed obsolete org.w3c.dom operations;
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@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@27947
    14
wenzelm@29203
    15
object XML
wenzelm@29203
    16
{
wenzelm@43767
    17
  /** XML trees **/
wenzelm@43767
    18
wenzelm@27947
    19
  /* datatype representation */
wenzelm@27947
    20
wenzelm@43780
    21
  type Attributes = Properties.T
wenzelm@27931
    22
wenzelm@38268
    23
  sealed abstract class Tree { override def toString = string_of_tree(this) }
wenzelm@38230
    24
  case class Elem(markup: Markup, body: List[Tree]) extends Tree
wenzelm@29204
    25
  case class Text(content: String) extends Tree
wenzelm@29203
    26
wenzelm@38230
    27
  def elem(name: String, body: List[Tree]) = Elem(Markup(name, Nil), body)
wenzelm@38230
    28
  def elem(name: String) = Elem(Markup(name, Nil), Nil)
wenzelm@33999
    29
wenzelm@38267
    30
  type Body = List[Tree]
wenzelm@38267
    31
wenzelm@29203
    32
wenzelm@29203
    33
  /* string representation */
wenzelm@29203
    34
wenzelm@38268
    35
  def string_of_body(body: Body): String =
wenzelm@38268
    36
  {
wenzelm@38268
    37
    val s = new StringBuilder
wenzelm@38268
    38
wenzelm@38268
    39
    def text(txt: String) {
wenzelm@38268
    40
      if (txt == null) s ++= txt
wenzelm@38268
    41
      else {
wenzelm@38268
    42
        for (c <- txt.iterator) c match {
wenzelm@38268
    43
          case '<' => s ++= "&lt;"
wenzelm@38268
    44
          case '>' => s ++= "&gt;"
wenzelm@38268
    45
          case '&' => s ++= "&amp;"
wenzelm@38268
    46
          case '"' => s ++= "&quot;"
wenzelm@38268
    47
          case '\'' => s ++= "&apos;"
wenzelm@38268
    48
          case _ => s += c
wenzelm@38268
    49
        }
wenzelm@34005
    50
      }
wenzelm@29203
    51
    }
wenzelm@38268
    52
    def attrib(p: (String, String)) { s ++= " "; s ++= p._1; s ++= "=\""; text(p._2); s ++= "\"" }
wenzelm@38268
    53
    def elem(markup: Markup) { s ++= markup.name; markup.properties.foreach(attrib) }
wenzelm@38268
    54
    def tree(t: Tree): Unit =
wenzelm@38268
    55
      t match {
wenzelm@38268
    56
        case Elem(markup, Nil) =>
wenzelm@38268
    57
          s ++= "<"; elem(markup); s ++= "/>"
wenzelm@38268
    58
        case Elem(markup, ts) =>
wenzelm@38268
    59
          s ++= "<"; elem(markup); s ++= ">"
wenzelm@38268
    60
          ts.foreach(tree)
wenzelm@38268
    61
          s ++= "</"; s ++= markup.name; s ++= ">"
wenzelm@38268
    62
        case Text(txt) => text(txt)
wenzelm@38268
    63
      }
wenzelm@38268
    64
    body.foreach(tree)
wenzelm@38268
    65
    s.toString
wenzelm@29203
    66
  }
wenzelm@29203
    67
wenzelm@38268
    68
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
wenzelm@27941
    69
wenzelm@27941
    70
wenzelm@49466
    71
  /* traverse text */
wenzelm@27941
    72
wenzelm@49466
    73
  def traverse_text[A](body: Body)(a: A)(op: (A, String) => A): A =
wenzelm@49466
    74
  {
wenzelm@49466
    75
    def traverse(x: A, t: Tree): A =
wenzelm@49466
    76
      t match {
wenzelm@49466
    77
        case Elem(_, ts) => (x /: ts)(traverse)
wenzelm@49466
    78
        case Text(s) => op(x, s)
wenzelm@49416
    79
      }
wenzelm@49466
    80
    (a /: body)(traverse)
wenzelm@49416
    81
  }
wenzelm@49416
    82
wenzelm@49466
    83
  def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length }
wenzelm@49466
    84
wenzelm@49466
    85
wenzelm@49466
    86
  /* text content */
wenzelm@49466
    87
wenzelm@49466
    88
  def content(body: Body): String =
wenzelm@49466
    89
  {
wenzelm@49466
    90
    val text = new StringBuilder(text_length(body))
wenzelm@49466
    91
    traverse_text(body)(()) { case (_, s) => text.append(s) }
wenzelm@49466
    92
    text.toString
wenzelm@49466
    93
  }
wenzelm@49466
    94
wenzelm@49466
    95
  def content(tree: Tree): String = content(List(tree))
wenzelm@27941
    96
wenzelm@27947
    97
wenzelm@44808
    98
wenzelm@44808
    99
  /** cache for partial sharing (weak table) **/
wenzelm@34108
   100
wenzelm@43745
   101
  class Cache(initial_size: Int = 131071, max_string: Int = 100)
wenzelm@34108
   102
  {
wenzelm@44704
   103
    private var table = new WeakHashMap[Any, WeakReference[Any]](initial_size)
wenzelm@38446
   104
wenzelm@44704
   105
    private def lookup[A](x: A): Option[A] =
wenzelm@44704
   106
    {
wenzelm@44704
   107
      val ref = table.get(x)
wenzelm@44704
   108
      if (ref == null) None
wenzelm@44704
   109
      else {
wenzelm@44704
   110
        val y = ref.asInstanceOf[WeakReference[A]].get
wenzelm@44704
   111
        if (y == null) None
wenzelm@44704
   112
        else Some(y)
wenzelm@38446
   113
      }
wenzelm@44704
   114
    }
wenzelm@44704
   115
    private def store[A](x: A): A =
wenzelm@44704
   116
    {
wenzelm@44704
   117
      table.put(x, new WeakReference[Any](x))
wenzelm@44704
   118
      x
wenzelm@44704
   119
    }
wenzelm@34108
   120
wenzelm@44704
   121
    private def trim_bytes(s: String): String = new String(s.toCharArray)
wenzelm@38869
   122
wenzelm@44704
   123
    private def _cache_string(x: String): String =
wenzelm@44704
   124
      lookup(x) match {
wenzelm@44704
   125
        case Some(y) => y
wenzelm@44704
   126
        case None =>
wenzelm@44704
   127
          val z = trim_bytes(x)
wenzelm@44704
   128
          if (z.length > max_string) z else store(z)
wenzelm@44704
   129
      }
wenzelm@44704
   130
    private def _cache_props(x: Properties.T): Properties.T =
wenzelm@44704
   131
      if (x.isEmpty) x
wenzelm@44704
   132
      else
wenzelm@34133
   133
        lookup(x) match {
wenzelm@34133
   134
          case Some(y) => y
wenzelm@44704
   135
          case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2))))
wenzelm@34133
   136
        }
wenzelm@44704
   137
    private def _cache_markup(x: Markup): Markup =
wenzelm@44704
   138
      lookup(x) match {
wenzelm@44704
   139
        case Some(y) => y
wenzelm@44704
   140
        case None =>
wenzelm@44704
   141
          x match {
wenzelm@44704
   142
            case Markup(name, props) =>
wenzelm@44704
   143
              store(Markup(_cache_string(name), _cache_props(props)))
wenzelm@44704
   144
          }
wenzelm@44704
   145
      }
wenzelm@44704
   146
    private def _cache_tree(x: XML.Tree): XML.Tree =
wenzelm@44704
   147
      lookup(x) match {
wenzelm@44704
   148
        case Some(y) => y
wenzelm@44704
   149
        case None =>
wenzelm@44704
   150
          x match {
wenzelm@44704
   151
            case XML.Elem(markup, body) =>
wenzelm@44704
   152
              store(XML.Elem(_cache_markup(markup), _cache_body(body)))
wenzelm@44704
   153
            case XML.Text(text) => store(XML.Text(_cache_string(text)))
wenzelm@44704
   154
          }
wenzelm@44704
   155
      }
wenzelm@44704
   156
    private def _cache_body(x: XML.Body): XML.Body =
wenzelm@44704
   157
      if (x.isEmpty) x
wenzelm@44704
   158
      else
wenzelm@34133
   159
        lookup(x) match {
wenzelm@34133
   160
          case Some(y) => y
wenzelm@44704
   161
          case None => x.map(_cache_tree(_))
wenzelm@34133
   162
        }
wenzelm@38446
   163
wenzelm@38446
   164
    // main methods
wenzelm@44705
   165
    def cache_string(x: String): String = synchronized { _cache_string(x) }
wenzelm@44705
   166
    def cache_markup(x: Markup): Markup = synchronized { _cache_markup(x) }
wenzelm@44705
   167
    def cache_tree(x: XML.Tree): XML.Tree = synchronized { _cache_tree(x) }
wenzelm@44705
   168
    def cache_body(x: XML.Body): XML.Body = synchronized { _cache_body(x) }
wenzelm@34108
   169
  }
wenzelm@34108
   170
wenzelm@34108
   171
wenzelm@43767
   172
wenzelm@43767
   173
  /** XML as data representation language **/
wenzelm@43767
   174
wenzelm@43767
   175
  class XML_Atom(s: String) extends Exception(s)
wenzelm@43767
   176
  class XML_Body(body: XML.Body) extends Exception
wenzelm@43767
   177
wenzelm@43767
   178
  object Encode
wenzelm@43767
   179
  {
wenzelm@43767
   180
    type T[A] = A => XML.Body
wenzelm@43767
   181
wenzelm@43767
   182
wenzelm@43778
   183
    /* atomic values */
wenzelm@43767
   184
wenzelm@43778
   185
    def long_atom(i: Long): String = i.toString
wenzelm@43767
   186
wenzelm@43778
   187
    def int_atom(i: Int): String = i.toString
wenzelm@43767
   188
wenzelm@43778
   189
    def bool_atom(b: Boolean): String = if (b) "1" else "0"
wenzelm@43767
   190
wenzelm@43778
   191
    def unit_atom(u: Unit) = ""
wenzelm@43767
   192
wenzelm@43767
   193
wenzelm@43767
   194
    /* structural nodes */
wenzelm@43767
   195
wenzelm@43767
   196
    private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts)
wenzelm@43767
   197
wenzelm@43781
   198
    private def vector(xs: List[String]): XML.Attributes =
wenzelm@46839
   199
      xs.zipWithIndex.map({ case (x, i) => (int_atom(i), x) })
wenzelm@43778
   200
wenzelm@43778
   201
    private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree =
wenzelm@43778
   202
      XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2)
wenzelm@43767
   203
wenzelm@43767
   204
wenzelm@43767
   205
    /* representation of standard types */
wenzelm@43767
   206
wenzelm@43780
   207
    val properties: T[Properties.T] =
wenzelm@43767
   208
      (props => List(XML.Elem(Markup(":", props), Nil)))
wenzelm@43767
   209
wenzelm@43767
   210
    val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s)))
wenzelm@43767
   211
wenzelm@43767
   212
    val long: T[Long] = (x => string(long_atom(x)))
wenzelm@43767
   213
wenzelm@43767
   214
    val int: T[Int] = (x => string(int_atom(x)))
wenzelm@43767
   215
wenzelm@43767
   216
    val bool: T[Boolean] = (x => string(bool_atom(x)))
wenzelm@43767
   217
wenzelm@43767
   218
    val unit: T[Unit] = (x => string(unit_atom(x)))
wenzelm@43767
   219
wenzelm@43767
   220
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   221
      (x => List(node(f(x._1)), node(g(x._2))))
wenzelm@43767
   222
wenzelm@43767
   223
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   224
      (x => List(node(f(x._1)), node(g(x._2)), node(h(x._3))))
wenzelm@43767
   225
wenzelm@43767
   226
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   227
      (xs => xs.map((x: A) => node(f(x))))
wenzelm@43767
   228
wenzelm@43767
   229
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   230
    {
wenzelm@43767
   231
      case None => Nil
wenzelm@43767
   232
      case Some(x) => List(node(f(x)))
wenzelm@43767
   233
    }
wenzelm@43767
   234
wenzelm@43778
   235
    def variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] =
wenzelm@43767
   236
    {
wenzelm@43767
   237
      case x =>
wenzelm@43767
   238
        val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get
wenzelm@43767
   239
        List(tagged(tag, f(x)))
wenzelm@43767
   240
    }
wenzelm@43767
   241
  }
wenzelm@43767
   242
wenzelm@43767
   243
  object Decode
wenzelm@43767
   244
  {
wenzelm@43767
   245
    type T[A] = XML.Body => A
wenzelm@43778
   246
    type V[A] = (List[String], XML.Body) => A
wenzelm@43767
   247
wenzelm@43767
   248
wenzelm@43778
   249
    /* atomic values */
wenzelm@43767
   250
wenzelm@43778
   251
    def long_atom(s: String): Long =
wenzelm@43767
   252
      try { java.lang.Long.parseLong(s) }
wenzelm@43767
   253
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   254
wenzelm@43778
   255
    def int_atom(s: String): Int =
wenzelm@43767
   256
      try { Integer.parseInt(s) }
wenzelm@43767
   257
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
wenzelm@43767
   258
wenzelm@43778
   259
    def bool_atom(s: String): Boolean =
wenzelm@43767
   260
      if (s == "1") true
wenzelm@43767
   261
      else if (s == "0") false
wenzelm@43767
   262
      else throw new XML_Atom(s)
wenzelm@43767
   263
wenzelm@43778
   264
    def unit_atom(s: String): Unit =
wenzelm@43767
   265
      if (s == "") () else throw new XML_Atom(s)
wenzelm@43767
   266
wenzelm@43767
   267
wenzelm@43767
   268
    /* structural nodes */
wenzelm@43767
   269
wenzelm@43767
   270
    private def node(t: XML.Tree): XML.Body =
wenzelm@43767
   271
      t match {
wenzelm@43767
   272
        case XML.Elem(Markup(":", Nil), ts) => ts
wenzelm@43767
   273
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   274
      }
wenzelm@43767
   275
wenzelm@43781
   276
    private def vector(atts: XML.Attributes): List[String] =
wenzelm@46839
   277
      atts.iterator.zipWithIndex.map(
wenzelm@46839
   278
        { case ((a, x), i) => if (int_atom(a) == i) x else throw new XML_Atom(a) }).toList
wenzelm@43778
   279
wenzelm@43778
   280
    private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) =
wenzelm@43767
   281
      t match {
wenzelm@43781
   282
        case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts))
wenzelm@43767
   283
        case _ => throw new XML_Body(List(t))
wenzelm@43767
   284
      }
wenzelm@43767
   285
wenzelm@43767
   286
wenzelm@43767
   287
    /* representation of standard types */
wenzelm@43767
   288
wenzelm@43780
   289
    val properties: T[Properties.T] =
wenzelm@43767
   290
    {
wenzelm@43767
   291
      case List(XML.Elem(Markup(":", props), Nil)) => props
wenzelm@43767
   292
      case ts => throw new XML_Body(ts)
wenzelm@43767
   293
    }
wenzelm@43767
   294
wenzelm@43767
   295
    val string: T[String] =
wenzelm@43767
   296
    {
wenzelm@43767
   297
      case Nil => ""
wenzelm@43767
   298
      case List(XML.Text(s)) => s
wenzelm@43767
   299
      case ts => throw new XML_Body(ts)
wenzelm@43767
   300
    }
wenzelm@43767
   301
wenzelm@43767
   302
    val long: T[Long] = (x => long_atom(string(x)))
wenzelm@43767
   303
wenzelm@43767
   304
    val int: T[Int] = (x => int_atom(string(x)))
wenzelm@43767
   305
wenzelm@43767
   306
    val bool: T[Boolean] = (x => bool_atom(string(x)))
wenzelm@43767
   307
wenzelm@43767
   308
    val unit: T[Unit] = (x => unit_atom(string(x)))
wenzelm@43767
   309
wenzelm@43767
   310
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
wenzelm@43767
   311
    {
wenzelm@43767
   312
      case List(t1, t2) => (f(node(t1)), g(node(t2)))
wenzelm@43767
   313
      case ts => throw new XML_Body(ts)
wenzelm@43767
   314
    }
wenzelm@43767
   315
wenzelm@43767
   316
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
wenzelm@43767
   317
    {
wenzelm@43767
   318
      case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3)))
wenzelm@43767
   319
      case ts => throw new XML_Body(ts)
wenzelm@43767
   320
    }
wenzelm@43767
   321
wenzelm@43767
   322
    def list[A](f: T[A]): T[List[A]] =
wenzelm@43767
   323
      (ts => ts.map(t => f(node(t))))
wenzelm@43767
   324
wenzelm@43767
   325
    def option[A](f: T[A]): T[Option[A]] =
wenzelm@43767
   326
    {
wenzelm@43767
   327
      case Nil => None
wenzelm@43767
   328
      case List(t) => Some(f(node(t)))
wenzelm@43767
   329
      case ts => throw new XML_Body(ts)
wenzelm@43767
   330
    }
wenzelm@43767
   331
wenzelm@43778
   332
    def variant[A](fs: List[V[A]]): T[A] =
wenzelm@43767
   333
    {
wenzelm@43767
   334
      case List(t) =>
wenzelm@43778
   335
        val (tag, (xs, ts)) = tagged(t)
wenzelm@43768
   336
        val f =
wenzelm@43768
   337
          try { fs(tag) }
wenzelm@43768
   338
          catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) }
wenzelm@43778
   339
        f(xs, ts)
wenzelm@43767
   340
      case ts => throw new XML_Body(ts)
wenzelm@43767
   341
    }
wenzelm@43767
   342
  }
wenzelm@27931
   343
}