Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/galite-02DD'
Browse files Browse the repository at this point in the history
	- Add the possibility to keep empty strings in the generated xml using the Factory generation task by Galite. [APPS-02DD][PR#637]
  • Loading branch information
mgrati committed Sep 19, 2024
2 parents f48930d + 8d29869 commit 9b9435a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import java.util.*
import org.apache.xmlbeans.*
import org.apache.xmlbeans.impl.common.XmlErrorWatcher
import org.apache.xmlbeans.impl.schema.PathResourceLoader
import org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl
import org.apache.xmlbeans.impl.tool.CodeGenUtil

import org.kopi.galite.util.xsdToFactory.options.FactoryGeneratorOptions
Expand Down Expand Up @@ -184,7 +183,7 @@ class FactoryGenerator {
factory.fileExtension
)
writer = IOUtil.getFactoryWriter(output)
printer.print(factory, writer, options.getAbstract!!)
printer.print(factory, writer, options.getAbstract!!, options.keepEmptyStrings!!)
writer.close()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class FactoryGeneratorOptions(var name: String? = null,
var source:String? = null,
var directory: String? = null,
var classpath: String? = null,
var getAbstract: Boolean? = false): Options("generator") {
var getAbstract: Boolean? = false,
var keepEmptyStrings: Boolean? = false): Options("generator") {

override fun processOption(code: Int, g: Getopt): Boolean {
when (code) {
Expand Down Expand Up @@ -56,27 +57,32 @@ class FactoryGeneratorOptions(var name: String? = null,
getAbstract = true
return true
}
'e'.code -> {
keepEmptyStrings = true
return true
}
else -> return super.processOption(code, g)
}
}

override val options: Array<String?>
get() {
val parent: Array<String?> = super.options
val total = arrayOfNulls<String>(parent.size + 6)
val total = arrayOfNulls<String>(parent.size + 7)
System.arraycopy(parent, 0, total, 0, parent.size)
total[parent.size + 0] = " --name, -n<String>: The name of the factory class to be generated"
total[parent.size + 1] = " --fpackage, -p<String>: The package of the factory class to be generated"
total[parent.size + 2] = " --source, -s<String>: Target binary directory for .xsb files."
total[parent.size + 3] = " --directory, -d<String>: Target directory for generated Factory files."
total[parent.size + 4] = " --classpath, -c<String>: Classpath specifying classes to include during compilation. pathA;pathB;pathC — Class search path of directories and JAR files."
total[parent.size + 5] = " --getAbstract, -a<boolean>: Generate methods for abstract types"
total[parent.size + 6] = " --keepEmptyStrings, -e<boolean>: Generate methods without checking for empty strings"

return total
}

override val shortOptions: String
get() = "n:p:s:d:c:b:a" + super.shortOptions
get() = "n:p:s:d:c:b:a:e" + super.shortOptions

override fun version() {
println("Version 1.0 released 10 Mai 2024")
Expand Down Expand Up @@ -104,7 +110,8 @@ class FactoryGeneratorOptions(var name: String? = null,
LongOpt("source", LongOpt.REQUIRED_ARGUMENT, null, 's'.code),
LongOpt("directory", LongOpt.REQUIRED_ARGUMENT, null, 'd'.code),
LongOpt("classpath", LongOpt.REQUIRED_ARGUMENT, null, 'c'.code),
LongOpt("gabstract", LongOpt.NO_ARGUMENT, null, 'a'.code)
LongOpt("getAbstract", LongOpt.NO_ARGUMENT, null, 'a'.code),
LongOpt("keepEmptyStrings", LongOpt.NO_ARGUMENT, null, 'e'.code)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,21 @@ class FactoryCodePrinter: Constants {
* @param factory The Factory object to print.
* @param writer The Writer object to write the generated code.
* @param getAbstract If true, includes abstract types.
* @param keepEmptyStrings If true, exclude the verification of empty strings.
* @throws IOException If an I/O error occurs.
*/
@Throws(IOException::class)
fun print(factory: Factory,
writer: Writer?,
getAbstract: Boolean) {
getAbstract: Boolean,
keepEmptyStrings: Boolean) {
this.writer = writer

extractClasseAttributes(factory, getAbstract)
printTopComment(factory.name!!, factory.isPrintHeader!!)
printPackage(factory.packageName!!)
printImports()
printFactory(factory)
printFactory(factory, keepEmptyStrings)
}

/**
Expand Down Expand Up @@ -229,7 +231,7 @@ class FactoryCodePrinter: Constants {
/**
* Adds the body of the crate fonction.
*/
private fun addBodyFunction(classFactory: ClassFactory) {
private fun addBodyFunction(classFactory: ClassFactory, keepEmptyStrings: Boolean) {
val functionDeclaration = "${indentation(1)}fun create${classFactory.className}("

emit(functionDeclaration, false)
Expand Down Expand Up @@ -260,7 +262,7 @@ class FactoryCodePrinter: Constants {
if(attribute.Required) {
emit("${indentation(2)}new${classFactory.className}.$attributeName = $value", true)
} else {
if(!"String".equals(attribute.type)) {
if(!"String".equals(attribute.type) || keepEmptyStrings) {
emit("${indentation(2)}$parameterName?.let { new${classFactory.className}.$attributeName = $value }", true)
} else {
emit("${indentation(2)}if (!$parameterName.isNullOrBlank()) {", true)
Expand Down Expand Up @@ -425,11 +427,11 @@ class FactoryCodePrinter: Constants {
/**
* Prints the factory by creating static methods.
*/
private fun printFactory(factory: Factory) {
private fun printFactory(factory: Factory, keepEmptyStrings: Boolean) {
startFactory(factory.fullName)
classesFactory.forEach {
addCommentFunction(it)
addBodyFunction(it)
addBodyFunction(it, keepEmptyStrings)
if (it.hasChoiceBloc) {
addSpecificCreateFunction(it)
addSpecificAddFunction(it)
Expand Down

0 comments on commit 9b9435a

Please sign in to comment.