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