KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > jdom > DOMMember


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.jdom;
12
13 import org.eclipse.jdt.core.Flags;
14 import org.eclipse.jdt.core.compiler.CharOperation;
15 import org.eclipse.jdt.core.jdom.*;
16 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17 import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
18 /**
19  * DOMMember provides an implementation of IDOMMember.
20  *
21  * @see IDOMMember
22  * @see DOMNode
23  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
24  * powerful, fine-grained DOM/AST API found in the
25  * org.eclipse.jdt.core.dom package.
26  */

27 abstract class DOMMember extends DOMNode implements IDOMMember {
28
29     /**
30      * The modifier flags for this member that can be
31      * analyzed with org.eclipse.jdt.core.Flags
32      */

33     protected int fFlags= 0;
34
35     /**
36      * The member's comments when it has been altered from
37      * the contents in the document, otherwise <code>null</code>.
38      */

39     protected String JavaDoc fComment= null;
40
41     /**
42      * The original inclusive source range of the
43      * member's preceding comments in the document,
44      * or -1's if the member did not originally have a
45      * comment.
46      */

47      protected int[] fCommentRange;
48
49
50     /**
51      * The member's modifiers textual representation when
52      * the modifiers (flags) have been altered from
53      * their original contents, otherwise <code>null</code>.
54      */

55      protected char[] fModifiers= null;
56
57     /**
58      * The original inclusive source range of the
59      * member's modifiers in the document, or -1's if
60      * the member did not originally have modifiers in
61      * the source code (that is, package default visibility).
62      */

63      protected int[] fModifierRange;
64
65 /**
66  * Constructs an empty member node.
67  */

68 DOMMember() {
69     // Constructs an empty member node
70
}
71 /**
72  * Creates a new member document fragment on the given range of the document.
73  *
74  * @param document - the document containing this node's original contents
75  * @param sourceRange - a two element array of integers describing the
76  * entire inclusive source range of this node within its document.
77  * Contents start on and include the character at the first position.
78  * Contents end on and include the character at the last position.
79  * An array of -1's indicates this node's contents do not exist
80  * in the document.
81  * @param name - the identifier portion of the name of this node, or
82  * <code>null</code> if this node does not have a name
83  * @param nameRange - a two element array of integers describing the
84  * entire inclusive source range of this node's name within its document,
85  * including any array qualifiers that might immediately follow the name.
86  * @param commentRange - a two element array describing the comments that precede
87  * the member declaration. The first matches the start of this node's
88  * sourceRange, and the second is the new-line or first non-whitespace
89  * character following the last comment. If no comments are present,
90  * this array contains two -1's.
91  * @param flags - an integer representing the modifiers for this member. The
92  * integer can be analyzed with org.eclipse.jdt.core.Flags
93  * @param modifierRange - a two element array describing the location of
94  * modifiers for this member within its source range. The first integer
95  * is the first character of the first modifier for this member, and
96  * the second integer is the last whitespace character preceeding the
97  * next part of this member declaration. If there are no modifiers present
98  * in this node's source code (that is, package default visibility), this array
99  * contains two -1's.
100  */

101 DOMMember(char[] document, int[] sourceRange, String JavaDoc name, int[] nameRange, int[] commentRange, int flags, int[] modifierRange) {
102     super(document, sourceRange, name, nameRange);
103     fFlags= flags;
104     fComment= null;
105     fCommentRange= commentRange;
106     fModifierRange= modifierRange;
107     setHasComment(commentRange[0] >= 0);
108 }
109 /**
110  * Appends the contents of this node to the given CharArrayBuffer, using
111  * the original document and indicies as a form for the current attribute values
112  * of this node.
113  *
114  * <p>To facilitate the implementation of generating contents for members,
115  * the content of members is split into three sections - the header,
116  * declaration, and body sections. The header section includes any preceding
117  * comments and modifiers. The declaration section includes the portion of
118  * the member declaration that follows any modifiers and precedes the
119  * member body. The body section includes the member body and any trailing
120  * whitespace.
121  *
122  * @see DOMNode#appendFragmentedContents(CharArrayBuffer)
123  */

124 protected void appendFragmentedContents(CharArrayBuffer buffer) {
125     if (isDetailed()) {
126         appendMemberHeaderFragment(buffer);
127         appendMemberDeclarationContents(buffer);
128         appendMemberBodyContents(buffer);
129     } else {
130         appendSimpleContents(buffer);
131     }
132 }
133 /**
134  * Appends this member's body contents to the given CharArrayBuffer.
135  * Body contents include the member body and any trailing whitespace.
136  */

137 protected abstract void appendMemberBodyContents(CharArrayBuffer buffer);
138 /**
139  * Appends this member's declaration contents to the given CharArrayBuffer.
140  * The declaration contents includes the portion of this member that
141  * appears after any modifiers and precedes the body.
142  */

143 protected abstract void appendMemberDeclarationContents(CharArrayBuffer buffer);
144 /**
145  * Appends this member's header contents to the given CharArrayBuffer.
146  * Header contents include any preceding comments and modifiers.
147  */

148 protected void appendMemberHeaderFragment(CharArrayBuffer buffer) {
149
150     int spaceStart, spaceEnd;
151
152     // space before comment
153
if (hasComment()) {
154         spaceStart= fSourceRange[0];
155         spaceEnd= fCommentRange[0];
156         if (spaceEnd > 0) {
157             buffer.append(fDocument, spaceStart, spaceEnd - spaceStart);
158         }
159     }
160     
161     String JavaDoc fragment= getComment();
162     if (fragment != null) {
163         buffer.append(fragment);
164     }
165
166     if (fCommentRange[1] >= 0) {
167         spaceStart= fCommentRange[1] + 1;
168     } else {
169         spaceStart= fSourceRange[0];
170     }
171     if (fModifierRange[0] >= 0) {
172         spaceEnd= fModifierRange[0] - 1;
173     } else {
174         spaceEnd= getMemberDeclarationStartPosition() - 1;
175     }
176
177     if (spaceEnd >= spaceStart) {
178         buffer.append(fDocument, spaceStart, spaceEnd + 1 - spaceStart);
179     }
180     buffer.append(getModifiersText());
181
182 }
183 /**
184  * Appends the contents of this node to the given CharArrayBuffer, using
185  * the original document and indicies as a form for the current attribute values
186  * of this node. This method is called when this node is know not to have
187  * detailed source indexes.
188  */

189 protected abstract void appendSimpleContents(CharArrayBuffer buffer);
190 /**
191  * Returns a copy of the given array with the new element appended
192  * to the end of the array.
193  */

194 protected String JavaDoc[] appendString(String JavaDoc[] list, String JavaDoc element) {
195     String JavaDoc[] copy= new String JavaDoc[list.length + 1];
196     System.arraycopy(list, 0, copy, 0, list.length);
197     copy[list.length]= element;
198     return copy;
199 }
200 /**
201  * Returns a <code>String</code> describing the modifiers for this member,
202  * ending with whitespace (if not empty). This value serves as a replacement
203  * value for the member's modifier range when the modifiers have been altered
204  * from their original contents.
205  */

206 protected char[] generateFlags() {
207     char[] flags= Flags.toString(getFlags()).toCharArray();
208     if (flags.length == 0) {
209         return flags;
210     } else {
211         return CharOperation.concat(flags, new char[] {' '});
212     }
213 }
214 /**
215  * @see IDOMMember#getComment()
216  */

217 public String JavaDoc getComment() {
218     becomeDetailed();
219     if (hasComment()) {
220         if (fComment != null) {
221             return fComment;
222         } else {
223             return new String JavaDoc(fDocument, fCommentRange[0], fCommentRange[1] + 1 - fCommentRange[0]);
224         }
225     } else {
226         return null;
227     }
228 }
229 /**
230  * @see IDOMMember#getFlags()
231  */

232 public int getFlags() {
233     return fFlags;
234 }
235 /**
236  * Returns the location of the first character in the member's declaration
237  * section.
238  */

239 protected abstract int getMemberDeclarationStartPosition();
240 /**
241  * Returns the String to be used for this member's flags when
242  * generating contents - either the original contents in the document
243  * or the replacement value.
244  */

245 protected char[] getModifiersText() {
246     if (fModifiers == null) {
247         if (fModifierRange[0] < 0) {
248             return null;
249         } else {
250             return CharOperation.subarray(fDocument, fModifierRange[0], fModifierRange[1] + 1);
251         }
252     } else {
253         return fModifiers;
254     }
255 }
256 /**
257  * Returns true if this member currently has a body.
258  */

259 protected boolean hasBody() {
260     return getMask(MASK_HAS_BODY);
261 }
262 /**
263  * Returns true if this member currently has a comment.
264  */

265 protected boolean hasComment() {
266     return getMask(MASK_HAS_COMMENT);
267 }
268 /**
269  * Offsets all the source indexes in this node by the given amount.
270  */

271 protected void offset(int offset) {
272     super.offset(offset);
273     offsetRange(fCommentRange, offset);
274     offsetRange(fModifierRange, offset);
275 }
276 /**
277  * @see IDOMMember#setComment(String)
278  */

279 public void setComment(String JavaDoc comment) {
280     becomeDetailed();
281     fComment= comment;
282     fragment();
283     setHasComment(comment != null);
284     /* see 1FVIJAH */
285     if (comment != null && comment.indexOf("@deprecated") >= 0) { //$NON-NLS-1$
286
fFlags= fFlags | ClassFileConstants.AccDeprecated;
287         return;
288     }
289     fFlags= fFlags & (~ClassFileConstants.AccDeprecated);
290 }
291 /**
292  * @see IDOMMember#setFlags(int)
293  */

294 public void setFlags(int flags) {
295     becomeDetailed();
296     if (Flags.isDeprecated(fFlags)) {
297         fFlags= flags | ClassFileConstants.AccDeprecated;
298     } else {
299         fFlags= flags & (~ClassFileConstants.AccDeprecated);
300     }
301     fragment();
302     fModifiers= generateFlags();
303 }
304 /**
305  * Sets the state of this member declaration as having
306  * a body.
307  */

308 protected void setHasBody(boolean hasBody) {
309     setMask(MASK_HAS_BODY, hasBody);
310 }
311 /**
312  * Sets the state of this member declaration as having
313  * a preceding comment.
314  */

315 protected void setHasComment(boolean hasComment) {
316     setMask(MASK_HAS_COMMENT, hasComment);
317 }
318 /**
319  * Sets the original position of the first character of this node's contents
320  * in its document. This method is only used during DOM creation while
321  * normalizing the source range of each node.
322  *
323  * Synchronize the start of the comment position with the start of the
324  * node.
325  */

326 protected void setStartPosition(int start) {
327     if (fCommentRange[0] >= 0) {
328         fCommentRange[0]= start;
329     }
330     super.setStartPosition(start);
331 }
332 /**
333  * @see DOMNode#shareContents(DOMNode)
334  */

335 protected void shareContents(DOMNode node) {
336     super.shareContents(node);
337     DOMMember member= (DOMMember)node;
338     fComment= member.fComment;
339     fCommentRange= rangeCopy(member.fCommentRange);
340     fFlags= member.fFlags;
341     fModifiers= member.fModifiers;
342     fModifierRange= rangeCopy(member.fModifierRange);
343 }
344 }
345
Popular Tags