### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/BinaryField.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/BinaryField.java,v retrieving revision 1.32 diff -u -r1.32 BinaryField.java --- model/org/eclipse/jdt/internal/core/BinaryField.java 13 Dec 2005 20:09:32 -0000 1.32 +++ model/org/eclipse/jdt/internal/core/BinaryField.java 3 Jan 2006 18:43:26 -0000 @@ -107,8 +107,8 @@ } } } -public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { - String contents = ((BinaryType) this.getDeclaringType()).getJavadocContents(monitor, defaultEncoding); +public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { + String contents = ((BinaryType) this.getDeclaringType()).getJavadocContents(monitor); if (contents == null) return null; int indexAnchor = contents.indexOf( JavadocConstants.ANCHOR_PREFIX_START + this.getElementName() + JavadocConstants.ANCHOR_PREFIX_END); Index: model/org/eclipse/jdt/internal/core/ClassFile.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFile.java,v retrieving revision 1.114 diff -u -r1.114 ClassFile.java --- model/org/eclipse/jdt/internal/core/ClassFile.java 29 Nov 2005 09:04:32 -0000 1.114 +++ model/org/eclipse/jdt/internal/core/ClassFile.java 3 Jan 2006 18:43:26 -0000 @@ -203,8 +203,8 @@ } return elt; } -public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { - return this.getType().getAttachedJavadoc(monitor, defaultEncoding); +public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { + return this.getType().getAttachedJavadoc(monitor); } /** * Returns the ClassFileReaderspecific for this IClassFile, based Index: model/org/eclipse/jdt/internal/core/PackageFragment.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/PackageFragment.java,v retrieving revision 1.73 diff -u -r1.73 PackageFragment.java --- model/org/eclipse/jdt/internal/core/PackageFragment.java 5 Dec 2005 19:10:44 -0000 1.73 +++ model/org/eclipse/jdt/internal/core/PackageFragment.java 3 Jan 2006 18:43:27 -0000 @@ -439,7 +439,7 @@ /* * @see IJavaElement#getAttachedJavadoc(IProgressMonitor) */ -public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { +public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject()); String cachedJavadoc = null; synchronized (projectInfo.javadocCache) { @@ -461,7 +461,7 @@ pathBuffer.append(packPath).append('/').append(JavadocConstants.PACKAGE_FILE_NAME); if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException(); - final String contents = getURLContents(String.valueOf(pathBuffer), defaultEncoding); + final String contents = getURLContents(String.valueOf(pathBuffer)); if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException(); if (contents == null) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this)); synchronized (projectInfo.javadocCache) { Index: model/org/eclipse/jdt/internal/core/BinaryMethod.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/BinaryMethod.java,v retrieving revision 1.82 diff -u -r1.82 BinaryMethod.java --- model/org/eclipse/jdt/internal/core/BinaryMethod.java 14 Dec 2005 03:45:29 -0000 1.82 +++ model/org/eclipse/jdt/internal/core/BinaryMethod.java 3 Jan 2006 18:43:26 -0000 @@ -205,7 +205,7 @@ public void run() { try { // this call has a side-effect on the per project info cache - nameCollector.setJavadoc(BinaryMethod.this.getAttachedJavadoc(null, "UTF-8")); //$NON-NLS-1$ + nameCollector.setJavadoc(BinaryMethod.this.getAttachedJavadoc(null)); } catch (JavaModelException e) { // ignore } @@ -455,10 +455,10 @@ buffer.append(this.occurrenceCount); } } -public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { +public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { IType declaringType = this.getDeclaringType(); - String contents = ((BinaryType) declaringType).getJavadocContents(monitor, defaultEncoding); + String contents = ((BinaryType) declaringType).getJavadocContents(monitor); return extractJavadoc(declaringType, contents); } private String extractJavadoc(IType declaringType, String contents) throws JavaModelException { Index: model/org/eclipse/jdt/internal/core/BinaryType.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/BinaryType.java,v retrieving revision 1.135 diff -u -r1.135 BinaryType.java --- model/org/eclipse/jdt/internal/core/BinaryType.java 17 Dec 2005 03:57:49 -0000 1.135 +++ model/org/eclipse/jdt/internal/core/BinaryType.java 3 Jan 2006 18:43:26 -0000 @@ -986,8 +986,8 @@ else buffer.append(""); //$NON-NLS-1$ } -public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { - final String contents = getJavadocContents(monitor, defaultEncoding); +public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { + final String contents = getJavadocContents(monitor); if (contents == null) return null; final int indexOfStartOfClassData = contents.indexOf(JavadocConstants.START_OF_CLASS_DATA); if (indexOfStartOfClassData == -1) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this)); @@ -1025,7 +1025,7 @@ } return contents.substring(indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA_LENGTH, indexOfNextSummary); } -public String getJavadocContents(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { +public String getJavadocContents(IProgressMonitor monitor) throws JavaModelException { PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject()); String cachedJavadoc = null; synchronized (projectInfo.javadocCache) { @@ -1064,7 +1064,7 @@ pathBuffer.append(pack.getElementName().replace('.', '/')).append('/').append(typeQualifiedName).append(JavadocConstants.HTML_EXTENSION); if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException(); - final String contents = getURLContents(String.valueOf(pathBuffer), defaultEncoding); + final String contents = getURLContents(String.valueOf(pathBuffer)); synchronized (projectInfo.javadocCache) { projectInfo.javadocCache.put(this, contents); } Index: model/org/eclipse/jdt/internal/core/JavaElement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaElement.java,v retrieving revision 1.114 diff -u -r1.114 JavaElement.java --- model/org/eclipse/jdt/internal/core/JavaElement.java 14 Dec 2005 14:53:31 -0000 1.114 +++ model/org/eclipse/jdt/internal/core/JavaElement.java 3 Jan 2006 18:43:26 -0000 @@ -38,6 +38,10 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement { // private static final QualifiedName PROJECT_JAVADOC= new QualifiedName(JavaCore.PLUGIN_ID, "project_javadoc_location"); //$NON-NLS-1$ + private static final byte[] CLOSING_DOUBLE_QUOTE = new byte[] { 34 }; + private static final byte[] CHARSET = new byte[] {99, 104, 97, 114, 115, 101, 116, 61 }; + private static final byte[] CONTENT_TYPE = new byte[] { 34, 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 34 }; + private static final byte[] CONTENT = new byte[] { 99, 111, 110, 116, 101, 110, 116, 61, 34 }; public static final char JEM_ESCAPE = '\\'; public static final char JEM_JAVAPROJECT = '='; public static final char JEM_PACKAGEFRAGMENTROOT = '/'; @@ -684,45 +688,82 @@ } /* - * @see IJavaElement#getAttachedJavadoc(IProgressMonitor, String) + * @see IJavaElement#getAttachedJavadoc(IProgressMonitor) */ - public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException { + public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { return null; } + public String getAttachedJavadoc(IProgressMonitor monitor, String encoding) throws JavaModelException { + return getAttachedJavadoc(monitor); + } + + int getIndexOf(byte[] array, byte[] toBeFound, int start) { + if (array == null || toBeFound == null) + return -1; + final int toBeFoundLength = toBeFound.length; + final int arrayLength = array.length; + if (arrayLength < toBeFoundLength) + return -1; + loop: for (int i = start, max = arrayLength - toBeFoundLength + 1; i < max; i++) { + if (array[i] == toBeFound[0]) { + for (int j = 1; j < toBeFoundLength; j++) { + if (array[i + j] != toBeFound[j]) + continue loop; + } + return i; + } + } + return -1; + } /* * We don't use getContentEncoding() on the URL connection, because it might leave open streams behind. * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=117890 * */ - protected String getURLContents(String docUrlValue, String defaultEncoding) throws JavaModelException { + protected String getURLContents(String docUrlValue) throws JavaModelException { InputStream stream = null; try { - String encoding = defaultEncoding; - try { - if (encoding == null) { - encoding = this.getJavaProject().getProject().getDefaultCharset(); - } - } catch (CoreException e) { - // ignore - } - // long time = System.currentTimeMillis(); URL docUrl = new URL(docUrlValue); URLConnection connection = docUrl.openConnection(); - // System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for opening connection for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$ - // time = System.currentTimeMillis(); if ("jar".equals(docUrl.getProtocol())) { //$NON-NLS-1$ // if jar protocol is using a cache, some file descriptors are left behind and the resource cannot be deleted connection.setUseCaches(false); } + String encoding = connection.getContentEncoding(); stream = new BufferedInputStream(connection.getInputStream()); - // System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for getting stream for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$ - // time = System.currentTimeMillis(); - char[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, -1, encoding); - // System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for reading stream for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$ - if (contents != null) { - // System.out.println("Size = " + (contents.length / 1024) + "kb");//$NON-NLS-1$//$NON-NLS-2$ - return String.valueOf(contents); + byte[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, connection.getContentLength()); + if (encoding == null) { + int index = getIndexOf(contents, CONTENT_TYPE, 0); + if (index != -1) { + index = getIndexOf(contents, CONTENT, index); + if (index != -1) { + int offset = index + CONTENT.length; + int index2 = getIndexOf(contents, CLOSING_DOUBLE_QUOTE, offset); + if (index2 != -1) { + final int charsetIndex = getIndexOf(contents, CHARSET, offset); + if (charsetIndex != -1) { + int start = charsetIndex + CHARSET.length; + encoding = new String(contents, start, index2 - start, "UTF-8"); //$NON-NLS-1$ + } + } + } + } + } + try { + if (encoding == null) { + encoding = this.getJavaProject().getProject().getDefaultCharset(); + } + } catch (CoreException e) { + // ignore } + if (contents != null) { + if (encoding != null) { + return new String(contents, encoding); + } else { + // platform encoding is used + return new String(contents); + } + } } catch (MalformedURLException e) { throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this)); } catch (FileNotFoundException e) { Index: model/org/eclipse/jdt/core/IJavaElement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaElement.java,v retrieving revision 1.30 diff -u -r1.30 IJavaElement.java --- model/org/eclipse/jdt/core/IJavaElement.java 13 Dec 2005 20:06:10 -0000 1.30 +++ model/org/eclipse/jdt/core/IJavaElement.java 3 Jan 2006 18:43:26 -0000 @@ -162,31 +162,37 @@ /** *

Returns the Javadoc as an html source if this element has an attached javadoc, * null otherwise.

- *

This should be used only for binary elements. Source elements will always return null. - * The encoding used to read the javadoc is the one defined by the content type of the - * file. If none is defined, then the specified encoding will be used as the default encoding.

+ *

This should be used only for binary elements. Source elements will always return null.

+ *

The encoding used to read the javadoc is the one defined by the content type of the + * file. If none is defined, then the project's encoding of this java element is used. If the project's + * encoding cannot be retrieved, then the platform encoding is used.

*

In case of the javadoc doesn't exist for this element, null is returned.

* *

The html is extracted from the attached javadoc and provided as is. No * transformation or validation is done.

* - *

If the default encoding is set to null, the default encoding of the project is used. If - * the default encoding of the project is not supported, then the platform encoding is used.

- * *

NOTE: This API is subject to change before the 3.2 release.

* * @param monitor the given progress monitor - * @param defaultEncoding the given default encoding * @exception JavaModelException if: * @return the extracted javadoc from the attached javadoc, null if none * @see IClasspathAttribute#JAVADOC_LOCATION_ATTRIBUTE_NAME * @since 3.2 */ - String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException; + String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException; + + /** + * @param monitor + * @param encoding + * @return the attached javadoc + * @throws JavaModelException + * @deprecated Use #{@link #getAttachedJavadoc(IProgressMonitor) getAttachedJavadoc(IProgressMonitor)} instead + */ + String getAttachedJavadoc(IProgressMonitor monitor, String encoding) throws JavaModelException; /** * Returns the resource that corresponds directly to this element,