001: /*******************************************************************************
002: * Copyright (c) 2004, 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.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.jdt.internal.compiler.util.Util;
016:
017: /**
018: * AST node for a text element within a doc comment.
019: * <pre>
020: * TextElement:
021: * Sequence of characters not including a close comment delimiter <b>*</b><b>/</b>
022: * </pre>
023: *
024: * @see Javadoc
025: * @since 3.0
026: */
027: public final class TextElement extends ASTNode implements IDocElement {
028:
029: /**
030: * The "test" structural property of this node type.
031: *
032: * @since 3.0
033: */
034: public static final SimplePropertyDescriptor TEXT_PROPERTY = new SimplePropertyDescriptor(
035: TextElement.class, "text", String.class, MANDATORY); //$NON-NLS-1$
036:
037: /**
038: * A list of property descriptors (element type:
039: * {@link StructuralPropertyDescriptor}),
040: * or null if uninitialized.
041: * @since 3.0
042: */
043: private static final List PROPERTY_DESCRIPTORS;
044:
045: static {
046: List propertyList = new ArrayList(2);
047: createPropertyList(TextElement.class, propertyList);
048: addProperty(TEXT_PROPERTY, propertyList);
049: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
050: }
051:
052: /**
053: * Returns a list of structural property descriptors for this node type.
054: * Clients must not modify the result.
055: *
056: * @param apiLevel the API level; one of the
057: * <code>AST.JLS*</code> constants
058: * @return a list of property descriptors (element type:
059: * {@link StructuralPropertyDescriptor})
060: * @since 3.0
061: */
062: public static List propertyDescriptors(int apiLevel) {
063: return PROPERTY_DESCRIPTORS;
064: }
065:
066: /**
067: * The text element; defaults to the empty string.
068: */
069: private String text = Util.EMPTY_STRING;
070:
071: /**
072: * Creates a new AST node for a text element owned by the given AST.
073: * The new node has an empty text string.
074: * <p>
075: * N.B. This constructor is package-private; all subclasses must be
076: * declared in the same package; clients are unable to declare
077: * additional subclasses.
078: * </p>
079: *
080: * @param ast the AST that is to own this node
081: */
082: TextElement(AST ast) {
083: super (ast);
084: }
085:
086: /* (omit javadoc for this method)
087: * Method declared on ASTNode.
088: */
089: final List internalStructuralPropertiesForType(int apiLevel) {
090: return propertyDescriptors(apiLevel);
091: }
092:
093: /* (omit javadoc for this method)
094: * Method declared on ASTNode.
095: */
096: final Object internalGetSetObjectProperty(
097: SimplePropertyDescriptor property, boolean get, Object value) {
098: if (property == TEXT_PROPERTY) {
099: if (get) {
100: return getText();
101: } else {
102: setText((String) value);
103: return null;
104: }
105: }
106: // allow default implementation to flag the error
107: return super .internalGetSetObjectProperty(property, get, value);
108: }
109:
110: /* (omit javadoc for this method)
111: * Method declared on ASTNode.
112: */
113: final int getNodeType0() {
114: return TEXT_ELEMENT;
115: }
116:
117: /* (omit javadoc for this method)
118: * Method declared on ASTNode.
119: */
120: ASTNode clone0(AST target) {
121: TextElement result = new TextElement(target);
122: result
123: .setSourceRange(this .getStartPosition(), this
124: .getLength());
125: result.setText(getText());
126: return result;
127: }
128:
129: /* (omit javadoc for this method)
130: * Method declared on ASTNode.
131: */
132: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
133: // dispatch to correct overloaded match method
134: return matcher.match(this , other);
135: }
136:
137: /* (omit javadoc for this method)
138: * Method declared on ASTNode.
139: */
140: void accept0(ASTVisitor visitor) {
141: visitor.visit(this );
142: visitor.endVisit(this );
143: }
144:
145: /**
146: * Returns this node's text.
147: *
148: * @return the text of this node
149: */
150: public String getText() {
151: return this .text;
152: }
153:
154: /**
155: * Sets the text of this node to the given value.
156: * <p>
157: * The text element typically includes leading and trailing
158: * whitespace that separates it from the immediately preceding
159: * or following elements. The text element must not include
160: * a block comment closing delimiter "*"+"/".
161: * </p>
162: *
163: * @param text the text of this node
164: * @exception IllegalArgumentException if the text is null
165: * or contains a block comment closing delimiter
166: */
167: public void setText(String text) {
168: if (text == null) {
169: throw new IllegalArgumentException();
170: }
171: if (text.indexOf("*/") > 0) { //$NON-NLS-1$
172: throw new IllegalArgumentException();
173: }
174: preValueChange(TEXT_PROPERTY);
175: this .text = text;
176: postValueChange(TEXT_PROPERTY);
177: }
178:
179: /* (omit javadoc for this method)
180: * Method declared on ASTNode.
181: */
182: int memSize() {
183: int size = BASE_NODE_SIZE + 1 * 4;
184: if (this .text != Util.EMPTY_STRING) {
185: // everything but our empty string costs
186: size += stringSize(this .text);
187: }
188: return size;
189: }
190:
191: /* (omit javadoc for this method)
192: * Method declared on ASTNode.
193: */
194: int treeSize() {
195: return memSize();
196: }
197: }
|