001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.java;
011:
012: import java.io.IOException;
013: import java.io.Reader;
014: import java.io.StringReader;
015:
016: import org.eclipse.core.runtime.IProgressMonitor;
017:
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.IMember;
020: import org.eclipse.jdt.core.JavaModelException;
021:
022: import org.eclipse.jdt.ui.JavadocContentAccess;
023:
024: import org.eclipse.jdt.internal.ui.JavaPlugin;
025: import org.eclipse.jdt.internal.ui.text.javadoc.JavaDoc2HTMLTextReader;
026:
027: public class ProposalInfo {
028:
029: private boolean fJavadocResolved = false;
030: private String fJavadoc = null;
031:
032: protected IJavaElement fElement;
033:
034: public ProposalInfo(IMember member) {
035: fElement = member;
036: }
037:
038: protected ProposalInfo() {
039: fElement = null;
040: }
041:
042: public IJavaElement getJavaElement() throws JavaModelException {
043: return fElement;
044: }
045:
046: /**
047: * Gets the text for this proposal info formatted as HTML, or
048: * <code>null</code> if no text is available.
049: *
050: * @param monitor a progress monitor
051: * @return the additional info text
052: */
053: public final String getInfo(IProgressMonitor monitor) {
054: if (!fJavadocResolved) {
055: fJavadocResolved = true;
056: fJavadoc = computeInfo(monitor);
057: }
058: return fJavadoc;
059: }
060:
061: /**
062: * Gets the text for this proposal info formatted as HTML, or
063: * <code>null</code> if no text is available.
064: *
065: * @param monitor a progress monitor
066: * @return the additional info text
067: */
068: private String computeInfo(IProgressMonitor monitor) {
069: try {
070: final IJavaElement javaElement = getJavaElement();
071: if (javaElement instanceof IMember) {
072: IMember member = (IMember) javaElement;
073: return extractJavadoc(member, monitor);
074: }
075: } catch (JavaModelException e) {
076: JavaPlugin.log(e);
077: } catch (IOException e) {
078: JavaPlugin.log(e);
079: }
080: return null;
081: }
082:
083: /**
084: * Extracts the javadoc for the given <code>IMember</code> and returns it
085: * as HTML.
086: *
087: * @param member the member to get the documentation for
088: * @param monitor a progress monitor
089: * @return the javadoc for <code>member</code> or <code>null</code> if
090: * it is not available
091: * @throws JavaModelException if accessing the javadoc fails
092: * @throws IOException if reading the javadoc fails
093: */
094: private String extractJavadoc(IMember member,
095: IProgressMonitor monitor) throws JavaModelException,
096: IOException {
097: if (member != null) {
098: Reader reader = getHTMLContentReader(member, monitor);
099: if (reader != null)
100: return getString(reader);
101: }
102: return null;
103: }
104:
105: private Reader getHTMLContentReader(IMember member,
106: IProgressMonitor monitor) throws JavaModelException {
107: Reader contentReader = JavadocContentAccess.getContentReader(
108: member, true);
109: if (contentReader != null)
110: return new JavaDoc2HTMLTextReader(contentReader);
111:
112: if (true && member.getOpenable().getBuffer() == null) { // only if no source available
113: String s = member.getAttachedJavadoc(monitor);
114: if (s != null)
115: return new StringReader(s);
116: }
117: return null;
118: }
119:
120: /**
121: * Gets the reader content as a String
122: *
123: * @param reader the reader
124: * @return the reader content as string
125: */
126: private static String getString(Reader reader) {
127: StringBuffer buf = new StringBuffer();
128: char[] buffer = new char[1024];
129: int count;
130: try {
131: while ((count = reader.read(buffer)) != -1)
132: buf.append(buffer, 0, count);
133: } catch (IOException e) {
134: return null;
135: }
136: return buf.toString();
137: }
138: }
|