improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / jdom / DOMImport.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.IJavaElement;
14 import net.sourceforge.phpdt.core.jdom.IDOMImport;
15 import net.sourceforge.phpdt.core.jdom.IDOMNode;
16 import net.sourceforge.phpdt.internal.compiler.util.Util;
17 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
18
19 /**
20  * DOMImport provides an implementation of IDOMImport.
21  *
22  * @see IDOMImport
23  * @see DOMNode
24  */
25 class DOMImport extends DOMNode implements IDOMImport {
26         /**
27          * Indicates if this import is an on demand type import
28          */
29         protected boolean fOnDemand;
30 /**
31  * Creates a new empty IMPORT node.
32  */
33 DOMImport() {
34         fName = "java.lang.*"; //$NON-NLS-1$
35         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
36 }
37 /**
38  * Creates a new detailed IMPORT document fragment on the given range of the document.
39  *
40  * @param document - the document containing this node's original contents
41  * @param sourceRange - a two element array of integers describing the
42  *              entire inclusive source range of this node within its document.
43  *              Contents start on and include the character at the first position.
44  *              Contents end on and include the character at the last position.
45  *              An array of -1's indicates this node's contents do not exist
46  *              in the document.
47  * @param name - the identifier portion of the name of this node, or
48  *              <code>null</code> if this node does not have a name
49  * @param nameRange - a two element array of integers describing the
50  *              entire inclusive source range of this node's name within its document,
51  *              including any array qualifiers that might immediately follow the name
52  *              or -1's if this node does not have a name.
53  * @param onDemand - indicates if this import is an on demand style import
54  */
55 DOMImport(char[] document, int[] sourceRange, String name, int[] nameRange, boolean onDemand) {
56         super(document, sourceRange, name, nameRange);
57         fOnDemand = onDemand;
58         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
59 }
60 /**
61  * Creates a new simple IMPORT document fragment on the given range of the document.
62  *
63  * @param document - the document containing this node's original contents
64  * @param sourceRange - a two element array of integers describing the
65  *              entire inclusive source range of this node within its document.
66  *              Contents start on and include the character at the first position.
67  *              Contents end on and include the character at the last position.
68  *              An array of -1's indicates this node's contents do not exist
69  *              in the document.
70  * @param name - the identifier portion of the name of this node, or
71  *              <code>null</code> if this node does not have a name
72  * @param onDemand - indicates if this import is an on demand style import
73  */
74 DOMImport(char[] document, int[] sourceRange, String name, boolean onDemand) {
75         this(document, sourceRange, name, new int[] {-1, -1}, onDemand);
76         fOnDemand = onDemand;
77         setMask(MASK_DETAILED_SOURCE_INDEXES, false);
78 }
79 /**
80  * @see DOMNode#appendFragmentedContents(CharArrayBuffer)
81  */
82 protected void appendFragmentedContents(CharArrayBuffer buffer) {
83         if (fNameRange[0] < 0) {
84                 buffer
85                         .append("include ") //$NON-NLS-1$
86                         .append(fName)
87                         .append(';')
88                         .append(Util.LINE_SEPARATOR);
89         } else {
90                 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
91                 //buffer.append(fDocument, fNameRange[0], fNameRange[1] - fNameRange[0] + 1);
92                 buffer.append(fName);
93                 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
94         }
95 }
96 /** 
97  * @see IDOMNode#getContents()
98  */
99 public String getContents() {
100         if (fName == null) {
101                 return null;
102         } else {
103                 return super.getContents();
104         }
105 }
106 /**
107  * @see DOMNode#getDetailedNode()
108  */
109 //protected DOMNode getDetailedNode() {
110 //      return (DOMNode)getFactory().createImport(getContents());
111 //}
112 /**
113  * @see IDOMNode#getJavaElement
114  */
115 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
116 //      if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
117 //              return ((ICompilationUnit)parent).getImport(getName());
118 //      } else {
119                 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
120 //      }
121 }
122 /**
123  * @see IDOMNode#getNodeType()
124  */
125 public int getNodeType() {
126         return IDOMNode.IMPORT;
127 }
128 /**
129  * @see IDOMImport#isOnDemand()
130  */
131 public boolean isOnDemand() {
132         return fOnDemand;       
133 }
134 /**
135  * @see DOMNode
136  */
137 protected DOMNode newDOMNode() {
138         return new DOMImport();
139 }
140 /**
141  * @see IDOMNode#setName(char[])
142  */
143 public void setName(String name) {
144         if (name == null) {
145                 throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
146         }
147         becomeDetailed();
148         super.setName(name);
149         fOnDemand = name.endsWith(".*"); //$NON-NLS-1$
150 }
151 /**
152  * @see IDOMNode#toString()
153  */
154 public String toString() {
155         return "IMPORT: " + getName(); //$NON-NLS-1$
156 }
157 }