本文整理汇总了Scala中scala.xml.transform.RuleTransformer类的典型用法代码示例。如果您正苦于以下问题:Scala RuleTransformer类的具体用法?Scala RuleTransformer怎么用?Scala RuleTransformer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RuleTransformer类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: InlineDsl
//设置package包名称以及导入依赖的类
import scala.xml._
import scala.xml.transform.{RewriteRule, RuleTransformer}
object InlineDsl {
def main(args: Array[String]): Unit = {
val xml = <root>
<a action="remove">element1</a> <a action="edit">element2</a>
</root>
val l = (xml \\ "a").map(_.text)
println(l)//List(element1, element2)
val removeIt = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case e: Elem if (e \ "@action").text == "remove" => NodeSeq.Empty
case `n` => n
}
}
val newXml = new RuleTransformer(removeIt).transform(xml)
println(newXml)
// <root><a action="edit">simple</a></root>
}
}
示例2: Helpers
//设置package包名称以及导入依赖的类
import sbt._
import sbt.Keys._
import scala.xml.{Comment, Elem, Node, NodeSeq}
import scala.xml.transform.{RewriteRule, RuleTransformer}
object Helpers {
def dropIfDependency(node: Node): Node = {
new RuleTransformer(new RewriteRule {
override def transform(node: Node): NodeSeq = node match {
case e: Elem if e.label == "dependency" => NodeSeq.Empty
case _ => node
}
}).transform(node).head
}
def pickFilesFromSubpaths(
roots: Seq[String],
subpath: File,
predicate: String => Boolean): Seq[(File, String)] = {
roots
.map(file(_) / subpath.toString)
.flatMap({ f =>
Path
.allSubpaths(f)
.filter({ case (_, relativePath) => predicate(relativePath) })
.toSeq
})
}
}
示例3: ExerciseNine
//设置package包名称以及导入依赖的类
package chapter.sixteen
import scala.xml.transform.{ RuleTransformer, RewriteRule }
import scala.xml.{ Attribute, Elem, Node, Null }
object ExerciseNine {
def setDefaultImgAlt(input: Node, value: String): Seq[Node] = {
val rule = new RewriteRule {
override def transform(n: Node) = n match {
case img @ <img /> if img \ "@alt" isEmpty => img.asInstanceOf[Elem] % Attribute(null, "alt", value, Null)
case _ => n
}
}
new RuleTransformer(rule).transform(input)
}
}
示例4: Exercise10
//设置package包名称以及导入依赖的类
package forimpatient.chapter16
import scala.xml._
import scala.xml.dtd.{DocType, PublicID}
import scala.xml.transform.{RewriteRule, RuleTransformer}
object Exercise10 extends App {
println("Chapter 16 Exercise 10")
val doc = <html><head><title/></head><body><div><img src="scala.jpg" alt="Scala logo"/><img
src="python.jpg"/><img src="java.jpg" alt="Java logo"/><img src="cpp.jpg"/></div><div><script type="text/javascript">{
PCData("""if (temp < 0) alert("Cold!")""")}</script></div></body></html>
addTODOs(doc)
def addTODOs(xml: Node) = {
val rule = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case im @ <img/> if im.attribute("alt").isEmpty => im.asInstanceOf[Elem] % Attribute(null, "alt", "TODO", Null)
case _ => n
}
}
val withTODO = new RuleTransformer(rule).apply(xml)
val filePath = "src/forimpatient/chapter16/doc.xhtml"
XML.save(filePath, withTODO, enc = "UTF-8", xmlDecl = true, doctype =
DocType("html", PublicID("-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
Nil))
}
}
示例5: Exercise09
//设置package包名称以及导入依赖的类
package forimpatient.chapter16
import scala.xml.{Attribute, Elem, Node, Null}
import scala.xml.transform.{RewriteRule, RuleTransformer}
object Exercise09 extends App {
println("Chapter 16 Exercise 09")
val doc = <html><body><img src="scala.jpg" alt="Scala logo"/><img
src="python.jpg"/><img src="java.jpg" alt="Java logo"/><img src="cpp.jpg"/></body></html>
val rule = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case im @ <img/> if im.attribute("alt").isEmpty => im.asInstanceOf[Elem] % Attribute(null, "alt", "TODO", Null)
case _ => n
}
}
val docWithTODO = new RuleTransformer(rule).transform(doc)
println(doc)
println(docWithTODO)
}