Skip to content

Commit

Permalink
Added inputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Feb 7, 2021
1 parent 57efe2c commit 5742460
Show file tree
Hide file tree
Showing 30 changed files with 166 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .bsp/sbt.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"sbt","version":"1.4.1","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["/usr/lib/jvm/jdk-12.0.2/bin/java","-Xms100m","-Xmx100m","-classpath","/usr/share/sbt/bin/sbt-launch.jar","xsbt.boot.Boot","-bsp"]}
{"name":"sbt","version":"1.4.7","bspVersion":"2.0.0-M5","languages":["scala"],"argv":["/usr/lib/jvm/jdk-12.0.2/bin/java","-Xms100m","-Xmx100m","-classpath","/usr/share/sbt/bin/sbt-launch.jar","xsbt.boot.Boot","-bsp"]}
47 changes: 32 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
lazy val scala212 = "2.12.12"
lazy val scala213 = "2.13.3"
lazy val scala213 = "2.13.4"
lazy val supportedScalaVersions = List(scala213, scala212)

// Local dependencies
lazy val srdfVersion = "0.1.76"
lazy val utilsVersion = "0.1.70"
lazy val srdfVersion = "0.1.90"
lazy val utilsVersion = "0.1.73"
lazy val documentVersion = "0.0.8"

// Dependency versions
lazy val antlrVersion = "4.7.1"
lazy val catsVersion = "2.2.0"
lazy val catsVersion = "2.3.0"
lazy val commonsTextVersion = "1.8"
lazy val circeVersion = "0.14.0-M1"
lazy val diffsonVersion = "4.0.0"
lazy val declineVersion = "1.3.0"
lazy val fs2Version = "2.5.0"
// lazy val effVersion = "4.6.1"
lazy val jenaVersion = "3.16.0"
lazy val jgraphtVersion = "1.3.1"
lazy val munitVersion = "0.7.21"
lazy val munitEffectVersion = "0.11.0"
lazy val logbackVersion = "1.2.3"
lazy val loggingVersion = "3.9.2"
lazy val rdf4jVersion = "3.0.0"
Expand Down Expand Up @@ -50,10 +53,17 @@ lazy val decline = "com.monovore" %% "decline"
lazy val declineEffect = "com.monovore" %% "decline-effect" % declineVersion
lazy val diffsonCirce = "org.gnieh" %% "diffson-circe" % diffsonVersion
// lazy val eff = "org.atnos" %% "eff" % effVersion
lazy val fs2 = "co.fs2" %% "fs2-core" % fs2Version
lazy val fs2io = "co.fs2" %% "fs2-io" % fs2Version

lazy val jgraphtCore = "org.jgrapht" % "jgrapht-core" % jgraphtVersion
lazy val logbackClassic = "ch.qos.logback" % "logback-classic" % logbackVersion
lazy val jenaArq = "org.apache.jena" % "jena-arq" % jenaVersion
lazy val jenaFuseki = "org.apache.jena" % "jena-fuseki-main" % jenaVersion
lazy val munit = "org.scalameta" %% "munit" % munitVersion
lazy val munitEffect = "org.typelevel" %% "munit-cats-effect-2" % munitEffectVersion


lazy val rdf4j_runtime = "org.eclipse.rdf4j" % "rdf4j-runtime" % rdf4jVersion

// WESO components
Expand Down Expand Up @@ -83,12 +93,16 @@ lazy val simulacrum = "org.typelevel" %% "simulacrum"
lazy val shapeMapsRoot = project
.in(file("."))
.enablePlugins(ScalaUnidocPlugin, SbtNativePackager, WindowsPlugin, JavaAppPackaging, LauncherJarPlugin)
.disablePlugins(RevolverPlugin)
// .disablePlugins(RevolverPlugin)
// .settings(
// buildInfoKeys := BuildInfoKey.ofN(name, version, scalaVersion, sbtVersion),
// buildInfoPackage := "es.weso.shaclex.buildinfo"
// )
.settings(commonSettings, packagingSettings, publishSettings, ghPagesSettings, wixSettings)
.settings(commonSettings,
packagingSettings,
publishSettings,
// ghPagesSettings,
wixSettings)
.aggregate(shapeMaps)
.dependsOn(shapeMaps)
.settings(
Expand Down Expand Up @@ -121,8 +135,8 @@ def testFilter(name: String): Boolean = /*(name endsWith "Test") && */ !compatFi
lazy val shapeMaps = project
.in(file("modules/shapeMaps"))
.enablePlugins(Antlr4Plugin)
.disablePlugins(RevolverPlugin)
.settings(commonSettings, publishSettings, antlrSettings("es.weso.shapeMaps.parser"))
// .disablePlugins(RevolverPlugin)
.settings(commonSettings, publishSettings, antlrSettings("es.weso.shapemaps.parser"))
.dependsOn()
.settings(
crossScalaVersions := supportedScalaVersions,
Expand All @@ -134,11 +148,14 @@ lazy val shapeMaps = project
scalaLogging,
catsCore,
catsKernel,
// catsMacros,
circeCore,
circeGeneric,
circeParser
)
circeParser,
fs2, fs2io,
munit % Test,
munitEffect % Test
),
testFrameworks += new TestFramework("munit.Framework")
)

def macroDependencies(scalaVersion: String) =
Expand Down Expand Up @@ -167,8 +184,8 @@ lazy val sharedDependencies = Seq(
)

lazy val packagingSettings = Seq(
mainClass in Compile := Some("es.weso.shapeMaps.Main"),
mainClass in assembly := Some("es.weso.shapeMaps.Main"),
mainClass in Compile := Some("es.weso.shapemaps.Main"),
mainClass in assembly := Some("es.weso.shapemaps.Main"),
test in assembly := {},
assemblyJarName in assembly := "shapeMaps.jar",
packageSummary in Linux := name.value,
Expand Down Expand Up @@ -220,9 +237,9 @@ lazy val wixSettings = Seq(
// wixProductUpgradeId := "4552fb0e-e257-4dbd-9ecb-dba9dbacf424"
)

lazy val ghPagesSettings = Seq(
/*lazy val ghPagesSettings = Seq(
git.remoteRepo := "[email protected]:weso/shapeMaps.git"
)
)*/

lazy val commonSettings = compilationSettings ++ sharedDependencies ++ Seq(
organization := "es.weso",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import io.circe._
import io.circe.syntax._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.PrefixMap
import es.weso.rdf.nodes.{IRI, RDFNode}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import io.circe._
import io.circe.syntax._

case class Info(
status: Status = Conformant,
reason: Option[String] = None,
appInfo: Option[Json] = None)
val status: Status = Conformant,
val reason: Option[String] = None,
val appInfo: Option[Json] = None)

object Info {
implicit val encodeInfo: Encoder[Info] = new Encoder[Info] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.nodes.{IRI, RDFNode}
import es.weso.rdf.path.SHACLPath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import cats.implicits._
import com.typesafe.scalalogging.LazyLogging
import es.weso.rdf.PREFIXES._
import es.weso.rdf.PrefixMap
import es.weso.rdf.nodes._
import es.weso.rdf.path._
import es.weso.shapeMaps.Parser._
import es.weso.shapeMaps.parser.NodeSelectorBaseVisitor
import es.weso.shapeMaps.parser.NodeSelectorParser.{StringContext => ShapeMapStringContext, _}
import es.weso.shapemaps.Parser._
import es.weso.shapemaps.parser.NodeSelectorBaseVisitor
import es.weso.shapemaps.parser.NodeSelectorParser.{StringContext => ShapeMapStringContext, _}
import es.weso.utils.FileUtils
import scala.jdk.CollectionConverters._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import java.io.{ByteArrayInputStream, InputStreamReader, Reader => JavaReader}
import java.nio.charset.StandardCharsets

import com.typesafe.scalalogging._
import es.weso.rdf._
import es.weso.shapeMaps.parser.{NodeSelectorLexer, NodeSelectorParser}
import es.weso.shapemaps.parser.{NodeSelectorLexer, NodeSelectorParser}
import org.antlr.v4.runtime._

object ParserNodeSelector extends LazyLogging {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import java.io.{ByteArrayInputStream, InputStreamReader, Reader => JavaReader}
import java.nio.charset.StandardCharsets

import com.typesafe.scalalogging._
import es.weso.rdf._
import es.weso.rdf.nodes.IRI
import es.weso.shapeMaps.parser.{ShapeMapLexer, ShapeMapParser}
import es.weso.shapemaps.parser.{ShapeMapLexer, ShapeMapParser}
import org.antlr.v4.runtime._
import cats.data._
import cats.implicits._

object Parser extends LazyLogging {

Expand All @@ -31,18 +33,18 @@ object Parser extends LazyLogging {
str: String,
base: Option[IRI],
nodesPrefixMap: PrefixMap,
shapesPrefixMap: PrefixMap): Either[String, QueryShapeMap] = {
shapesPrefixMap: PrefixMap): Either[NonEmptyList[String], QueryShapeMap] = {
val s = removeBOM(str)
val reader: JavaReader =
new InputStreamReader(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)))
parseSchemaReader(reader, base, nodesPrefixMap, shapesPrefixMap)
parseReader(reader, base, nodesPrefixMap, shapesPrefixMap)
}

def parseSchemaReader(
def parseReader(
reader: JavaReader,
base: Option[IRI],
nodesPrefixMap: PrefixMap,
shapesPrefixMap: PrefixMap): Either[String, QueryShapeMap] = {
shapesPrefixMap: PrefixMap): Either[NonEmptyList[String], QueryShapeMap] = {
val input: CharStream = CharStreams.fromReader(reader)
val lexer: ShapeMapLexer = new ShapeMapLexer(input)
val tokens: CommonTokenStream = new CommonTokenStream(lexer)
Expand All @@ -55,11 +57,10 @@ object Parser extends LazyLogging {
val maker = new ShapeMapsMaker(base,nodesPrefixMap,shapesPrefixMap)
val builder = maker.visit(parser.shapeMap()).asInstanceOf[Builder[QueryShapeMap]]
val errors = errorListener.getErrors
if (errors.length > 0) {
Left(errors.mkString("\n"))
} else {
builder
}
NonEmptyList.fromList(errors).fold(
builder.leftMap(NonEmptyList.one(_)))(
_.asLeft
)
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import java.util

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.nodes.{IRI, RDFNode}
import io.circe.{Decoder, DecodingFailure, Encoder, Json}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.PrefixMap
import es.weso.rdf.nodes.IRI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import cats._
import cats.implicits._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.nodes._
import cats._
Expand All @@ -16,6 +16,9 @@ import cats.effect.IO
import java.nio.file.Path
import es.weso.utils.FileUtils
import scala.util.control.NoStackTrace
import java.io.InputStreamReader
import java.io.InputStream
import cats.data.NonEmptyList

abstract class ShapeMap {
val associations: List[Association]
Expand Down Expand Up @@ -109,26 +112,39 @@ object ShapeMap {

def availableFormats: List[String] = ShapeMapFormat.availableFormatNames
def empty: ShapeMap = FixedShapeMap.empty

def fromInputStream(
is: InputStream,
format: String,
base: Option[IRI],
nodesPrefixMap: PrefixMap,
shapesPrefixMap: PrefixMap
): Either[NonEmptyList[String],ShapeMap] = {
val reader = new InputStreamReader(is)
Parser.parseReader(reader, base, nodesPrefixMap, shapesPrefixMap)
}

def fromURI(uri: String,
format: String,
base: Option[IRI],
nodesPrefixMap: PrefixMap,
shapesPrefixMap: PrefixMap): Either[String, ShapeMap] = {
shapesPrefixMap: PrefixMap): IO[Either[NonEmptyList[String], ShapeMap]] = {
val t = Try {
val contents = Source.fromURL(uri).mkString
val either: Either[String, ShapeMap] = {
val either: Either[NonEmptyList[String], ShapeMap] = {
fromString(contents, format, base, nodesPrefixMap, shapesPrefixMap)
}
either
}
t.fold(e => Left(s"Exception obtaining URI contents. URI = ${uri}. Error: ${e.getLocalizedMessage}"),
identity
t.fold(
e => IO.raiseError(new RuntimeException(s"Exception obtaining URI contents. URI = ${uri}. Error: ${e.getLocalizedMessage}")),
e => IO.pure(e)
)
}

case class ShapeMapFromPathException(msg: String, path: Path, format: String, base: Option[IRI], nodesPrefixMap: PrefixMap, shapesPrefixMap: PrefixMap)
case class ShapeMapFromPathException(errors: NonEmptyList[String], path: Path, format: String, base: Option[IRI], nodesPrefixMap: PrefixMap, shapesPrefixMap: PrefixMap)
extends RuntimeException(s"""|Error obtaining shapeMap from path
|Msg: ${msg}
|Errors: ${errors.toList.mkString("\n")}
|Absolute path: ${path.toFile().getAbsolutePath()}
|Format: $format
|Base: ${base.map(_.getLexicalForm).getOrElse("")}
Expand All @@ -143,37 +159,37 @@ object ShapeMap {
nodesPrefixMap: PrefixMap = PrefixMap.empty,
shapesPrefixMap: PrefixMap = PrefixMap.empty): IO[ShapeMap] = for {
str <- FileUtils.getContents(path)
shapeMap <- fromString(str, format, base, nodesPrefixMap, shapesPrefixMap).fold(
str => IO.raiseError(ShapeMapFromPathException(str,path,format,base,nodesPrefixMap,shapesPrefixMap)),
v => v.pure[IO]
sm <- fromString(str, format, base, nodesPrefixMap, shapesPrefixMap).fold(
nes => IO.raiseError(ShapeMapFromPathException(nes,path,format,base,nodesPrefixMap,shapesPrefixMap)),
sm => IO.pure(sm)
)
} yield shapeMap
} yield sm

def fromString(str: String,
format: String,
base: Option[IRI] = None,
nodesPrefixMap: PrefixMap = PrefixMap.empty,
shapesPrefixMap: PrefixMap = PrefixMap.empty
): Either[String,ShapeMap] =
): Either[NonEmptyList[String],ShapeMap] =
format.toUpperCase match {
case "JSON" => fromJson(str)
case "COMPACT" => {
fromCompact(str,base,nodesPrefixMap,shapesPrefixMap)
}
case _ => Left(s"Unknown format for shapeMap")
case _ => Left(NonEmptyList.one(s"Unknown format for shapeMap"))
}

def fromCompact(
str: String,
base: Option[IRI] = None,
nodesPrefixMap: PrefixMap = PrefixMap.empty,
shapesPrefixMap: PrefixMap = PrefixMap.empty): Either[String, ShapeMap] = {
shapesPrefixMap: PrefixMap = PrefixMap.empty): Either[NonEmptyList[String], ShapeMap] = {
if (str.isEmpty) Right(ShapeMap.empty)
else Parser.parse(str, base, nodesPrefixMap, shapesPrefixMap)
}

def fromJson(jsonStr: String): Either[String, ShapeMap] = {
decode[ShapeMap](jsonStr).leftMap(_.getMessage)
def fromJson(jsonStr: String): Either[NonEmptyList[String], ShapeMap] = {
decode[ShapeMap](jsonStr).leftMap(s => NonEmptyList.one(s.getMessage))
}

def parseResultMap(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps
import cats.implicits._

sealed abstract class ShapeMapFormat {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.weso.shapeMaps
package es.weso.shapemaps

import es.weso.rdf.nodes.{BNode, IRI, RDFNode}
import io.circe.{Decoder, DecodingFailure, Encoder, Json}
Expand Down
Loading

0 comments on commit 5742460

Please sign in to comment.