Skip to content

Commit

Permalink
[scala] Implicitly search for NamespaceContext in xpath macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 committed Mar 20, 2024
1 parent 4087951 commit fdf98d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.github.simy4.xpath.expr.Expr
import com.github.simy4.xpath.parser.XPathParser
import reflect.macros.blackbox

import javax.xml.namespace.NamespaceContext
import javax.xml.xpath.XPathExpressionException

final class XPathLiteral(private val sc: StringContext) extends AnyVal {
Expand All @@ -28,13 +29,14 @@ final class XPathLiteral(private val sc: StringContext) extends AnyVal {
@SuppressWarnings(Array("org.wartremover.warts.Null"))
object XPathLiteral {
def xpathImpl(c: blackbox.Context)(args: c.Expr[Any]*): c.Expr[Expr] = {
import c.universe._
import c.universe.*

c.prefix.tree match {
case Apply(_, List(Apply(_, List(lit @ Literal(Constant(str: String)))))) =>
try {
val _ = new XPathParser(null).parse(str)
reify(new XPathParser(null).parse(c.Expr[String](lit).splice))
val namespaceContext = c.inferImplicitValue(weakTypeOf[NamespaceContext], silent = true).orElse(q"null")
val _ = new XPathParser(null).parse(str)
reify(new XPathParser(c.Expr[NamespaceContext](namespaceContext).splice).parse(c.Expr[String](lit).splice))
} catch {
case xpee: XPathExpressionException =>
c.abort(c.enclosingPosition, s"Illegal XPath expression: ${xpee.getMessage}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package com.github.simy4.xpath.scala.xpath
import com.github.simy4.xpath.expr.Expr as JExpr
import com.github.simy4.xpath.parser.XPathParser

import javax.xml.namespace.NamespaceContext
import javax.xml.xpath.XPathExpressionException

import scala.quoted.{ quotes, Expr, Exprs, Quotes, Varargs }
import scala.quoted.{ Expr, Exprs, Quotes, Varargs, quotes }

object XPathLiteral:
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.Null", "org.wartremover.warts.IterableOps"))
Expand All @@ -30,8 +30,9 @@ object XPathLiteral:
case '{ StringContext(${ Varargs(Exprs(args)) }*) } if args.size == 1 =>
try {
val const = args.head
val namespaceContext = Expr.summon[NamespaceContext].getOrElse('{null})
val _ = new XPathParser(null).parse(const)
'{ new XPathParser(null).parse(${ Expr(const) }) }
'{ new XPathParser(${ namespaceContext }).parse(${ Expr(const) }) }
} catch {
case xpee: XPathExpressionException =>
report.errorAndAbort(s"Illegal XPath expression: ${xpee.getMessage}", sc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import parser.XPathParser

@SuppressWarnings(Array("org.wartremover.warts.Null"))
class XPathLiteralSpec extends AnyFunSpec with Matchers {
import scala.implicits._
import scala.implicits.*

describe("xpathLiteral") {
it("should compile")(assertCompiles("""val expr = xpath"ancestor::author[parent::book][1]""""))
Expand Down

0 comments on commit fdf98d9

Please sign in to comment.