public RamlResourceSet() { final List<URIHandler> uriHandlers = Arrays.asList( new PlatformResourceURIHandlerImpl(), new FileURIHandlerImpl(), new EFSURIHandlerImpl(), new ArchiveURIHandlerImpl(), new ContentNegotiationURIHandler(ACCEPT_HEADER_VALUE)); final ExtensibleURIConverterImpl uriConverter = new ExtensibleURIConverterImpl(uriHandlers, ContentHandler.Registry.INSTANCE.contentHandlers()); setURIConverter(uriConverter); final Resource.Factory.Registry resourceFactoryRegistry = getResourceFactoryRegistry(); final RamlResourceFactory resourceFactory = new RamlResourceFactory(); resourceFactoryRegistry .getExtensionToFactoryMap().put("raml", resourceFactory); resourceFactoryRegistry.getProtocolToFactoryMap() .put("http", resourceFactory); final Map<String, Object> contentTypeToFactoryMap = resourceFactoryRegistry.getContentTypeToFactoryMap(); for (final String contentType : ACCEPTED_MIME_TYPES) { contentTypeToFactoryMap .put(contentType, resourceFactory); } addBuiltinTypes(); }
@Test public void testSimple() throws Exception { // this fails see bug #252181 String model = "grammar foo with org.eclipse.xtext.common.Terminals Honolulu : name=ID;"; // load grammar model XtextResourceSet rs = get(XtextResourceSet.class); Resource resource = rs.createResource(URI.createURI("foo.xtext"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); resource.load(new StringInputStream(model), null); Grammar object = (Grammar) resource.getContents().get(0); // modify first rule object.getRules().get(0).setName("HONOLULU"); // save ByteArrayOutputStream out = new ByteArrayOutputStream(); resource.save(out, SaveOptions.newBuilder().format().getOptions().toOptionsMap()); String result = new String(out.toByteArray()); // check assertFalse(model.equals(result)); String expectedModel = LineDelimiters.toPlatform("grammar foo with org.eclipse.xtext.common.Terminals\n\nHONOLULU:\n name=ID;"); assertEquals(expectedModel, result); }
@Test public void testBug_266807() throws Exception { final XtextResourceSet rs = this.<XtextResourceSet>get(XtextResourceSet.class); rs.setClasspathURIContext(this.getClass()); StringConcatenation _builder = new StringConcatenation(); _builder.append("classpath:/"); String _replace = this.getClass().getPackage().getName().replace(Character.valueOf('.').charValue(), Character.valueOf('/').charValue()); _builder.append(_replace); _builder.append("/Test.xtext"); Resource _createResource = rs.createResource( URI.createURI(_builder.toString()), ContentHandler.UNSPECIFIED_CONTENT_TYPE); final XtextResource resource = ((XtextResource) _createResource); resource.load(null); EList<Resource.Diagnostic> _errors = resource.getErrors(); for (final Resource.Diagnostic d : _errors) { Assert.fail(d.getMessage()); } }
/** * Returns the given property's Eclipse value converted to EMF's corresponding basic value. * @param qualifiedName the name of the property for which this value applies. * @param value the value to convert. * @return the given property's Eclipse value converted to EMF's corresponding basic value. */ protected Object getDescriptionValue(QualifiedName qualifiedName, Object value) { if (value == null) { return null; } else if (IContentDescription.BYTE_ORDER_MARK.equals(qualifiedName)) { for (ByteOrderMark byteOrderMarker : ContentHandler.ByteOrderMark.values()) { if (value == byteOrderMarker.bytes()) { return byteOrderMarker; } } return null; } else { return value; } }
/** * Returns whether the named property is one requested in the options. * @param property the property in question. * @param options the options in which to look for the requested property. * @return whether the named property is one requested in the options. * @see #getRequestedProperties(Map) */ protected boolean isRequestedProperty(String property, Map<?, ?> options) { if (ContentHandler.VALIDITY_PROPERTY.equals(property) || ContentHandler.CONTENT_TYPE_PROPERTY.equals(property)) { return true; } else { Set<String> requestedProperties = getRequestedProperties(options); if (requestedProperties == null) { return true; } else { return requestedProperties.contains(property); } } }
/** * Returns the line delimiter of the input stream; it's computed from the bytes interpreted using the {@link #getCharset(URI, InputStream, Map, Map) appropriate character set}. * @param uri the URI of the input stream. * @param inputStream the input stream. * @param options any options that might influence the interpretation of the content. * @param context a cache for previously computed information. * @return the line delimiter of the input stream. * @throws IOException if there is a problem loading the content. * @since 2.9 */ protected String getLineDelimiter(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { String result = (String)context.get(ContentHandler.LINE_DELIMITER_PROPERTY); if (result == null) { String charset = getCharset(uri, inputStream, options, context); if (charset != null) { result = getLineDelimiter(inputStream, charset); if (result != null) { context.put(ContentHandler.LINE_DELIMITER_PROPERTY, result); } } } return result; }
/** * Returns the given property's basic EMF value converted to the corresponding Eclipse value. * @param qualifiedName the name of the property for which this value applies. * @param value the value to convert. * @return the given property's basic EMF value converted to the corresponding Eclipse value. */ protected Object getDescriptionValue(QualifiedName qualifiedName, Object value) { if (value == null) { return null; } else if (IContentDescription.BYTE_ORDER_MARK.equals(qualifiedName)) { return ((ContentHandler.ByteOrderMark)value).bytes(); } else { return value; } }
@Override public void generate(Grammar grammar, XpandExecutionContext ctx) { RuleNames.ensureAdapterInstalled(grammar); super.generate(grammar, ctx); final ResourceSaveIndicator isSaving = new ResourceSaveIndicator(); // create a defensive clone Grammar copy = deepCopy(grammar, isSaving); ResourceSet set = copy.eResource().getResourceSet(); // save grammar model String path; if (xmlVersion == null) { path = GrammarUtil.getClasspathRelativePathToBinGrammar(copy); } else { log.warn("'xmlVersion' has been specified for this " + GrammarAccessFragment.class.getSimpleName() + ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback."); path = GrammarUtil.getClasspathRelativePathToXmi(copy); } URI uri = URI.createURI(ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath() + "/" + path); Resource resource = set.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); addAllGrammarsToResource(resource, copy, new HashSet<Grammar>()); isSaving.set(Boolean.TRUE); Map<String, Object> saveOptions = Maps.newHashMap(); if (resource instanceof XMLResource) { ((XMLResource) resource).setXMLVersion(getXmlVersion()); } else if (resource instanceof BinaryResourceImpl){ saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1); saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.TRUE); } try { resource.save(saveOptions); } catch (IOException e) { log.error(e.getMessage(), e); } finally { isSaving.set(Boolean.FALSE); } }
/** * @since 2.4 */ protected void movePackageToNewResource(EPackage pack, ResourceSet set) { Resource resource = set.createResource( URI.createURI("___temp___." + FragmentFakingEcoreResourceFactoryImpl.ECORE_SUFFIX), ContentHandler.UNSPECIFIED_CONTENT_TYPE); resource.setURI(URI.createURI(pack.getNsURI())); resource.getContents().add(pack); }
protected Resource createResourceForEPackages(Grammar grammar, XpandExecutionContext ctx, List<EPackage> packs, ResourceSet rs) { URI ecoreFileUri = getEcoreFileUri(grammar, ctx); ecoreFileUri = toPlatformResourceURI(ecoreFileUri); Resource existing = rs.getResource(ecoreFileUri, false); if (existing != null) { existing.unload(); rs.getResources().remove(existing); } Resource ecoreFile = rs.createResource(ecoreFileUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); ecoreFile.getContents().addAll(packs); return ecoreFile; }
@Override protected synchronized URIConverter getURIConverter() { if (this.uriConverter == null) { List<ContentHandler> withoutPlatformDelegate = Lists.newArrayList(); for (ContentHandler contentHandler : ContentHandler.Registry.INSTANCE.contentHandlers()) { if (!isTooEager(contentHandler)) withoutPlatformDelegate.add(contentHandler); } this.uriConverter = new ExtensibleURIConverterImpl(URIHandler.DEFAULT_HANDLERS, withoutPlatformDelegate); } return this.uriConverter; }
protected void movePackageToNewResource(final EPackage pack, final ResourceSet set) { final Resource resource = set.createResource( URI.createURI(("___temp___." + FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX)), ContentHandler.UNSPECIFIED_CONTENT_TYPE); resource.setURI(URI.createURI(pack.getNsURI())); resource.getContents().add(pack); }
protected Resource createResourceForEPackages(final Grammar grammar, final List<EPackage> packs, final ResourceSet rs) { final URI ecoreFileUri = this.getEcoreFileUri(grammar); final Resource existing = rs.getResource(ecoreFileUri, false); if ((existing != null)) { existing.unload(); rs.getResources().remove(existing); } final Resource ecoreFile = rs.createResource(ecoreFileUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); ecoreFile.getContents().addAll(packs); return ecoreFile; }
@Test public void testSimple() throws Exception { ResourceSet rs = new ResourceSetImpl(); Resource foo = rs.createResource(URI.createURI("foo.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage(); foo.getContents().add(ePackage); Resource bar = rs.createResource(URI.createURI("bar.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); bar.getContents().add(EcoreFactory.eINSTANCE.createEAttribute()); assertEquals(true, EcoreUtil2.isValidUri(ePackage, URI.createURI("bar.xmi"))); }
@Test public void testEPackageURI() throws Exception { ResourceSet rs = new ResourceSetImpl(); Resource foo = rs.createResource(URI.createURI("foo.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage(); foo.getContents().add(ePackage); assertEquals(true, EcoreUtil2.isValidUri(ePackage, URI.createURI(EcorePackage.eNS_URI))); }
@Test public void testClone() throws Exception { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE); ResourceSetImpl rs = new ResourceSetImpl(); Resource r1 = rs.createResource(URI.createURI("foo.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); Resource r2 = rs.createResource(URI.createURI("bar.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); EClass a = EcoreFactory.eINSTANCE.createEClass(); a.setName("a"); EClass b = EcoreFactory.eINSTANCE.createEClass(); r1.getContents().add(a); b.setName("b"); b.getESuperTypes().add(a); r2.getContents().add(b); ResourceSetImpl clone = EcoreUtil2.clone(new ResourceSetImpl(), rs); EList<Resource> list = clone.getResources(); Resource resA = list.get(0); assertEquals(URI.createURI("foo.xmi"),resA.getURI()); assertNotSame(resA, r1); Resource resB = list.get(1); assertEquals(URI.createURI("bar.xmi"),resB.getURI()); assertNotSame(resB, r2); EClass a1 = (EClass)resA.getContents().get(0); EClass b1 = (EClass)resB.getContents().get(0); assertEquals("a", a1.getName()); assertNotSame(a, a1); assertEquals("b", b1.getName()); assertNotSame(b, b1); assertSame(b1.getESuperTypes().get(0),a1); assertSame(b.getESuperTypes().get(0),a); }
public void _testXtestSerializationSelfTest() throws Exception { Resource res = this.<XtextResourceSet>get(XtextResourceSet.class).createResource(URI.createURI("myfile.xtext"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); res.getContents().add(this.<XtextGrammarAccess>get(XtextGrammarAccess.class).getGrammar()); OutputStream outputStream = new ByteArrayOutputStream(); res.save(outputStream, Collections.<Object, Object>emptyMap()); }
/** * Finds the {@link IResourceServiceProvider} for a language given its file extension. * * @param extension * the language's file extension * @return the resource service provider for the {@code extension} or null if there is none */ public IResourceServiceProvider getResourceServiceProviderByExtension(final String extension) { Object object = IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().get(extension); if (object instanceof IResourceServiceProvider) { return (IResourceServiceProvider) object; } else { URI fake = URI.createURI("fake:/foo." + extension); //$NON-NLS-1$ return IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(fake, ContentHandler.UNSPECIFIED_CONTENT_TYPE); } }
protected XtextResource createResource(ResourceSet resourceSet, URI uri) { //TODO use the resource factory directly (injected), since the use might open any file with this editor. Resource aResource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); if (!(aResource instanceof XtextResource)) throw new IllegalStateException("The resource factory registered for " + uri + " does not yield an XtextResource. Make sure the file name extension is correct (case matters)."); return (XtextResource) aResource; }
private XtextResource getResource(ResourceSet resourceSet, URI uri) { Resource aResource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); if (!(aResource instanceof XtextResource)) throw new IllegalStateException("The resource factory registered for " + uri + " does not yield an XtextResource. Make sure the right resource factory has been registered."); return (XtextResource) aResource; }
public EnsembleResourceSet() { super(); for (EnsembleResourceFactory factory : factories) { factory.setResourceSet(this); } List<URIHandler> handlers = new ArrayList<URIHandler>(URIHandler.DEFAULT_HANDLERS); handlers.addAll(0, getURIHandlerContributions()); setURIConverter(new ExtensibleURIConverterImpl(handlers, ContentHandler.Registry.INSTANCE.contentHandlers())); }
protected String getContentTypeIdentifier(URI uri) { try { Map<String, ?> contentDescription = getURIConverter().contentDescription(uri, getContentDescriptionOptions()); return (String)contentDescription.get(ContentHandler.CONTENT_TYPE_PROPERTY); } catch (IOException e) { return null; } }
public void put(int priority, ContentHandler contentHandler) { Integer integerPriority = priority; List<ContentHandler> contentHandlers = get(integerPriority); if (contentHandlers == null) { put(integerPriority, contentHandlers = new ArrayList<ContentHandler>()); } contentHandlers.add(contentHandler); }
public Resource.Factory.Registry getResourceFactoryRegistry() { if (resourceFactoryRegistry == null) { resourceFactoryRegistry = new ResourceFactoryRegistryImpl() { @Override protected Resource.Factory delegatedGetFactory(URI uri, String contentTypeIdentifier) { return convert (getFactory (uri, Resource.Factory.Registry.INSTANCE.getProtocolToFactoryMap(), Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(), Resource.Factory.Registry.INSTANCE.getContentTypeToFactoryMap(), contentTypeIdentifier, false)); } @Override protected URIConverter getURIConverter() { return ResourceSetImpl.this.getURIConverter(); } @Override protected Map<?, ?> getContentDescriptionOptions() { return new ExtensibleURIConverterImpl.OptionsMap(ContentHandler.OPTION_REQUESTED_PROPERTIES, CONTENT_TYPE_REQUESTED_PROPERTIES, getLoadOptions()); } }; } return resourceFactoryRegistry; }
public EList<ContentHandler> getContentHandlers() { if (contentHandlers == null) { contentHandlers = new ContentHandlerList(); } return contentHandlers; }
/** * This base implementation handles computing the {@link ContentHandler#BYTE_ORDER_MARK_PROPERTY}, * the {@link ContentHandler#CHARSET_PROPERTY character set property}, * and the {@link ContentHandler#LINE_DELIMITER_PROPERTY line delimiter property}. * for each such {@link #isRequestedProperty(String, Map) requested property}. */ public Map<String, Object> contentDescription(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { Map<String, Object> result = createContentDescription(ContentHandler.Validity.INDETERMINATE); if (isRequestedProperty(ContentHandler.BYTE_ORDER_MARK_PROPERTY, options)) { ByteOrderMark byteOrderMark = getByteOrderMark(uri, inputStream, options, context); if (byteOrderMark != null) { result.put(ContentHandler.BYTE_ORDER_MARK_PROPERTY, byteOrderMark); } } if (isRequestedProperty(ContentHandler.CHARSET_PROPERTY, options)) { String charset = getCharset(uri, inputStream, options, context); if (charset != null) { result.put(ContentHandler.CHARSET_PROPERTY, charset); } } if (isRequestedProperty(ContentHandler.LINE_DELIMITER_PROPERTY, options)) { String lineDelimiter = getLineDelimiter(uri, inputStream, options, context); if (lineDelimiter != null) { result.put(ContentHandler.LINE_DELIMITER_PROPERTY, lineDelimiter); } } return result; }
/** * Returns the byte order marker at the start of the input stream. * @param uri the URI of the input stream. * @param inputStream the input stream to scan. * @param options any options to influence the behavior; this base implementation ignores this. * @param context the cache for fetching and storing a previous computation of the byte order marker; this base implementation caches {@link ContentHandler#BYTE_ORDER_MARK_PROPERTY}. * @return the byte order marker at the start of the input stream. * @throws IOException */ protected ByteOrderMark getByteOrderMark(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { ByteOrderMark result = (ByteOrderMark)context.get(ContentHandler.BYTE_ORDER_MARK_PROPERTY); if (result == null) { result = ByteOrderMark.read(inputStream); inputStream.reset(); context.put(ContentHandler.BYTE_ORDER_MARK_PROPERTY, result); } return result; }
/** * This base implementation handles looking up the {@link ContentHandler#BYTE_ORDER_MARK_PROPERTY} if that's a {@link #isRequestedProperty(String, Map) requested property}. */ public Map<String, Object> contentDescription(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { Map<String, Object> result = createContentDescription(ContentHandler.Validity.INDETERMINATE); if (isRequestedProperty(ContentHandler.BYTE_ORDER_MARK_PROPERTY, options)) { result.put(ContentHandler.BYTE_ORDER_MARK_PROPERTY, getByteOrderMark(uri, inputStream, options, context)); } return result; }
@Override public EList<ContentHandler> getContentHandlers() { return existing.getContentHandlers(); }
@Override public IResourceServiceProvider getResourceServiceProvider(URI uri) { return getResourceServiceProvider(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); }
protected void writeGrammar() { final Wrapper<Boolean> isSaving = Wrapper.<Boolean>wrap(Boolean.valueOf(false)); final ResourceSet cloneInto = new ResourceSetImpl(); Map<String, Object> _extensionToFactoryMap = cloneInto.getResourceFactoryRegistry().getExtensionToFactoryMap(); FragmentFakingEcoreResource.FactoryImpl _factoryImpl = new FragmentFakingEcoreResource.FactoryImpl(isSaving); _extensionToFactoryMap.put( FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX, _factoryImpl); final ResourceSet resourceSet = EcoreUtil2.<ResourceSet>clone(cloneInto, this.getLanguage().getGrammar().eResource().getResourceSet()); EObject _head = IterableExtensions.<EObject>head(resourceSet.getResource(this.getLanguage().getGrammar().eResource().getURI(), true).getContents()); final Grammar copy = ((Grammar) _head); String _xifexpression = null; if ((this.xmlVersion == null)) { _xifexpression = GrammarUtil.getClasspathRelativePathToBinGrammar(copy); } else { String _xblockexpression = null; { String _simpleName = GrammarAccessFragment2.class.getSimpleName(); String _plus = ("The property \'xmlVersion\' has been specified for this " + _simpleName); String _plus_1 = (_plus + ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback."); GrammarAccessFragment2.LOG.warn(_plus_1); _xblockexpression = GrammarUtil.getClasspathRelativePathToXmi(copy); } _xifexpression = _xblockexpression; } final String path = _xifexpression; final URI uri = this.getProjectConfig().getRuntime().getSrcGen().getURI(path); final Resource resource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE); HashSet<Grammar> _hashSet = new HashSet<Grammar>(); this.addAllGrammarsToResource(resource, copy, _hashSet); isSaving.set(Boolean.valueOf(true)); final Map<String, Object> saveOptions = CollectionLiterals.<String, Object>newHashMap(); if ((resource instanceof XMLResource)) { String _elvis = null; if (this.xmlVersion != null) { _elvis = this.xmlVersion; } else { _elvis = "1.0"; } ((XMLResource)resource).setXMLVersion(_elvis); } else { if ((resource instanceof BinaryResourceImpl)) { saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1); saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.valueOf(true)); } } try { resource.save(saveOptions); } catch (final Throwable _t) { if (_t instanceof IOException) { final IOException e = (IOException)_t; GrammarAccessFragment2.LOG.error(e.getMessage(), e); } else { throw Exceptions.sneakyThrow(_t); } } finally { isSaving.set(Boolean.valueOf(false)); } }