protected XtextResource createResource(String content) { XtextResource result = this.resourceProvider.createResource(); try { result.load(new StringInputStream(content, result.getEncoding()), Collections.emptyMap()); } catch (Exception e) { throw new RuntimeException(e); } return result; }
public String getEncoding() throws CoreException { if(resource instanceof XtextResource) { return ((XtextResource) resource).getEncoding(); } return getFile().getCharset(); }
@Override protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException { setEncodingFromOptions(options); IParseResult result = parser.parse(new InputStreamReader(inputStream, getEncoding())); updateInternalState(this.parseResult, 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 reparse(String newContent) throws IOException { try { isUpdating = true; clearInternalState(); doLoad(new StringInputStream(newContent, getEncoding()), null); setModified(false); } finally { isUpdating = false; } }
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); } }
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); } } }