-
Notifications
You must be signed in to change notification settings - Fork 108
/
XmlExtractor.scala
291 lines (242 loc) · 9.83 KB
/
XmlExtractor.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
The MIT License (MIT)
Copyright (c) 2016 Yang Bo & REA Group Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.thoughtworks.binding
import macrocompat.bundle
import scala.reflect.macros.blackbox
import com.thoughtworks.Extractor._
import com.thoughtworks.binding.XmlExtractor._
import org.apache.commons.lang3.text.translate.EntityArrays._
/**
* @author 杨博 (Yang Bo) <pop.atry@gmail.com>
*/
@bundle
trait XmlExtractor {
val c: blackbox.Context
import c.universe._
private def nodeBuffer: PartialFunction[Tree, List[Tree]] = {
case q"""{
val $$buf = new _root_.scala.xml.NodeBuffer()
..$pushChildrenTree
$$buf
}""" =>
for {
pushChild <- pushChildrenTree
} yield {
val q"$$buf.$$amp$$plus($child)" = pushChild
child
}
}
protected final val NodeBuffer = nodeBuffer.extract
private def nodeBufferStar(child: List[Tree]): List[Tree] = {
child match {
case Nil =>
Nil
case List(q"""${NodeBuffer(children)}: _*""") =>
children
}
}
private def prefix: PartialFunction[Tree, Option[String]] = {
case q"null" => None
case Literal(Constant(p: String)) => Some(p)
}
private val Prefix = prefix.extract
private def elem: PartialFunction[Tree, (QName, List[(QName, Tree)], Boolean, List[Tree])] = {
case Block(Nil,
q"""
{
var $$md: _root_.scala.xml.MetaData = _root_.scala.xml.Null;
..$attributes
new _root_.scala.xml.Elem(
${Prefix(prefixOption)},
${Literal(Constant(localPart: String))},
$$md, $$scope,
${Literal(Constant(minimizeEmpty: Boolean))},
..$child
)
}
""") =>
(QName(prefixOption, localPart), attributes.view.reverse.map {
case q"""$$md = new _root_.scala.xml.UnprefixedAttribute(${Literal(Constant(key: String))}, $value, $$md)""" =>
UnprefixedName(key) -> value
case q"""$$md = new _root_.scala.xml.PrefixedAttribute(${Literal(Constant(pre: String))}, ${Literal(
Constant(key: String))}, $value, $$md)""" =>
PrefixedName(pre, key) -> value
}.toList, minimizeEmpty, nodeBufferStar(child))
case Block(Nil,
Block(
Nil,
q"""
new _root_.scala.xml.Elem(
${Prefix(prefixOption)},
${Literal(Constant(localPart: String))},
_root_.scala.xml.Null,
$$scope,
${Literal(Constant(minimizeEmpty: Boolean))},
..$child
)
"""
)) =>
(QName(prefixOption, localPart), Nil, minimizeEmpty, nodeBufferStar(child))
}
@deprecated("This [[Elem]] extractor does not support xmlns. Use [[Element]] instead.", "11.9.0")
protected final val Elem = elem.extract
private def elemWithMetaData: PartialFunction[List[Tree], (QName, List[(QName, Tree)], Boolean, List[Tree])] = {
case q"var $$md: _root_.scala.xml.MetaData = _root_.scala.xml.Null" +:
(attributes :+
q"""
new _root_.scala.xml.Elem(
${Prefix(prefixOption)},
${Literal(Constant(localPart: String))},
$$md, $$scope,
${Literal(Constant(minimizeEmpty: Boolean))},
..$child
)
""") =>
(QName(prefixOption, localPart), attributes.map {
case q"""$$md = new _root_.scala.xml.UnprefixedAttribute(${Literal(Constant(key: String))}, $value, $$md)""" =>
UnprefixedName(key) -> value
case q"""$$md = new _root_.scala.xml.PrefixedAttribute(${Literal(Constant(pre: String))}, ${Literal(
Constant(key: String))}, $value, $$md)""" =>
PrefixedName(pre, key) -> value
}, minimizeEmpty, nodeBufferStar(child))
case Seq(
q"""
new _root_.scala.xml.Elem(
${Prefix(prefixOption)},
${Literal(Constant(localPart: String))},
_root_.scala.xml.Null,
$$scope,
${Literal(Constant(minimizeEmpty: Boolean))},
..$child
)
"""
) =>
(QName(prefixOption, localPart), Nil, minimizeEmpty, nodeBufferStar(child))
}
private val ElemWithMetaData = elemWithMetaData.extract
private def element
: PartialFunction[Tree,
(QName, List[(Option[String] /*prefix*/, Tree)], List[(QName, Tree)], Boolean, List[Tree])] = {
case q"""{
var $$tmpscope: _root_.scala.xml.NamespaceBinding = $outerScope;
..$xmlnses;
{
val $$scope: _root_.scala.xml.NamespaceBinding = $$tmpscope;
..${ElemWithMetaData(tagName, attributes, minimizeEmpty, children)}
}
}""" =>
val namespaceBindings = xmlnses.map {
case q"$tmpscope = new _root_.scala.xml.NamespaceBinding($prefixOrNull, $uri, $$tmpscope);" =>
val prefixOption = prefixOrNull match {
case q"null" =>
None
case Literal(Constant(prefix: String)) =>
Some(prefix)
}
prefixOption -> uri
}
(tagName, namespaceBindings, attributes, minimizeEmpty, children)
case Block(Nil, q"{..${ElemWithMetaData(tagName, attributes, minimizeEmpty, children)}}") =>
(tagName, Nil, attributes, minimizeEmpty, children)
}
protected val Element = element.extract
private def textUris: PartialFunction[Tree, Seq[Tree]] = {
case text @ (Text(_) | EntityRef(_)) => Seq(text)
case q"null" => Nil
case NodeBuffer(texts @ ((Text(_) | EntityRef(_)) +: _)) => texts
case Literal(Constant(data: String)) => Seq(q"new _root_.scala.xml.Text($data)")
}
protected final val TextUris = textUris.extract
private def entityRef: PartialFunction[Tree, String] = {
case q"""new _root_.scala.xml.EntityRef(${Literal(Constant(entityName: String))})""" =>
entityName
}
protected final val EntityRef = entityRef.extract
private def text: PartialFunction[Tree, String] = {
case q"""new _root_.scala.xml.Text(${Literal(Constant(data: String))})""" =>
data
}
protected final val Text = text.extract
private def textAttribute: PartialFunction[Tree, String] = {
case Text(data) => data
case EmptyAttribute() => ""
}
@deprecated("Use [[TextAttributes]] instead", "11.9.0")
protected final val TextAttribute = textAttribute.extract
private def textAttributes: PartialFunction[Tree, Seq[Tree]] = {
case text @ (Text(_) | EntityRef(_)) => Seq(text)
case EmptyAttribute() => Nil
case NodeBuffer(texts @ ((Text(_) | EntityRef(_)) +: _)) => texts
}
protected final val TextAttributes = textAttributes.extract
private def comment: PartialFunction[Tree, String] = {
case q"""new _root_.scala.xml.Comment(${Literal(Constant(commentText: String))})""" =>
commentText
}
protected final val Comment = comment.extract
private def procInstr: PartialFunction[Tree, (String, String)] = {
case q"""
new _root_.scala.xml.ProcInstr(
${Literal(Constant(target: String))},
${Literal(Constant(proctext: String))}
)
""" =>
(target, proctext)
}
protected final val ProcInstr = procInstr.extract
private def pcData: PartialFunction[Tree, String] = {
case q"""
new _root_.scala.xml.PCData(
${Literal(Constant(data: String))}
)
""" =>
data
}
protected final val PCData = pcData.extract
protected final val HtmlEntityName = XmlExtractor.HtmlEntityRefMap.extract
protected final val XmlEntityName = XmlExtractor.XmlEntityRefMap.extract
protected object EmptyAttribute {
def unapply(tree: Tree) = {
val tpe = tree.tpe
tpe != null && tpe =:= typeOf[Nil.type]
}
}
}
object XmlExtractor {
sealed trait QName
object QName {
def apply(prefixOption: Option[String], localPart: String) = {
prefixOption match {
case None => UnprefixedName(localPart)
case Some(prefix) => PrefixedName(prefix, localPart)
}
}
}
final case class UnprefixedName(localPart: String) extends QName
final case class PrefixedName(prefix: String, localPart: String) extends QName
private val EntityRefRegex = "&(.*);".r
private val XmlEntityRefMap = (for {
Array(character, EntityRefRegex(reference)) <- BASIC_ESCAPE.view
} yield reference -> character).toMap
private val HtmlEntityRefMap = (for {
entityArray <- Seq(BASIC_ESCAPE, ISO8859_1_ESCAPE, HTML40_EXTENDED_ESCAPE).view
Array(character, EntityRefRegex(reference)) <- entityArray.view
} yield reference -> character).toMap
}