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


Scala InputSource类代码示例

本文整理汇总了Scala中org.xml.sax.InputSource的典型用法代码示例。如果您正苦于以下问题:Scala InputSource类的具体用法?Scala InputSource怎么用?Scala InputSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了InputSource类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: PageDownloader

//设置package包名称以及导入依赖的类
package com.nekopiano.scala.rest.client

import dispatch.classic.url
import dispatch.classic.Http
import java.io.StringReader
import nu.validator.htmlparser.common.XmlViolationPolicy
import nu.validator.htmlparser.sax.HtmlParser
import org.xml.sax.InputSource
import scalax.io.JavaConverters._
import scalax.io.Resource
import scala.xml.Node
import scala.xml.parsing.NoBindingFactoryAdapter


object PageDownloader {

  def main(args: Array[String]): Unit = {

    val h = new Http
    val req = url("http://www.sample.jp/products/") >\ "UTF-8"
    val html = h(req as_str)

    val rootNode = toNode(html)
    val links = rootNode \\ "@href" filter (_.text matches "/products/[a-z]+/[a-z0-9]+/")

    val pages = links.map(a => {
      "http://www.sample.jp" + a.text + "shiyo.htm"
    }).sorted.distinct
    println("pages.size=" + pages.size)

    pages.foreach(url => {
      println(url)
      val data = Resource.fromURL(url).byteArray
      println("downloading: %s ..." format url)
      val r = "[a-z]+/[a-z0-9]+/shiyo.htm".r
      val productName = r.findAllIn(url).matchData.mkString.dropRight(10).replace("/", "-")
      Resource.fromFile(new java.io.File("samplepages\\" + productName + ".html")).write(data)
    })

  }

  def toNode(str: String): Node = {
    val hp = new HtmlParser
    hp.setNamePolicy(XmlViolationPolicy.ALLOW)

    val saxer = new NoBindingFactoryAdapter
    hp.setContentHandler(saxer)
    hp.parse(new InputSource(new StringReader(str)))

    saxer.rootElem
  }

} 
开发者ID:lamusique,项目名称:ScalaSandbox,代码行数:54,代码来源:PageDownloader.scala

示例2: PageDownloader

//设置package包名称以及导入依赖的类
package com.nekopiano.scala.scalasandbox.download

import dispatch.classic.url
import dispatch.classic.Http
import java.io.StringReader
import nu.validator.htmlparser.common.XmlViolationPolicy
import nu.validator.htmlparser.sax.HtmlParser
import org.xml.sax.InputSource
import scalax.io.JavaConverters._
import scalax.io.Resource
import scala.xml.Node
import scala.xml.parsing.NoBindingFactoryAdapter


object PageDownloader {

  def main(args: Array[String]): Unit = {

    val h = new Http
    val req = url("http://www.sample.jp/products/") >\ "UTF-8"
    val html = h(req as_str)

    val rootNode = toNode(html)
    val links = rootNode \\ "@href" filter (_.text matches "/products/[a-z]+/[a-z0-9]+/")

    val pages = links.map(a => {
      "http://www.sample.jp" + a.text + "shiyo.htm"
    }).sorted.distinct
    println("pages.size=" + pages.size)

    pages.foreach(url => {
      println(url)
      val data = Resource.fromURL(url).byteArray
      println("downloading: %s ..." format url)
      val r = "[a-z]+/[a-z0-9]+/shiyo.htm".r
      val productName = r.findAllIn(url).matchData.mkString.dropRight(10).replace("/", "-")
      Resource.fromFile(new java.io.File("samplepages\\" + productName + ".html")).write(data)
    })

  }

  def toNode(str: String): Node = {
    val hp = new HtmlParser
    hp.setNamePolicy(XmlViolationPolicy.ALLOW)

    val saxer = new NoBindingFactoryAdapter
    hp.setContentHandler(saxer)
    hp.parse(new InputSource(new StringReader(str)))

    saxer.rootElem
  }

} 
开发者ID:lamusique,项目名称:ScalaSandbox,代码行数:54,代码来源:PageDownloader.scala

示例3: Starscream

//设置package包名称以及导入依赖的类
package com.iofficecorp.svg

import java.io.Reader
import java.io.Writer

import org.xml.sax.{XMLReader, Attributes, InputSource}
import org.xml.sax.helpers.XMLReaderFactory

import org.dom4j.io.XMLWriter


object Starscream {
  private implicit class FilterChain(val reader: XMLReader) {
    def chain(newReader: XMLReader => XMLReader): FilterChain =
      new FilterChain(newReader(reader))
  }

  def transformSVGFromReaderToWriter(
      reader: Reader,
      writer: Writer,
      scale:Float = 1.0f,
      minimum:Float = Float.MinValue)
      : Unit = {
    val filter =
      XMLReaderFactory.createXMLReader()
        .chain(new ScaleFilter(_, scale))
        .chain(new MinimumFilter(_, minimum))
        .chain(new StripWhitespaceFilter(_, "id"))
        .chain(new StripWhitespaceFilter(_, "href"))
        .reader
    val xmlWriter = new XMLWriter(writer)
    filter.setContentHandler(xmlWriter)
    filter.parse(new InputSource(reader))
  }
} 
开发者ID:iOffice,项目名称:starscream,代码行数:36,代码来源:Starscream.scala


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