KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > jdom > DOMCompilationUnit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.jdom;
12
13 import org.eclipse.jdt.core.Flags;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IPackageFragment;
16 import org.eclipse.jdt.core.jdom.*;
17 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
18 import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
19 import org.eclipse.jdt.internal.core.util.Messages;
20 import org.eclipse.jdt.internal.core.util.Util;
21 /**
22  * DOMCompilation unit provides an implementation of IDOMCompilationUnit.
23  *
24  * @see IDOMCompilationUnit
25  * @see DOMNode
26  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
27  * powerful, fine-grained DOM/AST API found in the
28  * org.eclipse.jdt.core.dom package.
29  */

30 class DOMCompilationUnit extends DOMNode implements IDOMCompilationUnit, SuffixConstants {
31
32     /**
33      * The comment and/or whitespace preceding the
34      * first document fragment in this compilation
35      * unit.
36      */

37     protected String JavaDoc fHeader;
38 /**
39  * Creates a new empty COMPILATION_UNIT document fragment.
40  */

41 DOMCompilationUnit() {
42     fHeader=""; //$NON-NLS-1$
43
}
44 /**
45  * Creates a new COMPILATION_UNIT on the given range of the document.
46  *
47  * @param document - the document containing this node's original contents
48  * @param sourceRange - a two element array of integers describing the
49  * entire inclusive source range of this node within its document.
50  * A compilation unit's source range is the entire document -
51  * the first integer is zero, and the second integer is the position
52  * of the last character in the document.
53  */

54 DOMCompilationUnit(char[] document, int[] sourceRange) {
55     super(document, sourceRange, null, new int[]{-1, -1});
56     fHeader = ""; //$NON-NLS-1$
57
}
58 /**
59  * @see DOMNode#appendContents(CharArrayBuffer)
60  */

61 protected void appendFragmentedContents(CharArrayBuffer buffer) {
62     buffer.append(getHeader());
63     appendContentsOfChildren(buffer);
64 }
65 /**
66  * @see IDOMNode#canHaveChildren()
67  */

68 public boolean canHaveChildren() {
69     return true;
70 }
71 /**
72  * @see IDOMCompilationUnit#getHeader()
73  */

74 public String JavaDoc getHeader() {
75     return fHeader;
76 }
77 /**
78  * @see IDOMNode#getJavaElement
79  */

80 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException JavaDoc {
81     if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
82         return ((IPackageFragment)parent).getCompilationUnit(getName());
83     } else {
84         throw new IllegalArgumentException JavaDoc(Messages.element_illegalParent);
85     }
86 }
87 /**
88  * @see IDOMCompilationUnit#getName()
89  */

90 public String JavaDoc getName() {
91     IDOMType topLevelType= null;
92     IDOMType firstType= null;
93     IDOMNode child= fFirstChild;
94     while (child != null) {
95         if (child.getNodeType() == IDOMNode.TYPE) {
96             IDOMType type= (IDOMType)child;
97             if (firstType == null) {
98                 firstType= type;
99             }
100             if (Flags.isPublic(type.getFlags())) {
101                 topLevelType= type;
102                 break;
103             }
104         }
105         child= child.getNextNode();
106     }
107     if (topLevelType == null) {
108         topLevelType= firstType;
109     }
110     if (topLevelType != null) {
111         return topLevelType.getName() + Util.defaultJavaExtension();
112     } else {
113         return null;
114     }
115 }
116 /**
117  * @see IDOMNode#getNodeType()
118  */

119 public int getNodeType() {
120     return IDOMNode.COMPILATION_UNIT;
121 }
122 /**
123  * Sets the header
124  */

125 protected void initalizeHeader() {
126     DOMNode child = (DOMNode)getFirstChild();
127     if (child != null) {
128         int childStart = child.getStartPosition();
129         if (childStart > 1) {
130             setHeader(new String JavaDoc(fDocument, 0, childStart));
131         }
132     }
133 }
134 /**
135  * @see IDOMNode#isAllowableChild(IDOMNode)
136  */

137 public boolean isAllowableChild(IDOMNode node) {
138     if (node != null) {
139         int type= node.getNodeType();
140         return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT || type == IDOMNode.TYPE;
141     } else {
142         return false;
143     }
144     
145 }
146 /**
147  * @see DOMNode
148  */

149 protected DOMNode newDOMNode() {
150     return new DOMCompilationUnit();
151 }
152 /**
153  * Normalizes this <code>DOMNode</code>'s source positions to include whitespace preceeding
154  * the node on the line on which the node starts, and all whitespace after the node up to
155  * the next node's start
156  */

157 void normalize(ILineStartFinder finder) {
158     super.normalize(finder);
159     initalizeHeader();
160 }
161 /**
162  * @see IDOMCompilationUnit#setHeader(String)
163  */

164 public void setHeader(String JavaDoc comment) {
165     fHeader= comment;
166     fragment();
167 }
168 /**
169  * @see IDOMCompilationUnit#setName(String)
170  */

171 public void setName(String JavaDoc name) {
172     // nothing to do
173
}
174 /**
175  * @see DOMNode#shareContents(DOMNode)
176  */

177 protected void shareContents(DOMNode node) {
178     super.shareContents(node);
179     fHeader= ((DOMCompilationUnit)node).fHeader;
180 }
181 /**
182  * @see IDOMNode#toString()
183  */

184 public String JavaDoc toString() {
185     return "COMPILATION_UNIT: " + getName(); //$NON-NLS-1$
186
}
187 }
188
Popular Tags