A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / jdom / DOMInitializer.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
12
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;
19
20 /**
21  * DOMInitializer provides an implementation of IDOMInitializer.
22  * 
23  * @see IDOMInitializer
24  * @see DOMNode
25  */
26 class DOMInitializer extends DOMMember implements IDOMInitializer {
27
28         /**
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>.
31          */
32         protected String fBody;
33
34         /**
35          * The original inclusive source range of the body in the document.
36          */
37         protected int[] fBodyRange;
38
39         /**
40          * Constructs an empty initializer node.
41          */
42         DOMInitializer() {
43
44         }
45
46         /**
47          * Creates a new detailed INITIALIZER document fragment on the given range
48          * of the document.
49          * 
50          * @param document -
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.
65          * @param flags -
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.
78          */
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];
86                 setHasBody(true);
87                 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
88         }
89
90         /**
91          * Creates a new simple INITIALIZER document fragment on the given range of
92          * the document.
93          * 
94          * @param document -
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.
103          * @param flags -
104          *            an integer representing the modifiers for this member. The
105          *            integer can be analyzed with net.sourceforge.phpdt.core.Flags
106          */
107         DOMInitializer(char[] document, int[] sourceRange, int flags) {
108                 this(document, sourceRange, new int[] { -1, -1 }, flags, new int[] {
109                                 -1, -1 }, -1);
110                 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
111
112         }
113
114         /**
115          * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
116          */
117         protected void appendMemberBodyContents(CharArrayBuffer buffer) {
118                 if (hasBody()) {
119                         buffer.append(getBody()).append(fDocument, fBodyRange[1] + 1,
120                                         fSourceRange[1] - fBodyRange[1]);
121                 } else {
122                         buffer.append("{}").append(Util.LINE_SEPARATOR); //$NON-NLS-1$
123                 }
124         }
125
126         /**
127          * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
128          */
129         protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {
130         }
131
132         /**
133          * @see DOMNode#appendSimpleContents(CharArrayBuffer)
134          */
135         protected void appendSimpleContents(CharArrayBuffer buffer) {
136                 // append eveything before my name
137                 buffer.append(fDocument, fSourceRange[0], fNameRange[0]
138                                 - fSourceRange[0]);
139                 // append my name
140                 buffer.append(fName);
141                 // append everything after my name
142                 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1]
143                                 - fNameRange[1]);
144         }
145
146         /**
147          * @see IDOMInitializer#getBody()
148          */
149         public String getBody() {
150                 becomeDetailed();
151                 if (hasBody()) {
152                         if (fBody != null) {
153                                 return fBody;
154                         } else {
155                                 return CharArrayOps.substring(fDocument, fBodyRange[0],
156                                                 fBodyRange[1] + 1 - fBodyRange[0]);
157                         }
158                 } else {
159                         return null;
160                 }
161         }
162
163         /**
164          * @see DOMNode#getDetailedNode()
165          */
166         // protected DOMNode getDetailedNode() {
167         // return (DOMNode)getFactory().createInitializer(getContents());
168         // }
169         /**
170          * @see IDOMNode#getJavaElement
171          */
172         public IJavaElement getJavaElement(IJavaElement parent)
173                         throws IllegalArgumentException {
174                 // if (parent.getElementType() == IJavaElement.TYPE) {
175                 // int count = 1;
176                 // IDOMNode previousNode = getPreviousNode();
177                 // while (previousNode != null) {
178                 // if (previousNode instanceof DOMInitializer) {
179                 // count++;
180                 // }
181                 // previousNode = previousNode.getPreviousNode();
182                 // }
183                 // return ((IType) parent).getInitializer(count);
184                 // } else {
185                 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
186                 // }
187         }
188
189         /**
190          * @see DOMMember#getMemberDeclarationStartPosition()
191          */
192         protected int getMemberDeclarationStartPosition() {
193                 return fBodyRange[0];
194         }
195
196         /**
197          * @see IDOMNode#getNodeType()
198          */
199         public int getNodeType() {
200                 return IDOMNode.INITIALIZER;
201         }
202
203         /**
204          * @see IDOMNode#isSigantureEqual(IDOMNode).
205          * 
206          * <p>
207          * This method always answers false since an initializer does not have a
208          * signature.
209          */
210         public boolean isSignatureEqual(IDOMNode node) {
211                 return false;
212         }
213
214         /**
215          * @see DOMNode
216          */
217         protected DOMNode newDOMNode() {
218                 return new DOMInitializer();
219         }
220
221         /**
222          * Offsets all the source indexes in this node by the given amount.
223          */
224         protected void offset(int offset) {
225                 super.offset(offset);
226                 offsetRange(fBodyRange, offset);
227         }
228
229         /**
230          * @see IDOMInitializer#setBody(char[])
231          */
232         public void setBody(String body) {
233                 becomeDetailed();
234                 fBody = body;
235                 setHasBody(body != null);
236                 fragment();
237         }
238
239         /**
240          * @see IDOMInitializer#setName(String)
241          */
242         public void setName(String name) {
243         }
244
245         /**
246          * @see DOMNode#shareContents(DOMNode)
247          */
248         protected void shareContents(DOMNode node) {
249                 super.shareContents(node);
250                 DOMInitializer init = (DOMInitializer) node;
251                 fBody = init.fBody;
252                 fBodyRange = rangeCopy(init.fBodyRange);
253         }
254
255         /**
256          * @see IDOMNode#toString()
257          */
258         public String toString() {
259                 return "INITIALIZER"; //$NON-NLS-1$
260         }
261 }