Java Code Examples for org.eclipse.core.resources.IFile#setCharset()

The following examples show how to use org.eclipse.core.resources.IFile#setCharset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EclipseBasedProjectModelSetup.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private void createProject(N4JSProjectName projectName, String string)
		throws CoreException, UnsupportedEncodingException {
	IProject project = workspace.getProject(projectName.toEclipseProjectName().getRawName());
	IFile projectDescriptionFile = project.getFile(PROJECT_DESCRIPTION_FILENAME);
	@SuppressWarnings("resource")
	StringInputStream content = new StringInputStream(string, Charsets.UTF_8.name());
	projectDescriptionFile.create(content, false, null);
	projectDescriptionFile.setCharset(Charsets.UTF_8.name(), null);

	IFolder src = project.getFolder("src");
	src.create(false, true, null);
	IFolder sub = src.getFolder("sub");
	sub.create(false, true, null);
	IFolder leaf = sub.getFolder("leaf");
	leaf.create(false, true, null);
	src.getFile("A.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	src.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("C.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	leaf.getFile("D.js").create(new ByteArrayInputStream(new byte[0]), false, null);

	ProjectTestsUtils.createDummyN4JSRuntime(project);
}
 
Example 2
Source File: EncodingUtils.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * In the case resource is IFile, its encoding is guessed from its first bytes (see {@link TextEncoding#readFileAndCodepage(java.io.InputStream, StringBuilder, CodepageId[])}) 
 * @param r
 * @param monitor
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws CoreException 
 */
public static void determineAndSetEncoding(IFile f, IProgressMonitor monitor) throws IOException, CoreException {
	if (!f.exists()) {
		return;
	}
	CodepageId cpId[] = {null};
	try(InputStream stream = f.getContents()){
		TextEncoding.readFileAndCodepage(stream, null, cpId);
		if (cpId[0] == null) {
			cpId[0] = null;
		}
		if(cpId[0] != null && !cpId[0].charsetName.equalsIgnoreCase(f.getCharset(true))) {
			f.setCharset(cpId[0].charsetName, monitor);
		}
	}
}
 
Example 3
Source File: GroovyFileStore.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doSave(final Object content) {
    if (content instanceof String) {
        if (getResource().exists() && content != null && content.equals(getContent())) {
            return;
        }
        try {
            final String scriptContent = (String) content;
            final InputStream is = new ByteArrayInputStream(scriptContent.getBytes(UTF_8));
            final IFile sourceFile = getResource();
            if (sourceFile.exists() && FileActionDialog.overwriteQuestion(getName())) {
                sourceFile.setContents(is, IResource.FOLDER, Repository.NULL_PROGRESS_MONITOR);
            } else {
                sourceFile.create(is, true, Repository.NULL_PROGRESS_MONITOR);
            }
            if (!UTF_8.equals(sourceFile.getCharset())) {
                sourceFile.setCharset(UTF_8, Repository.NULL_PROGRESS_MONITOR);
            }
        } catch (final Exception e) {
            BonitaStudioLog.error(e);
        }
    }

}
 
Example 4
Source File: CrossflowDiagramEditorUtil.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
/**
* @generated
*/
public static void setCharset(IFile file) {
	if (file == null) {
		return;
	}
	try {
		file.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$
	} catch (CoreException e) {
		CrossflowDiagramEditorPlugin.getInstance().logError("Unable to set charset for file " + file.getFullPath(), //$NON-NLS-1$
				e);
	}
}
 
Example 5
Source File: EncodingTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testFileEncodingIsUsedInEMFResource() throws Exception {
	IFile file = createFile("foo/x_default.encodinguitestlanguage", "");
	openEditorAndCheckEncoding(file, root().getDefaultCharset());
	file = createFile("foo/x_utf.encodinguitestlanguage", "");
	file.setCharset("UTF-8", null);
	openEditorAndCheckEncoding(file, "UTF-8");
	file = createFile("foo/x_iso.encodinguitestlanguage", "");
	file.setCharset("ISO-8859-1", null);
	openEditorAndCheckEncoding(file, "ISO-8859-1");
}
 
Example 6
Source File: UnicodeEscapeTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private void createFile(IProject project, String content, String encoding) throws Exception {
	IFile file = workbenchTestHelper.createFile("Example.xtend", "");
	if (encoding == null)
		encoding = project.getDefaultCharset();
	else
		file.setCharset(encoding, null);
	file.setContents(new StringInputStream(content, encoding), true, true, null);
	project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
}
 
Example 7
Source File: DiagramCreator.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected void setCharset(IFile file) {
	if (file == null) {
		return;
	}
	try {
		file.setCharset("UTF-8", new NullProgressMonitor());
	} catch (CoreException e) {
		e.printStackTrace();
	}
}
 
Example 8
Source File: ScopedPreferences.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private void dumpSaveDataToFile(Map<String, Object> saveData, IFile yamlFile, boolean exists) throws IOException,
        CoreException {
    String dumpAsMap = YamlWrapper.dumpAsMap(saveData);
    if (!exists) {
        // Create empty (so that we can set the charset properly later on).
        yamlFile.create(new ByteArrayInputStream("".getBytes()), true, new NullProgressMonitor());
    }
    yamlFile.setCharset("UTF-8", new NullProgressMonitor());
    yamlFile.setContents(new ByteArrayInputStream(dumpAsMap.getBytes(StandardCharsets.UTF_8)), true, true,
            new NullProgressMonitor());
}
 
Example 9
Source File: InternalImpl.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void changeFileEncoding(String projectName, String path, String charset)
    throws RemoteException {
  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

  IFile file = project.getFile(path);

  try {
    file.setCharset(charset, null);
  } catch (CoreException e) {
    log.error(e.getMessage(), e);
    throw new RemoteException(e.getMessage(), e.getCause());
  }
}
 
Example 10
Source File: ProcessDiagramEditorUtil.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
* @generated
*/
public static void setCharset(IFile file) {
	if (file == null) {
		return;
	}
	try {
		file.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$
	} catch (CoreException e) {
		ProcessDiagramEditorPlugin.getInstance().logError("Unable to set charset for file " + file.getFullPath(), //$NON-NLS-1$
				e);
	}
}
 
Example 11
Source File: ProcBuilder.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private void setCharset(final IFile file) {
    if (file == null) {
        return;
    }
    try {
        file.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$
    } catch (final CoreException e) {
        ProcessDiagramEditorPlugin.getInstance().logError("Unable to set charset for file " + file.getFullPath(), e); //$NON-NLS-1$
    }
}