new version with WorkingCopy Management
[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.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;
20
21 /**
22  * DOMInitializer provides an implementation of IDOMInitializer.
23  *
24  * @see IDOMInitializer
25  * @see DOMNode
26  */
27 class DOMInitializer extends DOMMember implements IDOMInitializer {
28
29         /**
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>.
33          */
34         protected String fBody;
35
36         /**
37          * The original inclusive source range of the
38          * body in the document.
39          */
40         protected int[]  fBodyRange;
41
42 /**
43  * Constructs an empty initializer node.
44  */
45 DOMInitializer() {
46
47 }
48 /**
49  * Creates a new detailed INITIALIZER document fragment on the given range of the document.
50  *
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
57  *              in the document.
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
71  *              contains two -1's.
72  * @param bodyStartPosition - the position of the open brace of the body
73  *              of this initialzer.
74  */
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];
80         setHasBody(true);
81         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
82 }
83 /**
84  * Creates a new simple INITIALIZER document fragment on the given range of the document.
85  *
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
92  *              in the document.
93  * @param flags - an integer representing the modifiers for this member. The
94  *              integer can be analyzed with org.eclipse.jdt.core.Flags
95  */
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);
99         
100 }
101 /**
102  * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
103  */
104 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
105         if (hasBody()) {
106                 buffer
107                         .append(getBody())
108                         .append(fDocument, fBodyRange[1] + 1, fSourceRange[1] - fBodyRange[1]);
109         } else {
110                 buffer.append("{}").append(Util.LINE_SEPARATOR); //$NON-NLS-1$
111         }
112 }
113 /**
114  * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
115  */
116 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {}
117 /**
118  * @see DOMNode#appendSimpleContents(CharArrayBuffer)
119  */
120 protected void appendSimpleContents(CharArrayBuffer buffer) {
121         // append eveything before my name
122         buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
123         // append my name
124         buffer.append(fName);
125         // append everything after my name
126         buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
127 }
128 /**
129  * @see IDOMInitializer#getBody()
130  */
131 public String getBody() {
132         becomeDetailed();
133         if (hasBody()) {
134                 if (fBody != null) {
135                         return fBody;
136                 } else {
137                         return CharArrayOps.substring(fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]);
138                 }
139         } else {
140                 return null;
141         }
142 }
143 /**
144  * @see DOMNode#getDetailedNode()
145  */
146 //protected DOMNode getDetailedNode() {
147 //      return (DOMNode)getFactory().createInitializer(getContents());
148 //}
149 /**
150  * @see IDOMNode#getJavaElement
151  */
152 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
153 //      if (parent.getElementType() == IJavaElement.TYPE) {
154 //              int count = 1;
155 //              IDOMNode previousNode = getPreviousNode();
156 //              while (previousNode != null) {
157 //                      if (previousNode instanceof DOMInitializer) {
158 //                              count++;
159 //                      }
160 //                      previousNode = previousNode.getPreviousNode();
161 //              }
162 //              return ((IType) parent).getInitializer(count);
163 //      } else {
164                 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
165 //      }
166 }
167 /**
168  * @see DOMMember#getMemberDeclarationStartPosition()
169  */
170 protected int getMemberDeclarationStartPosition() {
171         return fBodyRange[0];
172 }
173 /**
174  * @see IDOMNode#getNodeType()
175  */
176 public int getNodeType() {
177         return IDOMNode.INITIALIZER;
178 }
179 /**
180  * @see IDOMNode#isSigantureEqual(IDOMNode).
181  *
182  * <p>This method always answers false since an initializer
183  * does not have a signature.
184  */
185 public boolean isSignatureEqual(IDOMNode node) {
186         return false;
187 }
188 /**
189  * @see DOMNode
190  */
191 protected DOMNode newDOMNode() {
192         return new DOMInitializer();
193 }
194 /**
195  * Offsets all the source indexes in this node by the given amount.
196  */
197 protected void offset(int offset) {
198         super.offset(offset);
199         offsetRange(fBodyRange, offset);
200 }
201 /**
202  * @see IDOMInitializer#setBody(char[])
203  */
204 public void setBody(String body) {
205         becomeDetailed();
206         fBody= body;
207         setHasBody(body != null);
208         fragment();
209 }
210 /**
211  * @see IDOMInitializer#setName(String)
212  */
213 public void setName(String name) {}
214 /**
215  * @see DOMNode#shareContents(DOMNode)
216  */
217 protected void shareContents(DOMNode node) {
218         super.shareContents(node);
219         DOMInitializer init= (DOMInitializer)node;
220         fBody= init.fBody;
221         fBodyRange= rangeCopy(init.fBodyRange);
222 }
223 /**
224  * @see IDOMNode#toString()
225  */
226 public String toString() {
227         return "INITIALIZER"; //$NON-NLS-1$
228 }
229 }