| /******************************************************************************* |
| * Copyright (c) 2019 CEA LIST. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v2.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v20.html |
| * |
| * Contributors: |
| * Shuai Li (CEA LIST) <shuai.li@cea.fr> - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.papyrus.iotml.wot.td.codegen.transformation; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.papyrus.designer.languages.common.base.HierarchyLocationStrategy; |
| import org.eclipse.papyrus.designer.languages.common.base.ModelElementsCreator; |
| import org.eclipse.papyrus.infra.tools.file.ProjectBasedFileAccess; |
| import org.eclipse.papyrus.iotml.wot.Thing; |
| import org.eclipse.papyrus.iotml.wot.td.codegen.generator.ThingGenerator; |
| import org.eclipse.uml2.uml.Element; |
| import org.eclipse.uml2.uml.PackageableElement; |
| import org.eclipse.uml2.uml.Property; |
| import org.eclipse.uml2.uml.util.UMLUtil; |
| |
| /** |
| * Main class of TD code generator. |
| */ |
| public class TDModelElementsCreator extends ModelElementsCreator { |
| |
| private static final String TD_LANG = "TD"; //$NON-NLS-1$ |
| |
| private static final String JSON_SUFFIX = "json"; |
| |
| protected String tdExt; |
| |
| /** |
| * Constructor. |
| * |
| * @param project |
| * the project in which the generated code should be placed |
| * @param modelToTD |
| * the translation of the Model to the TD code |
| */ |
| public TDModelElementsCreator(IProject project, PackageableElement pe) { |
| this(project, TD_LANG, pe); |
| } |
| |
| /** |
| * Constructor. |
| * |
| * @param project |
| * the project in which the generated code should be placed |
| * @param language |
| * the generator language |
| * @param modelToTD |
| * the translation of the Model to the TD code |
| */ |
| public TDModelElementsCreator(IProject project, String language, PackageableElement pe) { |
| super(new ProjectBasedFileAccess(project), new HierarchyLocationStrategy(), language); |
| this.project = project; |
| tdExt = JSON_SUFFIX; |
| } |
| |
| |
| /** |
| * @see org.eclipse.papyrus.designer.languages.common.base.ModelElementsCreator#noCodeGen(org.eclipse.uml2.uml.Element) |
| * |
| * @param element |
| * @return |
| */ |
| @Override |
| protected boolean noCodeGen(Element element) { |
| return false; |
| } |
| |
| /** |
| * @see org.eclipse.papyrus.designer.languages.common.base.ModelElementsCreator#createPackageableElementFile(org.eclipse.uml2.uml.PackageableElement, org.eclipse.core.runtime.IProgressMonitor) |
| * |
| * @param pe |
| * @param monitor |
| */ |
| @Override |
| protected void createPackageableElementFile(PackageableElement pe, IProgressMonitor monitor) { |
| generateThing(pe); |
| for (Element element : pe.allOwnedElements()) { |
| if (element instanceof Property) { |
| Property property = (Property) element; |
| if (property.getType() != null) { |
| generateThing(property.getType()); |
| } |
| } |
| generateThing(element); |
| } |
| } |
| |
| /** |
| * Generate the TD code for each sub entity. |
| * |
| * @param topEntity |
| * the top entity (top class) |
| * @param modelToTD |
| * the translation of the Model to the TD code |
| */ |
| private void generateThing(Element element) { |
| Thing thing = UMLUtil.getStereotypeApplication(element, Thing.class); |
| if (thing != null) { |
| generateTDCode(thing); |
| } |
| } |
| |
| |
| /** |
| * Generate the final TD code file. |
| */ |
| private void generateTDCode(Thing thing) { |
| String result = ThingGenerator.generateTDCode(thing).toString(); |
| String fileNameTD = locStrategy.getFileName(thing.getBase_Class()) + "." + tdExt; //$NON-NLS-1$ |
| fileSystemAccess.generateFile(fileNameTD, result); |
| } |
| } |