|
Added
Link Here
|
| 1 |
package org.eclipse.php.internal.ui.documentation; |
| 2 |
|
| 3 |
import java.text.MessageFormat; |
| 4 |
import java.util.*; |
| 5 |
|
| 6 |
import org.eclipse.dltk.ast.references.SimpleReference; |
| 7 |
import org.eclipse.dltk.ast.references.TypeReference; |
| 8 |
import org.eclipse.dltk.ast.references.VariableReference; |
| 9 |
import org.eclipse.dltk.core.*; |
| 10 |
import org.eclipse.dltk.internal.core.util.MethodOverrideTester; |
| 11 |
import org.eclipse.dltk.ui.ScriptElementLabels; |
| 12 |
import org.eclipse.php.core.compiler.PHPFlags; |
| 13 |
import org.eclipse.php.internal.core.ast.nodes.Identifier; |
| 14 |
import org.eclipse.php.internal.core.compiler.ast.nodes.PHPDocBlock; |
| 15 |
import org.eclipse.php.internal.core.compiler.ast.nodes.PHPDocTag; |
| 16 |
import org.eclipse.php.internal.core.typeinference.PHPModelUtils; |
| 17 |
import org.eclipse.php.internal.ui.PHPUiPlugin; |
| 18 |
import org.eclipse.php.internal.ui.corext.util.SuperTypeHierarchyCache; |
| 19 |
|
| 20 |
/** |
| 21 |
* Helper to get the content of a Javadoc comment as HTML. |
| 22 |
* |
| 23 |
* <p> |
| 24 |
* <strong>This is work in progress. Parts of this will later become API through |
| 25 |
* {@link JavadocContentAccess}</strong> |
| 26 |
* </p> |
| 27 |
* |
| 28 |
* @since 3.4 |
| 29 |
*/ |
| 30 |
@SuppressWarnings({ "restriction", "unchecked", "rawtypes" }) |
| 31 |
public class PHPDocumentationContentAccess { |
| 32 |
|
| 33 |
private static final String BLOCK_TAG_START = "<dl>"; //$NON-NLS-1$ |
| 34 |
private static final String BLOCK_TAG_END = "</dl>"; //$NON-NLS-1$ |
| 35 |
|
| 36 |
private static final String BlOCK_TAG_ENTRY_START = "<dd>"; //$NON-NLS-1$ |
| 37 |
private static final String BlOCK_TAG_ENTRY_END = "</dd>"; //$NON-NLS-1$ |
| 38 |
|
| 39 |
private static final String PARAM_NAME_START = "<b>"; //$NON-NLS-1$ |
| 40 |
private static final String PARAM_NAME_END = "</b> "; //$NON-NLS-1$ |
| 41 |
private static final int PARAMETER_TYPE_TYPE = 1; |
| 42 |
private static final int PARAMETER_NAME_TYPE = 2; |
| 43 |
private static final int PARAMETER_DESCRIPTION_TYPE = 3; |
| 44 |
|
| 45 |
/** |
| 46 |
* Implements the "Algorithm for Inheriting Method Comments" as specified |
| 47 |
* for <a href= |
| 48 |
* "http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/javadoc.html#inheritingcomments" |
| 49 |
* >1.4.2</a>, <a href= |
| 50 |
* "http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javadoc.html#inheritingcomments" |
| 51 |
* >1.5</a>, and <a href= |
| 52 |
* "http://java.sun.com/javase/6/docs/technotes/tools/windows/javadoc.html#inheritingcomments" |
| 53 |
* >1.6</a>. |
| 54 |
* |
| 55 |
* <p> |
| 56 |
* Unfortunately, the implementation is broken in Javadoc implementations |
| 57 |
* since 1.5, see <a |
| 58 |
* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6376959">Sun's |
| 59 |
* bug</a>. |
| 60 |
* </p> |
| 61 |
* |
| 62 |
* <p> |
| 63 |
* We adhere to the spec. |
| 64 |
* </p> |
| 65 |
*/ |
| 66 |
private static abstract class InheritDocVisitor { |
| 67 |
public static final Object STOP_BRANCH = new Object() { |
| 68 |
public String toString() { |
| 69 |
return "STOP_BRANCH";} //$NON-NLS-1$ |
| 70 |
}; |
| 71 |
public static final Object CONTINUE = new Object() { |
| 72 |
public String toString() { |
| 73 |
return "CONTINUE";} //$NON-NLS-1$ |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
* Visits a type and decides how the visitor should proceed. |
| 78 |
* |
| 79 |
* @param currType |
| 80 |
* the current type |
| 81 |
* @return <ul> |
| 82 |
* <li>{@link #STOP_BRANCH} to indicate that no Javadoc has been |
| 83 |
* found and visiting super types should stop here</li> |
| 84 |
* <li>{@link #CONTINUE} to indicate that no Javadoc has been |
| 85 |
* found and visiting super types should continue</li> |
| 86 |
* <li>an {@link Object} or <code>null</code>, to indicate that |
| 87 |
* visiting should be cancelled immediately. The returned value |
| 88 |
* is the result of |
| 89 |
* {@link #visitInheritDoc(IType, ITypeHierarchy)}</li> |
| 90 |
* </ul> |
| 91 |
* @throws ModelException |
| 92 |
* unexpected problem |
| 93 |
* @see #visitInheritDoc(IType, ITypeHierarchy) |
| 94 |
*/ |
| 95 |
public abstract Object visit(IType currType) throws ModelException; |
| 96 |
|
| 97 |
/** |
| 98 |
* Visits the super types of the given <code>currentType</code>. |
| 99 |
* |
| 100 |
* @param currentType |
| 101 |
* the starting type |
| 102 |
* @param typeHierarchy |
| 103 |
* a super type hierarchy that contains |
| 104 |
* <code>currentType</code> |
| 105 |
* @return the result from a call to {@link #visit(IType)}, or |
| 106 |
* <code>null</code> if none of the calls returned a result |
| 107 |
* @throws ModelException |
| 108 |
* unexpected problem |
| 109 |
*/ |
| 110 |
public Object visitInheritDoc(IType currentType, |
| 111 |
ITypeHierarchy typeHierarchy) throws ModelException { |
| 112 |
ArrayList visited = new ArrayList(); |
| 113 |
visited.add(currentType); |
| 114 |
// Object result = visitInheritDocInterfaces(visited, currentType, |
| 115 |
// typeHierarchy); |
| 116 |
// if (result != InheritDocVisitor.CONTINUE) |
| 117 |
// return result; |
| 118 |
Object result; |
| 119 |
IType[] superClasses = typeHierarchy.getSuperclass(currentType); |
| 120 |
for (IType superClass : superClasses) { |
| 121 |
while (superClass != null && !visited.contains(superClass)) { |
| 122 |
result = visit(superClass); |
| 123 |
if (result == InheritDocVisitor.STOP_BRANCH) { |
| 124 |
return null; |
| 125 |
} else if (result == InheritDocVisitor.CONTINUE) { |
| 126 |
visited.add(superClass); |
| 127 |
result = visitInheritDocInterfaces(visited, superClass, |
| 128 |
typeHierarchy); |
| 129 |
if (result != InheritDocVisitor.CONTINUE) |
| 130 |
return result; |
| 131 |
else |
| 132 |
superClasses = typeHierarchy |
| 133 |
.getSuperclass(superClass); |
| 134 |
} else { |
| 135 |
return result; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Visits the super interfaces of the given type in the given hierarchy, |
| 144 |
* thereby skipping already visited types. |
| 145 |
* |
| 146 |
* @param visited |
| 147 |
* set of visited types |
| 148 |
* @param currentType |
| 149 |
* type whose super interfaces should be visited |
| 150 |
* @param typeHierarchy |
| 151 |
* type hierarchy (must include <code>currentType</code>) |
| 152 |
* @return the result, or {@link #CONTINUE} if no result has been found |
| 153 |
* @throws ModelException |
| 154 |
* unexpected problem |
| 155 |
*/ |
| 156 |
private Object visitInheritDocInterfaces(ArrayList visited, |
| 157 |
IType currentType, ITypeHierarchy typeHierarchy) |
| 158 |
throws ModelException { |
| 159 |
ArrayList toVisitChildren = new ArrayList(); |
| 160 |
IType[] superInterfaces = typeHierarchy.getSuperclass(currentType); |
| 161 |
for (int i = 0; i < superInterfaces.length; i++) { |
| 162 |
IType superInterface = superInterfaces[i]; |
| 163 |
if (visited.contains(superInterface)) |
| 164 |
continue; |
| 165 |
visited.add(superInterface); |
| 166 |
Object result = visit(superInterface); |
| 167 |
if (result == InheritDocVisitor.STOP_BRANCH) { |
| 168 |
// skip |
| 169 |
} else if (result == InheritDocVisitor.CONTINUE) { |
| 170 |
toVisitChildren.add(superInterface); |
| 171 |
} else { |
| 172 |
return result; |
| 173 |
} |
| 174 |
} |
| 175 |
for (Iterator iter = toVisitChildren.iterator(); iter.hasNext();) { |
| 176 |
IType child = (IType) iter.next(); |
| 177 |
Object result = visitInheritDocInterfaces(visited, child, |
| 178 |
typeHierarchy); |
| 179 |
if (result != InheritDocVisitor.CONTINUE) |
| 180 |
return result; |
| 181 |
} |
| 182 |
return InheritDocVisitor.CONTINUE; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
private static class JavadocLookup { |
| 187 |
private static final JavadocLookup NONE = new JavadocLookup(null) { |
| 188 |
public CharSequence getInheritedMainDescription(IMethod method) { |
| 189 |
return null; |
| 190 |
} |
| 191 |
|
| 192 |
public CharSequence getInheritedParamDescription(IMethod method, |
| 193 |
int i) { |
| 194 |
return null; |
| 195 |
} |
| 196 |
|
| 197 |
public CharSequence getInheritedReturnDescription(IMethod method) { |
| 198 |
return null; |
| 199 |
} |
| 200 |
|
| 201 |
public CharSequence getInheritedExceptionDescription( |
| 202 |
IMethod method, String name) { |
| 203 |
return null; |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
private static interface DescriptionGetter { |
| 208 |
/** |
| 209 |
* Returns a Javadoc tag description or <code>null</code>. |
| 210 |
* |
| 211 |
* @param contentAccess |
| 212 |
* the content access |
| 213 |
* @return the description, or <code>null</code> if none |
| 214 |
* @throws ModelException |
| 215 |
* unexpected problem |
| 216 |
*/ |
| 217 |
CharSequence getDescription( |
| 218 |
PHPDocumentationContentAccess contentAccess) |
| 219 |
throws ModelException; |
| 220 |
} |
| 221 |
|
| 222 |
private final IType fStartingType; |
| 223 |
private final HashMap fContentAccesses; |
| 224 |
|
| 225 |
private ITypeHierarchy fTypeHierarchy; |
| 226 |
private MethodOverrideTester fOverrideTester; |
| 227 |
|
| 228 |
private JavadocLookup(IType startingType) { |
| 229 |
fStartingType = startingType; |
| 230 |
fContentAccesses = new HashMap(); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* For the given method, returns the main description from an overridden |
| 235 |
* method. |
| 236 |
* |
| 237 |
* @param method |
| 238 |
* a method |
| 239 |
* @return the description that replaces the |
| 240 |
* <code>{@inheritDoc}</code> tag, or <code>null</code> if |
| 241 |
* none could be found |
| 242 |
*/ |
| 243 |
public CharSequence getInheritedMainDescription(IMethod method) { |
| 244 |
return getInheritedDescription(method, new DescriptionGetter() { |
| 245 |
public CharSequence getDescription( |
| 246 |
PHPDocumentationContentAccess contentAccess) { |
| 247 |
return contentAccess.getMainDescription(); |
| 248 |
} |
| 249 |
}); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* For the given method, returns the @param tag description for the |
| 254 |
* given parameter from an overridden method. |
| 255 |
* |
| 256 |
* @param method |
| 257 |
* a method |
| 258 |
* @param paramIndex |
| 259 |
* the index of the parameter |
| 260 |
* @return the description that replaces the |
| 261 |
* <code>{@inheritDoc}</code> tag, or <code>null</code> if |
| 262 |
* none could be found |
| 263 |
*/ |
| 264 |
public CharSequence getInheritedParamDescription(IMethod method, |
| 265 |
final int paramIndex) { |
| 266 |
return getInheritedDescription(method, new DescriptionGetter() { |
| 267 |
public CharSequence getDescription( |
| 268 |
PHPDocumentationContentAccess contentAccess) |
| 269 |
throws ModelException { |
| 270 |
return contentAccess |
| 271 |
.getInheritedParamDescription(paramIndex); |
| 272 |
} |
| 273 |
}); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* For the given method, returns the @param tag description for the |
| 278 |
* given parameter from an overridden method. |
| 279 |
* |
| 280 |
* @param method |
| 281 |
* a method |
| 282 |
* @param paramIndex |
| 283 |
* the index of the parameter |
| 284 |
* @return the description that replaces the |
| 285 |
* <code>{@inheritDoc}</code> tag, or <code>null</code> if |
| 286 |
* none could be found |
| 287 |
*/ |
| 288 |
public CharSequence getInheritedParamType(IMethod method, |
| 289 |
final int paramIndex) { |
| 290 |
return getInheritedDescription(method, new DescriptionGetter() { |
| 291 |
public CharSequence getDescription( |
| 292 |
PHPDocumentationContentAccess contentAccess) |
| 293 |
throws ModelException { |
| 294 |
return contentAccess.getInheritedParamType(paramIndex); |
| 295 |
} |
| 296 |
}); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* For the given method, returns the @return tag description from an |
| 301 |
* overridden method. |
| 302 |
* |
| 303 |
* @param method |
| 304 |
* a method |
| 305 |
* @return the description that replaces the |
| 306 |
* <code>{@inheritDoc}</code> tag, or <code>null</code> if |
| 307 |
* none could be found |
| 308 |
*/ |
| 309 |
public CharSequence getInheritedReturnDescription(IMethod method) { |
| 310 |
return getInheritedDescription(method, new DescriptionGetter() { |
| 311 |
public CharSequence getDescription( |
| 312 |
PHPDocumentationContentAccess contentAccess) { |
| 313 |
return contentAccess.getReturnDescription(); |
| 314 |
} |
| 315 |
}); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* For the given method, returns the @throws/@exception tag description |
| 320 |
* for the given exception from an overridden method. |
| 321 |
* |
| 322 |
* @param method |
| 323 |
* a method |
| 324 |
* @param simpleName |
| 325 |
* the simple name of an exception |
| 326 |
* @return the description that replaces the |
| 327 |
* <code>{@inheritDoc}</code> tag, or <code>null</code> if |
| 328 |
* none could be found |
| 329 |
*/ |
| 330 |
public CharSequence getInheritedExceptionDescription(IMethod method, |
| 331 |
final String simpleName) { |
| 332 |
return getInheritedDescription(method, new DescriptionGetter() { |
| 333 |
public CharSequence getDescription( |
| 334 |
PHPDocumentationContentAccess contentAccess) { |
| 335 |
return contentAccess.getExceptionDescription(simpleName); |
| 336 |
} |
| 337 |
}); |
| 338 |
} |
| 339 |
|
| 340 |
private CharSequence getInheritedDescription(final IMethod method, |
| 341 |
final DescriptionGetter descriptionGetter) { |
| 342 |
try { |
| 343 |
return (CharSequence) new InheritDocVisitor() { |
| 344 |
public Object visit(IType currType) throws ModelException { |
| 345 |
IMethod overridden = getOverrideTester() |
| 346 |
.findOverriddenMethodInType(currType, method); |
| 347 |
if (overridden == null) |
| 348 |
return InheritDocVisitor.CONTINUE; |
| 349 |
|
| 350 |
PHPDocumentationContentAccess contentAccess = getJavadocContentAccess(overridden); |
| 351 |
if (contentAccess == null) { |
| 352 |
// if (overridden.getOpenable().getBuffer() == null) |
| 353 |
// { |
| 354 |
// return InheritDocVisitor.CONTINUE; |
| 355 |
// } else { |
| 356 |
// return InheritDocVisitor.CONTINUE; |
| 357 |
// } |
| 358 |
return InheritDocVisitor.CONTINUE; |
| 359 |
} |
| 360 |
|
| 361 |
CharSequence overriddenDescription = descriptionGetter |
| 362 |
.getDescription(contentAccess); |
| 363 |
if (overriddenDescription != null) |
| 364 |
return overriddenDescription; |
| 365 |
else |
| 366 |
return InheritDocVisitor.CONTINUE; |
| 367 |
} |
| 368 |
}.visitInheritDoc(method.getDeclaringType(), getTypeHierarchy()); |
| 369 |
} catch (ModelException e) { |
| 370 |
PHPUiPlugin.log(e); |
| 371 |
} |
| 372 |
return null; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* @param method |
| 377 |
* the method |
| 378 |
* @return the Javadoc content access for the given method, or |
| 379 |
* <code>null</code> if no Javadoc could be found in source |
| 380 |
* @throws ModelException |
| 381 |
* unexpected problem |
| 382 |
*/ |
| 383 |
private PHPDocumentationContentAccess getJavadocContentAccess( |
| 384 |
IMethod method) throws ModelException { |
| 385 |
Object cached = fContentAccesses.get(method); |
| 386 |
if (cached != null) |
| 387 |
return (PHPDocumentationContentAccess) cached; |
| 388 |
if (fContentAccesses.containsKey(method)) |
| 389 |
return null; |
| 390 |
|
| 391 |
// IBuffer buf = method.getOpenable().getBuffer(); |
| 392 |
// if (buf == null) { // no source attachment found |
| 393 |
// fContentAccesses.put(method, null); |
| 394 |
// return null; |
| 395 |
// } |
| 396 |
|
| 397 |
PHPDocBlock javadoc = PHPModelUtils.getDocBlock(method); |
| 398 |
if (javadoc == null) { |
| 399 |
fContentAccesses.put(method, null); |
| 400 |
return null; |
| 401 |
} |
| 402 |
|
| 403 |
PHPDocumentationContentAccess contentAccess = new PHPDocumentationContentAccess( |
| 404 |
method, javadoc, this); |
| 405 |
fContentAccesses.put(method, contentAccess); |
| 406 |
return contentAccess; |
| 407 |
} |
| 408 |
|
| 409 |
private ITypeHierarchy getTypeHierarchy() throws ModelException { |
| 410 |
if (fTypeHierarchy == null) |
| 411 |
fTypeHierarchy = SuperTypeHierarchyCache |
| 412 |
.getTypeHierarchy(fStartingType); |
| 413 |
return fTypeHierarchy; |
| 414 |
} |
| 415 |
|
| 416 |
private MethodOverrideTester getOverrideTester() throws ModelException { |
| 417 |
if (fOverrideTester == null) |
| 418 |
fOverrideTester = SuperTypeHierarchyCache |
| 419 |
.getMethodOverrideTester(fStartingType); |
| 420 |
return fOverrideTester; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
private final IMember fMember; |
| 425 |
/** |
| 426 |
* The method, or <code>null</code> if {@link #fMember} is not a method |
| 427 |
* where {@inheritDoc} could work. |
| 428 |
*/ |
| 429 |
private final IMethod fMethod; |
| 430 |
private final PHPDocBlock fJavadoc; |
| 431 |
private final JavadocLookup fJavadocLookup; |
| 432 |
|
| 433 |
private StringBuffer fBuf; |
| 434 |
private StringBuffer fMainDescription; |
| 435 |
private StringBuffer fReturnDescription; |
| 436 |
private StringBuffer[] fParamDescriptions; |
| 437 |
private StringBuffer[] fParamTypes; |
| 438 |
private HashMap<String, StringBuffer> fExceptionDescriptions; |
| 439 |
|
| 440 |
private PHPDocumentationContentAccess(IMethod method, PHPDocBlock javadoc, |
| 441 |
JavadocLookup lookup) { |
| 442 |
fMember = method; |
| 443 |
fMethod = method; |
| 444 |
fJavadoc = javadoc; |
| 445 |
fJavadocLookup = lookup; |
| 446 |
} |
| 447 |
|
| 448 |
private PHPDocumentationContentAccess(IMember member, PHPDocBlock javadoc) { |
| 449 |
fMember = member; |
| 450 |
fMethod = null; |
| 451 |
fJavadoc = javadoc; |
| 452 |
fJavadocLookup = JavadocLookup.NONE; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Gets an IMember's Javadoc comment content from the source or Javadoc |
| 457 |
* attachment and renders the tags and links in HTML. Returns |
| 458 |
* <code>null</code> if the member does not contain a Javadoc comment or if |
| 459 |
* no source is available. |
| 460 |
* |
| 461 |
* @param member |
| 462 |
* the member to get the Javadoc of |
| 463 |
* @param useAttachedJavadoc |
| 464 |
* if <code>true</code> Javadoc will be extracted from attached |
| 465 |
* Javadoc if there's no source |
| 466 |
* @return the Javadoc comment content in HTML or <code>null</code> if the |
| 467 |
* member does not have a Javadoc comment or if no source is |
| 468 |
* available |
| 469 |
* @throws ModelException |
| 470 |
* is thrown when the element's Javadoc can not be accessed |
| 471 |
*/ |
| 472 |
public static String getHTMLContent(IMember member) throws ModelException { |
| 473 |
return getHTMLContentFromSource(member); |
| 474 |
} |
| 475 |
|
| 476 |
private static StringBuffer createSuperMethodReferences(final IMethod method) |
| 477 |
throws ModelException { |
| 478 |
IType type = method.getDeclaringType(); |
| 479 |
ITypeHierarchy hierarchy = SuperTypeHierarchyCache |
| 480 |
.getTypeHierarchy(type); |
| 481 |
final MethodOverrideTester tester = SuperTypeHierarchyCache |
| 482 |
.getMethodOverrideTester(type); |
| 483 |
|
| 484 |
final ArrayList<IMethod> superInterfaceMethods = new ArrayList<IMethod>(); |
| 485 |
final IMethod[] superClassMethod = { null }; |
| 486 |
new InheritDocVisitor() { |
| 487 |
public Object visit(IType currType) throws ModelException { |
| 488 |
IMethod overridden = tester.findOverriddenMethodInType( |
| 489 |
currType, method); |
| 490 |
if (overridden == null) |
| 491 |
return InheritDocVisitor.CONTINUE; |
| 492 |
|
| 493 |
if (PHPFlags.isInterface(currType.getFlags())) |
| 494 |
superInterfaceMethods.add(overridden); |
| 495 |
else |
| 496 |
superClassMethod[0] = overridden; |
| 497 |
|
| 498 |
return STOP_BRANCH; |
| 499 |
} |
| 500 |
}.visitInheritDoc(type, hierarchy); |
| 501 |
|
| 502 |
boolean hasSuperInterfaceMethods = superInterfaceMethods.size() != 0; |
| 503 |
if (!hasSuperInterfaceMethods && superClassMethod[0] == null) |
| 504 |
return null; |
| 505 |
|
| 506 |
StringBuffer buf = new StringBuffer(); |
| 507 |
buf.append("<div>"); //$NON-NLS-1$ |
| 508 |
if (hasSuperInterfaceMethods) { |
| 509 |
buf.append("<b>"); //$NON-NLS-1$ |
| 510 |
buf.append(PHPDocumentationMessages.JavaDoc2HTMLTextReader_specified_by_section); |
| 511 |
buf.append("</b> "); //$NON-NLS-1$ |
| 512 |
for (Iterator<IMethod> iter = superInterfaceMethods.iterator(); iter |
| 513 |
.hasNext();) { |
| 514 |
IMethod overridden = (IMethod) iter.next(); |
| 515 |
buf.append(createMethodInTypeLinks(overridden)); |
| 516 |
if (iter.hasNext()) |
| 517 |
buf.append(ScriptElementLabels.COMMA_STRING); |
| 518 |
} |
| 519 |
} |
| 520 |
if (superClassMethod[0] != null) { |
| 521 |
if (hasSuperInterfaceMethods) |
| 522 |
buf.append(ScriptElementLabels.COMMA_STRING); |
| 523 |
buf.append("<b>"); //$NON-NLS-1$ |
| 524 |
buf.append(PHPDocumentationMessages.JavaDoc2HTMLTextReader_overrides_section); |
| 525 |
buf.append("</b> "); //$NON-NLS-1$ |
| 526 |
buf.append(createMethodInTypeLinks(superClassMethod[0])); |
| 527 |
} |
| 528 |
buf.append("</div>"); //$NON-NLS-1$ |
| 529 |
return buf; |
| 530 |
} |
| 531 |
|
| 532 |
private static String createMethodInTypeLinks(IMethod overridden) { |
| 533 |
CharSequence methodLink = createSimpleMemberLink(overridden); |
| 534 |
CharSequence typeLink = createSimpleMemberLink(overridden |
| 535 |
.getDeclaringType()); |
| 536 |
String methodInType = MessageFormat.format( |
| 537 |
PHPDocumentationMessages.JavaDoc2HTMLTextReader_method_in_type, |
| 538 |
new Object[] { methodLink, typeLink }); |
| 539 |
return methodInType; |
| 540 |
} |
| 541 |
|
| 542 |
private static CharSequence createSimpleMemberLink(IMember member) { |
| 543 |
StringBuffer buf = new StringBuffer(); |
| 544 |
buf.append("<a href='"); //$NON-NLS-1$ |
| 545 |
// try { |
| 546 |
// String uri= |
| 547 |
// JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, member); |
| 548 |
// buf.append(uri); |
| 549 |
// } catch (URISyntaxException e) { |
| 550 |
// JavaPlugin.log(e); |
| 551 |
// } |
| 552 |
buf.append("'>"); //$NON-NLS-1$ |
| 553 |
ScriptElementLabels.getDefault().getElementLabel(member, 0, buf); |
| 554 |
buf.append("</a>"); //$NON-NLS-1$ |
| 555 |
return buf; |
| 556 |
} |
| 557 |
|
| 558 |
private static String getHTMLContentFromSource(IMember member) |
| 559 |
throws ModelException { |
| 560 |
return javadoc2HTML(member); |
| 561 |
} |
| 562 |
|
| 563 |
private static PHPDocBlock getJavadocNode(IMember member) { |
| 564 |
if (member instanceof IType) { |
| 565 |
return PHPModelUtils.getDocBlock((IType) member); |
| 566 |
} |
| 567 |
if (member instanceof IMethod) { |
| 568 |
return PHPModelUtils.getDocBlock((IMethod) member); |
| 569 |
} |
| 570 |
if (member instanceof IField) { |
| 571 |
return PHPModelUtils.getDocBlock((IField) member); |
| 572 |
} |
| 573 |
return null; |
| 574 |
} |
| 575 |
|
| 576 |
private static String javadoc2HTML(IMember member) { |
| 577 |
PHPDocBlock javadoc = getJavadocNode(member); |
| 578 |
|
| 579 |
if (javadoc == null) { |
| 580 |
javadoc = new PHPDocBlock(0, 0, null, new PHPDocTag[0]); |
| 581 |
} |
| 582 |
if (canInheritJavadoc(member)) { |
| 583 |
IMethod method = (IMethod) member; |
| 584 |
IType declaringType = method.getDeclaringType(); |
| 585 |
JavadocLookup lookup; |
| 586 |
if (declaringType == null) { |
| 587 |
lookup = JavadocLookup.NONE; |
| 588 |
} else { |
| 589 |
lookup = new JavadocLookup(method.getDeclaringType()); |
| 590 |
} |
| 591 |
return new PHPDocumentationContentAccess(method, javadoc, lookup) |
| 592 |
.toHTML(); |
| 593 |
} |
| 594 |
return new PHPDocumentationContentAccess(member, javadoc).toHTML(); |
| 595 |
} |
| 596 |
|
| 597 |
private static boolean canInheritJavadoc(IMember member) { |
| 598 |
if (member instanceof IMethod && member.getScriptProject().exists()) { |
| 599 |
/* |
| 600 |
* Exists test catches ExternalJavaProject, in which case no |
| 601 |
* hierarchy can be built. |
| 602 |
*/ |
| 603 |
try { |
| 604 |
return !((IMethod) member).isConstructor(); |
| 605 |
} catch (ModelException e) { |
| 606 |
PHPUiPlugin.log(e); |
| 607 |
} |
| 608 |
} |
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
private String toHTML() { |
| 613 |
fBuf = new StringBuffer(); |
| 614 |
|
| 615 |
if (appendBuiltinDoc(fMember, fBuf)) { |
| 616 |
return fBuf.toString(); |
| 617 |
} |
| 618 |
|
| 619 |
// After first loop, non-null entries in the following two lists are |
| 620 |
// missing and need to be inherited: |
| 621 |
List<String> parameterNames = initParameterNames(); |
| 622 |
List<String> exceptionNames = new ArrayList<String>(); |
| 623 |
|
| 624 |
PHPDocTag deprecatedTag = null; |
| 625 |
PHPDocTag returnTag = null; |
| 626 |
List<PHPDocTag> parameters = new ArrayList<PHPDocTag>(); |
| 627 |
List<PHPDocTag> exceptions = new ArrayList<PHPDocTag>(); |
| 628 |
List<PHPDocTag> versions = new ArrayList<PHPDocTag>(); |
| 629 |
List<PHPDocTag> authors = new ArrayList<PHPDocTag>(); |
| 630 |
List<PHPDocTag> sees = new ArrayList<PHPDocTag>(); |
| 631 |
List<PHPDocTag> since = new ArrayList<PHPDocTag>(); |
| 632 |
List<PHPDocTag> rest = new ArrayList<PHPDocTag>(); |
| 633 |
String shortDescription = fJavadoc.getShortDescription(); |
| 634 |
PHPDocTag[] tags = fJavadoc.getTags(); |
| 635 |
for (PHPDocTag tag : tags) { |
| 636 |
if (PHPDocTag.PARAM == tag.getTagKind()) { |
| 637 |
parameters.add(tag); |
| 638 |
SimpleReference[] fragments = tag.getReferences(); |
| 639 |
if (fragments.length == 0) { |
| 640 |
if (parameterNames.size() > parameters.indexOf(tag)) |
| 641 |
parameterNames.set(parameters.indexOf(tag), null); |
| 642 |
} |
| 643 |
for (SimpleReference reference : fragments) { |
| 644 |
String name = reference.getName(); |
| 645 |
if (reference instanceof TypeReference) { |
| 646 |
// parameterTypes.add(name); |
| 647 |
} else if (reference instanceof VariableReference) { |
| 648 |
int paramIndex = parameterNames.indexOf(name); |
| 649 |
if (paramIndex != -1) { |
| 650 |
parameterNames.set(paramIndex, null); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} else if (PHPDocTag.RETURN == tag.getTagKind()) { |
| 655 |
if (returnTag == null) |
| 656 |
returnTag = tag; // the Javadoc tool only shows the first |
| 657 |
// return tag |
| 658 |
|
| 659 |
} else if (PHPDocTag.THROWS == tag.getTagKind()) { |
| 660 |
exceptions.add(tag); |
| 661 |
SimpleReference[] fragments = tag.getReferences(); |
| 662 |
if (fragments.length > 0) { |
| 663 |
Object first = fragments[0]; |
| 664 |
if (first instanceof TypeReference) { |
| 665 |
exceptionNames.add(((TypeReference) first).getName()); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
} else if (PHPDocTag.SINCE == tag.getTagKind()) { |
| 670 |
since.add(tag); |
| 671 |
} else if (PHPDocTag.VERSION == tag.getTagKind()) { |
| 672 |
versions.add(tag); |
| 673 |
} else if (PHPDocTag.AUTHOR == tag.getTagKind()) { |
| 674 |
authors.add(tag); |
| 675 |
} else if (PHPDocTag.SEE == tag.getTagKind()) { |
| 676 |
sees.add(tag); |
| 677 |
} else if (PHPDocTag.DEPRECATED == tag.getTagKind()) { |
| 678 |
if (deprecatedTag == null) |
| 679 |
deprecatedTag = tag; // the Javadoc tool only shows the |
| 680 |
// first deprecated tag |
| 681 |
} else { |
| 682 |
rest.add(tag); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if (deprecatedTag != null) |
| 687 |
handleDeprecatedTag(deprecatedTag); |
| 688 |
if (shortDescription != null && shortDescription.length() > 0) |
| 689 |
fBuf.append(shortDescription); |
| 690 |
else if (fMethod != null) { |
| 691 |
CharSequence inherited = fJavadocLookup |
| 692 |
.getInheritedMainDescription(fMethod); |
| 693 |
handleInherited(inherited); |
| 694 |
} |
| 695 |
|
| 696 |
CharSequence[] parameterDescriptions = new CharSequence[parameterNames |
| 697 |
.size()]; |
| 698 |
CharSequence[] parameterTypes = new CharSequence[parameterNames.size()]; |
| 699 |
boolean hasInheritedParameters = inheritParameterDescriptions( |
| 700 |
parameterNames, parameterDescriptions, parameterTypes); |
| 701 |
boolean hasParameters = parameters.size() > 0 || hasInheritedParameters; |
| 702 |
|
| 703 |
CharSequence returnDescription = null; |
| 704 |
if (returnTag == null) |
| 705 |
returnDescription = fJavadocLookup |
| 706 |
.getInheritedReturnDescription(fMethod); |
| 707 |
boolean hasReturnTag = returnTag != null || returnDescription != null; |
| 708 |
|
| 709 |
CharSequence[] exceptionDescriptions = new CharSequence[exceptionNames |
| 710 |
.size()]; |
| 711 |
boolean hasInheritedExceptions = inheritExceptionDescriptions( |
| 712 |
exceptionNames, exceptionDescriptions); |
| 713 |
boolean hasExceptions = exceptions.size() > 0 || hasInheritedExceptions; |
| 714 |
|
| 715 |
if (hasParameters |
| 716 |
|| hasReturnTag |
| 717 |
|| hasExceptions |
| 718 |
|| versions.size() > 0 |
| 719 |
|| authors.size() > 0 |
| 720 |
|| since.size() > 0 |
| 721 |
|| sees.size() > 0 |
| 722 |
|| rest.size() > 0 |
| 723 |
|| (fBuf.length() > 0 && (parameterDescriptions.length > 0 || exceptionDescriptions.length > 0))) { |
| 724 |
handleSuperMethodReferences(); |
| 725 |
fBuf.append(BLOCK_TAG_START); |
| 726 |
handleParameterTags(parameters, parameterNames, parameterTypes, |
| 727 |
parameterDescriptions); |
| 728 |
handleReturnTag(returnTag, returnDescription); |
| 729 |
handleExceptionTags(exceptions, exceptionNames, |
| 730 |
exceptionDescriptions); |
| 731 |
handleBlockTags( |
| 732 |
PHPDocumentationMessages.JavaDoc2HTMLTextReader_since_section, |
| 733 |
since); |
| 734 |
handleBlockTags( |
| 735 |
PHPDocumentationMessages.JavaDoc2HTMLTextReader_version_section, |
| 736 |
versions); |
| 737 |
handleBlockTags( |
| 738 |
PHPDocumentationMessages.JavaDoc2HTMLTextReader_author_section, |
| 739 |
authors); |
| 740 |
handleBlockTags( |
| 741 |
PHPDocumentationMessages.JavaDoc2HTMLTextReader_see_section, |
| 742 |
sees); |
| 743 |
handleBlockTags(rest); |
| 744 |
fBuf.append(BLOCK_TAG_END); |
| 745 |
|
| 746 |
} else if (fBuf.length() > 0) { |
| 747 |
handleSuperMethodReferences(); |
| 748 |
} |
| 749 |
|
| 750 |
String result = fBuf.toString(); |
| 751 |
fBuf = null; |
| 752 |
return result; |
| 753 |
} |
| 754 |
|
| 755 |
private void handleDeprecatedTag(PHPDocTag tag) { |
| 756 |
fBuf.append("<p><b>"); //$NON-NLS-1$ |
| 757 |
fBuf.append(PHPDocumentationMessages.JavaDoc2HTMLTextReader_deprecated_section); |
| 758 |
fBuf.append("</b> <i>"); //$NON-NLS-1$ |
| 759 |
handleContentElements(tag); |
| 760 |
fBuf.append("</i><p>"); //$NON-NLS-1$ |
| 761 |
} |
| 762 |
|
| 763 |
private void handleSuperMethodReferences() { |
| 764 |
if (fMethod != null && fMethod.getDeclaringType() != null) { |
| 765 |
try { |
| 766 |
StringBuffer superMethodReferences = createSuperMethodReferences(fMethod); |
| 767 |
if (superMethodReferences != null) |
| 768 |
fBuf.append(superMethodReferences); |
| 769 |
} catch (ModelException e) { |
| 770 |
PHPUiPlugin.log(e); |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
private List<String> initParameterNames() { |
| 776 |
if (fMethod != null) { |
| 777 |
try { |
| 778 |
return new ArrayList<String>(Arrays.asList(fMethod |
| 779 |
.getParameterNames())); |
| 780 |
} catch (ModelException e) { |
| 781 |
PHPUiPlugin.log(e); |
| 782 |
} |
| 783 |
} |
| 784 |
return Collections.EMPTY_LIST; |
| 785 |
} |
| 786 |
|
| 787 |
private boolean inheritParameterDescriptions(List<String> parameterNames, |
| 788 |
CharSequence[] parameterDescriptions, CharSequence[] parameterTypes) { |
| 789 |
boolean hasInheritedParameters = false; |
| 790 |
if (fMethod != null && fMethod.getDeclaringType() == null) { |
| 791 |
return hasInheritedParameters; |
| 792 |
} |
| 793 |
for (int i = 0; i < parameterNames.size(); i++) { |
| 794 |
String name = (String) parameterNames.get(i); |
| 795 |
if (name != null) { |
| 796 |
parameterDescriptions[i] = fJavadocLookup |
| 797 |
.getInheritedParamDescription(fMethod, i); |
| 798 |
parameterTypes[i] = fJavadocLookup.getInheritedParamType( |
| 799 |
fMethod, i); |
| 800 |
if (parameterDescriptions[i] != null) |
| 801 |
hasInheritedParameters = true; |
| 802 |
} |
| 803 |
} |
| 804 |
return hasInheritedParameters; |
| 805 |
} |
| 806 |
|
| 807 |
private boolean inheritExceptionDescriptions(List<String> exceptionNames, |
| 808 |
CharSequence[] exceptionDescriptions) { |
| 809 |
boolean hasInheritedExceptions = false; |
| 810 |
if (fMethod != null && fMethod.getDeclaringType() == null) { |
| 811 |
return hasInheritedExceptions; |
| 812 |
} |
| 813 |
for (int i = 0; i < exceptionNames.size(); i++) { |
| 814 |
String name = (String) exceptionNames.get(i); |
| 815 |
if (name != null) { |
| 816 |
exceptionDescriptions[i] = fJavadocLookup |
| 817 |
.getInheritedExceptionDescription(fMethod, name); |
| 818 |
if (exceptionDescriptions[i] != null) |
| 819 |
hasInheritedExceptions = true; |
| 820 |
} |
| 821 |
} |
| 822 |
return hasInheritedExceptions; |
| 823 |
} |
| 824 |
|
| 825 |
CharSequence getMainDescription() { |
| 826 |
if (fMainDescription == null) { |
| 827 |
fMainDescription = new StringBuffer(); |
| 828 |
fBuf = fMainDescription; |
| 829 |
|
| 830 |
String shortDescription = fJavadoc.getShortDescription(); |
| 831 |
if (shortDescription != null && shortDescription.length() > 0) { |
| 832 |
fMainDescription.append(shortDescription); |
| 833 |
} |
| 834 |
fBuf = null; |
| 835 |
} |
| 836 |
return fMainDescription.length() > 0 ? fMainDescription : null; |
| 837 |
} |
| 838 |
|
| 839 |
CharSequence getReturnDescription() { |
| 840 |
if (fReturnDescription == null) { |
| 841 |
fReturnDescription = new StringBuffer(); |
| 842 |
fBuf = fReturnDescription; |
| 843 |
|
| 844 |
PHPDocTag[] tags = fJavadoc.getTags(); |
| 845 |
for (PHPDocTag tag : tags) { |
| 846 |
if (PHPDocTag.RETURN == tag.getTagKind()) { |
| 847 |
handleContentElements(tag); |
| 848 |
break; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
fBuf = null; |
| 853 |
} |
| 854 |
return fReturnDescription.length() > 0 ? fReturnDescription : null; |
| 855 |
} |
| 856 |
|
| 857 |
CharSequence getInheritedParamDescription(int paramIndex) |
| 858 |
throws ModelException { |
| 859 |
if (fMethod != null) { |
| 860 |
String[] parameterNames = fMethod.getParameterNames(); |
| 861 |
if (paramIndex >= parameterNames.length) { |
| 862 |
return null; |
| 863 |
} |
| 864 |
if (fParamDescriptions == null) { |
| 865 |
fParamDescriptions = new StringBuffer[parameterNames.length]; |
| 866 |
} else { |
| 867 |
StringBuffer description = fParamDescriptions[paramIndex]; |
| 868 |
if (description != null) { |
| 869 |
return description.length() > 0 ? description : null; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
StringBuffer description = new StringBuffer(); |
| 874 |
fParamDescriptions[paramIndex] = description; |
| 875 |
fBuf = description; |
| 876 |
|
| 877 |
String paramName = parameterNames[paramIndex]; |
| 878 |
String info = getParameterInfo(fJavadoc, paramName, |
| 879 |
PARAMETER_DESCRIPTION_TYPE); |
| 880 |
if (info != null) |
| 881 |
description.append(info); |
| 882 |
fBuf = null; |
| 883 |
return description.length() > 0 ? description : null; |
| 884 |
} |
| 885 |
return null; |
| 886 |
} |
| 887 |
|
| 888 |
CharSequence getInheritedParamType(int paramIndex) throws ModelException { |
| 889 |
if (fMethod != null) { |
| 890 |
String[] parameterNames = fMethod.getParameterNames(); |
| 891 |
if (paramIndex >= parameterNames.length) { |
| 892 |
return null; |
| 893 |
} |
| 894 |
if (fParamTypes == null) { |
| 895 |
fParamTypes = new StringBuffer[parameterNames.length]; |
| 896 |
} else { |
| 897 |
StringBuffer typeName = fParamTypes[paramIndex]; |
| 898 |
if (typeName != null) { |
| 899 |
return typeName.length() > 0 ? typeName : null; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
StringBuffer typeName = new StringBuffer(); |
| 904 |
fParamTypes[paramIndex] = typeName; |
| 905 |
fBuf = typeName; |
| 906 |
|
| 907 |
String paramName = parameterNames[paramIndex]; |
| 908 |
String info = getParameterInfo(fJavadoc, paramName, |
| 909 |
PARAMETER_TYPE_TYPE); |
| 910 |
if (info != null) |
| 911 |
typeName.append(info); |
| 912 |
|
| 913 |
fBuf = null; |
| 914 |
return typeName.length() > 0 ? typeName : null; |
| 915 |
} |
| 916 |
return null; |
| 917 |
} |
| 918 |
|
| 919 |
CharSequence getExceptionDescription(String simpleName) { |
| 920 |
if (fMethod != null) { |
| 921 |
if (fExceptionDescriptions == null) { |
| 922 |
fExceptionDescriptions = new HashMap(); |
| 923 |
} else { |
| 924 |
StringBuffer description = (StringBuffer) fExceptionDescriptions |
| 925 |
.get(simpleName); |
| 926 |
if (description != null) { |
| 927 |
return description.length() > 0 ? description : null; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
StringBuffer description = new StringBuffer(); |
| 932 |
fExceptionDescriptions.put(simpleName, description); |
| 933 |
fBuf = description; |
| 934 |
|
| 935 |
List tags = Arrays.asList(fJavadoc.getTags()); |
| 936 |
for (Iterator iter = tags.iterator(); iter.hasNext();) { |
| 937 |
PHPDocTag tag = (PHPDocTag) iter.next(); |
| 938 |
if (PHPDocTag.THROWS == tag.getTagKind()) { |
| 939 |
List fragments = Arrays.asList(tag.getReferences()); |
| 940 |
if (fragments.size() > 0) { |
| 941 |
Object first = fragments.get(0); |
| 942 |
if (first instanceof Identifier) { |
| 943 |
String name = ((Identifier) first).getName(); |
| 944 |
if (name.equals(simpleName)) { |
| 945 |
if (fragments.size() > 1) |
| 946 |
handleContentElements(tag); |
| 947 |
break; |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
fBuf = null; |
| 955 |
return description.length() > 0 ? description : null; |
| 956 |
} |
| 957 |
return null; |
| 958 |
} |
| 959 |
|
| 960 |
private void handleContentElements(PHPDocTag tag) { |
| 961 |
fBuf.append(tag.getValue()); |
| 962 |
} |
| 963 |
|
| 964 |
private boolean handleInherited(CharSequence inherited) { |
| 965 |
if (inherited == null) |
| 966 |
return false; |
| 967 |
|
| 968 |
fBuf.append(inherited); |
| 969 |
return true; |
| 970 |
} |
| 971 |
|
| 972 |
private void handleBlockTags(String title, List tags) { |
| 973 |
if (tags.isEmpty()) |
| 974 |
return; |
| 975 |
|
| 976 |
handleBlockTagTitle(title); |
| 977 |
|
| 978 |
for (Iterator iter = tags.iterator(); iter.hasNext();) { |
| 979 |
PHPDocTag tag = (PHPDocTag) iter.next(); |
| 980 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 981 |
if (PHPDocTag.SEE == tag.getTagKind()) { |
| 982 |
handleSeeTag(tag); |
| 983 |
} else { |
| 984 |
handleContentElements(tag); |
| 985 |
} |
| 986 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
private void handleReturnTag(PHPDocTag tag, CharSequence returnDescription) { |
| 991 |
if (tag == null && returnDescription == null) |
| 992 |
return; |
| 993 |
|
| 994 |
handleBlockTagTitle(PHPDocumentationMessages.JavaDoc2HTMLTextReader_returns_section); |
| 995 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 996 |
if (tag != null) |
| 997 |
handleContentElements(tag); |
| 998 |
else |
| 999 |
fBuf.append(returnDescription); |
| 1000 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1001 |
} |
| 1002 |
|
| 1003 |
private void handleBlockTags(List tags) { |
| 1004 |
for (Iterator iter = tags.iterator(); iter.hasNext();) { |
| 1005 |
PHPDocTag tag = (PHPDocTag) iter.next(); |
| 1006 |
if (tag.getTagKind() == PHPDocTag.VAR) { |
| 1007 |
handleBlockTagTitle("Type"); |
| 1008 |
} else { |
| 1009 |
handleBlockTagTitle(PHPDocTag.getTagKind(tag.getTagKind())); |
| 1010 |
} |
| 1011 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1012 |
if (tag.getTagKind() == PHPDocTag.LINK) { |
| 1013 |
handleLinkTag(tag); |
| 1014 |
} else { |
| 1015 |
handleContentElements(tag); |
| 1016 |
} |
| 1017 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
private void handleBlockTagTitle(String title) { |
| 1022 |
fBuf.append("<dt>"); //$NON-NLS-1$ |
| 1023 |
fBuf.append(title); |
| 1024 |
fBuf.append("</dt>"); //$NON-NLS-1$ |
| 1025 |
} |
| 1026 |
|
| 1027 |
private void handleSeeTag(PHPDocTag tag) { |
| 1028 |
handleLink(Arrays.asList(tag.getReferences())); |
| 1029 |
} |
| 1030 |
|
| 1031 |
private void handleExceptionTags(List tags, List exceptionNames, |
| 1032 |
CharSequence[] exceptionDescriptions) { |
| 1033 |
if (tags.size() == 0 && containsOnlyNull(exceptionNames)) |
| 1034 |
return; |
| 1035 |
|
| 1036 |
handleBlockTagTitle(PHPDocumentationMessages.JavaDoc2HTMLTextReader_throws_section); |
| 1037 |
|
| 1038 |
for (Iterator iter = tags.iterator(); iter.hasNext();) { |
| 1039 |
PHPDocTag tag = (PHPDocTag) iter.next(); |
| 1040 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1041 |
handleThrowsTag(tag); |
| 1042 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1043 |
} |
| 1044 |
for (int i = 0; i < exceptionDescriptions.length; i++) { |
| 1045 |
CharSequence description = exceptionDescriptions[i]; |
| 1046 |
String name = (String) exceptionNames.get(i); |
| 1047 |
if (name != null) { |
| 1048 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1049 |
// handleLink(Collections.singletonList( |
| 1050 |
// .newSimpleName(name))); |
| 1051 |
if (description != null) { |
| 1052 |
fBuf.append(ScriptElementLabels.CONCAT_STRING); |
| 1053 |
fBuf.append(description); |
| 1054 |
} |
| 1055 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
private void handleThrowsTag(PHPDocTag tag) { |
| 1061 |
List<SimpleReference> fragments = Arrays.asList(tag.getReferences()); |
| 1062 |
int size = fragments.size(); |
| 1063 |
if (size > 0) { |
| 1064 |
String exceptionName = ""; |
| 1065 |
if (fragments.get(0) instanceof TypeReference) { |
| 1066 |
exceptionName = fragments.get(0).getName().trim(); |
| 1067 |
fBuf.append(exceptionName); |
| 1068 |
} |
| 1069 |
String value = tag.getValue().trim(); |
| 1070 |
String description = value.substring(exceptionName.length()); |
| 1071 |
if (description.length() > 0) { |
| 1072 |
fBuf.append(ScriptElementLabels.CONCAT_STRING); |
| 1073 |
fBuf.append(description.trim()); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
private void handleParameterTags(List tags, List parameterNames, |
| 1079 |
CharSequence[] parameterTypes, CharSequence[] parameterDescriptions) { |
| 1080 |
if (tags.size() == 0 && containsOnlyNull(parameterNames)) |
| 1081 |
return; |
| 1082 |
|
| 1083 |
handleBlockTagTitle(PHPDocumentationMessages.JavaDoc2HTMLTextReader_parameters_section); |
| 1084 |
|
| 1085 |
for (Iterator iter = tags.iterator(); iter.hasNext();) { |
| 1086 |
PHPDocTag tag = (PHPDocTag) iter.next(); |
| 1087 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1088 |
handleParamTag(tag); |
| 1089 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1090 |
} |
| 1091 |
for (int i = 0; i < parameterDescriptions.length; i++) { |
| 1092 |
CharSequence description = parameterDescriptions[i]; |
| 1093 |
String name = (String) parameterNames.get(i); |
| 1094 |
if (name != null) { |
| 1095 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1096 |
if (parameterTypes[i] != null) { |
| 1097 |
fBuf.append(PARAM_NAME_START); |
| 1098 |
fBuf.append(parameterTypes[i]); |
| 1099 |
fBuf.append(PARAM_NAME_END); |
| 1100 |
} |
| 1101 |
fBuf.append(PARAM_NAME_END); |
| 1102 |
fBuf.append(PARAM_NAME_START); |
| 1103 |
fBuf.append(name); |
| 1104 |
fBuf.append(PARAM_NAME_END); |
| 1105 |
if (description != null) |
| 1106 |
fBuf.append(description); |
| 1107 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
private void handleParamTag(PHPDocTag tag) { |
| 1113 |
if (tag.getReferences().length == 0) { |
| 1114 |
fBuf.append(tag.getValue()); |
| 1115 |
return; |
| 1116 |
} |
| 1117 |
String parameterName = getParameterInfo(tag, PARAMETER_NAME_TYPE); |
| 1118 |
String parameterType = getParameterInfo(tag, PARAMETER_TYPE_TYPE); |
| 1119 |
String description = getParameterInfo(tag, PARAMETER_DESCRIPTION_TYPE); |
| 1120 |
fBuf.append(BlOCK_TAG_ENTRY_START); |
| 1121 |
if (parameterType != null) { |
| 1122 |
fBuf.append(PARAM_NAME_START); |
| 1123 |
fBuf.append(parameterType); |
| 1124 |
fBuf.append(PARAM_NAME_END); |
| 1125 |
} |
| 1126 |
fBuf.append(PARAM_NAME_START); |
| 1127 |
fBuf.append(parameterName); |
| 1128 |
fBuf.append(PARAM_NAME_END); |
| 1129 |
if (description != null) |
| 1130 |
fBuf.append(description); |
| 1131 |
fBuf.append(BlOCK_TAG_ENTRY_END); |
| 1132 |
} |
| 1133 |
|
| 1134 |
private void handleLinkTag(PHPDocTag tag) { |
| 1135 |
fBuf.append("<a href="); |
| 1136 |
fBuf.append(tag.getValue()); |
| 1137 |
fBuf.append(">").append(tag.getValue()).append("</a>"); |
| 1138 |
} |
| 1139 |
|
| 1140 |
private void handleLink(List fragments) { |
| 1141 |
// int fs = fragments.size(); |
| 1142 |
// if (fs > 0) { |
| 1143 |
// Object first = fragments.get(0); |
| 1144 |
// String refTypeName = null; |
| 1145 |
// String refMemberName = null; |
| 1146 |
// String[] refMethodParamTypes = null; |
| 1147 |
// String[] refMethodParamNames = null; |
| 1148 |
// if (first instanceof Name) { |
| 1149 |
// Name name = (Name) first; |
| 1150 |
// refTypeName = name.getFullyQualifiedName(); |
| 1151 |
// } else if (first instanceof MemberRef) { |
| 1152 |
// MemberRef memberRef = (MemberRef) first; |
| 1153 |
// Name qualifier = memberRef.getQualifier(); |
| 1154 |
// refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$ |
| 1155 |
// refMemberName = memberRef.getName().getIdentifier(); |
| 1156 |
// } else if (first instanceof MethodRef) { |
| 1157 |
// MethodRef methodRef = (MethodRef) first; |
| 1158 |
// Name qualifier = methodRef.getQualifier(); |
| 1159 |
// refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$ |
| 1160 |
// refMemberName = methodRef.getName().getIdentifier(); |
| 1161 |
// List params = methodRef.parameters(); |
| 1162 |
// int ps = params.size(); |
| 1163 |
// refMethodParamTypes = new String[ps]; |
| 1164 |
// refMethodParamNames = new String[ps]; |
| 1165 |
// for (int i = 0; i < ps; i++) { |
| 1166 |
// MethodRefParameter param = (MethodRefParameter) params |
| 1167 |
// .get(i); |
| 1168 |
// refMethodParamTypes[i] = ASTNodes.asString(param.getType()); |
| 1169 |
// SimpleName paramName = param.getName(); |
| 1170 |
// if (paramName != null) |
| 1171 |
// refMethodParamNames[i] = paramName.getIdentifier(); |
| 1172 |
// } |
| 1173 |
// } |
| 1174 |
// |
| 1175 |
// if (refTypeName != null) { |
| 1176 |
// fBuf.append("<a href='"); //$NON-NLS-1$ |
| 1177 |
// try { |
| 1178 |
// String scheme = JavaElementLinks.JAVADOC_SCHEME; |
| 1179 |
// String uri = JavaElementLinks.createURI(scheme, fMember, |
| 1180 |
// refTypeName, refMemberName, refMethodParamTypes); |
| 1181 |
// fBuf.append(uri); |
| 1182 |
// } catch (URISyntaxException e) { |
| 1183 |
// PHPUiPlugin.log(e); |
| 1184 |
// } |
| 1185 |
// fBuf.append("'>"); //$NON-NLS-1$ |
| 1186 |
// if (fs > 1) { |
| 1187 |
// // if (fs == 2 && fragments.get(1) instanceof TextElement) { |
| 1188 |
// // String text= removeLeadingWhitespace(((TextElement) |
| 1189 |
// // fragments.get(1)).getText()); |
| 1190 |
// // if (text.length() != 0) |
| 1191 |
// // handleText(text); |
| 1192 |
// // else |
| 1193 |
// // //throws |
| 1194 |
// // } |
| 1195 |
// handleContentElements(fragments.subList(1, fs)); |
| 1196 |
// } else { |
| 1197 |
// fBuf.append(refTypeName); |
| 1198 |
// if (refMemberName != null) { |
| 1199 |
// if (refTypeName.length() > 0) { |
| 1200 |
// fBuf.append('.'); |
| 1201 |
// } |
| 1202 |
// fBuf.append(refMemberName); |
| 1203 |
// if (refMethodParamTypes != null) { |
| 1204 |
// fBuf.append('('); |
| 1205 |
// for (int i = 0; i < refMethodParamTypes.length; i++) { |
| 1206 |
// String pType = refMethodParamTypes[i]; |
| 1207 |
// fBuf.append(pType); |
| 1208 |
// String pName = refMethodParamNames[i]; |
| 1209 |
// if (pName != null) { |
| 1210 |
// fBuf.append(' ').append(pName); |
| 1211 |
// } |
| 1212 |
// if (i < refMethodParamTypes.length - 1) { |
| 1213 |
// fBuf.append(", "); //$NON-NLS-1$ |
| 1214 |
// } |
| 1215 |
// } |
| 1216 |
// fBuf.append(')'); |
| 1217 |
// } |
| 1218 |
// } |
| 1219 |
// } |
| 1220 |
// fBuf.append("</a>"); //$NON-NLS-1$ |
| 1221 |
// } else { |
| 1222 |
// handleContentElements(fragments); |
| 1223 |
// } |
| 1224 |
// } |
| 1225 |
} |
| 1226 |
|
| 1227 |
private boolean containsOnlyNull(List parameterNames) { |
| 1228 |
for (Iterator iter = parameterNames.iterator(); iter.hasNext();) { |
| 1229 |
if (iter.next() != null) |
| 1230 |
return false; |
| 1231 |
} |
| 1232 |
return true; |
| 1233 |
} |
| 1234 |
|
| 1235 |
private String getParameterInfo(PHPDocBlock phpDoc, String paramName, |
| 1236 |
int infoType) { |
| 1237 |
for (PHPDocTag tag : phpDoc.getTags(PHPDocTag.PARAM)) { |
| 1238 |
String name = getParameterInfo(tag, PARAMETER_NAME_TYPE); |
| 1239 |
if (name.equals(paramName)) { |
| 1240 |
return getParameterInfo(tag, infoType); |
| 1241 |
} |
| 1242 |
continue; |
| 1243 |
} |
| 1244 |
return null; |
| 1245 |
} |
| 1246 |
|
| 1247 |
private String getParameterInfo(PHPDocTag tag, int infoType) { |
| 1248 |
if (tag.getTagKind() != PHPDocTag.PARAM) { |
| 1249 |
return null; |
| 1250 |
} |
| 1251 |
SimpleReference typeRef = null; |
| 1252 |
SimpleReference variableRef = null; |
| 1253 |
String value = tag.getValue(); |
| 1254 |
if (tag.getReferences().length != 2) { |
| 1255 |
return null; |
| 1256 |
} |
| 1257 |
for (SimpleReference reference : tag.getReferences()) { |
| 1258 |
if (reference instanceof TypeReference) { |
| 1259 |
typeRef = reference; |
| 1260 |
} else if (reference instanceof VariableReference) { |
| 1261 |
variableRef = reference; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
if (infoType == PARAMETER_DESCRIPTION_TYPE) { |
| 1265 |
int typeRefIndex = value.indexOf(typeRef.getName()); |
| 1266 |
int variableRefIndex = value.indexOf(variableRef.getName()); |
| 1267 |
int lastRefIndex = typeRefIndex > variableRefIndex ? typeRefIndex |
| 1268 |
+ typeRef.getName().length() : variableRefIndex |
| 1269 |
+ variableRef.getName().length(); |
| 1270 |
return value.substring(lastRefIndex).trim(); |
| 1271 |
} else if (infoType == PARAMETER_TYPE_TYPE) { |
| 1272 |
return typeRef.getName(); |
| 1273 |
} else if (infoType == PARAMETER_NAME_TYPE) { |
| 1274 |
return variableRef.getName(); |
| 1275 |
} |
| 1276 |
return null; |
| 1277 |
} |
| 1278 |
|
| 1279 |
private boolean appendBuiltinDoc(IMember element, StringBuffer buf) { |
| 1280 |
String builtinDoc = BuiltinDoc.getString(element.getElementName()); |
| 1281 |
if (builtinDoc.length() > 0) { |
| 1282 |
// String fileName = getFileName(element); |
| 1283 |
// |
| 1284 |
// // append the file name |
| 1285 |
// appendDefinitionRow(FIELD_LOCATION, fileName, buf); |
| 1286 |
// |
| 1287 |
// // append the class name if it exists |
| 1288 |
// IType declaringType = element.getDeclaringType(); |
| 1289 |
// appendTypeInfoRow(declaringType, buf); |
| 1290 |
|
| 1291 |
buf.append(builtinDoc); |
| 1292 |
return true; |
| 1293 |
} |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
|
| 1297 |
} |