43780
|
1 |
/* Title: Pure/General/properties.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Property lists.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Properties
|
|
11 |
{
|
65624
|
12 |
/* entries */
|
|
13 |
|
43780
|
14 |
type Entry = (java.lang.String, java.lang.String)
|
|
15 |
type T = List[Entry]
|
|
16 |
|
64358
|
17 |
def defined(props: T, name: java.lang.String): java.lang.Boolean =
|
|
18 |
props.exists({ case (x, _) => x == name })
|
|
19 |
|
|
20 |
def get(props: T, name: java.lang.String): Option[java.lang.String] =
|
|
21 |
props.collectFirst({ case (x, y) if x == name => y })
|
|
22 |
|
|
23 |
def put(props: T, entry: Entry): T =
|
|
24 |
{
|
|
25 |
val (x, y) = entry
|
|
26 |
def update(ps: T): T =
|
|
27 |
ps match {
|
|
28 |
case (p @ (x1, _)) :: rest =>
|
|
29 |
if (x1 == x) (x1, y) :: rest else p :: update(rest)
|
|
30 |
case Nil => Nil
|
|
31 |
}
|
|
32 |
if (defined(props, x)) update(props) else entry :: props
|
|
33 |
}
|
|
34 |
|
|
35 |
|
65624
|
36 |
/* multi-line entries */
|
|
37 |
|
|
38 |
val separator = '\u000b'
|
|
39 |
|
|
40 |
def encode_lines(s: java.lang.String): java.lang.String = s.replace('\n', separator)
|
|
41 |
def decode_lines(s: java.lang.String): java.lang.String = s.replace(separator, '\n')
|
|
42 |
|
|
43 |
def encode_lines(props: T): T = props.map({ case (a, b) => (a, encode_lines(b)) })
|
|
44 |
def decode_lines(props: T): T = props.map({ case (a, b) => (a, decode_lines(b)) })
|
|
45 |
|
|
46 |
def lines_nonempty(x: java.lang.String, ys: List[java.lang.String]): Properties.T =
|
|
47 |
if (ys.isEmpty) Nil else List((x, cat_lines(ys)))
|
|
48 |
|
|
49 |
|
|
50 |
/* entry types */
|
64358
|
51 |
|
43780
|
52 |
class String(val name: java.lang.String)
|
|
53 |
{
|
|
54 |
def apply(value: java.lang.String): T = List((name, value))
|
|
55 |
def unapply(props: T): Option[java.lang.String] =
|
|
56 |
props.find(_._1 == name).map(_._2)
|
|
57 |
}
|
|
58 |
|
48344
|
59 |
class Boolean(val name: java.lang.String)
|
|
60 |
{
|
|
61 |
def apply(value: scala.Boolean): T = List((name, Value.Boolean(value)))
|
|
62 |
def unapply(props: T): Option[scala.Boolean] =
|
|
63 |
props.find(_._1 == name) match {
|
|
64 |
case None => None
|
|
65 |
case Some((_, value)) => Value.Boolean.unapply(value)
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
48015
|
69 |
class Int(val name: java.lang.String)
|
43780
|
70 |
{
|
|
71 |
def apply(value: scala.Int): T = List((name, Value.Int(value)))
|
|
72 |
def unapply(props: T): Option[scala.Int] =
|
|
73 |
props.find(_._1 == name) match {
|
|
74 |
case None => None
|
|
75 |
case Some((_, value)) => Value.Int.unapply(value)
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
48015
|
79 |
class Long(val name: java.lang.String)
|
43780
|
80 |
{
|
|
81 |
def apply(value: scala.Long): T = List((name, Value.Long(value)))
|
|
82 |
def unapply(props: T): Option[scala.Long] =
|
|
83 |
props.find(_._1 == name) match {
|
|
84 |
case None => None
|
|
85 |
case Some((_, value)) => Value.Long.unapply(value)
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
48015
|
89 |
class Double(val name: java.lang.String)
|
43780
|
90 |
{
|
|
91 |
def apply(value: scala.Double): T = List((name, Value.Double(value)))
|
|
92 |
def unapply(props: T): Option[scala.Double] =
|
|
93 |
props.find(_._1 == name) match {
|
|
94 |
case None => None
|
|
95 |
case Some((_, value)) => Value.Double.unapply(value)
|
|
96 |
}
|
|
97 |
}
|
65592
|
98 |
|
|
99 |
|
|
100 |
/* cached store */
|
|
101 |
|
|
102 |
trait Store
|
|
103 |
{
|
|
104 |
val xml_cache: XML.Cache = new XML.Cache()
|
|
105 |
|
|
106 |
def encode_properties(ps: T): Bytes =
|
|
107 |
Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
|
|
108 |
|
|
109 |
def decode_properties(bs: Bytes): T =
|
|
110 |
xml_cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
|
|
111 |
|
|
112 |
def compress_properties(ps: List[T], options: XZ.Options = XZ.options()): Bytes =
|
|
113 |
{
|
|
114 |
if (ps.isEmpty) Bytes.empty
|
|
115 |
else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
|
|
116 |
}
|
|
117 |
|
|
118 |
def uncompress_properties(bs: Bytes): List[T] =
|
|
119 |
{
|
|
120 |
if (bs.isEmpty) Nil
|
|
121 |
else
|
|
122 |
XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text)).
|
|
123 |
map(xml_cache.props(_))
|
|
124 |
}
|
|
125 |
}
|
43780
|
126 |
}
|