Skip to content

Commit

Permalink
more CIMTool refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Derrick Oswald committed Apr 16, 2020
1 parent e0187ae commit 66d199e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 108 deletions.
5 changes: 2 additions & 3 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/CIMTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.io.Closeable
import java.io.File
import java.util.Properties

import org.slf4j.Logger
import org.slf4j.LoggerFactory

object CIMTool
Expand Down Expand Up @@ -44,8 +43,8 @@ object CIMTool
case Some (options) =>
if (options.valid)
{
System.setProperty (org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, options.loglevel.toString)
val log: Logger = LoggerFactory.getLogger (getClass)
val _ = System.setProperty (org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, options.loglevel.toString)
val log = LoggerFactory.getLogger (getClass)
val file = options.cim.file
log.info (s"""generating CIM classes from file "$file"""")
val parser = ModelParser (new File (s"private_data/$file"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ final case class CIMToolOptions (
unittest: Boolean = false,
loglevel: LogLevels.Value = LogLevels.WARN,
cim: CIMVersion = cim100,
target: Target = scala_language,
target: Target = ScalaLanguage,
directory: String = "target"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.ninecode.cim.tool

import scopt.OptionParser

@SuppressWarnings (Array ("org.wartremover.warts.NonUnitStatements"))
class CIMToolOptionsParser (APPLICATION_NAME: String, APPLICATION_VERSION: String)
extends OptionParser[CIMToolOptions](APPLICATION_NAME)
{
Expand All @@ -26,39 +27,38 @@ class CIMToolOptionsParser (APPLICATION_NAME: String, APPLICATION_VERSION: Strin
}
}

opt [Unit]("unittest").
hidden ().
action ((_, c) => { unittest = true; c.copy (unittest = true) }).
text ("unit testing - don't call sys.exit() [%s]".format (default.unittest))
opt[Unit]("unittest")
.hidden ()
.action ((_, c) => { unittest = true; c.copy (unittest = true) })
.text (s"unit testing - don't call sys.exit() [${default.unittest}]")

opt [LogLevels.Value]("log").
action ((x, c) => c.copy (loglevel = x)).
text ("log level, one of %s [%s]".format (LogLevels.values.iterator.mkString (","), default.loglevel))
opt[LogLevels.Value]("log")
.action ((x, c) => c.copy (loglevel = x))
.text (s"log level, one of ${LogLevels.values.mkString (",")} [${default.loglevel}]")

opt [CIMVersion]("cim").
action ((x, c) => c.copy (cim = x)).
text ("cim version, one of %s [%s]".format (VersionRead.versions.mkString (","), default.cim.name))
opt[CIMVersion]("cim")
.action ((x, c) => c.copy (cim = x))
.text (s"cim version, one of ${VersionRead.versions.mkString (",")} [${default.cim.name}]")

opt [Target]("target").
action ((x, c) => c.copy (target = x)).
text ("output target language, one of %s [%s]".format (TargetRead.languages.mkString (","), default.target.name))
opt[Target]("target")
.action ((x, c) => c.copy (target = x))
.text (s"output target language, one of ${TargetRead.languages.mkString (",")} [${default.target.name}]")

opt [String]("directory").
action ((x, c) => c.copy (directory = x)).
text ("output directory [%s]".format (default.directory))
opt[String]("directory")
.action ((x, c) => c.copy (directory = x))
.text (s"output directory [${default.directory}]")

help ("help").
hidden ().
validate (Unit => { helpout = true; Right (Unit) })
help ("help")
.hidden ()
.validate (Unit => { helpout = true; Right (Unit) })

version ("version").
validate (Unit => { versionout = true; Right (Unit) }).
text ("Scala: %s, Spark: %s, %s: %s".format (
APPLICATION_VERSION.split ("-")(0),
APPLICATION_VERSION.split ("-")(1),
APPLICATION_NAME,
APPLICATION_VERSION.split ("-")(2)
)
version ("version")
.validate (Unit => { versionout = true; Right (Unit) })
.text (
{
val version = APPLICATION_VERSION.split ("-")
s"Scala: ${version(0)}, Spark: ${version(1)}, $APPLICATION_NAME: ${version(2)}"
}
)

checkConfig (o => { o.valid = !(helpout || versionout); Right (Unit) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package ch.ninecode.cim.tool

class CIMVersionReader extends Read[CIMVersion]
{
val CIMVersions: List[CIMVersion] = List (cim14, cim15, cim16, ENTSOE, cim100)
val CIMVersions: List[CIMVersion] = List[CIMVersion] (cim14, cim15, cim16, ENTSOE, cim100)
def arity = 1
def reads: String => CIMVersion = (s: String) => CIMVersions.find (_.name == s).getOrElse (
{
Expand Down
78 changes: 44 additions & 34 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/JavaDoc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ case class JavaDoc (
group_description: String = null)
{
lazy val spaces: String = (for (_ <- 0 until leftpad) yield " ").mkString ("")
val regex: Pattern = Pattern.compile ("""([\s\S^.]*?\.)\s*?(\p{Upper}.*)|([\s\S]*[\n])(.*)""", Pattern.DOTALL)
val (summary, body) =
lazy val regex: Pattern = Pattern.compile ("""([\s\S^.]*?\.)\s*?(\p{Upper}.*)|([\s\S]*[\n])(.*)""", Pattern.DOTALL)
lazy val (summary, body) =
if (null != note)
{
val n = note.replace ("\r\n", "\n").split ("\n").map (_.trim).mkString ("\n")
val matcher = regex.matcher (n)
if (matcher.find ())
({ (if (null != matcher.group (1)) matcher.group (1) else matcher.group (3)).trim },
{ (if (null != matcher.group (2)) matcher.group (2) else matcher.group (4)).trim })
{
val h = matcher.group (1)
val d = matcher.group (2)
val (header, description) = if ((null == h) || (null == d))
(matcher.group (3), matcher.group (4))
else
(h, d)
(toList (header), toList (description))
}
else
(n.trim, "")
(toList (n), List ())
}
else
("", "")
(List (), List ())

def edit (s: String): String =
{
Expand All @@ -38,42 +45,45 @@ case class JavaDoc (
l4.mkString ("\n")
}

def asterisks (s: String): String =
{
s.replace ("\n", "\n * ")
}
def toList (s: String): List[String] = if ("" == s) List () else edit (s).split ("\n").toList

def addGroupStuff (s: StringBuilder): Unit =
def groupStuff: List[String] =
{
if ((null != group) && ("" != group))
s.append (s"\n * @group $group")
if ((null != group_name) && ("" != group_name))
s.append (s"\n * @groupname $group $group_name")
if ((null != group_description) && ("" != group_description))
s.append (s"\n * @groupdesc $group $group_description")
def compose (prefix: String, suffix: String): Option[String] =
{
if ((null != group) && ("" != group) && (null != suffix))
if ("" == suffix)
Some (s"$prefix $group")
else
Some (s"$prefix $group $suffix")
else
None
}

val strings =
compose ("@group", "") ::
compose ("@groupname", group_name) ::
compose ("@groupdesc", group_description) ::
Nil
strings.flatten
}

def contents: String =
{
val s = new StringBuilder ()
if ((null != note) && (note != ""))
val text: List[String] = if ((null != note) && (note != ""))
{
s.append (""" * """)
s.append (asterisks (edit (summary)))
if ("" != body)
{
s.append ("""
| *
| * """.stripMargin)
s.append (asterisks (edit (body)))
}
s.append ("""
| *""".stripMargin)
for (member <- members)
s.append (s"\n${member.javaDoc}")
val b = (if (body.nonEmpty) (List ("") :: body :: Nil).flatten else List ())
val m = for (member <- members.toList)
yield member.javaDoc
(summary ::
b ::
List ("") ::
m ::
Nil).flatten
}
addGroupStuff (s)
s.toString
else
List ()
(text :: groupStuff :: Nil).flatten .map (l => if ("" == l) " *" else s" * $l").mkString ("\n")
}

def asText: String =
Expand Down
4 changes: 2 additions & 2 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/JavaScript.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths

import scala.collection.mutable

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import scala.collection.mutable

case class JavaScript (parser: ModelParser, options: CIMToolOptions) extends CodeGenerator
{
val log: Logger = LoggerFactory.getLogger (getClass)
Expand Down
18 changes: 5 additions & 13 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/Member.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ case class Member (
l4.mkString ("\n")
}

/**
* Add Javadoc asterisks in summary or body text.
*
* @param s the string to prefix lines with asterisks
* @return the Javadoc string
*/
def asterisks (s: String): String = s.replace ("\n", "\n * ")

/**
* Generate the Javadoc string for a member.
*
Expand All @@ -74,14 +66,14 @@ case class Member (
s" [[ch.ninecode.model.$referenced_class $referenced_class]]"
else
""
val summary = if ("" != jd.summary)
asterisks (edit (jd.summary))
val summary = if (jd.summary.nonEmpty)
jd.summary.mkString ("\n * ")
else
"<em>undocumented</em>"
val body = if ("" != jd.body)
s"\n * ${asterisks (edit (jd.body))}"
val body = if (jd.body.nonEmpty)
jd.body.mkString ("\n * ", "\n * ", "")
else
""
s" * @param $name$ref $summary$body"
s"@param $name$ref $summary$body"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.nio.charset.Charset

import scala.collection.JavaConversions._
import scala.collection.SortedSet

import com.healthmarketscience.jackcess.Database
import com.healthmarketscience.jackcess.DatabaseBuilder
import com.healthmarketscience.jackcess.Table
Expand Down
22 changes: 8 additions & 14 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/Row.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ case class Row (fields: com.healthmarketscience.jackcess.Row)

def getInt (name: String): Int =
{
val raw = fields.get (name)
if ((null != raw) && raw.isInstanceOf[Int])
raw.asInstanceOf[Int]
else
0
fields.get (name) match
{
case i: Integer => i
case _ => 0
}
}

def getString (name: String): String =
Expand All @@ -24,15 +24,9 @@ case class Row (fields: com.healthmarketscience.jackcess.Row)

def getTrimmedString (name: String): String =
{
val text = getString (name)
if (null != text)
{
val trimmed = text.trim
if (0 != trimmed.length)
trimmed
else
null
}
val trimmed = getString (name).trim
if (0 != trimmed.length)
trimmed
else
null
}
Expand Down
13 changes: 8 additions & 5 deletions CIMTool/src/main/scala/ch/ninecode/cim/tool/Scala.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import scala.collection.mutable
import scala.collection.SortedSet

import org.slf4j.Logger
import org.slf4j.LoggerFactory

case class Scala (parser: ModelParser, options: CIMToolOptions) extends CodeGenerator
{
val log: Logger = LoggerFactory.getLogger (getClass)
Expand Down Expand Up @@ -189,6 +189,7 @@ case class Scala (parser: ModelParser, options: CIMToolOptions) extends CodeGene
else
s.append (""" Element: Element = sup.asInstanceOf[Element]""")
s.append ("""
|
| //
| // Row overrides
| //
Expand All @@ -203,7 +204,8 @@ case class Scala (parser: ModelParser, options: CIMToolOptions) extends CodeGene
| * @groupname Row SQL Row Implementation
| * @groupdesc Row Members related to implementing the SQL Row interface
| */
| override def copy (): Row = { clone ().asInstanceOf[Row] }""".stripMargin)
| override def copy (): Row = { clone ().asInstanceOf[Row] }
|""".stripMargin)
s.append ("""
| override def export_fields: String =
| {
Expand Down Expand Up @@ -397,7 +399,8 @@ case class Scala (parser: ModelParser, options: CIMToolOptions) extends CodeGene
registers = registers :+ """ %s.register""".format (sc.register (pkg))
package_docs = package_docs :+ " *"
package_docs = package_docs :+ s" * ===${pkg.name}==="
package_docs = package_docs :+ JavaDoc (pkg.notes, 0).contents
if (pkg.notes != null)
package_docs = package_docs :+ JavaDoc (pkg.notes, 0).contents
}
else
log.debug ("no text generated for package %s (%s)".format (pkg.xuid, pkg.name))
Expand Down
Loading

0 comments on commit 66d199e

Please sign in to comment.