src/Pure/General/yxml.scala
author wenzelm
Sun, 17 Aug 2008 21:11:06 +0200
changeset 27930 2b44df907cc2
child 27943 f34ff5e7728f
permissions -rw-r--r--
Efficient text representation of XML trees.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/yxml.scala
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     4
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     5
Efficient text representation of XML trees.
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     6
*/
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     7
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     8
package isabelle
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     9
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    10
import java.util.regex.Pattern
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    11
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    12
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    13
object YXML {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    14
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    15
  /* markers */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    16
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    17
  private val X = '\5'
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    18
  private val Y = '\6'
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    19
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    20
  def detect(source: CharSequence) = {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    21
    source.length >= 2 &&
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    22
    source.charAt(0) == X &&
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    23
    source.charAt(1) == Y
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    24
  }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    25
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    26
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    27
  /* parsing */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    28
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    29
  class BadYXML(msg: String) extends Exception
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    30
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    31
  private def err(msg: String) = throw new BadYXML(msg)
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    32
  private def err_attribute() = err("bad attribute")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    33
  private def err_element() = err("bad element")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    34
  private def err_unbalanced(name: String) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    35
    if (name == "") err("unbalanced element")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    36
    else err("unbalanced element \"" + name + "\"")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    37
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    38
  private val X_pattern = Pattern.compile(Pattern.quote(X.toString))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    39
  private val Y_pattern = Pattern.compile(Pattern.quote(Y.toString))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    40
  private val eq_pattern = Pattern.compile(Pattern.quote("="))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    41
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    42
  private def parse_attrib(s: String) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    43
    eq_pattern.split(s, 2).toList match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    44
      case List(x, y) => if (x != "") (x, y) else err_attribute()
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    45
      case _ => err_attribute()
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    46
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    47
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    48
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    49
  def parse_body(source: CharSequence) = {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    50
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    51
    /* stack operations */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    52
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    53
    var stack: List[((String, XML.Attributes), List[XML.Tree])] = null
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    54
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    55
    def add(x: XML.Tree) = stack match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    56
      case ((elem, body) :: pending) => stack = (elem, x :: body) :: pending
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    57
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    58
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    59
    def push(name: String, atts: XML.Attributes) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    60
      if (name == "") err_element()
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    61
      else stack = ((name, atts), Nil) :: stack
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    62
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    63
    def pop() = stack match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    64
      case ((("", _), _) :: _) => err_unbalanced("")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    65
      case (((name, atts), body) :: pending) =>
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    66
        stack = pending; add(XML.Elem(name, atts, body.reverse))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    67
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    68
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    69
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    70
    /* parse chunks */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    71
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    72
    stack = List((("", Nil), Nil))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    73
    for (chunk <- X_pattern.split(source) if chunk != "") {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    74
      Y_pattern.split(chunk).toList match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    75
        case Nil => pop()
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    76
        case "" :: name :: atts => push(name, atts.map(parse_attrib))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    77
        case txts => for (txt <- txts) add(XML.Text(txt))
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    78
      }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    79
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    80
    stack match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    81
      case List((("", _), result)) => result.reverse
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    82
      case ((name, _), _) :: _ => err_unbalanced(name)
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    83
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    84
  }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    85
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    86
  def parse(source: CharSequence) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    87
    parse_body(source) match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    88
      case List(result) => result
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    89
      case Nil => XML.Text("")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    90
      case _ => err("multiple results")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    91
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    92
}