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


Scala ParseException类代码示例

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


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

示例1: ParsedToken

//设置package包名称以及导入依赖的类
package com.malliina.aws.cognito

import java.text.ParseException
import java.util.Date

import com.nimbusds.jwt.SignedJWT

import scala.util.Either.RightProjection
import scala.util.Try

case class ParsedToken(issuer: Issuer,
                       tokenUse: String,
                       key: Key,
                       expiration: Date,
                       signed: SignedJWT) extends JWT

object ParsedToken {
  val TokenUse = "token_use"

  
  def parse(token: RawToken): Either[JWTError, ParsedToken] =
    for {
      signed <- parseToken(token).right
      claims <- mustExist(signed.getJWTClaimsSet, failInfo = "claims")
      issuer <- mustExist(claims.getIssuer, "issuer")
      tokenUse <- mustExist(Try(claims.getStringClaim(TokenUse)).toOption.orNull, TokenUse)
      key <- mustExist(signed.getHeader.getKeyID, "key")
      expiration <- mustExist(claims.getExpirationTime, "expiration")
    } yield {
      ParsedToken(Issuer(issuer), tokenUse, Key(key), expiration, signed)
    }

  // Parsing helpers

  def parseToken(token: RawToken): Either[ParseError, SignedJWT] =
    parseOrError(SignedJWT.parse(token.token), _ => s"Invalid JWT: '$token'.")

  def parseOrError[T](code: => T): Either[ParseError, T] =
    parseOrError(code, e => Option(e.getMessage).getOrElse("Parse error."))

  def parseOrError[T](code: => T, errorMessage: ParseException => String): Either[ParseError, T] =
    try {
      Right(code)
    } catch {
      case e: ParseException =>
        Left(ParseError(errorMessage(e), e))
    }

  def mustExist[T](nullable: => T, failInfo: String): RightProjection[DataMissing, T] =
    Utils.mustExist(nullable, DataMissing(failInfo))
} 
开发者ID:malliina,项目名称:cognito-utils,代码行数:52,代码来源:ParsedToken.scala

示例2: CronScheduleSpec

//设置package包名称以及导入依赖的类
package model

import java.text.ParseException
import java.util.GregorianCalendar

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatestplus.play.PlaySpec

@RunWith(classOf[JUnitRunner])
class CronScheduleSpec extends PlaySpec {

  "Cron scheduler" should {

    "successfully parse cron entry for 10pm every day" in {
      val cronSchedule = CronSchedule("0", "22", "*", "*", "?")
      val date = new GregorianCalendar(2015, 10, 5, 21, 0).getTime
      val expectedNextDate = new GregorianCalendar(2015, 10, 5, 22, 0).getTime
      val nextDate = cronSchedule.nextRunAfter(date)
      nextDate must be(expectedNextDate)
    }

    "Throw exception on creation if cron schedlue is invalid" in {
      intercept[ParseException] {
        CronSchedule("0", "22", "*", "*", "*")
      }
    }
  }

} 
开发者ID:gilt,项目名称:sundial,代码行数:31,代码来源:CronScheduleSpec.scala

示例3: DateUtil

//设置package包名称以及导入依赖的类
package com.wizardsofsmart.cineaste.util

import java.text.{ParseException, SimpleDateFormat}

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat

object DateUtil {
   val completeFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-M-d")
   val yearAndMonthFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-M")
   val yearFormat: SimpleDateFormat = new SimpleDateFormat("yyyy")
   val completeFormatter = DateTimeFormat.forPattern("MMMM d, yyyy")
   val yearAndMonthFormatter = DateTimeFormat.forPattern("MMMM, yyyy")
   val yearFormatter = DateTimeFormat.forPattern("yyyy")


   def prettyPrintDate(date: String): Option[String] = {
      if (isCompleteDate(date)) {
         Some(completeFormatter.print(new DateTime(date)))
      } else if (isYearAndMonth(date)) {
         Some(yearAndMonthFormatter.print(new DateTime(date)))
      } else if (isYear(date)) {
         Some(yearFormatter.print(new DateTime(date)))
      } else {
         None
      }
   }

   private def isCompleteDate(date: String): Boolean = {
      try {
         completeFormat.parse(date)
         true
      } catch {
         case _: ParseException => false
      }
   }

   private def isYearAndMonth(date: String): Boolean = {
      try {
         yearAndMonthFormat.parse(date)
         true
      } catch {
         case _: ParseException => false
      }
   }

   private def isYear(date: String): Boolean = {
      try {
         yearFormat.parse(date)
         true
      } catch {
         case _: ParseException => false
      }
   }

} 
开发者ID:jtortorelli,项目名称:cineaste,代码行数:57,代码来源:DateUtil.scala


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