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

The following examples show how to use org.eclipse.xtext.resource.XtextResource#setURI() . 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: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testResourcesAreInMap_03() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  EList<Resource> _resources = rs.getResources();
  _resources.add(resource);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
  resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
  resource.setURI(URI.createFileURI(new File("bar").getAbsolutePath()));
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
  resource.setURI(null);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 2
Source File: ProposalXpectMethod.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private XtextResource getNewResource(String content, URI uri) {
	IParseResult parseResult = parser.parse(new StringReader(content));
	ResourceSet resourceSet2 = resourceSetProvider.get();
	XtextResource newXtextResource = resourceProvider.get();
	newXtextResource.setURI(uri);
	resourceSet2.getResources().add(newXtextResource);
	newXtextResource.setParseResult(parseResult);
	return newXtextResource;
}
 
Example 3
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 4
Source File: RefactoringCrossReferenceSerializerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSerializer() throws Exception {
	RefactoringCrossReferenceSerializer.RefTextEvaluator evaluator = new RefactoringCrossReferenceSerializer.RefTextEvaluator() {
		@Override
		public boolean isValid(IEObjectDescription target) {
			return true;
		}
		
		@Override
		public boolean isBetterThan(String newText, String currentText) {
			return newText.length() < currentText.length();
		}
	};
	String model = "bar { ref foo } foo";
	String wsRelativePath = "test/test."+getCurrentFileExtension();
	IFile file = createFile(wsRelativePath, model);
	Main main = (Main) getModel(file.getContents());
	XtextResource resource = (XtextResource) main.eResource();
	resource.setURI(URI.createPlatformResourceURI(wsRelativePath, true));
	
	Element bar = (Element) main.getElements().get(0);
	Element foo = bar.getReferenced().get(0);
	assertEquals("foo", foo.getName());
	
	CrossReference crossref = GrammarUtil.containedCrossReferences(grammarAccess.getElementRule()).get(0);
	TextRegion linkTextRegion = new TextRegion(model.lastIndexOf("foo"), 3);
	String linkText = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
	assertEquals(linkText, "foo");
	assertTrue(status.getRefactoringStatus().isOK());
	
	foo.setName("fooBar");
	resource.getCache().clear(resource);
	String linkText1 = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
	assertEquals(linkText1, "fooBar");
	assertTrue(status.getRefactoringStatus().isOK());

	assertEquals(foo, ((Main) resource.getContents().get(0)).getElements().get(1));
	foo.setName("bar");
	resource.getCache().clear(resource);
	assertNull(facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status));
}
 
Example 5
Source File: Bug287988WithEagerLinkingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	factory = new ResourceFactoryImpl(){
		@Override
		public Resource createResource(URI uri) {
			XtextResource resource = new XtextResource();
			injectMembers(resource);
			resource.setLinker(get(Linker.class));
			resource.setURI(uri);
			return resource;
		}
	};
	Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("bug287988testlanguage", factory);
}
 
Example 6
Source File: GrammarFlatteningTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public Grammar getModel(final String model, final boolean dropUnreachable) throws Exception {
  EObject _model = super.getModel(model);
  Grammar grammar = ((Grammar) _model);
  RuleNames ruleNames = RuleNames.getRuleNames(grammar, false);
  RuleFilter filter = new RuleFilter();
  filter.setDiscardUnreachableRules(dropUnreachable);
  Grammar result = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
  XtextResource resource = this.<XtextResource>get(XtextResource.class);
  resource.getContents().add(result);
  resource.setURI(URI.createURI("synthetic://flattened.xtext"));
  return result;
}
 
Example 7
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreInMap() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
  EList<Resource> _resources = rs.getResources();
  _resources.add(resource);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 8
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreInMap_02() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 9
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreCleared_01() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  rs.getResources().clear();
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 10
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreClearedWithDeliverFalse_01() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  rs.eSetDeliver(false);
  rs.getResources().clear();
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 11
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreInMapWithNormalizedURI_01() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createURI("/a/../foo"));
  EList<Resource> _resources = rs.getResources();
  _resources.add(resource);
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 12
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreInMapWithNormalizedURI_02() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createURI("/a/../foo"));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 13
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreInMapWithNormalizedURI_03() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  EList<Resource> _resources = rs.getResources();
  _resources.add(resource);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
  Assert.assertEquals(0, rs.getNormalizationMap().size());
  resource.setURI(URI.createURI("/a/../foo"));
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
  Assert.assertEquals(1, rs.getNormalizationMap().size());
  Assert.assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
  resource.setURI(URI.createURI("/a/../bar"));
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
  Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
  Assert.assertEquals(1, rs.getNormalizationMap().size());
  Assert.assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
  resource.setURI(null);
  Assert.assertEquals(1, rs.getURIResourceMap().size());
  Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
  Assert.assertEquals(0, rs.getNormalizationMap().size());
  rs.getResources().remove(resource);
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  Assert.assertEquals(0, rs.getNormalizationMap().size());
}
 
Example 14
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreClearedWithNormalizedURI_01() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createURI("/a/../foo"));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  rs.getResources().clear();
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 15
Source File: AbstractXtextResourceSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testResourcesAreClearedWithDeliverFalseWithNormalizedURI_01() {
  final XtextResourceSet rs = this.createEmptyResourceSet();
  Assert.assertEquals(0, rs.getURIResourceMap().size());
  final XtextResource resource = new XtextResource();
  resource.setURI(URI.createURI("//a/../foo"));
  EList<Resource> _resources = rs.getResources();
  ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
  Iterables.<Resource>addAll(_resources, _newArrayList);
  Assert.assertEquals(2, rs.getURIResourceMap().size());
  rs.eSetDeliver(false);
  rs.getResources().clear();
  Assert.assertTrue(resource.eAdapters().isEmpty());
  Assert.assertEquals(0, rs.getURIResourceMap().size());
}
 
Example 16
Source File: AbstractSGenTest.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected EObject parseExpression(String expression, String ruleName) {
	XtextResource resource = resourceProvider.get();
	resource.setURI(URI.createPlatformPluginURI("path", true));
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setName(ruleName);
	IParseResult result = parser.parse(parserRule, new StringReader(
			expression));
	EObject rootASTElement = result.getRootASTElement();
	resource.getContents().add(rootASTElement);
	ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer();
	linker.linkModel(result.getRootASTElement(), diagnosticsConsumer);
	if (result.hasSyntaxErrors()) {
		StringBuilder errorMessages = new StringBuilder();
		Iterable<INode> syntaxErrors = result.getSyntaxErrors();
		for (INode iNode : syntaxErrors) {
			errorMessages.append(iNode.getSyntaxErrorMessage());
			errorMessages.append("\n");
		}
		throw new RuntimeException(
				"Could not parse expression, syntax errors: "
						+ errorMessages);
	}
	if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) {
		throw new RuntimeException("Error during linking: "
				+ diagnosticsConsumer.getResult(Severity.ERROR));
	}
	return rootASTElement;
}