/** * Transparently handles the deprecated options that could be passed as * map-entries to {@link org.eclipse.emf.ecore.resource.Resource#save(Map)} * and converts them to semantically equal {@link SaveOptions}. * * @param saveOptions the options-map or <code>null</code> if none. * @return the options to use. Will never return <code>null</code>. */ @SuppressWarnings("deprecation") public static SaveOptions getOptions(Map<?, ?> saveOptions) { if (saveOptions == null || saveOptions.isEmpty()) return defaultOptions(); if (saveOptions.containsKey(KEY)) return (SaveOptions) saveOptions.get(KEY); if (saveOptions.containsKey(XtextResource.OPTION_SERIALIZATION_OPTIONS)) return ((org.eclipse.xtext.parsetree.reconstr.SerializerOptions) saveOptions.get(XtextResource.OPTION_SERIALIZATION_OPTIONS)).toSaveOptions(); if (Boolean.TRUE.equals(saveOptions.get(XtextResource.OPTION_FORMAT))) { return newBuilder().format().getOptions(); } return defaultOptions(); }
/** * @noreference */ public static String grammarFragmentToString(final ISerializer serializer, final EObject object, final String prefix) { String s = null; try { final SaveOptions options = SaveOptions.newBuilder().format().getOptions(); s = serializer.serialize(object, options); } catch (final Throwable _t) { if (_t instanceof Exception) { final Exception e = (Exception)_t; s = e.toString(); } else { throw Exceptions.sneakyThrow(_t); } } String _replace = s.trim().replaceAll("(\\r?\\n)", ("$1" + prefix)).replace("\\u", "\\\\u"); String _plus = (prefix + _replace); s = _plus; return s; }
/** * Serializes an {@link com.modelsolv.reprezen.restapi.ZenModel} at the given path. * * @param path * Location of the RESTApi metamodel. * @param model * {@link com.modelsolv.reprezen.restapi.ZenModel} to serialize. */ @Override public void serialize(String path, ZenModel model) throws RestApiException { XtextResourceSet resourceSet = new XtextDslStandaloneSetup().createInjectorAndDoEMFRegistration() .getInstance(XtextResourceSet.class); org.eclipse.emf.ecore.resource.Resource resource = resourceSet.createResource(URI.createFileURI(path)); resource.getContents().add(model); try { resource.save(SaveOptions.newBuilder().noValidation().getOptions().toOptionsMap()); } catch (IOException e) { throw new RestApiException(String.format("Could not save the ZenModel at location '%s'", path), e); } }
public void serialize(EObject object, ISourceAppender appender, boolean isFormatting) throws IOException { final AppenderBasedTokenStream stream = new AppenderBasedTokenStream(appender); final SaveOptions options; if (isFormatting) { options = SaveOptions.newBuilder().format().getOptions(); } else { options = SaveOptions.defaultOptions(); } serialize(object, stream, options); stream.flush(); }
public SaveOptions toSaveOptions() { SaveOptions.Builder builder = SaveOptions.newBuilder(); if (!isValidateConcreteSyntax()) builder.noValidation(); if (isFormatting()) builder.format(); return builder.getOptions(); } }
/** * This is equal to <code>builder().getOptions()</code>. */ public static SaveOptions defaultOptions() { return newBuilder().getOptions(); }