| /** |
| * |
| * Copyright (c) 2011, 2016 - 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.ide.core.ui.softwarefactory.extender; |
| |
| import java.io.IOException; |
| import java.text.Normalizer; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.util.Diagnostic; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.Diagnostician; |
| import org.eclipse.osbp.xtext.addons.EObjectHelper; |
| import org.eclipse.xtext.naming.QualifiedName; |
| import org.eclipse.xtext.resource.IEObjectDescription; |
| import org.eclipse.xtext.resource.IResourceDescription; |
| import org.eclipse.xtext.resource.IResourceDescriptions; |
| import org.eclipse.xtext.resource.SaveOptions; |
| import org.eclipse.xtext.resource.XtextResourceSet; |
| import org.eclipse.xtext.ui.resource.XtextResourceSetProvider; |
| import org.eclipse.xtext.validation.FeatureBasedDiagnostic; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class ModelExtenderUtils { |
| private ModelExtenderUtils() {} |
| public enum DataType { |
| NONE("none", null, null), DOUBLE("double", null, null), DOUBLECOLON("double", "###.00", "de_DE"), INT("int", null, null), |
| UUID("uuid", null, null), TEXT("String", null, null), DATE("Date", "yyyyMMdd", null), BOOLEAN("boolean", null, null), |
| // wora 15.12.2017 missing datatype |
| BLOBMAPPING("BlobMapping", null, null); |
| |
| private String value; |
| private String format; |
| private String locale; |
| |
| private DataType(String value, String format, String locale) { |
| this.value = value; |
| this.format = format; |
| this.locale = locale; |
| } |
| |
| private DataType(DataType type) { |
| this.value = type.value; |
| this.format = type.format; |
| this.locale = type.locale; |
| } |
| |
| public String getValue() { |
| return value; |
| } |
| |
| public String getFormat() { |
| return format; |
| } |
| |
| public void setFormat(String format) { |
| this.format = format; |
| } |
| |
| public String getLocale() { |
| return locale; |
| } |
| |
| } |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(ModelExtenderUtils.class); |
| |
| // persist the modified model |
| public static void writeModel(EObject model, String id) { |
| try { |
| LOGGER.info("running diagnostic for {}",id); |
| Diagnostic diagnostic = Diagnostician.INSTANCE.validate(model.eResource().getContents().get(0)); |
| int sev = diagnostic.getSeverity(); |
| if (sev <= Diagnostic.WARNING) { |
| LOGGER.info("writing {}",id); |
| model.eResource().save(SaveOptions.newBuilder().format().getOptions().toOptionsMap()); |
| } else if(sev == Diagnostic.ERROR) { |
| // wora 11.12.2017 just making sure we get a proper error message shown to us |
| String err = ""; |
| if(!model.eResource().getErrors().isEmpty()){ // In case the model itself contains errors |
| err = model.eResource().getErrors().get(0).toString(); |
| } |
| String childrenErrorDetail = ""; |
| for(Object diag : diagnostic.getChildren()){ |
| FeatureBasedDiagnostic f = ((FeatureBasedDiagnostic) diag); |
| String mes = f.getMessage(); |
| String source = f.getSource(); |
| childrenErrorDetail += "\n\nERROR MESSAGE: "+mes+ "\n\nSOURCE:"+ source + "\n\n"; |
| } |
| err = "Grammar Diagnostic ERROR\nMODEL: "+id+ childrenErrorDetail; |
| |
| LOGGER.error("diagnosic error {}", err); |
| throw new IllegalArgumentException(err); |
| } |
| LOGGER.info("finished writing {}",id); |
| } catch (IOException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| // get a name without characters from locale anomaly or punctuation, replace blanks with underscores |
| // avoid some keywords |
| public static String normalizeName(String name) { |
| String normalized = Normalizer.normalize(name, Normalizer.Form.NFKD).toLowerCase().replaceAll("[^a-z0-9 ]", "").replace(" ", "_"); |
| if(normalized.equalsIgnoreCase("description")) { |
| normalized = "_"+normalized; |
| } |
| return normalized; |
| } |
| |
| // get an EObject from a full qualified datatype name |
| public static EObject findAllEObject(IResourceDescriptions descs, EClass type, String fqn) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjects(type, QualifiedName.create(fqn.split("\\.")), true); |
| if(result.iterator().hasNext()) { |
| IEObjectDescription desc = result.iterator().next(); |
| return desc.getEObjectOrProxy(); |
| } |
| return null; |
| } |
| |
| // get an EObject list |
| public static boolean findAllEObjects(IResourceDescriptions descs, EClass type, List<EObject> objects) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjectsByType(type); |
| Iterator<IEObjectDescription> iter = result.iterator(); |
| while(iter.hasNext()) { |
| IEObjectDescription desc = iter.next(); |
| objects.add(desc.getEObjectOrProxy()); |
| } |
| return !objects.isEmpty(); |
| } |
| |
| // get an EObject from a full qualified datatype name |
| public static EObject findEObject(IResourceDescription descs, EClass type, String fqn) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjects(type, QualifiedName.create(fqn.split("\\.")), true); |
| if(result.iterator().hasNext()) { |
| IEObjectDescription desc = result.iterator().next(); |
| return desc.getEObjectOrProxy(); |
| } |
| return null; |
| } |
| |
| // get all EObjects from a class |
| public static boolean findEObjects(IResourceDescription descs, EClass type, List<EObject> objects) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjectsByType(type); |
| Iterator<IEObjectDescription> iter = result.iterator(); |
| while(iter.hasNext()) { |
| IEObjectDescription desc = iter.next(); |
| objects.add(desc.getEObjectOrProxy()); |
| } |
| return !objects.isEmpty(); |
| } |
| |
| /** |
| * Returns the model which is to be modified, identified by projectPath. |
| * |
| * @param projectPath |
| * @return |
| */ |
| public static <A extends EObject> A loadSemanticModel(ModelInstanceDescription packageDelta, XtextResourceSetProvider provider) { |
| LOGGER.info("loading:" + packageDelta.getProject().getName()); |
| XtextResourceSet rs = (XtextResourceSet) provider.get(packageDelta.getProject()); |
| Resource modelResource = rs.getResource(packageDelta.getModelURI(), true); |
| try { |
| modelResource.load(null); |
| } catch (IOException e) { |
| LOGGER.error(e.getLocalizedMessage()); |
| return null; |
| } |
| @SuppressWarnings("unchecked") |
| A model = (A) EObjectHelper.getSemanticElement(modelResource); |
| LOGGER.info("finished loading:" + packageDelta.getProject().getName()); |
| return model; |
| } |
| } |