com.metaring.generator.util.java.Extensions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of generator-util-java Show documentation
Show all versions of generator-util-java Show documentation
MetaRing Utilities for Java-Based Code Generators
/**
* Copyright 2019 MetaRing s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metaring.generator.util.java;
import com.metaring.generator.model.data.Attribute;
import com.metaring.generator.model.data.Data;
import com.metaring.generator.model.data.Element;
import com.metaring.generator.model.data.Functionality;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
@SuppressWarnings("all")
public final class Extensions {
public static CharSequence getGeneratedPackageDeclaration(final Element element) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package �element.packageFQN�;");
return _builder;
}
public static CharSequence getPackageFQN(final Element element) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("�IF element.packagesChain !== null && !element.packagesChain.empty��FOR pn : element.packagesChain��pn��IF element.packagesChain.last !== pn�.�ENDIF��ENDFOR��ENDIF��IF typeof(Module).isInstance(element)�.�element.name��ENDIF�");
return _builder;
}
public static CharSequence getPackagePath(final Element element) {
String _replace = Extensions.getPackageFQN(element).toString().replace(".", "/");
return ((CharSequence) _replace);
}
public static String getDotAwareName(final com.metaring.generator.model.data.Module module) {
String name = module.getName();
boolean _contains = name.contains(".");
if (_contains) {
int _lastIndexOf = name.lastIndexOf(".");
int _plus = (_lastIndexOf + 1);
name = name.substring(_plus);
}
return name;
}
public static String getNativeFullyQualifiedNameForImport(final Attribute attribute) {
return Extensions.getNativeFullyQualifiedName(attribute, true);
}
public static String getNativeFullyQualifiedName(final Attribute attribute) {
return Extensions.getNativeFullyQualifiedName(attribute, false);
}
public static String getNativeFullyQualifiedName(final Data data) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("�FOR pn : data.packagesChain��pn�.�ENDFOR��data.name.toFirstUpper�");
return _builder.toString();
}
private static String getNativeFullyQualifiedName(final Attribute attribute, final boolean forImport) {
boolean _tripleEquals = (attribute == null);
if (_tripleEquals) {
return null;
}
boolean _isNative = attribute.isNative();
boolean _not = (!_isNative);
if (_not) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("�FOR pn : attribute.packagesChain��pn�.�ENDFOR��attribute.name.toFirstUpper��IF attribute.enumerator�Enumerator�ENDIF��IF attribute.many�Series�ENDIF�");
return _builder.toString();
}
if ((forImport && (((com.metaring.generator.model.util.Extensions.isSingleText(attribute) || com.metaring.generator.model.util.Extensions.isSingleDigit(attribute)) || com.metaring.generator.model.util.Extensions.isSingleRealDigit(attribute)) || com.metaring.generator.model.util.Extensions.isSingleTruth(attribute)))) {
return null;
}
boolean _isSingleText = com.metaring.generator.model.util.Extensions.isSingleText(attribute);
if (_isSingleText) {
return "java.lang.String";
}
boolean _isSingleDigit = com.metaring.generator.model.util.Extensions.isSingleDigit(attribute);
if (_isSingleDigit) {
return "java.lang.Long";
}
boolean _isSingleRealDigit = com.metaring.generator.model.util.Extensions.isSingleRealDigit(attribute);
if (_isSingleRealDigit) {
return "java.lang.Double";
}
boolean _isSingleTruth = com.metaring.generator.model.util.Extensions.isSingleTruth(attribute);
if (_isSingleTruth) {
return "java.lang.Boolean";
}
String fqn = (com.metaring.generator.model.util.Extensions.DEFAULT_PACKAGE_NAME + ".type.");
boolean _isUnknown = com.metaring.generator.model.util.Extensions.isUnknown(attribute);
boolean _not_1 = (!_isUnknown);
if (_not_1) {
boolean _isMany = attribute.isMany();
if (_isMany) {
String _fqn = fqn;
fqn = (_fqn + "series.");
}
String _fqn_1 = fqn;
String _name = attribute.getName();
fqn = (_fqn_1 + _name);
boolean _isMany_1 = attribute.isMany();
if (_isMany_1) {
String _fqn_2 = fqn;
fqn = (_fqn_2 + "Series");
}
} else {
String _fqn_3 = fqn;
fqn = (_fqn_3 + "DataRepresentation");
}
return fqn;
}
public static String getType(final Attribute attribute) {
boolean _tripleEquals = (attribute == null);
if (_tripleEquals) {
return null;
}
boolean _isNative = attribute.isNative();
boolean _not = (!_isNative);
if (_not) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("�attribute.name.toFirstUpper��IF attribute.enumerator�Enumerator�ENDIF��IF attribute.many�Series�ENDIF�");
return _builder.toString();
}
boolean _isSingleText = com.metaring.generator.model.util.Extensions.isSingleText(attribute);
if (_isSingleText) {
return "String";
}
boolean _isSingleDigit = com.metaring.generator.model.util.Extensions.isSingleDigit(attribute);
if (_isSingleDigit) {
return "Long";
}
boolean _isSingleRealDigit = com.metaring.generator.model.util.Extensions.isSingleRealDigit(attribute);
if (_isSingleRealDigit) {
return "Double";
}
boolean _isSingleTruth = com.metaring.generator.model.util.Extensions.isSingleTruth(attribute);
if (_isSingleTruth) {
return "Boolean";
}
String name = "";
boolean _isUnknown = com.metaring.generator.model.util.Extensions.isUnknown(attribute);
boolean _not_1 = (!_isUnknown);
if (_not_1) {
String _name = name;
String _name_1 = attribute.getName();
name = (_name + _name_1);
boolean _isMany = attribute.isMany();
if (_isMany) {
String _name_2 = name;
name = (_name_2 + "Series");
}
} else {
String _name_3 = name;
name = (_name_3 + "DataRepresentation");
}
return name;
}
public static String getDataOrNativeTypeFromJsonCreatorMethod(final Attribute attribute, final String jsonVarName) {
String _createDataOrNativeTypeFromJsonCreatorMethod = Extensions.createDataOrNativeTypeFromJsonCreatorMethod(attribute, jsonVarName);
return ("dataRepresentation" + _createDataOrNativeTypeFromJsonCreatorMethod);
}
public static String getDataOrNativeTypeFromJsonCreatorMethod(final Attribute attribute) {
return Extensions.createDataOrNativeTypeFromJsonCreatorMethod(attribute, null);
}
private static String createDataOrNativeTypeFromJsonCreatorMethod(final Attribute attribute, final String jsonVarName) {
try {
String typeName = attribute.getName();
boolean _isNative = attribute.isNative();
boolean _not = (!_isNative);
if (_not) {
typeName = StringExtensions.toFirstUpper(typeName);
boolean _isEnumerator = attribute.isEnumerator();
if (_isEnumerator) {
String _typeName = typeName;
typeName = (_typeName + "Enumerator");
}
}
boolean _isUnknown = com.metaring.generator.model.util.Extensions.isUnknown(attribute);
if (_isUnknown) {
boolean _tripleEquals = (jsonVarName == null);
if (_tripleEquals) {
return "";
}
typeName = "";
}
if (((!com.metaring.generator.model.util.Extensions.isUnknown(attribute)) && attribute.isMany())) {
String _typeName_1 = typeName;
typeName = (_typeName_1 + "Series");
}
String method = ".";
boolean _tripleEquals_1 = (jsonVarName == null);
if (_tripleEquals_1) {
String _method = method;
method = (_method + "as");
} else {
String _method_1 = method;
method = (_method_1 + "get");
}
boolean _isNative_1 = attribute.isNative();
if (_isNative_1) {
String _method_2 = method;
method = (_method_2 + typeName);
}
String _method_3 = method;
method = (_method_3 + "(");
boolean _tripleNotEquals = (jsonVarName != null);
if (_tripleNotEquals) {
String _method_4 = method;
method = (_method_4 + (("\"" + jsonVarName) + "\""));
}
boolean _isNative_2 = attribute.isNative();
boolean _not_1 = (!_isNative_2);
if (_not_1) {
boolean _tripleNotEquals_1 = (jsonVarName != null);
if (_tripleNotEquals_1) {
String _method_5 = method;
method = (_method_5 + ", ");
}
String _method_6 = method;
method = (_method_6 + (typeName + ".class"));
}
String _method_7 = method;
method = (_method_7 + ")");
return method;
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
e.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return "";
}
public static String getDataOrNativeTypeFromJsonCreatorMethodForFunctionality(final Attribute attribute, final String jsonVarName) {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("�jsonVarName� == null ? null : �jsonVarName�.trim().isEmpty() ? null : �jsonVarName�.equals(\"null\") ? null : ");
String singleNativePreamble = _builder.toString();
boolean _isSingleText = com.metaring.generator.model.util.Extensions.isSingleText(attribute);
if (_isSingleText) {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("�singleNativePreamble��jsonVarName�.substring(1, �jsonVarName�.length() - 1)");
return _builder_1.toString();
}
boolean _isSingleDigit = com.metaring.generator.model.util.Extensions.isSingleDigit(attribute);
if (_isSingleDigit) {
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("�singleNativePreamble�Long.parseLong(�jsonVarName�)");
return _builder_2.toString();
}
boolean _isSingleRealDigit = com.metaring.generator.model.util.Extensions.isSingleRealDigit(attribute);
if (_isSingleRealDigit) {
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append("�singleNativePreamble�Double.parseDouble(�jsonVarName�)");
return _builder_3.toString();
}
boolean _isSingleTruth = com.metaring.generator.model.util.Extensions.isSingleTruth(attribute);
if (_isSingleTruth) {
StringConcatenation _builder_4 = new StringConcatenation();
_builder_4.append("�singleNativePreamble�Boolean.parseBoolean(�jsonVarName�)");
return _builder_4.toString();
}
String typeName = attribute.getName();
boolean _isNative = attribute.isNative();
boolean _not = (!_isNative);
if (_not) {
typeName = StringExtensions.toFirstUpper(typeName);
boolean _isEnumerator = attribute.isEnumerator();
if (_isEnumerator) {
String _typeName = typeName;
typeName = (_typeName + "Enumerator");
}
}
if (((!com.metaring.generator.model.util.Extensions.isUnknown(attribute)) && attribute.isMany())) {
String _typeName_1 = typeName;
typeName = (_typeName_1 + "Series");
}
String _staticFieldNameForTools = com.metaring.generator.model.util.Extensions.toStaticFieldNameForTools(typeName);
String method = ("Tools.FACTORY_" + _staticFieldNameForTools);
boolean _isNative_1 = attribute.isNative();
boolean _not_1 = (!_isNative_1);
if (_not_1) {
method = typeName;
}
return (((method + ".fromJson(") + jsonVarName) + ")");
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
e.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return "";
}
public static boolean shouldImportTools(final Functionality functionality) {
return Extensions.shouldImportTools(functionality.getInput(), true);
}
public static boolean shouldImportTools(final Attribute attribute) {
return Extensions.shouldImportTools(attribute, false);
}
public static boolean shouldImportTools(final Attribute attribute, final boolean alsoUnknown) {
return ((attribute != null) && ((com.metaring.generator.model.util.Extensions.isEmail(attribute) || ((attribute.isNative() &&
attribute.isMany()) &&
(!com.metaring.generator.model.util.Extensions.isUnknown(attribute)))) || (com.metaring.generator.model.util.Extensions.isUnknown(attribute) && alsoUnknown)));
}
}