### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/SourceMapper.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceMapper.java,v retrieving revision 1.150 diff -u -r1.150 SourceMapper.java --- model/org/eclipse/jdt/internal/core/SourceMapper.java 28 Jul 2010 16:17:01 -0000 1.150 +++ model/org/eclipse/jdt/internal/core/SourceMapper.java 20 Oct 2010 18:07:36 -0000 @@ -27,6 +27,7 @@ import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -868,12 +869,13 @@ char[] source = null; + IProject project = pkgFrag.getPackageFragmentRoot().getJavaProject().getProject(); JavaModelManager javaModelManager = JavaModelManager.getJavaModelManager(); try { javaModelManager.cacheZipFiles(this); // Cache any zip files we open during this operation if (this.rootPath != null) { - source = getSourceForRootPath(this.rootPath, name); + source = getSourceForRootPath(this.rootPath, name, project); } if (source == null) { @@ -882,7 +884,7 @@ loop: for (Iterator iterator = this.rootPaths.iterator(); iterator.hasNext(); ) { String currentRootPath = (String) iterator.next(); if (!currentRootPath.equals(this.rootPath)) { - source = getSourceForRootPath(currentRootPath, name); + source = getSourceForRootPath(currentRootPath, name, project); if (source != null) { // remember right root path this.rootPath = currentRootPath; @@ -901,7 +903,7 @@ return source; } - private char[] getSourceForRootPath(String currentRootPath, String name) { + private char[] getSourceForRootPath(String currentRootPath, String name, IProject project) { String newFullName; if (!currentRootPath.equals(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH)) { if (currentRootPath.endsWith("/")) { //$NON-NLS-1$ @@ -912,22 +914,47 @@ } else { newFullName = name; } - return this.findSource(newFullName); + return this.findSource(newFullName, project); } - public char[] findSource(String fullName) { + public char[] findSource(String fullName, IProject project) { char[] source = null; Object target = JavaModel.getTarget(this.sourcePath, true); + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511 + // Use the character encoding set by the user than the default one + String charSet = null; if (target instanceof IContainer) { + // For external folders, get use the project's encoding + if (project != null && ExternalFoldersManager.isInternalPathForExternalFolder(((IContainer)target).getFullPath())) { + try { + charSet = project.getDefaultCharset(); + } catch (CoreException e) { + // Proceed with null encoding + } + } IResource res = ((IContainer)target).findMember(fullName); if (res instanceof IFile) { try { - source = org.eclipse.jdt.internal.core.util.Util.getResourceContentsAsCharArray((IFile)res); + if (charSet == null) + source = org.eclipse.jdt.internal.core.util.Util.getResourceContentsAsCharArray((IFile)res); + else + source = org.eclipse.jdt.internal.core.util.Util.getResourceContentsAsCharArray((IFile)res, charSet); } catch (JavaModelException e) { // ignore } } } else { + try { + // If the archive (IResource) is available from the workspace, use it's encoding + // Else take it from the project + if (target instanceof IFile) + charSet = ((IFile)target).getCharset(); + else if (project != null) + charSet = project.getDefaultCharset(); + } catch (CoreException e) { + // Ignore + } + // try to get the entry ZipEntry entry = null; ZipFile zip = null; @@ -937,7 +964,7 @@ entry = zip.getEntry(fullName); if (entry != null) { // now read the source code - source = readSource(entry, zip); + source = readSource(entry, zip, charSet); } } catch (CoreException e) { return null; @@ -948,6 +975,9 @@ return source; } + public char[] findSource(String fullName) { + return findSource(fullName, null); + } /** @@ -1275,11 +1305,11 @@ this.typeDepth = -1; } } - private char[] readSource(ZipEntry entry, ZipFile zip) { + private char[] readSource(ZipEntry entry, ZipFile zip, String charSet) { try { byte[] bytes = Util.getZipEntryByteContent(entry, zip); if (bytes != null) { - return Util.bytesToChar(bytes, this.encoding); + return Util.bytesToChar(bytes, charSet == null ? this.encoding : charSet); } } catch (IOException e) { // ignore