release version 1.10.0
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>ru.entaxy.esb.platform.runtime.base.connecting</groupId>
|
||||
<artifactId>generator</artifactId>
|
||||
<version>1.9.0</version>
|
||||
<version>1.10.0</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@ -33,11 +33,6 @@
|
||||
<artifactId>generator-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ru.entaxy.esb.system.core</groupId>
|
||||
<artifactId>template</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
|
@ -2,7 +2,7 @@
|
||||
* ~~~~~~licensing~~~~~~
|
||||
* ftl-generator
|
||||
* ==========
|
||||
* Copyright (C) 2020 - 2023 EmDev LLC
|
||||
* Copyright (C) 2020 - 2024 EmDev LLC
|
||||
* ==========
|
||||
* You may not use this file except in accordance with the License Terms of the Copyright
|
||||
* Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
|
||||
@ -52,215 +52,213 @@ import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import ru.entaxy.base.generator.template.TemplateAwareGenerator;
|
||||
import ru.entaxy.base.generator.template.TemplateImpl;
|
||||
import ru.entaxy.base.generator.template.exception.TemplateNotFound;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.AbstractSelfPublishGenerator;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generator;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.GeneratorService;
|
||||
import ru.entaxy.esb.platform.runtime.base.connecting.generator.factory.GeneratorFactory;
|
||||
import ru.entaxy.esb.system.common.exception.TemplateNotFound;
|
||||
|
||||
@Component(
|
||||
service = {GeneratorService.class},
|
||||
property = {GeneratorService.PROP_GENERATOR_TYPE + "=" + FTLGenerator.GENERATOR_NAME},
|
||||
immediate = true
|
||||
)
|
||||
public class FTLGenerator extends AbstractSelfPublishGenerator<FTLGenerator>
|
||||
implements Generator, TemplateAwareGenerator {
|
||||
service = {GeneratorService.class},
|
||||
property = {GeneratorService.PROP_GENERATOR_TYPE + "=" + FTLGenerator.GENERATOR_NAME},
|
||||
immediate = true)
|
||||
public class FTLGenerator extends AbstractSelfPublishGenerator<FTLGenerator>
|
||||
implements Generator, TemplateAwareGenerator {
|
||||
|
||||
public final static String GENERATOR_NAME = "ftl";
|
||||
|
||||
public final static String GENERATOR_NAME = "ftl";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(FTLGenerator.class);
|
||||
|
||||
public static final String PROP_TEMPLATE_NAME = "template.name";
|
||||
public static final String PROP_TEMPLATE_PATH = "template.path";
|
||||
|
||||
|
||||
private static final String ATTR_GENERATED_TYPE = "generated.type";
|
||||
|
||||
|
||||
private static final String FTL_EXTENSION = ".ftl";
|
||||
private static final String XML_EXTENSION = ".xml";
|
||||
|
||||
|
||||
private static final String DEFAULT_TEMPLATE_PATH = "/template/";
|
||||
|
||||
|
||||
// private BlueprintGenerator blueprintGenerator;
|
||||
private ru.entaxy.base.generator.template.Template template;
|
||||
|
||||
@Activate
|
||||
public void activate() {
|
||||
GeneratorFactory.register(FTLGenerator.GENERATOR_NAME, this);
|
||||
GeneratorFactory.register(FTLGenerator.GENERATOR_NAME, this);
|
||||
}
|
||||
|
||||
|
||||
@Deactivate
|
||||
public void deactivate() {
|
||||
GeneratorFactory.unregister(GENERATOR_NAME);
|
||||
GeneratorFactory.unregister(GENERATOR_NAME);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> getSupportedTemplateTypes() {
|
||||
return Arrays.asList(new String[] {"ftl"});
|
||||
return Arrays.asList(new String[] {"ftl"});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Generated generate(ru.entaxy.base.generator.template.Template template, Map<String, Object> properties)
|
||||
throws Exception {
|
||||
return generateForTemplate(template, properties);
|
||||
throws Exception {
|
||||
return generateForTemplate(template, properties);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Generated generate(Map<String, Object> properties) throws Exception {
|
||||
prepareTemplate(properties);
|
||||
return generateForTemplate(template, properties);
|
||||
prepareTemplate(properties);
|
||||
return generateForTemplate(template, properties);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isGeneratable(Map<String, Object> properties) throws Exception {
|
||||
prepareTemplate(properties);
|
||||
if (template == null)
|
||||
return false;
|
||||
URL templateURL = template.getTemplateLocation()==null
|
||||
?FrameworkUtil.getBundle(FTLGenerator.class).getEntry(DEFAULT_TEMPLATE_PATH)
|
||||
:template.getTemplateLocation();
|
||||
try {
|
||||
Template temp = getTemplateByFullName(templateURL, template.getTemplateFullName());
|
||||
return (temp != null);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
prepareTemplate(properties);
|
||||
if (template == null)
|
||||
return false;
|
||||
URL templateURL = template.getTemplateLocation() == null
|
||||
? FrameworkUtil.getBundle(FTLGenerator.class).getEntry(DEFAULT_TEMPLATE_PATH)
|
||||
: template.getTemplateLocation();
|
||||
try {
|
||||
Template temp = getTemplateByFullName(templateURL, template.getTemplateFullName());
|
||||
return (temp != null);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Generated generateForTemplate(ru.entaxy.base.generator.template.Template template,
|
||||
Map<String, Object> properties) throws Exception {
|
||||
|
||||
URL templateURL = template.getTemplateLocation()==null
|
||||
?FrameworkUtil.getBundle(FTLGenerator.class).getEntry(DEFAULT_TEMPLATE_PATH)
|
||||
:template.getTemplateLocation();
|
||||
try {
|
||||
return doGenerate(
|
||||
templateURL,
|
||||
template.getTemplateFullName(),
|
||||
properties);
|
||||
} catch (Exception e) {
|
||||
log.error("Error generating for template [" + template.getId() + "]", e);
|
||||
throw e;
|
||||
}
|
||||
public Generated generateForTemplate(ru.entaxy.base.generator.template.Template template,
|
||||
Map<String, Object> properties) throws Exception {
|
||||
|
||||
URL templateURL = template.getTemplateLocation() == null
|
||||
? FrameworkUtil.getBundle(FTLGenerator.class).getEntry(DEFAULT_TEMPLATE_PATH)
|
||||
: template.getTemplateLocation();
|
||||
try {
|
||||
return doGenerate(
|
||||
templateURL,
|
||||
template.getTemplateFullName(),
|
||||
properties);
|
||||
} catch (Exception e) {
|
||||
log.error("Error generating for template [" + template.getId() + "]", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static class Enricher {
|
||||
Generated origin;
|
||||
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
|
||||
String extraPrefix = "extra.";
|
||||
|
||||
public Enricher(Generated origin) {
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public Enricher cleanOrigin() {
|
||||
this.origin.getProperties().clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher property(String name, Object value) {
|
||||
properties.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher properties(Map<String, Object> properties) {
|
||||
this.properties.putAll(properties);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher extraPrefix(String extraPrefix) {
|
||||
this.extraPrefix = extraPrefix;
|
||||
return this;
|
||||
}
|
||||
Generated origin;
|
||||
|
||||
public Enricher extra(String name, Object value) {
|
||||
properties.put(extraPrefix + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher extras(Map<String, Object> properties) {
|
||||
for (Map.Entry<String, Object> entry: properties.entrySet())
|
||||
this.properties.put(extraPrefix + entry.getKey(), entry.getValue());
|
||||
return this;
|
||||
}
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
|
||||
public Generated get() {
|
||||
this.origin.getProperties().putAll(this.properties);
|
||||
this.origin.getProperties().put("_extraPrefix", extraPrefix);
|
||||
return this.origin;
|
||||
}
|
||||
String extraPrefix = "extra.";
|
||||
|
||||
public Enricher(Generated origin) {
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public Enricher cleanOrigin() {
|
||||
this.origin.getProperties().clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher property(String name, Object value) {
|
||||
properties.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher properties(Map<String, Object> properties) {
|
||||
this.properties.putAll(properties);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher extraPrefix(String extraPrefix) {
|
||||
this.extraPrefix = extraPrefix;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher extra(String name, Object value) {
|
||||
properties.put(extraPrefix + name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enricher extras(Map<String, Object> properties) {
|
||||
for (Map.Entry<String, Object> entry : properties.entrySet())
|
||||
this.properties.put(extraPrefix + entry.getKey(), entry.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generated get() {
|
||||
this.origin.getProperties().putAll(this.properties);
|
||||
this.origin.getProperties().put("_extraPrefix", extraPrefix);
|
||||
return this.origin;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static Enricher enrich(Generated origin) {
|
||||
return new Enricher(origin);
|
||||
return new Enricher(origin);
|
||||
}
|
||||
|
||||
private Generated doGenerate(URL templateUrl, String templateFullName, Map<String, Object> params)
|
||||
throws IOException, TemplateException, TemplateNotFound {
|
||||
Template temp = getTemplateByFullName(templateUrl, templateFullName);
|
||||
Object value = temp.getCustomAttribute(FTLGenerator.ATTR_GENERATED_TYPE);
|
||||
String type = null==value?null:value.toString();
|
||||
log.debug("--TEMPLATE--\n" + templateUrl.toString() + templateFullName + "\ngenerated.type: " + (null==value?Generated.GENERATED_TYPE_UNKNOWN:value.toString()));
|
||||
String type = null == value ? null : value.toString();
|
||||
log.debug("--TEMPLATE--\n" + templateUrl.toString() + templateFullName + "\ngenerated.type: "
|
||||
+ (null == value ? Generated.GENERATED_TYPE_UNKNOWN : value.toString()));
|
||||
try (StringWriter writer = new StringWriter()) {
|
||||
temp.process(params, writer);
|
||||
return enrich(Generated.create(type, writer.toString()))
|
||||
.properties(params)
|
||||
.extraPrefix("generator.")
|
||||
.extra("templateUrl", templateUrl)
|
||||
.extra("templateFullName", value)
|
||||
.get();
|
||||
.properties(params)
|
||||
.extraPrefix("generator.")
|
||||
.extra("templateUrl", templateUrl)
|
||||
.extra("templateFullName", value)
|
||||
.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected void prepareTemplate(Map<String, Object> generationProperties) {
|
||||
if (this.template != null)
|
||||
return;
|
||||
|
||||
Map<String, Object> finalProperties = new HashMap<>();
|
||||
finalProperties.putAll(initialProperties);
|
||||
finalProperties.putAll(generationProperties);
|
||||
|
||||
TemplateImpl temp = new TemplateImpl();
|
||||
temp.setBundleContext(
|
||||
(BundleContext)finalProperties.getOrDefault(Generator.PROP_TARGET_BUNDLE_CONTEXT
|
||||
, FrameworkUtil.getBundle(FTLGenerator.class).getBundleContext())
|
||||
);
|
||||
temp.setTemplateName((String)finalProperties.getOrDefault(PROP_TEMPLATE_NAME
|
||||
, (String)finalProperties.getOrDefault(Generator.PROP_USAGE_TYPE
|
||||
, "root")));
|
||||
temp.setPath((String)finalProperties.getOrDefault(PROP_TEMPLATE_NAME
|
||||
, DEFAULT_TEMPLATE_PATH)
|
||||
);
|
||||
|
||||
temp.setTemplateFullName(temp.getTemplateFileName() + FTL_EXTENSION);
|
||||
|
||||
this.template = temp;
|
||||
|
||||
if (this.template != null)
|
||||
return;
|
||||
|
||||
Map<String, Object> finalProperties = new HashMap<>();
|
||||
finalProperties.putAll(initialProperties);
|
||||
finalProperties.putAll(generationProperties);
|
||||
|
||||
TemplateImpl temp = new TemplateImpl();
|
||||
temp.setBundleContext(
|
||||
(BundleContext) finalProperties.getOrDefault(Generator.PROP_TARGET_BUNDLE_CONTEXT,
|
||||
FrameworkUtil.getBundle(FTLGenerator.class).getBundleContext()));
|
||||
temp.setTemplateName((String) finalProperties.getOrDefault(PROP_TEMPLATE_NAME,
|
||||
(String) finalProperties.getOrDefault(Generator.PROP_USAGE_TYPE, "root")));
|
||||
temp.setPath((String) finalProperties.getOrDefault(PROP_TEMPLATE_NAME, DEFAULT_TEMPLATE_PATH));
|
||||
|
||||
temp.setTemplateFullName(temp.getTemplateFileName() + FTL_EXTENSION);
|
||||
|
||||
this.template = temp;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private Generated doGenerate(URL templateUrl, String templateName, String systemName, Map<String, String> params)
|
||||
throws IOException, TemplateException, TemplateNotFound {
|
||||
Template temp = getTemplateByName(templateUrl, templateName);
|
||||
Object value = temp.getCustomAttribute(FTLGenerator.ATTR_GENERATED_TYPE);
|
||||
String type = null==value?null:value.toString();
|
||||
log.info("--BLUEPRINT--\n" + templateUrl.toString() + templateName + "\ngenerated.type: " + (null==value?Generated.GENERATED_TYPE_UNKNOWN:value.toString()));
|
||||
String type = null == value ? null : value.toString();
|
||||
log.info("--BLUEPRINT--\n" + templateUrl.toString() + templateName + "\ngenerated.type: "
|
||||
+ (null == value ? Generated.GENERATED_TYPE_UNKNOWN : value.toString()));
|
||||
try (StringWriter writer = new StringWriter()) {
|
||||
temp.process(params, writer);
|
||||
return Generated.create(type, writer.toString());
|
||||
return Generated.create(type, writer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private Template getTemplateByFullName(URL templateUrl, String templateFullName) throws IOException, TemplateNotFound {
|
||||
private Template getTemplateByFullName(URL templateUrl, String templateFullName)
|
||||
throws IOException, TemplateNotFound {
|
||||
Configuration cfg = getConfiguration(templateUrl);
|
||||
return cfg.getTemplate(templateFullName);
|
||||
}
|
||||
|
||||
|
||||
private Template getTemplateByName(URL templateUrl, String templateName) throws IOException, TemplateNotFound {
|
||||
return getTemplateByFullName(templateUrl, templateName + FTL_EXTENSION);
|
||||
return getTemplateByFullName(templateUrl, templateName + FTL_EXTENSION);
|
||||
}
|
||||
|
||||
private Configuration getConfiguration(URL templateUrl) throws TemplateNotFound {
|
||||
@ -270,6 +268,9 @@ public class FTLGenerator extends AbstractSelfPublishGenerator<FTLGenerator>
|
||||
freemarkerConfig.setTemplateLoader(getURLTemplateLoader(templateUrl));
|
||||
freemarkerConfig.setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);
|
||||
freemarkerConfig.setTagSyntax(SQUARE_BRACKET_TAG_SYNTAX);
|
||||
//no localization of number and boolean
|
||||
freemarkerConfig.setNumberFormat("computer");
|
||||
freemarkerConfig.setBooleanFormat("c");
|
||||
return freemarkerConfig;
|
||||
}
|
||||
|
||||
@ -278,14 +279,14 @@ public class FTLGenerator extends AbstractSelfPublishGenerator<FTLGenerator>
|
||||
@Override
|
||||
protected URL getURL(String templateName) {
|
||||
try {
|
||||
if (templateName.contains(":"))
|
||||
return new URL(templateName);
|
||||
if (templateName.contains(":"))
|
||||
return new URL(templateName);
|
||||
return new URL(templateUrl + templateName);
|
||||
} catch (MalformedURLException e) {
|
||||
// skip printing typical error
|
||||
// while searching for template
|
||||
if ((templateUrl!=null) || (!"root.ftl".equals(templateName)))
|
||||
log.error(e.getMessage(), e);
|
||||
// skip printing typical error
|
||||
// while searching for template
|
||||
if ((templateUrl != null) || (!"root.ftl".equals(templateName)))
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user