Java Code Examples for org.eclipse.xtext.resource.XtextResource#save()

The following examples show how to use org.eclipse.xtext.resource.XtextResource#save() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnicodeTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testWrite() throws Exception {
	XtextResource resource = getResource("", "test.mydsl");
	Model model = UnicodeFactory.eINSTANCE.createModel();
	resource.getContents().add(model);
	GString s0 = UnicodeFactory.eINSTANCE.createGString();
	s0.setName(UMLAUTS);
	model.getStrings().add(s0);
	QuotedString s1 = UnicodeFactory.eINSTANCE.createQuotedString();
	s1.setName(UMLAUTS);
	model.getStrings().add(s1);
	QuotedString s2 = UnicodeFactory.eINSTANCE.createQuotedString();
	s2.setName(QUOTED_UMLAUTS);
	model.getStrings().add(s2);
	QuotedString s3 = UnicodeFactory.eINSTANCE.createQuotedString();
	s3.setName(MIXED_UMLAUTS);
	model.getStrings().add(s3);
	
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	resource.save(outputStream, null);
	String textualModel = new String(outputStream.toByteArray(), getCharsetForTest());
	assertEquals(doubleQuote(UMLAUTS + " \"" + UMLAUTS + "\" \"" + QUOTED_UMLAUTS + "\" \"" + MIXED_UMLAUTS + "\""), textualModel); 
}
 
Example 2
Source File: Bug302128Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testTheBug2() throws Exception {
	with(new Bug302128TestLanguageStandaloneSetup());
	String text = "VARIABLE += value.val value2.val\n" 
			+ "VARIABLE2 += value3.val value4.val\n\n"
			+ "#Comment comment comment\n\n" 
			+ "VARIABLE3 += value5.val value6.val\n"
			+ "VARIABLE4 += value.val value2.val\n" 
			+ "VARIABLE5 += value3.val value4.val\n\n" 
			+ "#Comment comment comment\n\n" +
			  "VARIABLE.varible += value5.val value6.val\n";
	XtextResource resource = getResource(new StringInputStream(text));
	Model model = (Model) resource.getContents().get(0);
	model.getElements().get(2).setValue("+= value5.val value6.val\n");

	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	resource.save(outputStream, null);
	assertEquals(text, new String(outputStream.toByteArray()));
}
 
Example 3
Source File: FormatterFacadeTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
private void assertResourceContentFormat(String source, String expected) throws IOException {
	final ResourceSet resourceSet = new XtextResourceSet();
	final URI createURI = URI.createURI("synthetic://to-be-formatted.sarl"); //$NON-NLS-1$
	final XtextResource resource = (XtextResource) this.resourceFactory.createResource(createURI);
	resourceSet.getResources().add(resource);
	try (StringInputStream stringInputStream = new StringInputStream(source)) {
		resource.load(stringInputStream, Collections.emptyMap());
	}
	this.facade.format(resource);
	final String actual; 
	try (ByteArrayOutputStream stringOutputStream = new ByteArrayOutputStream()) {
		resource.save(stringOutputStream, Collections.emptyMap());
		stringOutputStream.flush();
		actual = stringOutputStream.toString();
	}
	assertEquals(expected, actual);
}
 
Example 4
Source File: DebugAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
protected void prettyPrint(String absoluteGrammarFileName, Charset encoding) {
	try {
		String content = readFileIntoString(absoluteGrammarFileName, encoding);
		final ILineSeparatorInformation lineSeparatorInformation = new ILineSeparatorInformation() {
			@Override
			public String getLineSeparator() {
				return DebugAntlrGeneratorFragment.this.getLineDelimiter();
			}
		};
		Injector injector = new SimpleAntlrStandaloneSetup() {
			@Override
			public Injector createInjector() {
				return Guice.createInjector(new SimpleAntlrRuntimeModule() {
					@Override
					public void configure(Binder binder) {
						super.configure(binder);
						binder.bind(ILineSeparatorInformation.class).toInstance(lineSeparatorInformation);
					}
				});
			}
		}.createInjectorAndDoEMFRegistration();
		XtextResource resource = injector.getInstance(XtextResource.class);
		resource.setURI(URI.createFileURI(absoluteGrammarFileName));
		resource.load(new StringInputStream(content, encoding.name()),
				Collections.singletonMap(XtextResource.OPTION_ENCODING, encoding.name()));
		if (!resource.getErrors().isEmpty()) {
			String errors = Joiner.on(getLineDelimiter()).join(resource.getErrors());
			throw new GeneratorWarning("Non fatal problem: Debug grammar could not be formatted due to:" + getLineDelimiter() + errors);
		}
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(content.length());
		resource.save(outputStream, SaveOptions.newBuilder().format().getOptions().toOptionsMap());
		String toBeWritten = new NewlineNormalizer(getLineDelimiter()).normalizeLineDelimiters(
				new String(outputStream.toByteArray(), encoding.name()));
		writeStringIntoFile(absoluteGrammarFileName, toBeWritten, encoding);
	} catch(IOException e) {
		throw new GeneratorWarning(e.getMessage());
	}
}
 
Example 5
Source File: EncodingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSaveIsoToIso() throws Exception {
	XtextResource resource = createXtextResource();
	resource.load(new ByteArrayInputStream(isoBytes), isoOptions);
	ByteArrayOutputStream isoSaveStream = new ByteArrayOutputStream();
	resource.save(isoSaveStream, null);
	isoSaveStream.close();
	byte[] savedIsoBytes = isoSaveStream.toByteArray();
	assertTrue(Arrays.equals(isoBytes, savedIsoBytes));
}
 
Example 6
Source File: EncodingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSaveUtfToUtf() throws Exception {
	XtextResource resource = createXtextResource();
	resource.load(new ByteArrayInputStream(utfBytes), utfOptions);
	ByteArrayOutputStream utfSaveStream = new ByteArrayOutputStream();
	resource.save(utfSaveStream, null);
	utfSaveStream.close();
	byte[] savedUtfBytes = utfSaveStream.toByteArray();
	assertTrue(Arrays.equals(utfBytes, savedUtfBytes));
}
 
Example 7
Source File: EncodingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSaveUtfToIso() throws Exception {
	XtextResource resource = createXtextResource();
	resource.load(new ByteArrayInputStream(utfBytes), utfOptions);
	ByteArrayOutputStream isoSaveStream = new ByteArrayOutputStream();
	resource.save(isoSaveStream, isoOptions);
	isoSaveStream.close();
	byte[] savedIsoBytes = isoSaveStream.toByteArray();
	assertTrue(Arrays.equals(isoBytes, savedIsoBytes));
}
 
Example 8
Source File: EncodingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSaveIsoToUtf() throws Exception {
	XtextResource resource = createXtextResource();
	resource.load(new ByteArrayInputStream(isoBytes), isoOptions);
	ByteArrayOutputStream utfSaveStream = new ByteArrayOutputStream();
	resource.save(utfSaveStream, utfOptions);
	byte[] savedUtfBytes = utfSaveStream.toByteArray();
	assertTrue(Arrays.equals(utfBytes, savedUtfBytes));
}
 
Example 9
Source File: XtextGrammarSerializationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void doTestSerialization(final String model, final String expectedModel) throws Exception {
  final XtextResource resource = this.getResourceFromString(model);
  Assert.assertTrue(resource.getErrors().isEmpty());
  EObject _rootASTElement = resource.getParseResult().getRootASTElement();
  final Grammar g = ((Grammar) _rootASTElement);
  Assert.assertNotNull(g);
  final OutputStream outputStream = new ByteArrayOutputStream();
  resource.save(outputStream, SaveOptions.newBuilder().format().getOptions().toOptionsMap());
  final String serializedModel = outputStream.toString();
  Assert.assertEquals(LineDelimiters.toPlatform(expectedModel), serializedModel);
}
 
Example 10
Source File: GrammarSerializerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testSerializeGrammar() throws Exception {
	String string = readFileIntoString("org/eclipse/xtext/parser/unorderedGroups/UnorderedGroupsTestLanguage.xtext");
	XtextResource resource = getResourceFromString(string);
	resource.getResourceSet().getLoadOptions().put(ResourceDescriptionsProvider.LIVE_SCOPE, Boolean.TRUE);
	resource.save(new ByteArrayOutputStream(), null);
}