1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IType;
15 import net.sourceforge.phpdt.core.jdom.IDOMInitializer;
16 import net.sourceforge.phpdt.core.jdom.IDOMNode;
17 import net.sourceforge.phpdt.internal.compiler.util.Util;
18 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
19 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
22 * DOMInitializer provides an implementation of IDOMInitializer.
24 * @see IDOMInitializer
27 class DOMInitializer extends DOMMember implements IDOMInitializer {
30 * The contents of the initializer's body when the
31 * body has been altered from the contents in the
32 * document, otherwise <code>null</code>.
34 protected String fBody;
37 * The original inclusive source range of the
38 * body in the document.
40 protected int[] fBodyRange;
43 * Constructs an empty initializer node.
49 * Creates a new detailed INITIALIZER document fragment on the given range of the document.
51 * @param document - the document containing this node's original contents
52 * @param sourceRange - a two element array of integers describing the
53 * entire inclusive source range of this node within its document.
54 * Contents start on and include the character at the first position.
55 * Contents end on and include the character at the last position.
56 * An array of -1's indicates this node's contents do not exist
58 * @param commentRange - a two element array describing the comments that precede
59 * the member declaration. The first matches the start of this node's
60 * sourceRange, and the second is the new-line or first non-whitespace
61 * character following the last comment. If no comments are present,
62 * this array contains two -1's.
63 * @param flags - an integer representing the modifiers for this member. The
64 * integer can be analyzed with org.eclipse.jdt.core.Flags
65 * @param modifierRange - a two element array describing the location of
66 * modifiers for this member within its source range. The first integer
67 * is the first character of the first modifier for this member, and
68 * the second integer is the last whitespace character preceeding the
69 * next part of this member declaration. If there are no modifiers present
70 * in this node's source code (that is, package default visibility), this array
72 * @param bodyStartPosition - the position of the open brace of the body
75 DOMInitializer(char[] document, int[] sourceRange, int[] commentRange, int flags, int[] modifierRange, int bodyStartPosition) {
76 super(document, sourceRange, null, new int[]{-1, -1}, commentRange, flags, modifierRange);
77 fBodyRange= new int[2];
78 fBodyRange[0]= bodyStartPosition;
79 fBodyRange[1]= sourceRange[1];
81 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
84 * Creates a new simple INITIALIZER document fragment on the given range of the document.
86 * @param document - the document containing this node's original contents
87 * @param sourceRange - a two element array of integers describing the
88 * entire inclusive source range of this node within its document.
89 * Contents start on and include the character at the first position.
90 * Contents end on and include the character at the last position.
91 * An array of -1's indicates this node's contents do not exist
93 * @param flags - an integer representing the modifiers for this member. The
94 * integer can be analyzed with org.eclipse.jdt.core.Flags
96 DOMInitializer(char[] document, int[] sourceRange, int flags) {
97 this(document, sourceRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, -1);
98 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
102 * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
104 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
108 .append(fDocument, fBodyRange[1] + 1, fSourceRange[1] - fBodyRange[1]);
110 buffer.append("{}").append(Util.LINE_SEPARATOR); //$NON-NLS-1$
114 * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
116 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {}
118 * @see DOMNode#appendSimpleContents(CharArrayBuffer)
120 protected void appendSimpleContents(CharArrayBuffer buffer) {
121 // append eveything before my name
122 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
124 buffer.append(fName);
125 // append everything after my name
126 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
129 * @see IDOMInitializer#getBody()
131 public String getBody() {
137 return CharArrayOps.substring(fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]);
144 * @see DOMNode#getDetailedNode()
146 //protected DOMNode getDetailedNode() {
147 // return (DOMNode)getFactory().createInitializer(getContents());
150 * @see IDOMNode#getJavaElement
152 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
153 // if (parent.getElementType() == IJavaElement.TYPE) {
155 // IDOMNode previousNode = getPreviousNode();
156 // while (previousNode != null) {
157 // if (previousNode instanceof DOMInitializer) {
160 // previousNode = previousNode.getPreviousNode();
162 // return ((IType) parent).getInitializer(count);
164 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
168 * @see DOMMember#getMemberDeclarationStartPosition()
170 protected int getMemberDeclarationStartPosition() {
171 return fBodyRange[0];
174 * @see IDOMNode#getNodeType()
176 public int getNodeType() {
177 return IDOMNode.INITIALIZER;
180 * @see IDOMNode#isSigantureEqual(IDOMNode).
182 * <p>This method always answers false since an initializer
183 * does not have a signature.
185 public boolean isSignatureEqual(IDOMNode node) {
191 protected DOMNode newDOMNode() {
192 return new DOMInitializer();
195 * Offsets all the source indexes in this node by the given amount.
197 protected void offset(int offset) {
198 super.offset(offset);
199 offsetRange(fBodyRange, offset);
202 * @see IDOMInitializer#setBody(char[])
204 public void setBody(String body) {
207 setHasBody(body != null);
211 * @see IDOMInitializer#setName(String)
213 public void setName(String name) {}
215 * @see DOMNode#shareContents(DOMNode)
217 protected void shareContents(DOMNode node) {
218 super.shareContents(node);
219 DOMInitializer init= (DOMInitializer)node;
221 fBodyRange= rangeCopy(init.fBodyRange);
224 * @see IDOMNode#toString()
226 public String toString() {
227 return "INITIALIZER"; //$NON-NLS-1$