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 / DOMCompilationUnit.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.Flags;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IPackageFragment;
16 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
17 import net.sourceforge.phpdt.core.jdom.IDOMNode;
18 import net.sourceforge.phpdt.core.jdom.IDOMType;
19 import net.sourceforge.phpdt.internal.compiler.util.Util;
20 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
21 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
22
23 /**
24  * DOMCompilation unit provides an implementation of IDOMCompilationUnit.
25  * 
26  * @see IDOMCompilationUnit
27  * @see DOMNode
28  */
29 class DOMCompilationUnit extends DOMNode implements IDOMCompilationUnit {
30
31         /**
32          * The comment and/or whitespace preceding the first document fragment in
33          * this compilation unit.
34          */
35         protected String fHeader;
36
37         /**
38          * Creates a new empty COMPILATION_UNIT document fragment.
39          */
40         DOMCompilationUnit() {
41                 fHeader = ""; //$NON-NLS-1$
42         }
43
44         /**
45          * Creates a new COMPILATION_UNIT on the given range of the document.
46          * 
47          * @param document -
48          *            the document containing this node's original contents
49          * @param sourceRange -
50          *            a two element array of integers describing the entire
51          *            inclusive source range of this node within its document. A
52          *            compilation unit's source range is the entire document - the
53          *            first integer is zero, and the second integer is the position
54          *            of the last character in the document.
55          */
56         DOMCompilationUnit(char[] document, int[] sourceRange) {
57                 super(document, sourceRange, null, new int[] { -1, -1 });
58                 fHeader = ""; //$NON-NLS-1$
59         }
60
61         /**
62          * @see DOMNode#appendContents(CharArrayBuffer)
63          */
64         protected void appendFragmentedContents(CharArrayBuffer buffer) {
65                 buffer.append(getHeader());
66                 appendContentsOfChildren(buffer);
67         }
68
69         /**
70          * @see IDOMNode#canHaveChildren()
71          */
72         public boolean canHaveChildren() {
73                 return true;
74         }
75
76         /**
77          * @see IDOMCompilationUnit#getHeader()
78          */
79         public String getHeader() {
80                 return fHeader;
81         }
82
83         /**
84          * @see IDOMNode#getJavaElement
85          */
86         public IJavaElement getJavaElement(IJavaElement parent)
87                         throws IllegalArgumentException {
88                 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
89                         return ((IPackageFragment) parent).getCompilationUnit(getName());
90                 } else {
91                         throw new IllegalArgumentException(Util
92                                         .bind("element.illegalParent")); //$NON-NLS-1$
93                 }
94         }
95
96         /**
97          * @see IDOMCompilationUnit#getName()
98          */
99         public String getName() {
100                 IDOMType topLevelType = null;
101                 IDOMType firstType = null;
102                 IDOMNode child = fFirstChild;
103                 while (child != null) {
104                         if (child.getNodeType() == IDOMNode.TYPE) {
105                                 IDOMType type = (IDOMType) child;
106                                 if (firstType == null) {
107                                         firstType = type;
108                                 }
109                                 if (Flags.isPublic(type.getFlags())) {
110                                         topLevelType = type;
111                                         break;
112                                 }
113                         }
114                         child = child.getNextNode();
115                 }
116                 if (topLevelType == null) {
117                         topLevelType = firstType;
118                 }
119                 if (topLevelType != null) {
120                         return topLevelType.getName() + ".java"; //$NON-NLS-1$
121                 } else {
122                         return null;
123                 }
124         }
125
126         /**
127          * @see IDOMNode#getNodeType()
128          */
129         public int getNodeType() {
130                 return IDOMNode.COMPILATION_UNIT;
131         }
132
133         /**
134          * Sets the header
135          */
136         protected void initalizeHeader() {
137                 DOMNode child = (DOMNode) getFirstChild();
138                 if (child != null) {
139                         int childStart = child.getStartPosition();
140                         if (childStart > 1) {
141                                 setHeader(CharArrayOps.substring(fDocument, 0, childStart));
142                         }
143                 }
144         }
145
146         /**
147          * @see IDOMNode#isAllowableChild(IDOMNode)
148          */
149         public boolean isAllowableChild(IDOMNode node) {
150                 if (node != null) {
151                         int type = node.getNodeType();
152                         return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT
153                                         || type == IDOMNode.TYPE;
154                 } else {
155                         return false;
156                 }
157
158         }
159
160         /**
161          * @see DOMNode
162          */
163         protected DOMNode newDOMNode() {
164                 return new DOMCompilationUnit();
165         }
166
167         /**
168          * Normalizes this <code>DOMNode</code>'s source positions to include
169          * whitespace preceeding the node on the line on which the node starts, and
170          * all whitespace after the node up to the next node's start
171          */
172         void normalize(ILineStartFinder finder) {
173                 super.normalize(finder);
174                 initalizeHeader();
175         }
176
177         /**
178          * @see IDOMCompilationUnit@setHeader(String)
179          */
180         public void setHeader(String comment) {
181                 fHeader = comment;
182                 fragment();
183         }
184
185         /**
186          * @see IDOMCompilationUnit#setName(String)
187          */
188         public void setName(String name) {
189         }
190
191         /**
192          * @see DOMNode#shareContents(DOMNode)
193          */
194         protected void shareContents(DOMNode node) {
195                 super.shareContents(node);
196                 fHeader = ((DOMCompilationUnit) node).fHeader;
197         }
198
199         /**
200          * @see IDOMNode#toString()
201          */
202         public String toString() {
203                 return "COMPILATION_UNIT: " + getName(); //$NON-NLS-1$
204         }
205 }