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
33 * first document fragment in this compilation
36 protected String fHeader;
38 * Creates a new empty COMPILATION_UNIT document fragment.
40 DOMCompilationUnit() {
41 fHeader=""; //$NON-NLS-1$
44 * Creates a new COMPILATION_UNIT on the given range of the document.
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.
53 DOMCompilationUnit(char[] document, int[] sourceRange) {
54 super(document, sourceRange, null, new int[]{-1, -1});
55 fHeader = ""; //$NON-NLS-1$
58 * @see DOMNode#appendContents(CharArrayBuffer)
60 protected void appendFragmentedContents(CharArrayBuffer buffer) {
61 buffer.append(getHeader());
62 appendContentsOfChildren(buffer);
65 * @see IDOMNode#canHaveChildren()
67 public boolean canHaveChildren() {
71 * @see IDOMCompilationUnit#getHeader()
73 public String getHeader() {
77 * @see IDOMNode#getJavaElement
79 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
80 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
81 return ((IPackageFragment)parent).getCompilationUnit(getName());
83 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
87 * @see IDOMCompilationUnit#getName()
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) {
99 if (Flags.isPublic(type.getFlags())) {
104 child= child.getNextNode();
106 if (topLevelType == null) {
107 topLevelType= firstType;
109 if (topLevelType != null) {
110 return topLevelType.getName() + ".java"; //$NON-NLS-1$
116 * @see IDOMNode#getNodeType()
118 public int getNodeType() {
119 return IDOMNode.COMPILATION_UNIT;
124 protected void initalizeHeader() {
125 DOMNode child = (DOMNode)getFirstChild();
127 int childStart = child.getStartPosition();
128 if (childStart > 1) {
129 setHeader(CharArrayOps.substring(fDocument, 0, childStart));
134 * @see IDOMNode#isAllowableChild(IDOMNode)
136 public boolean isAllowableChild(IDOMNode node) {
138 int type= node.getNodeType();
139 return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT || type == IDOMNode.TYPE;
148 protected DOMNode newDOMNode() {
149 return new DOMCompilationUnit();
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
156 void normalize(ILineStartFinder finder) {
157 super.normalize(finder);
161 * @see IDOMCompilationUnit@setHeader(String)
163 public void setHeader(String comment) {
168 * @see IDOMCompilationUnit#setName(String)
170 public void setName(String name) {}
172 * @see DOMNode#shareContents(DOMNode)
174 protected void shareContents(DOMNode node) {
175 super.shareContents(node);
176 fHeader= ((DOMCompilationUnit)node).fHeader;
179 * @see IDOMNode#toString()
181 public String toString() {
182 return "COMPILATION_UNIT: " + getName(); //$NON-NLS-1$