本文整理汇总了Scala中scala.xml.transform.RewriteRule类的典型用法代码示例。如果您正苦于以下问题:Scala RewriteRule类的具体用法?Scala RewriteRule怎么用?Scala RewriteRule使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RewriteRule类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: Img2TxtProcessor
//设置package包名称以及导入依赖的类
package pl.mojepanstwo.sap.toakoma.processors
import java.io.{File, FileInputStream, InputStreamReader}
import org.springframework.batch.item.ItemProcessor
import pl.mojepanstwo.sap.toakoma._
import scala.xml.{Elem, Node, XML}
import scala.xml.transform.RewriteRule
import sys.process._
class Img2TxtProcessor extends ItemProcessor[Model, Model] {
override def process(item:Model): Model = {
item.linksHtml.foreach { case (key, dirPath) =>
try {
val xml = XML.load(new InputStreamReader(new FileInputStream(item.xmlPath(key)), "UTF-8"))
val changed = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case elem @ Elem(_, "img", _, _, child @ _*) => {
val src = elem.attribute("src").get
val imageFile = new File(dirPath + "/" + src)
val cmd = "tesseract " +
imageFile.getAbsolutePath + " " +
"stdout " +
"-l pol"
var result = cmd !!
if(Option(result).exists(_.trim.isEmpty)) return n
result = result.replaceAll("-\n", "")
result = result.replaceAll("—\n", "")
return(<textFromImg>{result}</textFromImg>)
}
case elem: Elem => elem copy (child = elem.child flatMap (this transform))
case other => other
}
} transform xml
XML.save(dirPath + "/text_from_image.xml", changed(0), "UTF-8", true)
item.xmlPath(key) = dirPath + "/text_from_image.xml"
} catch {
case e: Throwable => println(e.printStackTrace())
}
}
item
}
}
示例4: 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)
}
}
示例5: setNameSpace
//设置package包名称以及导入依赖的类
package com.github.eerohele.expek
import scala.xml.transform.RewriteRule
import scala.xml.{Elem, NamespaceBinding, Node, TopScope}
def setNameSpace(uri: String)(node: Node): Node = {
object Modify extends RewriteRule {
val binding = NamespaceBinding(None.orNull, uri, TopScope)
override def transform(n: Node): Seq[Node] = n match {
case e: Elem => e.copy(scope = binding, child = e.child.map(Modify))
case n: Node => n
}
}
Modify(node)
}
}
示例6: Append
//设置package包名称以及导入依赖的类
package naoh.lamp
import scala.xml.Elem
import scala.xml.Node
import scala.xml.NodeSeq
import scala.xml.transform.RewriteRule
case class Append(append: NodeSeq, lamp: Lamp) extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case [email protected](prefix, label, attr, scope, [email protected]_*) if lamp(elem) =>
Elem(prefix, label, attr, scope, true, nodes ++ append: _*)
case other =>
other
}
}
case class Replace(node: NodeSeq, lamp: Lamp) extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case [email protected](_, _, _, _, _*) if lamp(elem) => node
case other => other
}
}
case class Transform(rule: Node => NodeSeq, lamp: Lamp) extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case [email protected](_, _, _, _, _*) if lamp(elem) => rule(elem)
case other => other
}
}
case class Chain(nextRule: RewriteRule, lamp: Lamp) extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case [email protected](prefix, label, attr, scope, [email protected]_*) if lamp(elem) =>
Elem(prefix, label, attr, scope, true, nodes.flatMap(nextRule): _*)
case other =>
other
}
}
示例7: 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))
}
}
示例8: 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)
}