use of org.eclipse.vorto.repository.core.ModelResource in project vorto by eclipse.
the class VortoModelImporter method refactor.
private FileUpload refactor(FileUpload fileUpload, String targetNamespace) {
IModelWorkspace workspace = IModelWorkspace.newReader().addFile(new ByteArrayInputStream(fileUpload.getContent()), ModelType.fromFileName(fileUpload.getFileName())).read();
ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(targetNamespace).execute();
ModelResource resource = new ModelResource(changeSet.get().get(0));
return FileUpload.create(fileUpload.getFileName(), resource.toDSL());
}
use of org.eclipse.vorto.repository.core.ModelResource in project vorto by eclipse.
the class VortoModelImporter method preValidation.
/**
* changes the namespace of the uploaded vorto model(s) , if target namespace is specified
*/
@Override
protected FileUpload preValidation(FileUpload fileUpload, Context context) {
if (context.getTargetNamespace().isPresent()) {
if (fileUpload.getFileExtension().endsWith(EXTENSION_ZIP)) {
ModelWorkspaceReader reader = IModelWorkspace.newReader();
getUploadedFilesFromZip(fileUpload.getContent()).stream().filter(this::isSupported).forEach(extractedFile -> {
reader.addFile(new ByteArrayInputStream(addVortolangIfMissing(extractedFile).getContent()), ModelType.fromFileName(extractedFile.getFileExtension()));
});
IModelWorkspace workspace = reader.read();
ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(context.getTargetNamespace().get()).execute();
ZipUploadFile zipFile = new ZipUploadFile(fileUpload.getFileName());
for (Model model : changeSet.get()) {
ModelResource resource = new ModelResource(model);
zipFile.addToZip(FileUpload.create(resource.getId().getPrettyFormat().replace("\\.", "_") + resource.getType().getExtension(), resource.toDSL()));
}
return zipFile.getFileUpload();
} else {
return refactor(addVortolangIfMissing(fileUpload), context.getTargetNamespace().get());
}
} else {
return addVortolangIfMissing(fileUpload);
}
}
use of org.eclipse.vorto.repository.core.ModelResource in project vorto by eclipse.
the class VortoModelImporter method convert.
@Override
protected List<ModelResource> convert(FileUpload fileUpload, Context context) {
List<ModelResource> result = new ArrayList<>();
if (fileUpload.getFileExtension().equalsIgnoreCase(EXTENSION_ZIP)) {
Collection<FileContent> fileContents = getFileContents(fileUpload);
result.addAll(fileContents.stream().map(fileContent -> (ModelResource) getModelParserFactory().getParser(fileContent.getFileName()).parse(new ByteArrayInputStream(fileContent.getContent()))).collect(Collectors.toList()));
} else {
final ModelResource modelInfo = (ModelResource) getModelParserFactory().getParser(fileUpload.getFileExtension()).parse(new ByteArrayInputStream(fileUpload.getContent()));
result.add(modelInfo);
}
return Collections.unmodifiableList(result);
}
use of org.eclipse.vorto.repository.core.ModelResource in project vorto by eclipse.
the class AbstractModelImporter method parseDSL.
protected ModelResource parseDSL(String fileName, byte[] content, Collection<FileContent> fileReferences) {
IModelParser modelParser = modelParserFactory.getParser(fileName);
modelParser.setWorkspace(new LocalModelWorkspace(modelRepoFactory, fileReferences));
modelParser.enableValidation();
return (ModelResource) modelParser.parse(new ByteArrayInputStream(content));
}
use of org.eclipse.vorto.repository.core.ModelResource in project vorto by eclipse.
the class TypeImportValidation method validate.
@Override
public void validate(ModelInfo modelResource, InvocationContext context) throws ValidationException {
if (!(modelResource instanceof ModelResource)) {
// do not check types if passed resource is not a parse emf model resource
return;
}
Collection<String> unImportedReferences = Lists.newArrayList();
ModelResource emfModel = (ModelResource) modelResource;
Model model = emfModel.getModel();
if (model == null)
return;
if (model instanceof Entity) {
unImportedReferences.addAll(getUnimportedProperties(((Entity) model).getProperties(), model.getReferences()));
} else if (model instanceof FunctionblockModel) {
unImportedReferences.addAll(validateFunctionBlock((FunctionblockModel) model));
} else if (model instanceof InformationModel) {
unImportedReferences.addAll(getUnimportedFunctionblocks(((InformationModel) model).getProperties(), model.getReferences()));
}
unImportedReferences.forEach(ref -> System.out.println("Missing : " + ref));
if (!unImportedReferences.isEmpty()) {
throw new ValidationException(errorMessage(unImportedReferences), modelResource);
}
}
Aggregations