Search in sources :

Example 1 with BinaryResourceImpl

use of org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl in project xtext-core by eclipse.

the class GrammarAccessFragment2 method writeGrammar.

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));
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) BinaryResourceImpl(org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) FragmentFakingEcoreResource(org.eclipse.xtext.xtext.generator.grammarAccess.FragmentFakingEcoreResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) FragmentFakingEcoreResource(org.eclipse.xtext.xtext.generator.grammarAccess.FragmentFakingEcoreResource) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 BinaryResourceImpl (org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 XMLResource (org.eclipse.emf.ecore.xmi.XMLResource)1 Grammar (org.eclipse.xtext.Grammar)1 FragmentFakingEcoreResource (org.eclipse.xtext.xtext.generator.grammarAccess.FragmentFakingEcoreResource)1