Skip to content

Commit

Permalink
Merge pull request #207 from Atry/xmlns
Browse files Browse the repository at this point in the history
Support xmlns
  • Loading branch information
Atry committed Sep 1, 2019
2 parents 011e9dd + 661dfa0 commit 746fc09
Showing 1 changed file with 74 additions and 1 deletion.
Expand Up @@ -111,8 +111,82 @@ trait XmlExtractor {
(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
Expand Down Expand Up @@ -177,7 +251,6 @@ trait XmlExtractor {

protected final val XmlEntityName = XmlExtractor.XmlEntityRefMap.extract


protected object EmptyAttribute {
def unapply(tree: Tree) = {
val tpe = tree.tpe
Expand Down

0 comments on commit 746fc09

Please sign in to comment.