public ITextRegion exec(XtextResource xtextResource) throws Exception { ITextRegion result = selection; if (xtextResource != null) { IParseResult parseResult = xtextResource.getParseResult(); if (parseResult != null) { result = internalSelect(xtextResource, selection); } } return result; } };
public final XtextResource getResourceAndExpect(InputStream in, URI uri, int expectedErrors) throws Exception { XtextResource resource = doGetResource(in, uri); checkNodeModel(resource); if (expectedErrors != UNKNOWN_EXPECTATION) { if (expectedErrors == EXPECT_ERRORS) assertFalse(Joiner.on('\n').join(resource.getErrors()), resource.getErrors().isEmpty()); else assertEquals(Joiner.on('\n').join(resource.getErrors()), expectedErrors, resource.getErrors().size()); } for(Diagnostic d: resource.getErrors()) { if (d instanceof ExceptionDiagnostic) fail(d.getMessage()); } if (expectedErrors == 0 && resource.getContents().size() > 0 && shouldTestSerializer(resource)) { SerializerTester tester = get(SerializerTester.class); EObject obj = resource.getContents().get(0); tester.assertSerializeWithNodeModel(obj); tester.assertSerializeWithoutNodeModel(obj); } return resource; }
public DeserializationConversionContext(XtextResource xr, String completeContent) throws IOException { this.grammarAccess = xr.getResourceServiceProvider().get(IGrammarAccess.class); this.idToEObjectMap = Lists.newArrayList(); this.completeContent = completeContent; this.hasErrors = false; fillIdToEObjectMap(xr); }
protected void clearInternalState() { for (EObject content : getContents()) { unload(content); } getContents().clear(); clearErrorsAndWarnings(); this.parseResult = null; }
protected void addSyntaxErrors() { getErrors().addAll(createDiagnostics(parseResult)); }
protected void clearErrorsAndWarnings() { getWarnings().clear(); getErrors().clear(); }
protected XtendFile getXtendFile(XtextResource resource) { if(resource == null || resource.getContents().isEmpty() || !(resource.getContents().get(0) instanceof XtendFile)) return null; else return (XtendFile) resource.getContents().get(0); }
public ContentAssistContext[] create(String document, ITextRegion selection, int offset, XtextResource resource) { this.document = document; this.selection = selection; this.resource = resource; //This is called to make sure late initialization is done. if (resource instanceof DerivedStateAwareResource) { resource.getContents(); } this.parseResult = resource.getParseResult(); if (parseResult == null) throw new NullPointerException("parseResult is null"); return doCreateContexts(offset); }
@Override public void processInput(String input, DeltaScenarioProcessor tester) throws Exception { XtextResource resource = tester.get(XtextResource.class); resource.setURI(URI.createURI("dummy.uri")); try { resource.load(new StringInputStream(input, "UTF-8"), Collections.singletonMap(XtextResource.OPTION_ENCODING, "UTF-8")); } catch (Exception e) { Assert.fail(e.getMessage()); } ICompositeNode rootNode = resource.getParseResult().getRootNode(); for(ILeafNode token: rootNode.getLeafNodes()) { int start = token.getTotalOffset(); int length = token.getTotalLength(); if (length > 1 && length != 3) { tester.processFile(input, input, start, length, ""); } } } },
public final XtextResource getResourceAndExpect(InputStream in, URI uri, int expectedErrors) throws Exception { XtextResource resource = doGetResource(in, uri); if (expectedErrors != UNKNOWN_EXPECTATION) { if (expectedErrors == EXPECT_ERRORS) assertFalse(resource.getErrors().toString(), resource.getErrors().isEmpty()); else assertEquals(resource.getErrors().toString(), expectedErrors, resource.getErrors().size()); } for(Diagnostic d: resource.getErrors()) { if (d instanceof ExceptionDiagnostic) fail(d.getMessage()); } for(Diagnostic d: resource.getWarnings()) System.out.println("Resource Warning: "+d); if (expectedErrors == 0 && resource.getContents().size() > 0 && shouldTestSerializer(resource)) { SerializerTester tester = get(SerializerTester.class); EObject obj = resource.getContents().get(0); tester.assertSerializeWithNodeModel(obj); tester.assertSerializeWithoutNodeModel(obj); } return resource; }
protected void loadResource(XtextResource resource, String document, String encoding) throws CoreException { try { // encoding can be null for FileRevisionEditorInput byte[] bytes = encoding != null ? document.getBytes(encoding) : document.getBytes(); resource.load(new ByteArrayInputStream(bytes), Collections.singletonMap(XtextResource.OPTION_ENCODING, encoding)); } catch (IOException ex) { String message = (ex.getMessage() != null ? ex.getMessage() : ex.toString()); IStatus s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, ex); throw new CoreException(s); } }
public Data exec(XtextResource state) throws Exception { IResourceServiceProvider provider = state.getResourceServiceProvider(); IStratumBreakpointSupport breakpointSupport = provider.get(IStratumBreakpointSupport.class); Data result = new Data(); result.name = state.getURI().lastSegment(); result.valid = breakpointSupport != null && breakpointSupport.isValidLineForBreakPoint(state, line); result.types = getClassNamePattern(state); result.lang = provider.get(LanguageInfo.class); return result; } });
protected Document getDocument(final XtextResource resource) { Document _elvis = null; Document _get = this.openDocuments.get(resource.getURI()); if (_get != null) { _elvis = _get; } else { String _text = resource.getParseResult().getRootNode().getText(); Document _document = new Document(Integer.valueOf(1), _text); _elvis = _document; } return _elvis; }
resource.load(context.getInputStream(), null); } catch (Exception e) { context.error(e.getMessage(), 0); if (resource.getParseResult() == null) { if (resource.getParseResult().hasSyntaxErrors()) { for (INode n : resource.getParseResult().getSyntaxErrors()) { syntaxErrors.add(new Pair<>("syntax error: " + n.getText(), n .getStartLine())); Model root = (Model) resource.getParseResult().getRootASTElement(); if (root.getNamespace() == null) { return null;
public R exec(XtextResource state) throws Exception { ResourceSet localContext = state.getResourceSet(); if (localContext != null) return work.exec(localContext); return null; } });
/** * Returns the {@link URI uri} of the associated {@link org.eclipse.emf.ecore.resource.Resource emf resource}. * May be null if no resource is available or its uri is <code>null</code>. * @return the resource uri if available. * @since 2.1 */ public URI getResourceURI() { if (resource != null) return resource.getURI(); return null; }
@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 static String getCompleteContent(XtextResource xr) throws IOException, UnsupportedEncodingException { XtextResourceSet resourceSet = (XtextResourceSet) xr.getResourceSet(); URIConverter uriConverter = resourceSet.getURIConverter(); URI uri = xr.getURI(); String encoding = xr.getEncoding(); InputStream inputStream = null; try { inputStream = uriConverter.createInputStream(uri); return getCompleteContent(encoding, inputStream); } finally { tryClose(inputStream, null); } }
protected void updateInternalState(IParseResult newParseResult) { this.parseResult = newParseResult; if (parseResult.getRootASTElement() != null && !getContents().contains(parseResult.getRootASTElement())) getContents().add(0, parseResult.getRootASTElement()); reattachModificationTracker(parseResult.getRootASTElement()); clearErrorsAndWarnings(); addSyntaxErrors(); doLinking(); }
public <Result> Result useTemporaryResource( ResourceSet resourceSet, Grammar language, AbstractRule rootRule, String content, IUnitOfWork<Result, XtextResource> runnable) { XtextResource resource = languageRegistry.createTemporaryResourceIn(language, resourceSet); if (rootRule != null) PartialParser.assignRootRule(resource, (ParserRule) rootRule); try { resource.load(new StringInputStream(content, resource.getEncoding()), null); return runnable.exec(resource); } catch(Exception e) { throw new RuntimeException(e); } finally { if (resource != null) { if (resource.isLoaded()) resource.unload(); resourceSet.getResources().remove(resource); } } }