KickJava   Java API By Example, From Geeks To Geeks.

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


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.IJavaElement;
14 import org.eclipse.jdt.core.IType;
15 import org.eclipse.jdt.core.jdom.*;
16 import org.eclipse.jdt.internal.core.util.Messages;
17 import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
18 import org.eclipse.jdt.internal.core.util.Util;
19 /**
20  * DOMInitializer provides an implementation of IDOMInitializer.
21  *
22  * @see IDOMInitializer
23  * @see DOMNode
24  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
25  * powerful, fine-grained DOM/AST API found in the
26  * org.eclipse.jdt.core.dom package.
27  */

28 class DOMInitializer extends DOMMember implements IDOMInitializer {
29
30     /**
31      * The contents of the initializer's body when the
32      * body has been altered from the contents in the
33      * document, otherwise <code>null</code>.
34      */

35     protected String JavaDoc fBody;
36
37     /**
38      * The original inclusive source range of the
39      * body in the document.
40      */

41     protected int[] fBodyRange;
42
43 /**
44  * Constructs an empty initializer node.
45  */

46 DOMInitializer() {
47     // Constructs an empty initializer node
48
}
49 /**
50  * Creates a new detailed INITIALIZER document fragment on the given range of the document.
51  *
52  * @param document - the document containing this node's original contents
53  * @param sourceRange - a two element array of integers describing the
54  * entire inclusive source range of this node within its document.
55  * Contents start on and include the character at the first position.
56  * Contents end on and include the character at the last position.
57  * An array of -1's indicates this node's contents do not exist
58  * in the document.
59  * @param commentRange - a two element array describing the comments that precede
60  * the member declaration. The first matches the start of this node's
61  * sourceRange, and the second is the new-line or first non-whitespace
62  * character following the last comment. If no comments are present,
63  * this array contains two -1's.
64  * @param flags - an integer representing the modifiers for this member. The
65  * integer can be analyzed with org.eclipse.jdt.core.Flags
66  * @param modifierRange - a two element array describing the location of
67  * modifiers for this member within its source range. The first integer
68  * is the first character of the first modifier for this member, and
69  * the second integer is the last whitespace character preceeding the
70  * next part of this member declaration. If there are no modifiers present
71  * in this node's source code (that is, package default visibility), this array
72  * contains two -1's.
73  * @param bodyStartPosition - the position of the open brace of the body
74  * of this initialzer.
75  */

76 DOMInitializer(char[] document, int[] sourceRange, int[] commentRange, int flags, int[] modifierRange, int bodyStartPosition) {
77     super(document, sourceRange, null, new int[]{-1, -1}, commentRange, flags, modifierRange);
78     fBodyRange= new int[2];
79     fBodyRange[0]= bodyStartPosition;
80     fBodyRange[1]= sourceRange[1];
81     setHasBody(true);
82     setMask(MASK_DETAILED_SOURCE_INDEXES, true);
83 }
84 /**
85  * Creates a new simple INITIALIZER document fragment on the given range of the document.
86  *
87  * @param document - the document containing this node's original contents
88  * @param sourceRange - a two element array of integers describing the
89  * entire inclusive source range of this node within its document.
90  * Contents start on and include the character at the first position.
91  * Contents end on and include the character at the last position.
92  * An array of -1's indicates this node's contents do not exist
93  * in the document.
94  * @param flags - an integer representing the modifiers for this member. The
95  * integer can be analyzed with org.eclipse.jdt.core.Flags
96  */

97 DOMInitializer(char[] document, int[] sourceRange, int flags) {
98     this(document, sourceRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, -1);
99     setMask(MASK_DETAILED_SOURCE_INDEXES, false);
100     
101 }
102 /**
103  * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
104  */

105 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
106     if (hasBody()) {
107         buffer
108             .append(getBody())
109             .append(fDocument, fBodyRange[1] + 1, fSourceRange[1] - fBodyRange[1]);
110     } else {
111         buffer.append("{}").append(Util.getLineSeparator(buffer.toString(), null)); //$NON-NLS-1$
112
}
113 }
114 /**
115  * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
116  */

117 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {
118     // nothing to do
119
}
120 /**
121  * @see DOMMember#appendSimpleContents(CharArrayBuffer)
122  */

123 protected void appendSimpleContents(CharArrayBuffer buffer) {
124     // append eveything before my name
125
buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
126     // append my name
127
buffer.append(fName);
128     // append everything after my name
129
buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
130 }
131 /**
132  * @see IDOMInitializer#getBody()
133  */

134 public String JavaDoc getBody() {
135     becomeDetailed();
136     if (hasBody()) {
137         if (fBody != null) {
138             return fBody;
139         } else {
140             return new String JavaDoc(fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]);
141         }
142     } else {
143         return null;
144     }
145 }
146 /**
147  * @see DOMNode#getDetailedNode()
148  */

149 protected DOMNode getDetailedNode() {
150     return (DOMNode)getFactory().createInitializer(getContents());
151 }
152 /**
153  * @see IDOMNode#getJavaElement
154  */

155 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException JavaDoc {
156     if (parent.getElementType() == IJavaElement.TYPE) {
157         int count = 1;
158         IDOMNode previousNode = getPreviousNode();
159         while (previousNode != null) {
160             if (previousNode instanceof DOMInitializer) {
161                 count++;
162             }
163             previousNode = previousNode.getPreviousNode();
164         }
165         return ((IType) parent).getInitializer(count);
166     } else {
167         throw new IllegalArgumentException JavaDoc(Messages.element_illegalParent);
168     }
169 }
170 /**
171  * @see DOMMember#getMemberDeclarationStartPosition()
172  */

173 protected int getMemberDeclarationStartPosition() {
174     return fBodyRange[0];
175 }
176 /**
177  * @see IDOMNode#getNodeType()
178  */

179 public int getNodeType() {
180     return IDOMNode.INITIALIZER;
181 }
182 /**
183  * @see IDOMNode#isSignatureEqual(IDOMNode)
184  *
185  * <p>This method always answers false since an initializer
186  * does not have a signature.
187  */

188 public boolean isSignatureEqual(IDOMNode node) {
189     return false;
190 }
191 /**
192  * @see DOMNode
193  */

194 protected DOMNode newDOMNode() {
195     return new DOMInitializer();
196 }
197 /**
198  * Offsets all the source indexes in this node by the given amount.
199  */

200 protected void offset(int offset) {
201     super.offset(offset);
202     offsetRange(fBodyRange, offset);
203 }
204 /**
205  * @see IDOMInitializer#setBody(String)
206  */

207 public void setBody(String JavaDoc body) {
208     becomeDetailed();
209     fBody= body;
210     setHasBody(body != null);
211     fragment();
212 }
213 /**
214  * @see IDOMInitializer#setName(String)
215  */

216 public void setName(String JavaDoc name) {
217     // initializers have no name
218
}
219 /**
220  * @see DOMNode#shareContents(DOMNode)
221  */

222 protected void shareContents(DOMNode node) {
223     super.shareContents(node);
224     DOMInitializer init= (DOMInitializer)node;
225     fBody= init.fBody;
226     fBodyRange= rangeCopy(init.fBodyRange);
227 }
228 /**
229  * @see IDOMNode#toString()
230  */

231 public String JavaDoc toString() {
232     return "INITIALIZER"; //$NON-NLS-1$
233
}
234 }
235
Popular Tags