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

The following examples show how to use org.eclipse.core.resources.IFile#getCharset() . 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: CreateTextFileChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public String getCurrentContent() throws JavaModelException {
	IFile file= getOldFile(new NullProgressMonitor());
	if (! file.exists())
		return ""; //$NON-NLS-1$
	InputStream stream= null;
	try{
		stream= file.getContents();
		String encoding= file.getCharset();
		String c= NLSUtil.readString(stream, encoding);
		return (c == null) ? "": c; //$NON-NLS-1$
	} catch (CoreException e){
		throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION);
	} finally {
		try {
			if (stream != null)
				stream.close();
		} catch (IOException x) {
		}
	}
}
 
Example 2
Source File: CreateCopyOfCompilationUnitChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void setEncoding(ICompilationUnit unit) {
	IResource resource= unit.getResource();
	// no file so the encoding is taken from the target
	if (!(resource instanceof IFile))
		return;
	IFile file= (IFile) resource;
	try {
		String encoding= file.getCharset(false);
		if (encoding != null) {
			setEncoding(encoding, true);
		} else {
			encoding= file.getCharset(true);
			if (encoding != null) {
				setEncoding(encoding, false);
			}
		}
	} catch (CoreException e) {
		// Take encoding from target
	}
}
 
Example 3
Source File: JavaSearchDocument.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public String getEncoding() {
	// Return the encoding of the associated file
	IFile resource = getFile();
	if (resource != null) {
		try {
			return resource.getCharset();
		}
		catch(CoreException ce) {
			try {
				return ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();
			} catch (CoreException e) {
				// use no encoding
			}
		}
	}
	return null;
}
 
Example 4
Source File: XsltQuickFix.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to edit the {@link IDocument} in the open editor. If the editor is not open,
 * reads the file from memory and transforms in place.
 */
@Override
public void run(IMarker marker) {
  try {
    IFile file = (IFile) marker.getResource();
    IDocument document = getCurrentDocument(file);
    URL xslPath = XsltQuickFix.class.getResource(xsltPath);
    if (document != null) {
      String currentContents = document.get();
      try (Reader documentReader = new StringReader(currentContents);
          InputStream stylesheet = xslPath.openStream();
          InputStream transformed = Xslt.applyXslt(documentReader, stylesheet)) {
        String encoding = file.getCharset();
        String newDoc = ValidationUtils.convertStreamToString(transformed, encoding);
        document.set(newDoc);
      }
    } else {
      Xslt.transformInPlace(file, xslPath);
    }
  } catch (IOException | TransformerException | CoreException ex) {
    logger.log(Level.SEVERE, ex.getMessage());
  }
}
 
Example 5
Source File: BuiltInConflictsCompareInput.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
private String getCharSet() {
	String charSet = null;
	String destFilePath = fDestinationFile.getAbsolutePath();
	String workspacePath;
	int index = destFilePath.indexOf(File.separator + Policy.bind("BuiltInConflictsCompareInput.1") + File.separator); //$NON-NLS-1$
	if (index == -1) {
		workspacePath = destFilePath;
	} else {
		workspacePath = destFilePath.substring(0, index) + File.separator + fileName;
	}
	File workspaceFile = new File(workspacePath);
	IFile destinationFile = (IFile) File2Resource
	.getResource(workspaceFile);
	if (destinationFile != null) {			
		try {
			charSet = destinationFile.getCharset();
		} catch (CoreException e) {}
	}
	return charSet;
}
 
Example 6
Source File: CreateCopyOfCompilationUnitChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void setEncoding(ICompilationUnit unit) {
	IResource resource= unit.getResource();
	// no file so the encoding is taken from the target
	if (!(resource instanceof IFile)) {
		return;
	}
	IFile file= (IFile) resource;
	try {
		String encoding= file.getCharset(false);
		if (encoding != null) {
			setEncoding(encoding, true);
		} else {
			encoding= file.getCharset(true);
			if (encoding != null) {
				setEncoding(encoding, false);
			}
		}
	} catch (CoreException e) {
		// Take encoding from target
	}
}
 
Example 7
Source File: CreateTextFileChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public String getCurrentContent() throws JavaModelException {
	IFile file= getOldFile(new NullProgressMonitor());
	if (! file.exists())
	 {
		return ""; //$NON-NLS-1$
	}
	InputStream stream= null;
	try{
		stream= file.getContents();
		String encoding= file.getCharset();
		String c= NLSUtil.readString(stream, encoding);
		return (c == null) ? "": c; //$NON-NLS-1$
	} catch (CoreException e){
		throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION);
	} finally {
		try {
			if (stream != null) {
				stream.close();
			}
		} catch (IOException x) {
		}
	}
}
 
Example 8
Source File: NewPackageWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void createPackageHtml(IPackageFragmentRoot root, IProgressMonitor monitor) throws CoreException {
	IWorkspace workspace= ResourcesPlugin.getWorkspace();
	IFolder createdPackage= workspace.getRoot().getFolder(fCreatedPackageFragment.getPath());
	IFile packageHtml= createdPackage.getFile(PACKAGE_HTML_FILENAME);
	String charset= packageHtml.getCharset();
	String content= buildPackageHtmlContent(root, charset);
	try {
		packageHtml.create(new ByteArrayInputStream(content.getBytes(charset)), false, monitor);
	} catch (UnsupportedEncodingException e) {
		String message= "charset " + charset + " not supported by platform"; //$NON-NLS-1$ //$NON-NLS-2$
		throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, message, e));
	}
}
 
Example 9
Source File: CreateFileChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initializeEncoding() {
	if (fEncoding == null) {
		fExplicitEncoding= false;
		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
		if (file != null) {
			try {
				if (file.exists()) {
					fEncoding= file.getCharset(false);
					if (fEncoding == null) {
						fEncoding= file.getCharset(true);
					} else {
						fExplicitEncoding= true;
					}
				} else {
					IContentType contentType= Platform.getContentTypeManager().findContentTypeFor(file.getName());
					if (contentType != null)
						fEncoding= contentType.getDefaultCharset();
					if (fEncoding == null)
						fEncoding= file.getCharset(true);
				}
			} catch (CoreException e) {
				fEncoding= ResourcesPlugin.getEncoding();
				fExplicitEncoding= true;
			}
		} else {
			fEncoding= ResourcesPlugin.getEncoding();
			fExplicitEncoding= true;
		}
	}
	Assert.isNotNull(fEncoding);
}
 
Example 10
Source File: BasicCompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initEncoding(IJavaElement javaElement) {
	if (javaElement != null) {
		try {
			IJavaProject javaProject = javaElement.getJavaProject();
			switch (javaElement.getElementType()) {
				case IJavaElement.COMPILATION_UNIT:
					IFile file = (IFile) javaElement.getResource();
					if (file != null) {
						this.encoding = file.getCharset();
						break;
					}
					// if no file, then get project encoding
					// $FALL-THROUGH$
				default:
					IProject project = (IProject) javaProject.getResource();
					if (project != null) {
						this.encoding = project.getDefaultCharset();
					}
					break;
			}
		} catch (CoreException e1) {
			this.encoding = null;
		}
	} else  {
		this.encoding = null;
	}
}
 
Example 11
Source File: Resources.java    From eclipse-encoding-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public static String getEncoding(IFile file) {
	try {
		return file.getCharset(false); // Non inheritance
	} catch (CoreException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 12
Source File: CreateFileChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void initializeEncoding() {
	if (fEncoding == null) {
		fExplicitEncoding= false;
		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
		if (file != null) {
			try {
				if (file.exists()) {
					fEncoding= file.getCharset(false);
					if (fEncoding == null) {
						fEncoding= file.getCharset(true);
					} else {
						fExplicitEncoding= true;
					}
				} else {
					IContentType contentType= Platform.getContentTypeManager().findContentTypeFor(file.getName());
					if (contentType != null) {
						fEncoding= contentType.getDefaultCharset();
					}
					if (fEncoding == null) {
						fEncoding= file.getCharset(true);
					}
				}
			} catch (CoreException e) {
				fEncoding= ResourcesPlugin.getEncoding();
				fExplicitEncoding= true;
			}
		} else {
			fEncoding= ResourcesPlugin.getEncoding();
			fExplicitEncoding= true;
		}
	}
	Assert.isNotNull(fEncoding);
}
 
Example 13
Source File: TextSearchVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String getCharSetName(IFile file) {
	try {
		return file.getCharset();
	} catch (CoreException e) {
		return "unknown"; //$NON-NLS-1$
	}
}
 
Example 14
Source File: XmlFileStream.java    From ContentAssist with MIT License 5 votes vote down vote up
/**
 * 
 * Writes the content of the DOM instance into an XML file.
 * @param doc the content of the DOM instance to be written
 * @param path the full path indicating the file which the contents are written into
 * @param file the input file
 */
public static void write(Document doc, String path, IFile file) {
    try {
        String encoding;
        if (file == null) {
            encoding = Charset.defaultCharset().name();
        } else {
            encoding = file.getCharset();
        }
        
        write(doc, path, encoding);
    } catch (CoreException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: ResourceMacro.java    From ContentAssist with MIT License 5 votes vote down vote up
/**
 * Returns the encoding of the changed source code.
 * @param elem the changed resource
 * @return the encoding of the source code, or <code>null</code>
 */
private String getEncoding(IJavaElement elem) {
    if (elem instanceof ICompilationUnit) {
        ICompilationUnit cu = (ICompilationUnit)elem;
        
        try {
            IFile file = (IFile)cu.getCorrespondingResource();
            return file.getCharset();
        } catch (CoreException e) {
        }
    }
    return null;
}
 
Example 16
Source File: IResourcesSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public static String fileToString(IFile file) throws CoreException, IOException {
	return new String(fileToByteArray(file), file.getCharset());
}
 
Example 17
Source File: EclipseResourceFileSystemAccess2.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
protected String getEncoding(IFile file) throws CoreException {
	return file.getCharset(true);
}
 
Example 18
Source File: IResourcesSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public static String fileToString(IFile file) throws CoreException, IOException {
	return new String(fileToByteArray(file), file.getCharset());
}
 
Example 19
Source File: RenameRefactoringXpectMethod.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Helper method to get the content of an resource at uri. Takes care of the encoding.
 *
 * @param uri
 *            URI to resource
 * @return content as string
 * @throws Exception
 *             in case of io or uri issues
 */
private String getContentForResourceUri(URI uri)
		throws Exception {

	String platformStr = uri.toString().replace("platform:/resource/", "");
	IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformStr));

	java.io.InputStream fileStream = file.getContents();

	java.util.Scanner s = new java.util.Scanner(fileStream, file.getCharset());
	s.useDelimiter("\\A");

	String content = s.hasNext() ? s.next() : "";

	fileStream.close();
	s.close();

	return content;
}
 
Example 20
Source File: QuickFixXpectMethod.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Helper method to get the content of an resource at uri. Takes care of the encoding.
 *
 * @param uri
 *            URI to resource
 * @return content as string
 * @throws Exception
 *             in case of io or uri issues
 */
private String getContentForResourceUri(URI uri)
		throws Exception {

	String platformStr = uri.toString().replace("platform:/resource/", "");
	IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformStr));

	java.io.InputStream fileStream = file.getContents();

	java.util.Scanner s = new java.util.Scanner(fileStream, file.getCharset());
	s.useDelimiter("\\A");

	String content = s.hasNext() ? s.next() : "";

	fileStream.close();
	s.close();

	return content;
}