-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
XmlEvent.scala
179 lines (152 loc) · 6.46 KB
/
XmlEvent.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2024 fs2-data Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fs2
package data
package xml
import cats.Show
import cats.syntax.all._
import fs2.Pure
import fs2.data.text.render.{DocEvent, Renderable, Renderer}
import scala.annotation.switch
sealed trait XmlEvent
object XmlEvent {
sealed trait XmlTexty extends XmlEvent {
def render: String
}
case object StartDocument extends XmlEvent
case class XmlDecl(version: String, encoding: Option[String], standalone: Option[Boolean]) extends XmlEvent
case class StartTag(name: QName, attributes: List[Attr], isEmpty: Boolean) extends XmlEvent {
def render(collapseEmpty: Boolean): String = {
val end = if (collapseEmpty && isEmpty) "/>" else ">"
val attrs = attributes.foldMap[String] { case Attr(n, v) => show""" $n="${v.foldMap[String](_.render)}"""" }
show"""<$name$attrs$end"""
}
}
case class XmlCharRef(value: Int) extends XmlTexty {
def render = f"&#x$value%04x;"
}
case class XmlEntityRef(name: String) extends XmlTexty {
def render = s"&$name;"
}
case class XmlString(s: String, isCDATA: Boolean) extends XmlTexty {
def render = if (isCDATA) show"<![CDATA[$s]]>" else s
}
case class XmlPI(target: String, content: String) extends XmlEvent
case class XmlDoctype(name: String, docname: String, systemid: Option[String]) extends XmlEvent
case class EndTag(name: QName) extends XmlEvent
case object EndDocument extends XmlEvent
case class Comment(comment: String) extends XmlEvent
implicit val show: Show[XmlEvent] = Show.show {
case t: XmlTexty => t.render
case s: StartTag => s.render(false)
case EndTag(n) => show"</$n>"
case Comment(content) => s"<!--$content-->"
case XmlDecl(version, encoding, standalone) =>
s"""<?xml version="$version"${encoding.foldMap(e => s""" encoding="$e"""")}${standalone.foldMap {
case true => s""" standalone="yes""""
case false => s""" standalone="no""""
}}?>"""
case XmlPI(target, content) => s"<?$target $content?>"
case _ => ""
}
implicit object renderable extends Renderable[XmlEvent] {
private final val TOP = 0
private final val TEXT = 1
private final val OTHER = 2
override def newRenderer(): Renderer[XmlEvent] = new Renderer[XmlEvent] {
private var state = TOP
private var skipClose = false
private def prefix(st: Int, close: Boolean = false): Stream[Pure, DocEvent] =
(state: @switch) match {
case TOP =>
state = st
Stream.empty
case TEXT if st == TEXT =>
state = st
softline
case _ =>
state = st
if (close)
Stream.emit(DocEvent.GroupEnd) ++ unindent ++ linebreak
else
linebreak
}
override def doc(evt: XmlEvent): Stream[Pure, DocEvent] =
evt match {
case StartDocument =>
Stream.empty
case XmlDecl(version, encoding, standalone) =>
val v = DocEvent.Text(s"""version="$version"""")
val e = encoding.fold(Stream.empty.covaryOutput[DocEvent])(e =>
Stream.emits(DocEvent.Line :: DocEvent.Text(s"""encoding="$e"""") :: Nil))
val s = standalone.fold(Stream.empty.covaryOutput[DocEvent]) { s =>
val bool = if (s) "yes" else "no"
Stream.emits(DocEvent.Line :: DocEvent.Text(s"""standalone="$bool"""") :: Nil)
}
prefix(OTHER) ++ Stream.emits(
DocEvent.GroupBegin :: DocEvent.Text("<?xml ") :: DocEvent.AlignBegin :: v :: Nil) ++ e ++ s ++ Stream
.emits(DocEvent.AlignEnd :: DocEvent.Text("?>") :: DocEvent.GroupEnd :: Nil)
case StartTag(name, Nil, true) =>
skipClose = true
prefix(OTHER) ++ Stream.emit(DocEvent.Text(show"<$name />"))
case StartTag(name, Nil, false) =>
skipClose = false
prefix(OTHER) ++ Stream.emits(
DocEvent.GroupBegin :: DocEvent.Text(show"<$name>") :: DocEvent.IndentBegin :: DocEvent.GroupBegin :: Nil)
case StartTag(name, as, isEmpty) =>
skipClose = isEmpty
val pfx =
if (isEmpty)
prefix(OTHER)
else
prefix(OTHER) ++ Stream.emits(DocEvent.GroupBegin :: Nil)
pfx ++ Stream.emits(
DocEvent.GroupBegin :: DocEvent.Text(show"<$name ") :: DocEvent.AlignBegin :: Nil) ++ Stream
.emits(as)
.map(a => DocEvent.Text(a.show))
.intersperse(DocEvent.Line) ++ Stream.emits(
DocEvent.AlignEnd :: DocEvent.Text(if (isEmpty) " />"
else ">") :: DocEvent.GroupEnd :: (if (isEmpty) Nil
else DocEvent.IndentBegin :: DocEvent.GroupBegin :: Nil))
case XmlString(s, false) =>
prefix(TEXT) ++ words(s)
case texty: XmlTexty =>
prefix(TEXT) ++ Stream.emit(DocEvent.Text(texty.render))
case XmlPI(_, _) =>
// TODO: add support for proper PI parsing so that we can render it properly
Stream.empty
case XmlDoctype(_, _, _) =>
// TODO: add support for proper Doctype parsing so that we can render it properly
Stream.empty
case EndTag(name) =>
val res =
if (skipClose)
Stream.empty
else
prefix(OTHER, true) ++ Stream.emits(DocEvent.Text(show"</$name>") :: DocEvent.GroupEnd :: Nil)
skipClose = false
res
case EndDocument =>
Stream.empty
case Comment(comment) =>
prefix(OTHER) ++
Stream.emits(DocEvent.GroupBegin :: DocEvent.Text("<!--") :: DocEvent.Line :: Nil) ++
words(comment) ++
Stream.emits(DocEvent.Line :: DocEvent.Text("-->") :: DocEvent.GroupEnd :: Nil)
}
}
}
}