org.eclipse.xtext.util.LazyStringInputStream Java Examples

The following examples show how to use org.eclipse.xtext.util.LazyStringInputStream. 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: AbstractHierarchyBuilderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected ResourceSet createResourceSet(@Extension final AbstractHierarchyBuilderTest.HierarchyBuilderTestConfiguration configuration) {
  try {
    final XtextResourceSet resourceSet = this.resourceSetProvider.get();
    for (final Pair<String, String> model : configuration.models) {
      {
        final Resource resource = resourceSet.createResource(URI.createURI(model.getKey()));
        String _value = model.getValue();
        LazyStringInputStream _lazyStringInputStream = new LazyStringInputStream(_value, "UTF-8");
        resource.load(_lazyStringInputStream, null);
        this._validationTestHelper.assertNoIssues(resource);
      }
    }
    return resourceSet;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #2
Source File: SmokeTestWriter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Script parse(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
	if (active) {
		if (in instanceof LazyStringInputStream) {
			try {
				String string = ((LazyStringInputStream) in).getString();
				if (string.length() < 1000 && seen.add(string)) {
					List<String> lines = CharStreams.readLines(new StringReader(string));
					if (lines.size() < 50) {
						System.out.println("\t@Test");
						System.out.format("\tdef void test_%04d() {", counter++);
						System.out.println();
						System.out.println("\t\t'''");
						for (String s : lines) {
							System.out.print("\t\t\t");
							System.out.println(s);
						}
						System.out.println("\t\t'''.assertNoException");
						System.out.println("\t}");
						System.out.println();
					}
				}
			} catch (IOException e) {
				// ignore
			}
		}
	}
	return super.parse(in, uriToUse, options, resourceSet);
}
 
Example #3
Source File: ProjectDescriptionLoader.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private <T extends EObject> T loadXtextFileAtLocation(SafeURI<?> location, String name, String content,
		Class<T> expectedTypeOfRoot) {
	SafeURI<?> fullLocation = location.appendSegment(name);
	try {
		// check whether a file exists at the given URI
		if (!fullLocation.exists()) {
			return null;
		}
		ResourceSet resourceSet = resourceSetProvider.get();
		Resource resource = resourceSet.createResource(fullLocation.toURI());
		if (resource != null) {
			try (LazyStringInputStream contentAsStream = new LazyStringInputStream(content,
					StandardCharsets.UTF_8)) {
				resource.load(contentAsStream, resourceSet.getLoadOptions());

				List<EObject> contents = resource.getContents();

				if (!contents.isEmpty()) {
					EObject root = contents.get(0);
					if (expectedTypeOfRoot.isInstance(root)) {
						final T rootCasted = expectedTypeOfRoot.cast(root);
						contents.clear();
						return rootCasted;
					}
				}
			}
		}
		return null;
	} catch (Exception e) {
		throw new WrappedException("failed to load Xtext file at " + fullLocation, e);
	}
}
 
Example #4
Source File: EmbeddedEditorModelAccess.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected XtextResource createResource(String content) {
	XtextResource result = this.resourceProvider.createResource();
	try {
		result.load(new LazyStringInputStream(content, result.getEncoding()), Collections.emptyMap());
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return result;
}
 
Example #5
Source File: ExternalContentSupport.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException {
	String content = contentProvider.getContent(uri);
	if (content != null) {
		return new LazyStringInputStream(content, Charsets.UTF_8);
	}
	return delegate.createInputStream(uri, options);
}
 
Example #6
Source File: XtextResource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
protected Reader createReader(InputStream inputStream) throws IOException {
	if (inputStream instanceof LazyStringInputStream) {
		return new StringReader(((LazyStringInputStream) inputStream).getString());
	}
	return new InputStreamReader(new BufferedInputStream(inputStream), getEncoding());
}
 
Example #7
Source File: XtextResource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void reparse(String newContent) throws IOException {
	try {
		isUpdating = true;
		clearInternalState();
		doLoad(new LazyStringInputStream(newContent, getEncoding()), null);
		setModified(false);
	} finally {
		isUpdating = false;
	}
}
 
Example #8
Source File: ResourceHelper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected InputStream getAsStream(CharSequence text) {
	return new LazyStringInputStream(text == null ? "" : text.toString());
}
 
Example #9
Source File: XtendBuilderParticipantTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testIncrementalBuild() throws Exception {
	IFile first = testHelper.createFile("incrementalBuild/First", "package incrementalBuild\n" + 
			"import incrementalBuild.Second.I\n" + 
			"class First {\n" + 
			"	def m() {}   \n" + 
			"	def I i() {\n" + 
			"		return [ cancelIndicator |\n" + 
			"		]\n" + 
			"	}\n" + 
			"	def Second get() {}\n" + 
			"}");
	assertTrue(first.exists());
	IFile second = testHelper.createFile("incrementalBuild/Second", "package incrementalBuild\n" + 
			"import java.util.List\n" + 
			"class Second {\n" + 
			"    public First first\n" + 
			"    static interface I {\n" + 
			"        def List<?> m(Object o)\n" + 
			"    }\n" + 
			"}");
	assertTrue(second.exists());
	waitForBuild();
	assertNoErrorsInWorkspace();

	IFile firstTarget = testHelper.getProject().getFile("/xtend-gen/incrementalBuild/First.java");
	assertTrue(firstTarget.exists());
	assertFalse(fileIsEmpty(firstTarget));
	
	IFile secondTarget = testHelper.getProject().getFile("/xtend-gen/incrementalBuild/Second.java");
	assertTrue(secondTarget.exists());
	assertFalse(fileIsEmpty(secondTarget));
	
	first.setContents(new LazyStringInputStream("package incrementalBuild\n" + 
			"import incrementalBuild.Second.I\n" + 
			"class First {\n" + 
			"	// removed def m() {}   \n" + 
			"	def I i() {\n" + 
			"		return [ cancelIndicator |\n" + 
			"		]\n" + 
			"	}\n" + 
			"	def Second get() {}\n" + 
			"}"), true, true, null);
	waitForBuild();
	
	assertTrue(firstTarget.exists());
	assertFalse(fileIsEmpty(firstTarget));
	
	assertTrue(secondTarget.exists());
	assertFalse(fileIsEmpty(secondTarget));
	
	assertNoErrorsInWorkspace();
}
 
Example #10
Source File: ResourceHelper.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected InputStream getAsStream(CharSequence text) {
	return new LazyStringInputStream(text == null ? "" : text.toString());
}
 
Example #11
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testNamedParameterAdjustment() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("Root<MyParam>: rule=Rule<true>;");
  _builder.newLine();
  _builder.append("Rule<MyParam>: name=ID child=Root<false>?;");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  EObject _model = this.getModel(grammarAsString);
  final Grammar grammar = ((Grammar) _model);
  final ResourceSet resourceSet = grammar.eResource().getResourceSet();
  final Resource otherResource = resourceSet.createResource(URI.createURI("other.xtext"));
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("grammar test.SubLang with test.Lang");
  _builder_1.newLine();
  _builder_1.append("import \'http://test\'");
  _builder_1.newLine();
  _builder_1.append("Root<MyParam>: rule=super::Rule<true>;");
  _builder_1.newLine();
  LazyStringInputStream _lazyStringInputStream = new LazyStringInputStream(_builder_1.toString());
  otherResource.load(_lazyStringInputStream, null);
  EObject _head = IterableExtensions.<EObject>head(otherResource.getContents());
  final Grammar subGrammar = ((Grammar) _head);
  AbstractRule _head_1 = IterableExtensions.<AbstractRule>head(subGrammar.getRules());
  final ParserRule rootRule = ((ParserRule) _head_1);
  AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules());
  final ParserRule parentRule = ((ParserRule) _last);
  AbstractElement _alternatives = parentRule.getAlternatives();
  AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
  final Assignment lastAssignment = ((Assignment) _last_1);
  AbstractElement _terminal = lastAssignment.getTerminal();
  final RuleCall ruleCall = ((RuleCall) _terminal);
  final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments());
  Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter());
  Condition _value = argument.getValue();
  Assert.assertFalse(((LiteralCondition) _value).isTrue());
}
 
Example #12
Source File: AbstractXtextTests.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Gets the string as input stream with specified encoding.
 * @since 2.16
 */
protected InputStream getAsStream(String model, Charset encoding) {
	return new LazyStringInputStream(model, encoding.name());
}