blob: 12d8c391950bae248886a337e7e5180ba6483b5c [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*
*/
package org.eclipse.osbp.xtext.oxtype.value;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXImportDeclaration;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXtypeFactory;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.xtype.XImportDeclaration;
import org.eclipse.xtext.xtype.XImportSection;
public class OXTypeUtil {
/**
* Tries to find the name by a "name" {@link EAttribute}.
*
* @param eObject
* @return
*/
public static String getNameOrNull(EObject eObject) {
EStructuralFeature nameFeature = eObject.eClass().getEStructuralFeature("name");
if (nameFeature != null) {
Object value = eObject.eGet(nameFeature);
if (value instanceof String) {
return (String) value;
}
}
return null;
}
public static String getImportedName(XImportDeclaration decl) {
OXImportDeclaration declCustom = (OXImportDeclaration) decl;
if (declCustom.isWildcard()) {
return declCustom.getImportedNamespace();
} else if (declCustom.isFqnImport()) {
return declCustom.getImportedFullyQualifiedName();
}
return declCustom.getImportedName();
}
public static List<OXImportDeclaration> getImportDeclarations(XImportSection section) {
@SuppressWarnings("unchecked")
List<OXImportDeclaration> result = section != null
? new ArrayList<OXImportDeclaration>(
(Collection<? extends OXImportDeclaration>) section.getImportDeclarations())
: new ArrayList<>();
return result;
}
public static void addFQNImport(XImportSection importSection, QualifiedName fqn) {
OXImportDeclaration decl = OXtypeFactory.eINSTANCE.createOXImportDeclaration();
decl.setFqnImport(true);
decl.setImportedFullyQualifiedName(fqn.toString());
importSection.getImportDeclarations().add(decl);
}
public static void addFQNImportIfNotExists(XImportSection importSection, QualifiedName fqn) {
// check if already exists
for (XImportDeclaration decl : importSection.getImportDeclarations()) {
OXImportDeclaration oDecl = (OXImportDeclaration) decl;
if (oDecl.isFqnImport() && oDecl.getImportedFullyQualifiedName().equals(fqn.toString())) {
return;
}
}
OXImportDeclaration decl = OXtypeFactory.eINSTANCE.createOXImportDeclaration();
decl.setFqnImport(true);
decl.setImportedFullyQualifiedName(fqn.toString());
importSection.getImportDeclarations().add(decl);
}
public static <T> T makeInstance(String modelExtension, Class<T> clazz) {
return IResourceServiceProvider.Registry.INSTANCE
.getResourceServiceProvider(URI.createFileURI("file://my.osbp." + modelExtension)).get(clazz);
}
}