当前位置: 首页>>代码示例>>Scala>>正文


Scala RuleTransformer类代码示例

本文整理汇总了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>
  }

} 
开发者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: 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

示例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))
  }

} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:33,代码来源:Exercise10.scala

示例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)
} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:24,代码来源:Exercise09.scala


注:本文中的scala.xml.transform.RuleTransformer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。