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 body has been altered
30 * from the contents in the document, otherwise <code>null</code>.
32 protected String fBody;
35 * The original inclusive source range of the body in the document.
37 protected int[] fBodyRange;
40 * Constructs an empty initializer node.
47 * Creates a new detailed INITIALIZER document fragment on the given range
51 * the document containing this node's original contents
52 * @param sourceRange -
53 * a two element array of integers describing the entire
54 * inclusive source range of this node within its document.
55 * Contents start on and include the character at the first
56 * position. Contents end on and include the character at the
57 * last position. An array of -1's indicates this node's contents
58 * do not exist in the document.
59 * @param commentRange -
60 * a two element array describing the comments that precede the
61 * member declaration. The first matches the start of this node's
62 * sourceRange, and the second is the new-line or first
63 * non-whitespace character following the last comment. If no
64 * comments are present, this array contains two -1's.
66 * an integer representing the modifiers for this member. The
67 * integer can be analyzed with net.sourceforge.phpdt.core.Flags
68 * @param modifierRange -
69 * a two element array describing the location of modifiers for
70 * this member within its source range. The first integer is the
71 * first character of the first modifier for this member, and the
72 * second integer is the last whitespace character preceeding the
73 * next part of this member declaration. If there are no
74 * modifiers present in this node's source code (that is, package
75 * default visibility), this array contains two -1's.
76 * @param bodyStartPosition -
77 * the position of the open brace of the body of this initialzer.
79 DOMInitializer(char[] document, int[] sourceRange, int[] commentRange,
80 int flags, int[] modifierRange, int bodyStartPosition) {
81 super(document, sourceRange, null, new int[] { -1, -1 }, commentRange,
82 flags, modifierRange);
83 fBodyRange = new int[2];
84 fBodyRange[0] = bodyStartPosition;
85 fBodyRange[1] = sourceRange[1];
87 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
91 * Creates a new simple INITIALIZER document fragment on the given range of
95 * the document containing this node's original contents
96 * @param sourceRange -
97 * a two element array of integers describing the entire
98 * inclusive source range of this node within its document.
99 * Contents start on and include the character at the first
100 * position. Contents end on and include the character at the
101 * last position. An array of -1's indicates this node's contents
102 * do not exist in the document.
104 * an integer representing the modifiers for this member. The
105 * integer can be analyzed with net.sourceforge.phpdt.core.Flags
107 DOMInitializer(char[] document, int[] sourceRange, int flags) {
108 this(document, sourceRange, new int[] { -1, -1 }, flags, new int[] {
110 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
115 * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
117 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
119 buffer.append(getBody()).append(fDocument, fBodyRange[1] + 1,
120 fSourceRange[1] - fBodyRange[1]);
122 buffer.append("{}").append(Util.LINE_SEPARATOR); //$NON-NLS-1$
127 * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
129 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {
133 * @see DOMNode#appendSimpleContents(CharArrayBuffer)
135 protected void appendSimpleContents(CharArrayBuffer buffer) {
136 // append eveything before my name
137 buffer.append(fDocument, fSourceRange[0], fNameRange[0]
140 buffer.append(fName);
141 // append everything after my name
142 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1]
147 * @see IDOMInitializer#getBody()
149 public String getBody() {
155 return CharArrayOps.substring(fDocument, fBodyRange[0],
156 fBodyRange[1] + 1 - fBodyRange[0]);
164 * @see DOMNode#getDetailedNode()
166 // protected DOMNode getDetailedNode() {
167 // return (DOMNode)getFactory().createInitializer(getContents());
170 * @see IDOMNode#getJavaElement
172 public IJavaElement getJavaElement(IJavaElement parent)
173 throws IllegalArgumentException {
174 // if (parent.getElementType() == IJavaElement.TYPE) {
176 // IDOMNode previousNode = getPreviousNode();
177 // while (previousNode != null) {
178 // if (previousNode instanceof DOMInitializer) {
181 // previousNode = previousNode.getPreviousNode();
183 // return ((IType) parent).getInitializer(count);
185 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
190 * @see DOMMember#getMemberDeclarationStartPosition()
192 protected int getMemberDeclarationStartPosition() {
193 return fBodyRange[0];
197 * @see IDOMNode#getNodeType()
199 public int getNodeType() {
200 return IDOMNode.INITIALIZER;
204 * @see IDOMNode#isSigantureEqual(IDOMNode).
207 * This method always answers false since an initializer does not have a
210 public boolean isSignatureEqual(IDOMNode node) {
217 protected DOMNode newDOMNode() {
218 return new DOMInitializer();
222 * Offsets all the source indexes in this node by the given amount.
224 protected void offset(int offset) {
225 super.offset(offset);
226 offsetRange(fBodyRange, offset);
230 * @see IDOMInitializer#setBody(char[])
232 public void setBody(String body) {
235 setHasBody(body != null);
240 * @see IDOMInitializer#setName(String)
242 public void setName(String name) {
246 * @see DOMNode#shareContents(DOMNode)
248 protected void shareContents(DOMNode node) {
249 super.shareContents(node);
250 DOMInitializer init = (DOMInitializer) node;
252 fBodyRange = rangeCopy(init.fBodyRange);
256 * @see IDOMNode#toString()
258 public String toString() {
259 return "INITIALIZER"; //$NON-NLS-1$