org.apache.commons.lang3.StringEscapeUtils Scala Examples

The following examples show how to use org.apache.commons.lang3.StringEscapeUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: TestSolrRelation.scala    From spark-solr   with Apache License 2.0 5 votes vote down vote up
package com.lucidworks.spark

import com.lucidworks.spark.util.SolrRelationUtil
import org.apache.commons.lang3.StringEscapeUtils
import org.apache.solr.client.solrj.SolrQuery
import org.apache.spark.sql.sources.{And, EqualTo, Or}
import org.apache.spark.sql.types._

class TestSolrRelation extends SparkSolrFunSuite with SparkSolrContextBuilder {

  test("Streaming expr schema") {
    val arrayJson = StringEscapeUtils.escapeJson(ArrayType(StringType).json)
    val longJson = StringEscapeUtils.escapeJson(LongType.json)
    val exprSchema = s"""arrayField:"${arrayJson}",longField:long"""
    val structFields = SolrRelation.parseSchemaExprSchemaToStructFields(exprSchema)
    assert(structFields.size == 2)
    logger.info(s"Parsed fields: ${structFields}")
    val arrayStructField = structFields.head
    val longStructField = structFields.last
    assert(arrayStructField.name === "arrayField")
    assert(arrayStructField.dataType === ArrayType(StringType))
    assert(arrayStructField.metadata.getBoolean("multiValued"))
    assert(longStructField.name  === "longField")
    assert(longStructField.dataType === LongType)
  }

  test("empty solr relation") {
    intercept[IllegalArgumentException] {
      new SolrRelation(Map.empty, None, sparkSession)
    }
  }

  test("Missing collection property") {
    intercept[IllegalArgumentException] {
      new SolrRelation(Map("zkhost" -> "localhost:121"), None, sparkSession).collection
    }
  }

  test("relation object creation") {
    val options = Map("zkhost" -> "dummy:9983", "collection" -> "test")
    val relation = new SolrRelation(options, None, sparkSession)
    assert(relation != null)
  }

  test("Scala filter expressions") {
    val filterExpr = Or(And(EqualTo("gender", "F"), EqualTo("artist", "Bernadette Peters")),And(EqualTo("gender", "M"), EqualTo("artist", "Girl Talk")))
    val solrQuery = new SolrQuery
    val schema = StructType(Seq(StructField("gender", DataTypes.StringType), StructField("artist", DataTypes.StringType)))
    SolrRelationUtil.applyFilter(filterExpr, solrQuery, schema)
    val fq = solrQuery.getFilterQueries
    assert(fq.length == 1)
    assert(fq(0) === """((gender:"F" AND artist:"Bernadette Peters") OR (gender:"M" AND artist:"Girl Talk"))""")
  }

  test("custom field types option") {
    val fieldTypeOption = "a:b,c,d:e"
    val fieldTypes = SolrRelation.parseUserSuppliedFieldTypes(fieldTypeOption)
    assert(fieldTypes.size ===  2)
    assert(fieldTypes.keySet === Set("a", "d"))
    assert(fieldTypes("a") === "b")
    assert(fieldTypes("d") === "e")
  }

  test("test commas in filter values") {
    val fieldValues = """a:"c,d e",f:g,h:"1, 35, 2""""
    val parsedFilters = SolrRelationUtil.parseCommaSeparatedValuesToList(fieldValues)
    assert(parsedFilters.head === """a:"c,d e"""")
    assert(parsedFilters(1) === "f:g")
    assert(parsedFilters(2) === """h:"1, 35, 2"""")
  }
} 
Example 2
Source File: SuiteToClass.scala    From AppCrawler   with Apache License 2.0 5 votes vote down vote up
package com.testerhome.appcrawler

import org.apache.commons.lang3.StringEscapeUtils
import javassist.{ClassPool, CtConstructor}

import scala.util.{Failure, Success, Try}


  def genTestCaseClass(className: String, superClassName: String, fields: Map[String, Any], directory: String): Unit = {
    val pool = ClassPool.getDefault
    //todo: 特殊字符处理
    val classNameFormat = className
    Try(pool.makeClass(classNameFormat)) match {
      case Success(classNew) => {
        classNew.setSuperclass(pool.get(superClassName))
        val init = new CtConstructor(null, classNew)
        val body = fields.map(field => {
          s"${field._1}_$$eq(${'"' + field._2.toString.replace("\"", "\\\"").replace("\\", "\\\\") + '"'}); "
        }).mkString("\n")
        init.setBody(s"{ ${body}\naddTestCase(); }")
        classNew.addConstructor(init)
        classNew.writeFile(directory)
      }
      case Failure(e) => {}
    }
  }

} 
Example 3
Source File: ParameterValue.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.models

import com.typesafe.config.{Config, ConfigValue, ConfigValueType}
import com.typesafe.config.impl.ConfigInt
import de.frosner.broccoli.services.{ParameterNotFoundException, ParameterValueParsingException}
import org.apache.commons.lang3.StringEscapeUtils
import play.api.libs.json._

import scala.util.{Failure, Success, Try}

object ParameterValue {

  implicit val parameterValueWrites: Writes[ParameterValue] = new Writes[ParameterValue] {
    override def writes(paramValue: ParameterValue) = paramValue.asJsValue
  }

  def fromConfigValue(paramName: String, parameterType: ParameterType, configValue: ConfigValue): Try[ParameterValue] =
    Try {
      parameterType match {
        case ParameterType.Raw =>
          RawParameterValue(configValue.unwrapped().toString)
        case ParameterType.String =>
          StringParameterValue(configValue.unwrapped.asInstanceOf[String])
        case ParameterType.Integer =>
          val number = configValue.unwrapped().asInstanceOf[Number]
          //noinspection ComparingUnrelatedTypes
          if (number == number.intValue())
            IntParameterValue(number.intValue())
          else
            throw ParameterValueParsingException(paramName, s"${number.toString} is not a valid integer")
        case ParameterType.Decimal =>
          DecimalParameterValue(BigDecimal(configValue.unwrapped().asInstanceOf[Number].toString))
      }
    }

  def fromJsValue(parameterName: String,
                  parameterInfos: Map[String, ParameterInfo],
                  jsValue: JsValue): Try[ParameterValue] =
    Try {
      val parameterInfo =
        parameterInfos.getOrElse(parameterName, throw ParameterNotFoundException(parameterName, parameterInfos.keySet))
      fromJsValue(parameterInfo.`type`, jsValue) match {
        case Success(param) => param
        case Failure(ex) =>
          throw ParameterValueParsingException(parameterName, ex.getMessage)
      }
    }

  def fromJsValue(parameterType: ParameterType, jsValue: JsValue): Try[ParameterValue] =
    Try {
      parameterType match {
        case ParameterType.Raw =>
          RawParameterValue(jsValue.as[String])
        case ParameterType.String =>
          StringParameterValue(jsValue.as[String])
        case ParameterType.Integer =>
          IntParameterValue(jsValue.as[Int])
        case ParameterType.Decimal =>
          DecimalParameterValue(jsValue.as[BigDecimal])
      }
    }
}
sealed trait ParameterValue {

  
  def asJsonString: String
  def asJsValue: JsValue
}

case class IntParameterValue(value: Int) extends ParameterValue {
  override def asJsonString: String = value.toString
  override def asJsValue: JsValue = JsNumber(value)
}
case class DecimalParameterValue(value: BigDecimal) extends ParameterValue {
  override def asJsonString: String = value.toString
  override def asJsValue: JsValue = JsNumber(value)
}
case class StringParameterValue(value: String) extends ParameterValue {
  override def asJsonString: String = StringEscapeUtils.escapeJson(value)
  override def asJsValue: JsValue = JsString(value)
}
case class RawParameterValue(value: String) extends ParameterValue {
  override def asJsonString: String = value
  override def asJsValue: JsValue = JsString(value)
} 
Example 4
Source File: CreateStringGenerator.scala    From morpheus   with Apache License 2.0 5 votes vote down vote up
package org.opencypher.okapi.tck.test


import org.apache.commons.lang3.StringEscapeUtils
import org.opencypher.okapi.impl.exception.NotImplementedException
import org.opencypher.okapi.tck.test.TckToCypherConverter.tckValueToCypherValue
import org.opencypher.okapi.api.value.CypherValue.{CypherList => OKAPICypherList, CypherMap => OKAPICypherMap, CypherNull => OKAPICypherNull, CypherString => OKAPICypherString, CypherValue => OKAPICypherValue}
import org.opencypher.tools.tck.SideEffectOps.Diff
import org.opencypher.tools.tck.values.{Backward => TCKBackward, CypherBoolean => TCKCypherBoolean, CypherFloat => TCKCypherFloat, CypherInteger => TCKCypherInteger, CypherList => TCKCypherList, CypherNode => TCKCypherNode, CypherNull => TCKCypherNull, CypherOrderedList => TCKCypherOrderedList, CypherPath => TCKCypherPath, CypherProperty => TCKCypherProperty, CypherPropertyMap => TCKCypherPropertyMap, CypherRelationship => TCKCypherRelationship, CypherString => TCKCypherString, CypherUnorderedList => TCKUnorderedList, CypherValue => TCKCypherValue, Forward => TCKForward}

object CreateStringGenerator {
  def tckCypherValueToCreateString(value: TCKCypherValue): String = {
    value match {
      case TCKCypherString(v) => s"TCKCypherString(${escapeString(v)})"
      case TCKCypherInteger(v) => s"TCKCypherInteger(${v}L)"
      case TCKCypherFloat(v) => s"TCKCypherFloat($v)"
      case TCKCypherBoolean(v) => s"TCKCypherBoolean($v)"
      case TCKCypherProperty(key, v) => s"""TCKCypherProperty("${escapeString(key)}",${tckCypherValueToCreateString(v)})"""
      case TCKCypherPropertyMap(properties) =>
        val propertiesCreateString = properties.map { case (key, v) => s"(${escapeString(key)}, ${tckCypherValueToCreateString(v)})" }.mkString(",")
        s"TCKCypherPropertyMap(Map($propertiesCreateString))"
      case l: TCKCypherList =>
        l match {
          case TCKCypherOrderedList(elems) => s"TCKCypherOrderedList(List(${elems.map(tckCypherValueToCreateString).mkString(",")}))"
          case _ => s"TCKCypherValue.apply(${escapeString(l.toString)}, false)"
        }
      case TCKCypherNull => "TCKCypherNull"
      case TCKCypherNode(labels, properties) =>
        val labelsString = if (labels.isEmpty) "" else s"Set(${labels.map(escapeString).mkString(",")})"
        val propertyString = if (properties.properties.isEmpty) "" else s"${tckCypherValueToCreateString(properties)}"
        val separatorString =
          if (properties.properties.isEmpty) ""
          else if (labels.isEmpty) "properties = "
          else ", "

        s"TCKCypherNode($labelsString$separatorString$propertyString)"
      case TCKCypherRelationship(typ, properties) =>
        val propertyString = if (properties.properties.isEmpty) "" else s", ${tckCypherValueToCreateString(properties)}"
        s"TCKCypherRelationship(${escapeString(typ)}$propertyString)"
      case TCKCypherPath(start, connections) =>
        val connectionsCreateString = connections.map {
          case TCKForward(r, n) => s"TCKForward(${tckCypherValueToCreateString(r)},${tckCypherValueToCreateString(n)})"
          case TCKBackward(r, n) => s"TCKBackward(${tckCypherValueToCreateString(r)},${tckCypherValueToCreateString(n)})"
        }.mkString(",")

        s"TCKCypherPath(${tckCypherValueToCreateString(start)},List($connectionsCreateString))"
      case other =>
        throw NotImplementedException(s"Converting Cypher value $value of type `${other.getClass.getSimpleName}`")
    }
  }

  def cypherValueToCreateString(value: OKAPICypherValue): String = {
    value match {
      case OKAPICypherList(l) => s"List(${l.map(cypherValueToCreateString).mkString(",")})"
      case OKAPICypherMap(m) =>
        val mapElementsString = m.map {
          case (key, cv) => s"(${escapeString(key)},${cypherValueToCreateString(cv)})"
        }.mkString(",")
        s"CypherMap($mapElementsString)"
      case OKAPICypherString(s) => escapeString(s)
      case OKAPICypherNull => "CypherNull"
      case _ => s"${value.getClass.getSimpleName}(${value.unwrap})"
    }
  }

  def tckCypherMapToTCKCreateString(cypherMap: Map[String, TCKCypherValue]): String = {
    val mapElementsString = cypherMap.map {
      case (key, cypherValue) => escapeString(key) -> tckCypherValueToCreateString(cypherValue)
    }
    s"Map(${mapElementsString.mkString(",")})"
  }

  def tckCypherMapToOkapiCreateString(cypherMap: Map[String, TCKCypherValue]): String = {
    val mapElementsString = cypherMap.map {
      case (key, tckCypherValue) => escapeString(key) -> cypherValueToCreateString(tckValueToCypherValue(tckCypherValue))
    }
    s"CypherMap(${mapElementsString.mkString(",")})"
  }

  def diffToCreateString(diff: Diff): String = {
    val diffs = diff.v.map { case (s, i) => escapeString(s) -> i }
    s"Diff(Map(${diffs.mkString(",")}}))"
  }

  def escapeString(s: String): String = {
    s""" "${StringEscapeUtils.escapeJava(s)}" """
  }

} 
Example 5
Source File: CategoricalRangeRule.scala    From deequ   with Apache License 2.0 5 votes vote down vote up
package com.amazon.deequ.suggestions.rules

import com.amazon.deequ.analyzers.{DataTypeInstances, Histogram}
import com.amazon.deequ.checks.Check
import com.amazon.deequ.constraints.Constraint.complianceConstraint
import com.amazon.deequ.profiles.ColumnProfile
import com.amazon.deequ.suggestions.ConstraintSuggestion
import org.apache.commons.lang3.StringEscapeUtils


case class CategoricalRangeRule() extends ConstraintRule[ColumnProfile] {

  override def shouldBeApplied(profile: ColumnProfile, numRecords: Long): Boolean = {
    val hasHistogram = profile.histogram.isDefined && profile.dataType == DataTypeInstances.String

    if (hasHistogram) {
      val entries = profile.histogram.get.values

      val numUniqueElements = entries.count { case (_, value) => value.absolute == 1L }

      val uniqueValueRatio = numUniqueElements.toDouble / entries.size

      // TODO find a principled way to define this threshold...
      uniqueValueRatio <= 0.1
    } else {
      false
    }
  }

  override def candidate(profile: ColumnProfile, numRecords: Long): ConstraintSuggestion = {

    val valuesByPopularity = profile.histogram.get.values.toArray
      .filterNot { case (key, _) => key == Histogram.NullFieldReplacement }
      .sortBy { case (_, value) => value.absolute }
      .reverse

    val categoriesSql = valuesByPopularity
      // the character "'" can be contained in category names
      .map { case (key, _) => key.replace("'", "''") }
      .mkString("'", "', '", "'")

    val categoriesCode = valuesByPopularity
      .map { case (key, _) => StringEscapeUtils.escapeJava(key) }
      .mkString(""""""", """", """", """"""")

    val description = s"'${profile.column}' has value range $categoriesSql"
    val columnCondition = s"`${profile.column}` IN ($categoriesSql)"
    val constraint = complianceConstraint(description, columnCondition, Check.IsOne)

    ConstraintSuggestion(
      constraint,
      profile.column,
      "Compliance: 1",
      description,
      this,
      s""".isContainedIn("${profile.column}", Array($categoriesCode))"""
    )
  }

  override val ruleDescription: String = "If we see a categorical range for a " +
    "column, we suggest an IS IN (...) constraint"
} 
Example 6
Source File: StringUtils.scala    From inox   with Apache License 2.0 5 votes vote down vote up
package inox
package utils

import org.apache.commons.lang3.StringEscapeUtils

object StringUtils {
  def toHex(i: Int): String = {
    if (0 <= i && i <= 9) i.toString else (i + 55).toChar.toString
  }

  def fromHex(c: Char): Int = {
    if (c >= 'A' && c <= 'F') (c - 55).toInt
    else if (c >= 'a' && c <= 'f') (c - 87).toInt
    else c.toString.toInt
  }

  def encodeByte(b: Byte): String = "\\x" + toHex((b >> 4) & 0xF) + toHex(b & 0xF)
  def decodeHex(s: String): Byte = ((fromHex(s.charAt(2)) << 4) + fromHex(s.charAt(3))).toByte

  private val hex = """^\\x[0-9A-Fa-f]{2}""".r

  object Hex {
    def unapply(s: String): Option[(Byte, String)] = {
      if (hex.findFirstIn(s).isDefined) {
        val (head, s2) = s.splitAt(4)
        Some((decodeHex(head), s2))
      } else {
        None
      }
    }
  }

  object JavaEncoded {
    def unapply(s: String): Option[(Byte, String)] = Hex.unapply(s).orElse {
      if (s.charAt(0) == '\\') {
        val Seq(b) = StringEscapeUtils.unescapeJava(s.take(2)).getBytes.toSeq
        Some((b, s.drop(2)))
      } else {
        None
      }
    }
  }

  def decode(s: String): String = if (s.isEmpty) s else (s match {
    case JavaEncoded(b, s2) => (b & 0xFF).toChar + decode(s2)
    case _ => s.head + decode(s.tail)
  })
}