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

The following examples show how to use org.eclipse.core.resources.IFile#getCharset() . These examples are extracted from open source projects. 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
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 2
/**
 * 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 3
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 4
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 5
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
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
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 Project: ContentAssist   File: ResourceMacro.java    License: 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 9
Source Project: ContentAssist   File: XmlFileStream.java    License: 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 10
private String getCharSetName(IFile file) {
	try {
		return file.getCharset();
	} catch (CoreException e) {
		return "unknown"; //$NON-NLS-1$
	}
}
 
Example 11
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 12
public static String getEncoding(IFile file) {
	try {
		return file.getCharset(false); // Non inheritance
	} catch (CoreException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 13
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 14
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 15
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 16
/**
 * 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 17
/**
 * 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 18
public static String fileToString(IFile file) throws CoreException, IOException {
	return new String(fileToByteArray(file), file.getCharset());
}
 
Example 19
/**
 * @since 2.3
 */
protected String getEncoding(IFile file) throws CoreException {
	return file.getCharset(true);
}
 
Example 20
public static String fileToString(IFile file) throws CoreException, IOException {
	return new String(fileToByteArray(file), file.getCharset());
}