Commit 4a10a2e7 authored by Jens Korinth's avatar Jens Korinth
Browse files

Add CoreDefinition scala class

* Scala model for core definitions
* support for reading and writing Json format
parent c58f5a12
import scala.io.Source
import play.api.libs.json._
import play.api.libs.functional.syntax._
/**
* Basic definition of a core for IP-XACT packaging.
**/
case class CoreDefinition(name: String, vendor: String, library: String, version: String, root: String) {
import CoreDefinition._
def write(filename: String) : Boolean = try {
val fw = new java.io.FileWriter(filename)
fw.append(Json.toJson(this).toString)
fw.flush()
fw.close()
true
} catch { case ex: Exception => println("ERROR: " + ex); false }
}
/**
* Contains methods for reading a core definition from Json.
**/
object CoreDefinition {
implicit val coreDefinitionWrites : Writes[CoreDefinition] = (
(JsPath \ "name").write[String] ~
(JsPath \ "vendor").write[String] ~
(JsPath \ "library").write[String] ~
(JsPath \ "version").write[String] ~
(JsPath \ "root").write[String]
)(unlift(CoreDefinition.unapply))
implicit val coreDefinitionReads : Reads[CoreDefinition] = (
(JsPath \ "name").read[String] ~
(JsPath \ "vendor").read[String] ~
(JsPath \ "library").read[String] ~
(JsPath \ "version").read[String] ~
(JsPath \ "root").read[String]
)(CoreDefinition.apply _)
def read(filename: String) : Option[CoreDefinition] = try {
val contents = Source.fromFile(filename).getLines.mkString("\n")
val json = Json.parse(contents)
json.validate[CoreDefinition] match {
case s: JsSuccess[CoreDefinition] => Some(s.get)
case e: JsError => { println("ERROR: " + e); None }
}
} catch { case ex: Exception => println("ERROR: " + ex); None }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment