templates.json.JSONLoader.vm Maven / Gradle / Ivy
#* @vtlvariable name="ctx" type="org.kevoree.modeling.kotlin.generator.GenerationContext" *#
#* @vtlvariable name="helper" type="org.kevoree.modeling.kotlin.generator.ProcessorHelperClass" *#
#* @vtlvariable name="model" type="org.eclipse.emf.ecore.xmi.XMIResource" *#
#* @vtlvariable name="elemToLoad" type="org.eclipse.emf.ecore.EClass" *#
#* @vtlvariable name="allEClass" type="org.eclipse.emf.ecore.EClass" *#
package ${helper.fqn($ctx, $ctx.getBasePackageForUtilitiesGeneration())}.loader
import java.io.InputStream
import java.io.ByteArrayInputStream
class JSONModelLoader : org.kevoree.modeling.api.ModelLoader {
val mainFactory = ${helper.fqn($ctx, $ctx.getBasePackageForUtilitiesGeneration())}.factory.MainFactory()
#if($ctx.jS)
override fun loadModelFromString(str: String) : List? {
val bytes = ByteArray(str.length)
var i = 0
while(i < str.length){
bytes.set(i,str.get(i) as Byte)
i = i +1
}
return deserialize(ByteArrayInputStream(bytes))
}
#else
override fun loadModelFromString(str: String) : List? {
return deserialize(ByteArrayInputStream(str.toByteArray()))
}
#end
override fun loadModelFromStream(inputStream: InputStream) : List? {
return deserialize(inputStream)
}
private fun deserialize(instream : InputStream): List {
var reader : JsonReader = JsonReader(instream)
val context = LoadingContext()
while(reader.hasNext() && (reader.peek() != JsonToken.END_DOCUMENT)) {
reader.beginObject()
val nextKey = reader.nextName()
if(nextKey.equals("eClass")) {
val eclassValue = reader.nextString()
when {
#foreach($rootElement in $helper.collectAllClassifiersInModel($model))
#if(!$rootElement.getClass().getName().toLowerCase().contains("eenum"))
eclassValue.equals("${helper.fqn($ctx,$rootElement.getEPackage())}:$rootElement.getName()") -> {
context.loadedRoots.add(load$rootElement.getName()(reader, context))
}
#end
#end
else -> { println("Unknown root type '" + eclassValue + "'. Loading of this element aborted.")}
}
} else {
println("Ignored key '" + nextKey + "' while looking for the root element 'eClass'")
}
reader.endObject()
}
for(res in context.resolvers) {
res.run()
}
return context.loadedRoots
}
private fun unescapeJSON(src : String) : String {
var builder : String? = null
var i : Int = 0
while (i < src.length) {
val c = src[i]
if(c == '&') {
if(builder == null) {
builder = src.substring(0,i)
}
if(src[i+1]=='a') {
builder = builder!! + "'"
i = i+6
} else if(src[i+1]=='q') {
builder = builder!! + "\""
i = i+6
} else {
println("Could not unescaped chain:" + src[i] + src[i+1])
}
} else {
if(builder != null) {
builder = builder!! + c
}
i++
}
}
if(builder != null) {
return builder!!
} else {
return src
}
}
#foreach($eClazz in $allEClass)
#if( $eClazz.getEReferenceType().isAbstract() || $eClazz.getEReferenceType().isInterface())
#else
#generateLoadMethod($eClazz)
#end
#end
#macro( generateLoadMethod $elemToLoad )
private fun load$elemToLoad.getName()(reader : JsonReader, context : LoadingContext) : $helper.fqn($ctx, $elemToLoad) {
val modelElem = mainFactory.get${elemToLoad.getEPackage().getName().substring(0,1).toUpperCase()}${elemToLoad.getEPackage().getName().substring(1)}Factory().create$elemToLoad.getName()()
while (reader.hasNext()) {
val nextName = reader.nextName()
when(nextName) {
#foreach($att in $elemToLoad.getEAllAttributes())
#set($methName = "set" + $att.getName().substring(0, 1).toUpperCase() + $att.getName().substring(1))
#if($helper.convertType($att.getEAttributeType()).equals("Int"))
"$att.getName()" -> {modelElem.$methName(reader.nextInt())}
#elseif($helper.convertType($att.getEAttributeType()).equals("Boolean"))
"$att.getName()" -> {modelElem.$methName(reader.nextBoolean())}
#elseif($helper.convertType($att.getEAttributeType()).equals("Double"))
"$att.getName()" -> {modelElem.$methName(reader.nextDouble())}
#elseif($helper.convertType($att.getEAttributeType()).equals("Long"))
"$att.getName()" -> {modelElem.$methName(reader.nextLong())}
#elseif($att.getEAttributeType().getClass().getName().toLowerCase().contains("eenum"))
"$att.getName()" -> {modelElem.$methName(${helper.fqn($ctx,$att.getEAttributeType())}.valueOf(reader.nextString()!!))}
#else
"$att.getName()" -> {modelElem.$methName(unescapeJSON(reader.nextString()!!))}
#end
#end
#foreach($ref in $elemToLoad.getEAllReferences())
#if($ref.isContainment())
"$ref.getName()" -> { ##is containement and many
#if($ref.isMany())
reader.beginArray()
while(reader.hasNext()){
#end
reader.beginObject()
#set($concreteSubTypes=$helper.getAllConcreteSubTypes($ref.getEReferenceType()))
#set($isConcret= (!$ref.getEReferenceType().isAbstract()) && (!$ref.getEReferenceType().isInterface()))
#if(!( ($isConcret && $concreteSubTypes.size() == 0) || (!$isConcret && $concreteSubTypes.size() == 1) ))
val nextKey = reader.nextName()
if(nextKey.equals("eClass")) {
val eclassValue = reader.nextString()
when(eclassValue) {
#if($isConcret)
"$helper.fqn($ctx, $ref.getEReferenceType().getEPackage()):$ref.getEReferenceType().getName()" -> {
val loadedElem = load$ref.getEReferenceType().getName()(reader, context)
modelElem.#if($ref.isMany())add#{else}set#{end}$ref.getName().substring(0,1).toUpperCase()$ref.getName().substring(1)(loadedElem)
}
#end
#foreach($concreteType in $concreteSubTypes)
"$helper.fqn($ctx, $concreteType.getEPackage()):$concreteType.getName()" -> {
val loadedElem = load$concreteType.getName()(reader, context)
modelElem.#if($ref.isMany())add#{else}set#{end}$ref.getName().substring(0,1).toUpperCase()$ref.getName().substring(1)(loadedElem)
}
#end
else -> {
println("Unknown root type '" + eclassValue + "'. Loading aborted.")
}
}
} else {
println("Ignored key '" + nextKey + "' while looking for the root element 'eClass'")
}
#else
#if($concreteSubTypes.size() == 1)
val loadedElem = load$concreteSubTypes.get(0).getName()(reader, context)
#else
val loadedElem = load$ref.getEReferenceType().getName()(reader, context)
#end
modelElem.#if($ref.isMany())add#{else}set#{end}$ref.getName().substring(0,1).toUpperCase()$ref.getName().substring(1)(loadedElem)
#end
reader.endObject()
#if($ref.isMany())
}
reader.endArray()
#end
}
#{else}
"$ref.getName()" -> {##is not containement
#if($ref.isMany())
reader.beginArray()
while(reader.hasNext()) {
#end
val xmiRef = reader.nextString()!!
#if($ref.isMany())
context.resolvers.add(JSONResolveCommand(context, modelElem,org.kevoree.modeling.api.util.ActionType.ADD, "$ref.getName()", xmiRef))
#else
context.resolvers.add(JSONResolveCommand(context, modelElem,org.kevoree.modeling.api.util.ActionType.SET, "$ref.getName()", xmiRef))
#end
#if($ref.isMany())
}
reader.endArray()
#end
}
#end
#end
"eClass" -> { reader.nextString() }
else -> {println("Tag unrecognized: " + nextName + " in $elemToLoad.getName()")}
}
}
return modelElem
}
#end
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy