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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
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;
24 * DOMCompilation unit provides an implementation of IDOMCompilationUnit.
26 * @see IDOMCompilationUnit
29 class DOMCompilationUnit extends DOMNode implements IDOMCompilationUnit {
32 * The comment and/or whitespace preceding the first document fragment in
33 * this compilation unit.
35 protected String fHeader;
38 * Creates a new empty COMPILATION_UNIT document fragment.
40 DOMCompilationUnit() {
41 fHeader = ""; //$NON-NLS-1$
45 * Creates a new COMPILATION_UNIT on the given range of the 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.
56 DOMCompilationUnit(char[] document, int[] sourceRange) {
57 super(document, sourceRange, null, new int[] { -1, -1 });
58 fHeader = ""; //$NON-NLS-1$
62 * @see DOMNode#appendContents(CharArrayBuffer)
64 protected void appendFragmentedContents(CharArrayBuffer buffer) {
65 buffer.append(getHeader());
66 appendContentsOfChildren(buffer);
70 * @see IDOMNode#canHaveChildren()
72 public boolean canHaveChildren() {
77 * @see IDOMCompilationUnit#getHeader()
79 public String getHeader() {
84 * @see IDOMNode#getJavaElement
86 public IJavaElement getJavaElement(IJavaElement parent)
87 throws IllegalArgumentException {
88 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
89 return ((IPackageFragment) parent).getCompilationUnit(getName());
91 throw new IllegalArgumentException(Util
92 .bind("element.illegalParent")); //$NON-NLS-1$
97 * @see IDOMCompilationUnit#getName()
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) {
109 if (Flags.isPublic(type.getFlags())) {
114 child = child.getNextNode();
116 if (topLevelType == null) {
117 topLevelType = firstType;
119 if (topLevelType != null) {
120 return topLevelType.getName() + ".java"; //$NON-NLS-1$
127 * @see IDOMNode#getNodeType()
129 public int getNodeType() {
130 return IDOMNode.COMPILATION_UNIT;
136 protected void initalizeHeader() {
137 DOMNode child = (DOMNode) getFirstChild();
139 int childStart = child.getStartPosition();
140 if (childStart > 1) {
141 setHeader(CharArrayOps.substring(fDocument, 0, childStart));
147 * @see IDOMNode#isAllowableChild(IDOMNode)
149 public boolean isAllowableChild(IDOMNode node) {
151 int type = node.getNodeType();
152 return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT
153 || type == IDOMNode.TYPE;
163 protected DOMNode newDOMNode() {
164 return new DOMCompilationUnit();
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
172 void normalize(ILineStartFinder finder) {
173 super.normalize(finder);
178 * @see IDOMCompilationUnit@setHeader(String)
180 public void setHeader(String comment) {
186 * @see IDOMCompilationUnit#setName(String)
188 public void setName(String name) {
192 * @see DOMNode#shareContents(DOMNode)
194 protected void shareContents(DOMNode node) {
195 super.shareContents(node);
196 fHeader = ((DOMCompilationUnit) node).fHeader;
200 * @see IDOMNode#toString()
202 public String toString() {
203 return "COMPILATION_UNIT: " + getName(); //$NON-NLS-1$