001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.jdom;
011:
012: import org.eclipse.jdt.core.Flags;
013: import org.eclipse.jdt.core.IJavaElement;
014: import org.eclipse.jdt.core.IPackageFragment;
015: import org.eclipse.jdt.core.jdom.*;
016: import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
017: import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
018: import org.eclipse.jdt.internal.core.util.Messages;
019: import org.eclipse.jdt.internal.core.util.Util;
020:
021: /**
022: * DOMCompilation unit provides an implementation of IDOMCompilationUnit.
023: *
024: * @see IDOMCompilationUnit
025: * @see DOMNode
026: * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
027: * powerful, fine-grained DOM/AST API found in the
028: * org.eclipse.jdt.core.dom package.
029: */
030: class DOMCompilationUnit extends DOMNode implements
031: IDOMCompilationUnit, SuffixConstants {
032:
033: /**
034: * The comment and/or whitespace preceding the
035: * first document fragment in this compilation
036: * unit.
037: */
038: protected String fHeader;
039:
040: /**
041: * Creates a new empty COMPILATION_UNIT document fragment.
042: */
043: DOMCompilationUnit() {
044: fHeader = ""; //$NON-NLS-1$
045: }
046:
047: /**
048: * Creates a new COMPILATION_UNIT on the given range of the document.
049: *
050: * @param document - the document containing this node's original contents
051: * @param sourceRange - a two element array of integers describing the
052: * entire inclusive source range of this node within its document.
053: * A compilation unit's source range is the entire document -
054: * the first integer is zero, and the second integer is the position
055: * of the last character in the document.
056: */
057: DOMCompilationUnit(char[] document, int[] sourceRange) {
058: super (document, sourceRange, null, new int[] { -1, -1 });
059: fHeader = ""; //$NON-NLS-1$
060: }
061:
062: /**
063: * @see DOMNode#appendContents(CharArrayBuffer)
064: */
065: protected void appendFragmentedContents(CharArrayBuffer buffer) {
066: buffer.append(getHeader());
067: appendContentsOfChildren(buffer);
068: }
069:
070: /**
071: * @see IDOMNode#canHaveChildren()
072: */
073: public boolean canHaveChildren() {
074: return true;
075: }
076:
077: /**
078: * @see IDOMCompilationUnit#getHeader()
079: */
080: public String getHeader() {
081: return fHeader;
082: }
083:
084: /**
085: * @see IDOMNode#getJavaElement
086: */
087: public IJavaElement getJavaElement(IJavaElement parent)
088: throws IllegalArgumentException {
089: if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
090: return ((IPackageFragment) parent)
091: .getCompilationUnit(getName());
092: } else {
093: throw new IllegalArgumentException(
094: Messages.element_illegalParent);
095: }
096: }
097:
098: /**
099: * @see IDOMCompilationUnit#getName()
100: */
101: public String getName() {
102: IDOMType topLevelType = null;
103: IDOMType firstType = null;
104: IDOMNode child = fFirstChild;
105: while (child != null) {
106: if (child.getNodeType() == IDOMNode.TYPE) {
107: IDOMType type = (IDOMType) child;
108: if (firstType == null) {
109: firstType = type;
110: }
111: if (Flags.isPublic(type.getFlags())) {
112: topLevelType = type;
113: break;
114: }
115: }
116: child = child.getNextNode();
117: }
118: if (topLevelType == null) {
119: topLevelType = firstType;
120: }
121: if (topLevelType != null) {
122: return topLevelType.getName() + Util.defaultJavaExtension();
123: } else {
124: return null;
125: }
126: }
127:
128: /**
129: * @see IDOMNode#getNodeType()
130: */
131: public int getNodeType() {
132: return IDOMNode.COMPILATION_UNIT;
133: }
134:
135: /**
136: * Sets the header
137: */
138: protected void initalizeHeader() {
139: DOMNode child = (DOMNode) getFirstChild();
140: if (child != null) {
141: int childStart = child.getStartPosition();
142: if (childStart > 1) {
143: setHeader(new String(fDocument, 0, childStart));
144: }
145: }
146: }
147:
148: /**
149: * @see IDOMNode#isAllowableChild(IDOMNode)
150: */
151: public boolean isAllowableChild(IDOMNode node) {
152: if (node != null) {
153: int type = node.getNodeType();
154: return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT
155: || type == IDOMNode.TYPE;
156: } else {
157: return false;
158: }
159:
160: }
161:
162: /**
163: * @see DOMNode
164: */
165: protected DOMNode newDOMNode() {
166: return new DOMCompilationUnit();
167: }
168:
169: /**
170: * Normalizes this <code>DOMNode</code>'s source positions to include whitespace preceeding
171: * the node on the line on which the node starts, and all whitespace after the node up to
172: * the next node's start
173: */
174: void normalize(ILineStartFinder finder) {
175: super .normalize(finder);
176: initalizeHeader();
177: }
178:
179: /**
180: * @see IDOMCompilationUnit#setHeader(String)
181: */
182: public void setHeader(String comment) {
183: fHeader = comment;
184: fragment();
185: }
186:
187: /**
188: * @see IDOMCompilationUnit#setName(String)
189: */
190: public void setName(String name) {
191: // nothing to do
192: }
193:
194: /**
195: * @see DOMNode#shareContents(DOMNode)
196: */
197: protected void shareContents(DOMNode node) {
198: super .shareContents(node);
199: fHeader = ((DOMCompilationUnit) node).fHeader;
200: }
201:
202: /**
203: * @see IDOMNode#toString()
204: */
205: public String toString() {
206: return "COMPILATION_UNIT: " + getName(); //$NON-NLS-1$
207: }
208: }
|