本文整理汇总了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>
}
}
开发者ID:mhddurrah,项目名称:scala_again,代码行数:29,代码来源:InlineDsl.scala
示例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
})
}
}
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:33,代码来源:Helpers.scala
示例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
}
}
开发者ID:PrawoPolskie,项目名称:toakoma,代码行数:56,代码来源:Img2TxtProcessor.scala
示例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)
}
}
开发者ID:deekim,项目名称:impatient-scala,代码行数:20,代码来源:ExerciseNine.scala
示例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)
}
}
开发者ID:eerohele,项目名称:expek,代码行数:20,代码来源:NodeRefinery.scala
示例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
}
}
开发者ID:naoh87,项目名称:lamp,代码行数:40,代码来源:Rules.scala
示例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))
}
}
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:33,代码来源:Exercise10.scala
示例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)
}
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:24,代码来源:Exercise09.scala
注:本文中的scala.xml.transform.RewriteRule类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论