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