mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 16:58:56 +00:00
Created encoding provider for project properties, which is required for running workflows that load resources with different encodings
Change-Id: I7fb9963e4fbd418855a574d11b811a4c0b177e58 Signed-off-by: Miro Spönemann <miro.spoenemann@itemis.de>
This commit is contained in:
parent
bcf56d635d
commit
7c56600678
3 changed files with 116 additions and 1 deletions
|
@ -49,6 +49,9 @@ public abstract class AbstractReader extends AbstractWorkflowComponent2 {
|
|||
|
||||
private List<Injector> injectors = Lists.newArrayList();
|
||||
|
||||
/**
|
||||
* Register a language setup. Only the first registered setup is used to inject the resource set.
|
||||
*/
|
||||
public void addRegister(ISetup setup) {
|
||||
injectors.add(setup.createInjectorAndDoEMFRegistration());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.parser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
/**
|
||||
* Use this class to retrieve resource encodings from Eclipse project properties. The provider can be used
|
||||
* without a running Eclipse context, but depends on resources being stored in Eclipse projects.
|
||||
* Parts of the implementation are copied from {@link org.eclipse.core.internal.preferences.EclipsePreferences}.
|
||||
*
|
||||
* @author Miro Spoenemann - Initial contribution and API
|
||||
* @since 2.8
|
||||
*/
|
||||
public class EclipseProjectPropertiesEncodingProvider extends IEncodingProvider.Runtime {
|
||||
|
||||
private static final String PROPERTIES_DIRNAME = ".settings";
|
||||
private static final String PREFS_FILE_EXTENSION = "prefs";
|
||||
private static final String PROJECT_PROPERTIES = "<project>";
|
||||
private static final String ENCODING_PREFIX = "encoding/";
|
||||
|
||||
@Override
|
||||
public String getEncoding(URI uri) {
|
||||
// If a default encoding is explicitly set, use that one
|
||||
String result = getDefaultEncoding();
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
// Read the project properties
|
||||
try {
|
||||
result = getFromProperties(uri);
|
||||
if (result != null)
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
// Fall back to the standard encoding
|
||||
return super.getEncoding(uri);
|
||||
}
|
||||
|
||||
protected String getFromProperties(URI uri) throws IOException {
|
||||
if (!uri.isHierarchical())
|
||||
return null;
|
||||
|
||||
URI projectURI = uri;
|
||||
boolean projectFound = false;
|
||||
do {
|
||||
projectURI = projectURI.trimSegments(1);
|
||||
if (new File(projectURI.path(), ".project").exists()) {
|
||||
projectFound = true;
|
||||
}
|
||||
if (!projectFound && projectURI.segmentCount() == 0)
|
||||
// The resource is not contained in a project
|
||||
return null;
|
||||
} while (!projectFound);
|
||||
|
||||
Properties properties = loadProperties(projectURI);
|
||||
URI resourceUri = URI.createHierarchicalURI(Arrays.copyOfRange(uri.segments(),
|
||||
projectURI.segmentCount(), uri.segmentCount()), null, null);
|
||||
return getValue(properties, resourceUri, ENCODING_PREFIX);
|
||||
}
|
||||
|
||||
protected Properties loadProperties(URI projectURI) throws IOException {
|
||||
URI propertiesUri = projectURI.appendSegment(PROPERTIES_DIRNAME).appendSegment("org.eclipse.core.resources")
|
||||
.appendFileExtension(PREFS_FILE_EXTENSION);
|
||||
InputStream input = null;
|
||||
Properties result = new Properties();
|
||||
try {
|
||||
input = new FileInputStream(propertiesUri.path());
|
||||
result.load(input);
|
||||
} catch (FileNotFoundException e) {
|
||||
// File doesn't exist but that's ok
|
||||
} finally {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getValue(Properties properties, URI resourceUri, String prefix) {
|
||||
URI path = resourceUri;
|
||||
do {
|
||||
String value = properties.getProperty(prefix + path.path());
|
||||
if (value != null)
|
||||
return value;
|
||||
path = path.trimSegments(1);
|
||||
} while (path.segmentCount() > 0);
|
||||
String value = properties.getProperty(prefix + PROJECT_PROPERTIES);
|
||||
if (value != null)
|
||||
return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@ import org.eclipse.xtext.linking.lazy.LazyURIEncoder;
|
|||
import org.eclipse.xtext.naming.IQualifiedNameProvider;
|
||||
import org.eclipse.xtext.naming.SimpleNameProvider;
|
||||
import org.eclipse.xtext.parser.IEncodingProvider;
|
||||
import org.eclipse.xtext.parser.EclipseProjectPropertiesEncodingProvider;
|
||||
import org.eclipse.xtext.parser.antlr.AntlrTokenToStringConverter;
|
||||
import org.eclipse.xtext.parser.antlr.ITokenDefProvider;
|
||||
import org.eclipse.xtext.parser.antlr.NullTokenDefProvider;
|
||||
|
@ -204,7 +205,7 @@ public abstract class DefaultRuntimeModule extends AbstractGenericModule {
|
|||
}
|
||||
|
||||
public void configureRuntimeEncodingProvider(Binder binder) {
|
||||
binder.bind(IEncodingProvider.class).annotatedWith(DispatchingProvider.Runtime.class).to(IEncodingProvider.Runtime.class);
|
||||
binder.bind(IEncodingProvider.class).annotatedWith(DispatchingProvider.Runtime.class).to(EclipseProjectPropertiesEncodingProvider.class);
|
||||
}
|
||||
|
||||
public Class<? extends Provider<IEncodingProvider>> provideIEncodingProvider() {
|
||||
|
|
Loading…
Reference in a new issue