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