protected String saveResource(Resource resource) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(content.length()); try { Map<Object, Object> options = Maps.newHashMap(); options.put(XtextResource.OPTION_ENCODING, INTERNAL_ENCODING); SaveOptions.defaultOptions().addTo(options); resource.save(out, options); String result = new String(out.toByteArray(), INTERNAL_ENCODING); return result; } finally { out.close(); } }
public SaveOptions getOptions() { return new SaveOptions(formatting, validating); } }
/** * 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(); }
/** * This is equal to <code>builder().getOptions()</code>. */ public static SaveOptions defaultOptions() { return newBuilder().getOptions(); }
protected SaveOptions getSaveOptions() { return SaveOptions.defaultOptions(); }
/** * 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 TreeConstructionReport serialize(EObject obj, ITokenStream tokenStream, SaveOptions options) throws IOException { if (options.isValidating()) { List<Diagnostic> diagnostics = new ArrayList<Diagnostic>(); validator.validateRecursive(obj, new IConcreteSyntaxValidator.DiagnosticListAcceptor(diagnostics), new HashMap<Object, Object>()); if (!diagnostics.isEmpty()) throw new IConcreteSyntaxValidator.InvalidConcreteSyntaxException( "These errors need to be fixed before the model can be serialized.", diagnostics); } ITokenStream formatterTokenStream; if(formatter instanceof IFormatterExtension) formatterTokenStream = ((IFormatterExtension) formatter).createFormatterStream(obj, null, tokenStream, !options.isFormatting()); else formatterTokenStream = formatter.createFormatterStream(null, tokenStream, !options.isFormatting()); TreeConstructionReport report = parseTreeReconstructor.serializeSubtree(obj, formatterTokenStream); formatterTokenStream.flush(); return report; }
public Map<Object, Object> toOptionsMap() { Map<Object, Object> result = Maps.newHashMapWithExpectedSize(2); addTo(result); return result; }
@Override public void doSave(OutputStream outputStream, Map<?, ?> options) throws IOException { if (getContents().isEmpty()) throw new IllegalStateException("The Xtext resource must contain at least one element."); SaveOptions saveOptions = SaveOptions.getOptions(options); setEncodingFromOptions(options); serializer.serialize(getContents().get(0), new OutputStreamWriter(outputStream, getEncoding()), saveOptions); }
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(); }
/** * @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; }
public String serialize(EObject obj) { return serialize(obj, SaveOptions.defaultOptions()); }
protected void serialize(EObject obj, ITokenStream tokenStream, SaveOptions options) throws IOException { // use the CSV as long as there are cases where is provides better messages than the serializer itself. if (options.isValidating()) { List<Diagnostic> diagnostics = new ArrayList<Diagnostic>(); validator.validateRecursive(obj, new IConcreteSyntaxValidator.DiagnosticListAcceptor(diagnostics), new HashMap<Object, Object>()); if (!diagnostics.isEmpty()) throw new IConcreteSyntaxValidator.InvalidConcreteSyntaxException( "These errors need to be fixed before the model can be serialized.", diagnostics); } ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR; ITokenStream formatterTokenStream; if(formatter instanceof IFormatterExtension) formatterTokenStream = ((IFormatterExtension) formatter).createFormatterStream(obj, null, tokenStream, !options.isFormatting()); else formatterTokenStream = formatter.createFormatterStream(null, tokenStream, !options.isFormatting()); EObject context = getContext(obj); ISequenceAcceptor acceptor = new TokenStreamSequenceAdapter(formatterTokenStream, errors); serialize(obj, context, acceptor, errors); formatterTokenStream.flush(); }
public SaveOptions toSaveOptions() { SaveOptions.Builder builder = SaveOptions.newBuilder(); if (!isValidateConcreteSyntax()) builder.noValidation(); if (isFormatting()) builder.format(); return builder.getOptions(); } }
public String serialize(EObject obj) { return serialize(obj, SaveOptions.defaultOptions()); }
var so = new SaveOptions({ resourceName: entityId + "/Addresses" }); // null = 'all-pending-changes'; saveOptions is the 2nd parameter myEntityManager.SaveChanges(addressEntities, so );
var so = new SaveOptions({ resourceName: "SaveWithComment", tag: "Whatever data you want" }); return myEntityManager.saveChanges(null, so);