| /** |
| ******************************************************************************** |
| * Copyright (c) 2018-2021 Robert Bosch GmbH and others. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Robert Bosch GmbH - initial API and implementation |
| ******************************************************************************** |
| */ |
| |
| package app4mc.example.transform.m2t.transformers; |
| |
| import java.util.Map; |
| |
| import org.eclipse.app4mc.amalthea.model.Amalthea; |
| import org.eclipse.app4mc.transformation.ServiceConstants; |
| import org.eclipse.app4mc.transformation.TransformationConstants; |
| import org.eclipse.app4mc.transformation.transformers.Model2TextRootTransformer; |
| import org.eclipse.app4mc.transformation.util.OutputBuffer; |
| import org.eclipse.app4mc.transformation.util.PropertyUtil; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.osgi.service.component.annotations.Activate; |
| import org.osgi.service.component.annotations.Component; |
| import org.osgi.service.component.annotations.ConfigurationPolicy; |
| import org.osgi.service.component.annotations.Reference; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.google.inject.Guice; |
| import com.google.inject.Injector; |
| |
| @Component( |
| configurationPid = ServiceConstants.SESSION_CONFIGURATION_PID, |
| configurationPolicy = ConfigurationPolicy.REQUIRE, |
| property = { ServiceConstants.TRANSFORMATION_PROPERTY + "=Amalthea2Text" }, |
| service = Model2TextRootTransformer.class |
| ) |
| public class ExampleModel2TextTransformer extends Model2TextRootTransformer { |
| |
| private static final Logger LOG = LoggerFactory.getLogger(ExampleModel2TextTransformer.class); |
| |
| @Reference |
| ExampleGuiceModuleFactory factory; |
| |
| private OutputBuffer outputBuffer; |
| |
| @Activate |
| void activate(final Map<String, ?> properties) { |
| LOG.info("ExampleModel2TextTransformer activated : {}", factory.getClass().getName()); |
| |
| outputBuffer = new OutputBuffer(); |
| outputBuffer.initialize(PropertyUtil.getProperty(TransformationConstants.M2T_OUTPUT_FOLDER, properties)); |
| outputBuffer.configureFiletype("TEXT", ".txt", "**************** BEGIN ***************\n\n", "***************** END ****************"); |
| } |
| |
| @Override |
| public void m2tTransformation(final ResourceSet inputResourceSet) { |
| |
| Injector injector = Guice.createInjector(factory.getModule()); |
| |
| ExampleTransformer exampleTransformer = injector.getInstance(ExampleTransformer.class); |
| |
| for (final Resource resource : inputResourceSet.getResources()) { |
| for (final EObject model : resource.getContents()) { |
| LOG.info("Processing file : {}", resource.getURI()); |
| |
| if (model instanceof Amalthea) { |
| exampleTransformer.generate((Amalthea) model, outputBuffer); |
| } |
| } |
| } |
| |
| outputBuffer.finish(false, false); |
| } |
| |
| } |