View | Details | Raw Unified | Return to bug 122506 | Differences between
and this patch

Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/BinaryField.java (-2 / +2 lines)
Lines 107-114 Link Here
107
		}
107
		}
108
	}
108
	}
109
}
109
}
110
public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
110
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
111
	String contents = ((BinaryType) this.getDeclaringType()).getJavadocContents(monitor, defaultEncoding);
111
	String contents = ((BinaryType) this.getDeclaringType()).getJavadocContents(monitor);
112
	if (contents == null) return null;
112
	if (contents == null) return null;
113
	int indexAnchor = contents.indexOf(
113
	int indexAnchor = contents.indexOf(
114
			JavadocConstants.ANCHOR_PREFIX_START + this.getElementName() + JavadocConstants.ANCHOR_PREFIX_END);
114
			JavadocConstants.ANCHOR_PREFIX_START + this.getElementName() + JavadocConstants.ANCHOR_PREFIX_END);
(-)model/org/eclipse/jdt/internal/core/ClassFile.java (-2 / +2 lines)
Lines 203-210 Link Here
203
	}
203
	}
204
	return elt;
204
	return elt;
205
}
205
}
206
public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
206
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
207
	return this.getType().getAttachedJavadoc(monitor, defaultEncoding);
207
	return this.getType().getAttachedJavadoc(monitor);
208
}
208
}
209
/**
209
/**
210
 * Returns the <code>ClassFileReader</code>specific for this IClassFile, based
210
 * Returns the <code>ClassFileReader</code>specific for this IClassFile, based
(-)model/org/eclipse/jdt/internal/core/PackageFragment.java (-2 / +2 lines)
Lines 439-445 Link Here
439
/*
439
/*
440
 * @see IJavaElement#getAttachedJavadoc(IProgressMonitor)
440
 * @see IJavaElement#getAttachedJavadoc(IProgressMonitor)
441
 */
441
 */
442
public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
442
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
443
	PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject());
443
	PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject());
444
	String cachedJavadoc = null;
444
	String cachedJavadoc = null;
445
	synchronized (projectInfo.javadocCache) {
445
	synchronized (projectInfo.javadocCache) {
Lines 461-467 Link Here
461
	pathBuffer.append(packPath).append('/').append(JavadocConstants.PACKAGE_FILE_NAME);
461
	pathBuffer.append(packPath).append('/').append(JavadocConstants.PACKAGE_FILE_NAME);
462
	
462
	
463
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
463
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
464
	final String contents = getURLContents(String.valueOf(pathBuffer), defaultEncoding);
464
	final String contents = getURLContents(String.valueOf(pathBuffer));
465
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
465
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
466
	if (contents == null) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
466
	if (contents == null) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
467
	synchronized (projectInfo.javadocCache) {
467
	synchronized (projectInfo.javadocCache) {
(-)model/org/eclipse/jdt/internal/core/BinaryMethod.java (-3 / +3 lines)
Lines 205-211 Link Here
205
				public void run() {
205
				public void run() {
206
					try {
206
					try {
207
						// this call has a side-effect on the per project info cache
207
						// this call has a side-effect on the per project info cache
208
						nameCollector.setJavadoc(BinaryMethod.this.getAttachedJavadoc(null, "UTF-8")); //$NON-NLS-1$
208
						nameCollector.setJavadoc(BinaryMethod.this.getAttachedJavadoc(null));
209
			        } catch (JavaModelException e) {
209
			        } catch (JavaModelException e) {
210
	 		        	// ignore
210
	 		        	// ignore
211
	 		        }
211
	 		        }
Lines 455-464 Link Here
455
		buffer.append(this.occurrenceCount);
455
		buffer.append(this.occurrenceCount);
456
	}
456
	}
457
}
457
}
458
public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
458
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
459
	IType declaringType = this.getDeclaringType();
459
	IType declaringType = this.getDeclaringType();
460
460
461
	String contents = ((BinaryType) declaringType).getJavadocContents(monitor, defaultEncoding);
461
	String contents = ((BinaryType) declaringType).getJavadocContents(monitor);
462
	return extractJavadoc(declaringType, contents);
462
	return extractJavadoc(declaringType, contents);
463
}
463
}
464
private String extractJavadoc(IType declaringType, String contents) throws JavaModelException {
464
private String extractJavadoc(IType declaringType, String contents) throws JavaModelException {
(-)model/org/eclipse/jdt/internal/core/BinaryType.java (-4 / +4 lines)
Lines 986-993 Link Here
986
	else
986
	else
987
		buffer.append("<anonymous>"); //$NON-NLS-1$
987
		buffer.append("<anonymous>"); //$NON-NLS-1$
988
}
988
}
989
public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
989
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
990
	final String contents = getJavadocContents(monitor, defaultEncoding);
990
	final String contents = getJavadocContents(monitor);
991
	if (contents == null) return null;
991
	if (contents == null) return null;
992
	final int indexOfStartOfClassData = contents.indexOf(JavadocConstants.START_OF_CLASS_DATA);
992
	final int indexOfStartOfClassData = contents.indexOf(JavadocConstants.START_OF_CLASS_DATA);
993
	if (indexOfStartOfClassData == -1) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this));
993
	if (indexOfStartOfClassData == -1) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this));
Lines 1025-1031 Link Here
1025
	}
1025
	}
1026
	return contents.substring(indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA_LENGTH, indexOfNextSummary);
1026
	return contents.substring(indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA_LENGTH, indexOfNextSummary);
1027
}
1027
}
1028
public String getJavadocContents(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
1028
public String getJavadocContents(IProgressMonitor monitor) throws JavaModelException {
1029
	PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject());
1029
	PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject());
1030
	String cachedJavadoc = null;
1030
	String cachedJavadoc = null;
1031
	synchronized (projectInfo.javadocCache) {
1031
	synchronized (projectInfo.javadocCache) {
Lines 1064-1070 Link Here
1064
	pathBuffer.append(pack.getElementName().replace('.', '/')).append('/').append(typeQualifiedName).append(JavadocConstants.HTML_EXTENSION);
1064
	pathBuffer.append(pack.getElementName().replace('.', '/')).append('/').append(typeQualifiedName).append(JavadocConstants.HTML_EXTENSION);
1065
	
1065
	
1066
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
1066
	if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
1067
	final String contents = getURLContents(String.valueOf(pathBuffer), defaultEncoding);
1067
	final String contents = getURLContents(String.valueOf(pathBuffer));
1068
	synchronized (projectInfo.javadocCache) {
1068
	synchronized (projectInfo.javadocCache) {
1069
		projectInfo.javadocCache.put(this, contents);
1069
		projectInfo.javadocCache.put(this, contents);
1070
	}
1070
	}
(-)model/org/eclipse/jdt/internal/core/JavaElement.java (-21 / +62 lines)
Lines 38-43 Link Here
38
public abstract class JavaElement extends PlatformObject implements IJavaElement {
38
public abstract class JavaElement extends PlatformObject implements IJavaElement {
39
//	private static final QualifiedName PROJECT_JAVADOC= new QualifiedName(JavaCore.PLUGIN_ID, "project_javadoc_location"); //$NON-NLS-1$
39
//	private static final QualifiedName PROJECT_JAVADOC= new QualifiedName(JavaCore.PLUGIN_ID, "project_javadoc_location"); //$NON-NLS-1$
40
40
41
	private static final byte[] CLOSING_DOUBLE_QUOTE = new byte[] { 34 };
42
	private static final byte[] CHARSET = new byte[] {99, 104, 97, 114, 115, 101, 116, 61 };
43
	private static final byte[] CONTENT_TYPE = new byte[] { 34, 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 34 };
44
	private static final byte[] CONTENT = new byte[] { 99, 111, 110, 116, 101, 110, 116, 61, 34 };
41
	public static final char JEM_ESCAPE = '\\';
45
	public static final char JEM_ESCAPE = '\\';
42
	public static final char JEM_JAVAPROJECT = '=';
46
	public static final char JEM_JAVAPROJECT = '=';
43
	public static final char JEM_PACKAGEFRAGMENTROOT = '/';
47
	public static final char JEM_PACKAGEFRAGMENTROOT = '/';
Lines 684-728 Link Here
684
	}
688
	}
685
	
689
	
686
	/*
690
	/*
687
	 * @see IJavaElement#getAttachedJavadoc(IProgressMonitor, String)
691
	 * @see IJavaElement#getAttachedJavadoc(IProgressMonitor)
688
	 */
692
	 */
689
	public String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException {
693
	public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
690
		return null;
694
		return null;
691
	}
695
	}
696
	public String getAttachedJavadoc(IProgressMonitor monitor, String encoding) throws JavaModelException {
697
		return getAttachedJavadoc(monitor);
698
	}
699
	
700
	int getIndexOf(byte[] array, byte[] toBeFound, int start) {
701
		if (array == null || toBeFound == null)
702
			return -1;
703
		final int toBeFoundLength = toBeFound.length;
704
		final int arrayLength = array.length;
705
		if (arrayLength < toBeFoundLength)
706
			return -1;
707
		loop: for (int i = start, max = arrayLength - toBeFoundLength + 1; i < max; i++) {
708
			if (array[i] == toBeFound[0]) {
709
				for (int j = 1; j < toBeFoundLength; j++) {
710
					if (array[i + j] != toBeFound[j])
711
						continue loop;
712
				}
713
				return i;
714
			}
715
		}
716
		return -1;
717
	}	
692
	/*
718
	/*
693
	 * We don't use getContentEncoding() on the URL connection, because it might leave open streams behind.
719
	 * We don't use getContentEncoding() on the URL connection, because it might leave open streams behind.
694
	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=117890 
720
	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=117890 
695
	 * 
721
	 * 
696
	 */
722
	 */
697
	protected String getURLContents(String docUrlValue, String defaultEncoding) throws JavaModelException {
723
	protected String getURLContents(String docUrlValue) throws JavaModelException {
698
		InputStream stream = null;
724
		InputStream stream = null;
699
		try {
725
		try {
700
			String encoding = defaultEncoding;
701
			try {
702
				if (encoding == null) {
703
					encoding = this.getJavaProject().getProject().getDefaultCharset();
704
				}
705
			} catch (CoreException e) {
706
				// ignore
707
			}
708
			// long time = System.currentTimeMillis();
709
			URL docUrl = new URL(docUrlValue);
726
			URL docUrl = new URL(docUrlValue);
710
			URLConnection connection = docUrl.openConnection();
727
			URLConnection connection = docUrl.openConnection();
711
			// System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for opening connection for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$
712
			// time = System.currentTimeMillis();
713
			if ("jar".equals(docUrl.getProtocol())) { //$NON-NLS-1$
728
			if ("jar".equals(docUrl.getProtocol())) { //$NON-NLS-1$
714
				// if jar protocol is using a cache, some file descriptors are left behind and the resource cannot be deleted
729
				// if jar protocol is using a cache, some file descriptors are left behind and the resource cannot be deleted
715
				connection.setUseCaches(false);
730
				connection.setUseCaches(false);
716
			}
731
			}
732
			String encoding = connection.getContentEncoding();
717
			stream = new BufferedInputStream(connection.getInputStream());
733
			stream = new BufferedInputStream(connection.getInputStream());
718
			// System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for getting stream for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$
734
			byte[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, connection.getContentLength());
719
			// time = System.currentTimeMillis();
735
			if (encoding == null) {
720
			char[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, -1, encoding);
736
				int index = getIndexOf(contents, CONTENT_TYPE, 0);
721
			// System.out.println("Time spent " + (System.currentTimeMillis() - time) + "ms for reading stream for " + docUrlValue); //$NON-NLS-1$//$NON-NLS-2$
737
				if (index != -1) {
722
			if (contents != null) {
738
					index = getIndexOf(contents, CONTENT, index);
723
				// System.out.println("Size = " + (contents.length / 1024) + "kb");//$NON-NLS-1$//$NON-NLS-2$
739
					if (index != -1) {
724
				return String.valueOf(contents);
740
						int offset = index + CONTENT.length;
741
						int index2 = getIndexOf(contents, CLOSING_DOUBLE_QUOTE, offset);
742
						if (index2 != -1) {
743
							final int charsetIndex = getIndexOf(contents, CHARSET, offset);
744
							if (charsetIndex != -1) {
745
								int start = charsetIndex + CHARSET.length;
746
								encoding = new String(contents, start, index2 - start, "UTF-8"); //$NON-NLS-1$
747
							}
748
						}
749
					}
750
				}
751
			}
752
			try {
753
				if (encoding == null) {
754
					encoding = this.getJavaProject().getProject().getDefaultCharset();
755
				}
756
			} catch (CoreException e) {
757
				// ignore
725
			}
758
			}
759
			if (contents != null) {
760
				if (encoding != null) {
761
					return new String(contents, encoding);
762
				} else {
763
					// platform encoding is used
764
					return new String(contents);
765
				}
766
			}			
726
 		} catch (MalformedURLException e) {
767
 		} catch (MalformedURLException e) {
727
 			throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
768
 			throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
728
		} catch (FileNotFoundException e) {
769
		} catch (FileNotFoundException e) {
(-)model/org/eclipse/jdt/core/IJavaElement.java (-9 / +15 lines)
Lines 162-192 Link Here
162
	/**
162
	/**
163
	 * <p>Returns the Javadoc as an html source if this element has an attached javadoc,
163
	 * <p>Returns the Javadoc as an html source if this element has an attached javadoc,
164
	 * null otherwise.</p> 
164
	 * null otherwise.</p> 
165
	 * <p>This should be used only for binary elements. Source elements will always return null.
165
	 * <p>This should be used only for binary elements. Source elements will always return null.</p>
166
	 * The encoding used to read the javadoc is the one defined by the content type of the
166
	 * <p>The encoding used to read the javadoc is the one defined by the content type of the
167
	 * file. If none is defined, then the specified encoding will be used as the default encoding.</p>
167
	 * file. If none is defined, then the project's encoding of this java element is used. If the project's 
168
	 * encoding cannot be retrieved, then the platform encoding is used.</p>
168
	 * <p>In case of the javadoc doesn't exist for this element, null is returned.</p>
169
	 * <p>In case of the javadoc doesn't exist for this element, null is returned.</p>
169
	 * 
170
	 * 
170
	 * <p>The html is extracted from the attached javadoc and provided as is. No
171
	 * <p>The html is extracted from the attached javadoc and provided as is. No
171
	 * transformation or validation is done.</p>
172
	 * transformation or validation is done.</p>
172
	 *
173
	 *
173
	 * <p>If the default encoding is set to null, the default encoding of the project is used. If
174
	 * the default encoding of the project is not supported, then the platform encoding is used.</p>
175
	 * 
176
	 * <p>NOTE: This API is subject to change before the 3.2 release.</p>
174
	 * <p>NOTE: This API is subject to change before the 3.2 release.</p>
177
	 * 
175
	 * 
178
	 * @param monitor the given progress monitor
176
	 * @param monitor the given progress monitor
179
	 * @param defaultEncoding the given default encoding
180
	 * @exception JavaModelException if:<ul>
177
	 * @exception JavaModelException if:<ul>
181
	 *  <li>this element does not exist</li>
178
	 *  <li>this element does not exist</li>
182
	 *  <li>retrieving the attached javadoc fails (timed-out, invalid URL, ...)
179
	 *  <li>retrieving the attached javadoc fails (timed-out, invalid URL, ...)</li>
183
	 *  <li>the format of the javadoc doesn't match expected standards (different anchors,...)</li>
180
	 *  <li>the format of the javadoc doesn't match expected standards (different anchors,...)</li>
184
	 *  </ul>
181
	 *  </ul>
185
	 * @return the extracted javadoc from the attached javadoc, null if none
182
	 * @return the extracted javadoc from the attached javadoc, null if none
186
	 * @see IClasspathAttribute#JAVADOC_LOCATION_ATTRIBUTE_NAME
183
	 * @see IClasspathAttribute#JAVADOC_LOCATION_ATTRIBUTE_NAME
187
	 * @since 3.2
184
	 * @since 3.2
188
	 */
185
	 */
189
	String getAttachedJavadoc(IProgressMonitor monitor, String defaultEncoding) throws JavaModelException;	
186
	String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException;		
187
188
	/**
189
	 * @param monitor
190
	 * @param encoding
191
	 * @return the attached javadoc
192
	 * @throws JavaModelException
193
	 * @deprecated Use #{@link #getAttachedJavadoc(IProgressMonitor) getAttachedJavadoc(IProgressMonitor)} instead
194
	 */
195
	String getAttachedJavadoc(IProgressMonitor monitor, String encoding) throws JavaModelException;
190
196
191
	/**
197
	/**
192
	 * Returns the resource that corresponds directly to this element,
198
	 * Returns the resource that corresponds directly to this element,

Return to bug 122506