package jetbrains.mps.tool.builder.converter; /*Generated by MPS */ import java.util.Map; import jetbrains.mps.core.platform.Platform; import jetbrains.mps.core.platform.PlatformFactory; import jetbrains.mps.core.platform.PlatformOptionsBuilder; import jetbrains.mps.RuntimeFlags; import jetbrains.mps.vfs.IFile; import jetbrains.mps.vfs.FileSystem; import org.jetbrains.mps.openapi.persistence.ModelFactory; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import jetbrains.mps.util.FileUtil; import java.util.HashMap; import jetbrains.mps.persistence.DefaultModelPersistence; import jetbrains.mps.persistence.MetaModelInfoProvider; import org.jetbrains.mps.openapi.model.SModel; import jetbrains.mps.extapi.persistence.FileDataSource; import jetbrains.mps.project.MPSExtentions; import java.io.IOException; import org.jetbrains.mps.openapi.persistence.ModelSaveException; public class ConvertToBinaryWorker { public ConvertToBinaryWorker() { } public void convert(final Map<String, String> map, final Boolean stripImplementation) { final Platform platform = PlatformFactory.initPlatform(PlatformOptionsBuilder.PERSISTENCE); RuntimeFlags.setMergeDriverMode(true); try { for (Map.Entry<String, String> entry : map.entrySet()) { convertModelToBinary(entry.getKey(), entry.getValue(), stripImplementation); } } finally { platform.dispose(); } } private void convertModelToBinary(String sourceFile, String destFile, boolean stripImplementation) { IFile source = FileSystem.getInstance().getFileByPath(sourceFile); ModelFactory modelFactory = PersistenceFacade.getInstance().getModelFactory(FileUtil.getExtension(source.getName())); if (modelFactory == null) { // assuming user knows what he's doing and supplied us with a model file, try default factory. modelFactory = PersistenceFacade.getInstance().getDefaultModelFactory(); } try { HashMap<String, String> options = new HashMap<String, String>(); options.put(DefaultModelPersistence.OPTION_STRIP_IMPLEMENTATION, Boolean.toString(stripImplementation)); options.put(MetaModelInfoProvider.OPTION_KEEP_READ_METAINFO, Boolean.TRUE.toString()); SModel model = modelFactory.load(new FileDataSource(source), options); PersistenceFacade.getInstance().getModelFactory(MPSExtentions.MODEL_BINARY).save(model, new FileDataSource(FileSystem.getInstance().getFileByPath(destFile))); } catch (RuntimeException ex) { System.out.printf("Conversion of %s\n", sourceFile); ex.printStackTrace(); throw ex; } catch (IOException ex) { throw new RuntimeException(String.format("Failed to read model from file %s", sourceFile), ex); } catch (ModelSaveException e) { throw new RuntimeException(String.format("Failed to write model in binary format to file %s", destFile), e); } } }