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.jdom.IDOMInitializer;
15 import net.sourceforge.phpdt.core.jdom.IDOMNode;
16 import net.sourceforge.phpdt.internal.compiler.util.Util;
17 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
18 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
21 * DOMInitializer provides an implementation of IDOMInitializer.
23 * @see IDOMInitializer
26 class DOMInitializer extends DOMMember implements IDOMInitializer {
29 * The contents of the initializer's body when the
30 * body has been altered from the contents in the
31 * document, otherwise <code>null</code>.
33 protected String fBody;
36 * The original inclusive source range of the
37 * body in the document.
39 protected int[] fBodyRange;
42 * Constructs an empty initializer node.
48 * Creates a new detailed INITIALIZER document fragment on the given range of the document.
50 * @param document - the document containing this node's original contents
51 * @param sourceRange - a two element array of integers describing the
52 * entire inclusive source range of this node within its document.
53 * Contents start on and include the character at the first position.
54 * Contents end on and include the character at the last position.
55 * An array of -1's indicates this node's contents do not exist
57 * @param commentRange - a two element array describing the comments that precede
58 * the member declaration. The first matches the start of this node's
59 * sourceRange, and the second is the new-line or first non-whitespace
60 * character following the last comment. If no comments are present,
61 * this array contains two -1's.
62 * @param flags - an integer representing the modifiers for this member. The
63 * integer can be analyzed with org.eclipse.jdt.core.Flags
64 * @param modifierRange - a two element array describing the location of
65 * modifiers for this member within its source range. The first integer
66 * is the first character of the first modifier for this member, and
67 * the second integer is the last whitespace character preceeding the
68 * next part of this member declaration. If there are no modifiers present
69 * in this node's source code (that is, package default visibility), this array
71 * @param bodyStartPosition - the position of the open brace of the body
74 DOMInitializer(char[] document, int[] sourceRange, int[] commentRange, int flags, int[] modifierRange, int bodyStartPosition) {
75 super(document, sourceRange, null, new int[]{-1, -1}, commentRange, flags, modifierRange);
76 fBodyRange= new int[2];
77 fBodyRange[0]= bodyStartPosition;
78 fBodyRange[1]= sourceRange[1];
80 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
83 * Creates a new simple INITIALIZER document fragment on the given range of the document.
85 * @param document - the document containing this node's original contents
86 * @param sourceRange - a two element array of integers describing the
87 * entire inclusive source range of this node within its document.
88 * Contents start on and include the character at the first position.
89 * Contents end on and include the character at the last position.
90 * An array of -1's indicates this node's contents do not exist
92 * @param flags - an integer representing the modifiers for this member. The
93 * integer can be analyzed with org.eclipse.jdt.core.Flags
95 DOMInitializer(char[] document, int[] sourceRange, int flags) {
96 this(document, sourceRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, -1);
97 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
101 * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
103 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
107 .append(fDocument, fBodyRange[1] + 1, fSourceRange[1] - fBodyRange[1]);
109 buffer.append("{}").append(Util.LINE_SEPARATOR); //$NON-NLS-1$
113 * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
115 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {}
117 * @see DOMNode#appendSimpleContents(CharArrayBuffer)
119 protected void appendSimpleContents(CharArrayBuffer buffer) {
120 // append eveything before my name
121 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
123 buffer.append(fName);
124 // append everything after my name
125 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
128 * @see IDOMInitializer#getBody()
130 public String getBody() {
136 return CharArrayOps.substring(fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]);
143 * @see DOMNode#getDetailedNode()
145 //protected DOMNode getDetailedNode() {
146 // return (DOMNode)getFactory().createInitializer(getContents());
149 * @see IDOMNode#getJavaElement
151 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
152 // if (parent.getElementType() == IJavaElement.TYPE) {
154 // IDOMNode previousNode = getPreviousNode();
155 // while (previousNode != null) {
156 // if (previousNode instanceof DOMInitializer) {
159 // previousNode = previousNode.getPreviousNode();
161 // return ((IType) parent).getInitializer(count);
163 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
167 * @see DOMMember#getMemberDeclarationStartPosition()
169 protected int getMemberDeclarationStartPosition() {
170 return fBodyRange[0];
173 * @see IDOMNode#getNodeType()
175 public int getNodeType() {
176 return IDOMNode.INITIALIZER;
179 * @see IDOMNode#isSigantureEqual(IDOMNode).
181 * <p>This method always answers false since an initializer
182 * does not have a signature.
184 public boolean isSignatureEqual(IDOMNode node) {
190 protected DOMNode newDOMNode() {
191 return new DOMInitializer();
194 * Offsets all the source indexes in this node by the given amount.
196 protected void offset(int offset) {
197 super.offset(offset);
198 offsetRange(fBodyRange, offset);
201 * @see IDOMInitializer#setBody(char[])
203 public void setBody(String body) {
206 setHasBody(body != null);
210 * @see IDOMInitializer#setName(String)
212 public void setName(String name) {}
214 * @see DOMNode#shareContents(DOMNode)
216 protected void shareContents(DOMNode node) {
217 super.shareContents(node);
218 DOMInitializer init= (DOMInitializer)node;
220 fBodyRange= rangeCopy(init.fBodyRange);
223 * @see IDOMNode#toString()
225 public String toString() {
226 return "INITIALIZER"; //$NON-NLS-1$