| /******************************************************************************* |
| * |
| * Copyright (c) 2018, 2020 Robert Bosch GmbH. |
| * |
| * 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.product.handlers; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.net.URL; |
| import java.util.Properties; |
| |
| import javax.inject.Named; |
| |
| import org.eclipse.app4mc.transformation.TransformationProcessor; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.runtime.FileLocator; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.e4.core.di.annotations.Execute; |
| import org.eclipse.e4.core.di.extensions.Service; |
| import org.eclipse.e4.ui.services.IServiceConstants; |
| import org.eclipse.jface.dialogs.ErrorDialog; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.swt.widgets.Shell; |
| import org.osgi.framework.Bundle; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| @SuppressWarnings("restriction") |
| public class SampleHandler { |
| |
| private static final Logger LOG = LoggerFactory.getLogger(SampleHandler.class); |
| |
| @Execute |
| public Object execute(Shell shell, |
| @Service TransformationProcessor cmd, |
| @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) throws ExecutionException { |
| |
| LOG.debug("\n\n\n****************************************************************************************************************"); |
| LOG.debug("TransformationProcessor : "+cmd.hashCode()); |
| try { |
| |
| Properties properties = new Properties(); |
| String input = getResourceFromPlugin("/input/amalthea_models"); |
| properties.put("input_models_folder", input); |
| File inputFile = new File(input); |
| File parentFile = inputFile.getParentFile().getParentFile(); |
| properties.put("output_folder", new File(parentFile + File.separator + "output/amalthea_models").getCanonicalPath()); |
| |
| properties.put("m2mTransformers", "Amalthea2SampleModel"); |
| properties.put("m2tTransformers", "Amalthea2Text"); |
| |
| cmd.startTransformation(properties); |
| // cmd.startTransformation(new File(getResourceFromPlugin("/input.properties"))); |
| |
| } catch (Exception e) { |
| ErrorDialog.openError(shell, "Error", e.getMessage(), Status.OK_STATUS); |
| } |
| |
| LOG.debug("****************************************************************************************************************"); |
| return null; |
| } |
| |
| public String getResourceFromPlugin(String path) throws IOException { |
| |
| Bundle bundle = Platform.getBundle("app4mc.example.transform.product"); |
| |
| if (bundle != null) { |
| URL entry = bundle.getEntry(path); |
| if (entry != null) { |
| URL fileURL = FileLocator.toFileURL(entry); |
| if (fileURL != null) { |
| return fileURL.getFile(); |
| } |
| } |
| } else { |
| LOG.error("Bundle app4mc.example.transform.product is not available, and due to this reason input.properties file can not be fetched"); |
| } |
| return null; |
| } |
| |
| |
| } |