1 /*******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * AST node for a tag within a doc comment.
19 * Tag elements nested within another tag element are called
23 * [ <b>@</b> Identifier ] { DocElement }
29 * <b>{</b> TagElement <b>}</b>
34 * @noinstantiate This class is not intended to be instantiated by clients.
36 public final class TagElement extends ASTNode implements IDocElement {
39 * The "tagName" structural property of this node type.
43 public static final SimplePropertyDescriptor TAG_NAME_PROPERTY =
44 new SimplePropertyDescriptor(TagElement.class, "tagName", String.class, OPTIONAL); //$NON-NLS-1$
47 * The "fragments" structural property of this node type.
50 public static final ChildListPropertyDescriptor FRAGMENTS_PROPERTY =
51 new ChildListPropertyDescriptor(TagElement.class, "fragments", IDocElement.class, CYCLE_RISK); //$NON-NLS-1$
54 * A list of property descriptors (element type:
55 * {@link StructuralPropertyDescriptor}),
56 * or null if uninitialized.
59 private static final List PROPERTY_DESCRIPTORS;
62 List propertyList = new ArrayList(3);
63 createPropertyList(TagElement.class, propertyList);
64 addProperty(TAG_NAME_PROPERTY, propertyList);
65 addProperty(FRAGMENTS_PROPERTY, propertyList);
66 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
70 * Returns a list of structural property descriptors for this node type.
71 * Clients must not modify the result.
73 * @param apiLevel the API level; one of the
74 * <code>AST.JLS*</code> constants
75 * @return a list of property descriptors (element type:
76 * {@link StructuralPropertyDescriptor})
79 public static List propertyDescriptors(int apiLevel) {
80 return PROPERTY_DESCRIPTORS;
84 * Standard doc tag name (value {@value}).
86 public static final String TAG_AUTHOR = "@author"; //$NON-NLS-1$
89 * Standard inline doc tag name (value {@value}).
91 * Note that this tag first appeared in J2SE 5.
95 public static final String TAG_CODE = "@code"; //$NON-NLS-1$
98 * Standard doc tag name (value {@value}).
100 public static final String TAG_DEPRECATED = "@deprecated"; //$NON-NLS-1$
103 * Standard inline doc tag name (value {@value}).
105 public static final String TAG_DOCROOT = "@docRoot"; //$NON-NLS-1$
108 * Standard doc tag name (value {@value}).
110 public static final String TAG_EXCEPTION = "@exception"; //$NON-NLS-1$
113 * Standard inline doc tag name (value {@value}).
115 public static final String TAG_INHERITDOC = "@inheritDoc"; //$NON-NLS-1$
118 * Standard inline doc tag name (value {@value}).
120 public static final String TAG_LINK = "@link"; //$NON-NLS-1$
123 * Standard inline doc tag name (value {@value}).
125 public static final String TAG_LINKPLAIN = "@linkplain"; //$NON-NLS-1$
128 * Standard inline doc tag name (value {@value}).
130 * Note that this tag first appeared in J2SE 5.
134 public static final String TAG_LITERAL = "@literal"; //$NON-NLS-1$
137 * Standard doc tag name (value {@value}).
139 public static final String TAG_PARAM = "@param"; //$NON-NLS-1$
142 * Standard doc tag name (value {@value}).
144 public static final String TAG_RETURN = "@return"; //$NON-NLS-1$
147 * Standard doc tag name (value {@value}).
149 public static final String TAG_SEE = "@see"; //$NON-NLS-1$
152 * Standard doc tag name (value {@value}).
154 public static final String TAG_SERIAL = "@serial"; //$NON-NLS-1$
157 * Standard doc tag name (value {@value}).
159 public static final String TAG_SERIALDATA= "@serialData"; //$NON-NLS-1$
162 * Standard doc tag name (value {@value}).
164 public static final String TAG_SERIALFIELD= "@serialField"; //$NON-NLS-1$
167 * Standard doc tag name (value {@value}).
169 public static final String TAG_SINCE = "@since"; //$NON-NLS-1$
172 * Standard doc tag name (value {@value}).
174 public static final String TAG_THROWS = "@throws"; //$NON-NLS-1$
177 * Standard inline doc tag name (value {@value}).
179 public static final String TAG_VALUE= "@value"; //$NON-NLS-1$
182 * Standard doc tag name (value {@value}).
184 public static final String TAG_VERSION = "@version"; //$NON-NLS-1$
187 * The tag name, or null if none; defaults to null.
189 private String optionalTagName = null;
192 * The list of doc elements (element type: <code>IDocElement</code>).
193 * Defaults to an empty list.
195 private ASTNode.NodeList fragments =
196 new ASTNode.NodeList(FRAGMENTS_PROPERTY);
199 * Creates a new AST node for a tag element owned by the given AST.
200 * The new node has no name and an empty list of fragments.
202 * N.B. This constructor is package-private; all subclasses must be
203 * declared in the same package; clients are unable to declare
204 * additional subclasses.
207 * @param ast the AST that is to own this node
209 TagElement(AST ast) {
213 /* (omit javadoc for this method)
214 * Method declared on ASTNode.
216 final List internalStructuralPropertiesForType(int apiLevel) {
217 return propertyDescriptors(apiLevel);
220 /* (omit javadoc for this method)
221 * Method declared on ASTNode.
223 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
224 if (property == TAG_NAME_PROPERTY) {
228 setTagName((String) value);
232 // allow default implementation to flag the error
233 return super.internalGetSetObjectProperty(property, get, value);
236 /* (omit javadoc for this method)
237 * Method declared on ASTNode.
239 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
240 if (property == FRAGMENTS_PROPERTY) {
243 // allow default implementation to flag the error
244 return super.internalGetChildListProperty(property);
247 /* (omit javadoc for this method)
248 * Method declared on ASTNode.
250 final int getNodeType0() {
254 /* (omit javadoc for this method)
255 * Method declared on ASTNode.
257 ASTNode clone0(AST target) {
258 TagElement result = new TagElement(target);
259 result.setSourceRange(this.getStartPosition(), this.getLength());
260 result.setTagName(getTagName());
261 result.fragments().addAll(ASTNode.copySubtrees(target, fragments()));
265 /* (omit javadoc for this method)
266 * Method declared on ASTNode.
268 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
269 // dispatch to correct overloaded match method
270 return matcher.match(this, other);
273 /* (omit javadoc for this method)
274 * Method declared on ASTNode.
276 void accept0(ASTVisitor visitor) {
277 boolean visitChildren = visitor.visit(this);
279 acceptChildren(visitor, this.fragments);
281 visitor.endVisit(this);
285 * Returns this node's tag name, or <code>null</code> if none.
286 * For top level doc tags such as parameter tags, the tag name
287 * includes the "@" character ("@param").
288 * For inline doc tags such as link tags, the tag name
289 * includes the "@" character ("@link").
290 * The tag name may also be <code>null</code>; this is used to
291 * represent the material at the start of a doc comment preceding
292 * the first explicit tag.
294 * @return the tag name, or <code>null</code> if none
296 public String getTagName() {
297 return this.optionalTagName;
301 * Sets the tag name of this node to the given value.
302 * For top level doc tags such as parameter tags, the tag name
303 * includes the "@" character ("@param").
304 * For inline doc tags such as link tags, the tag name
305 * includes the "@" character ("@link").
306 * The tag name may also be <code>null</code>; this is used to
307 * represent the material at the start of a doc comment preceding
308 * the first explicit tag.
310 * @param tagName the tag name, or <code>null</code> if none
312 public void setTagName(String tagName) {
313 preValueChange(TAG_NAME_PROPERTY);
314 this.optionalTagName = tagName;
315 postValueChange(TAG_NAME_PROPERTY);
319 * Returns the live list of fragments in this tag element.
321 * The fragments cover everything following the tag name
322 * (or everything if there is no tag name), and generally omit
323 * embedded line breaks (and leading whitespace on new lines,
324 * including any leading "*"). {@link org.eclipse.jdt.core.dom.TagElement}
325 * nodes are used to represent tag elements (e.g., "@link")
326 * nested within this tag element.
329 * Here are some typical examples:
331 * <li>"@see Foo#bar()" - TagElement with tag name "@see";
332 * fragments() contains a single MethodRef node</li>
333 * <li>"@param args the program arguments" -
334 * TagElement with tag name "@param";
335 * 2 fragments: SimpleName ("args"), TextElement
336 * (" the program arguments")</li>
337 * <li>"@return See {@link #foo foo} instead." -
338 * TagElement with tag name "@return";
339 * 3 fragments: TextElement ("See "),
340 * TagElement (for "@link #foo foo"),
341 * TextElement (" instead.")</li>
343 * The use of Name, MethodRef, and MemberRef nodes within
344 * tag elements allows these fragments to be queried for
345 * binding information.
348 * Adding and removing nodes from this list affects this node
349 * dynamically. The nodes in this list may be of various
350 * types, including {@link TextElement},
351 * {@link org.eclipse.jdt.core.dom.TagElement}, {@link Name},
352 * {@link MemberRef}, and {@link MethodRef}.
353 * Clients should assume that the list of types may grow in
354 * the future, and write their code to deal with unexpected
355 * nodes types. However, attempts to add a non-proscribed type
356 * of node will trigger an exception.
358 * @return the live list of doc elements in this tag element
359 * (element type: <code>ASTNode</code>)
361 public List fragments() {
362 return this.fragments;
366 * Returns whether this tag element is nested within another
367 * tag element. Nested tag elements appears enclosed in
368 * "{" and "}"; certain doc tags, including "@link" and
369 * "@linkplain" are only meaningful as nested tags.
370 * Top-level (i.e., non-nested) doc tags begin on a new line;
371 * certain doc tags, including "@param" and
372 * "@see" are only meaningful as top-level tags.
374 * This convenience methods checks to see whether the parent
375 * of this node is of type {@link org.eclipse.jdt.core.dom.TagElement}.
378 * @return <code>true</code> if this node is a nested tag element,
379 * and false if this node is either parented by a doc comment node
380 * ({@link Javadoc}), or is unparented
382 public boolean isNested() {
383 return (getParent() instanceof TagElement);
386 /* (omit javadoc for this method)
387 * Method declared on ASTNode.
390 int size = BASE_NODE_SIZE + 2 * 4 + stringSize(this.optionalTagName);
394 /* (omit javadoc for this method)
395 * Method declared on ASTNode.
398 return memSize() + this.fragments.listSize();