improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceRefElement.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;
12
13 import java.util.HashMap;
14
15 import net.sourceforge.phpdt.core.IBuffer;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IJavaProject;
19 import net.sourceforge.phpdt.core.IMember;
20 import net.sourceforge.phpdt.core.IOpenable;
21 import net.sourceforge.phpdt.core.ISourceManipulation;
22 import net.sourceforge.phpdt.core.ISourceRange;
23 import net.sourceforge.phpdt.core.ISourceReference;
24 import net.sourceforge.phpdt.core.JavaModelException;
25 import net.sourceforge.phpdt.core.WorkingCopyOwner;
26 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
27
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.IProgressMonitor;
31
32
33 /**
34  * Abstract class for Java elements which implement ISourceReference.
35  */
36 /* package */ abstract class SourceRefElement extends JavaElement implements ISourceReference {
37         protected SourceRefElement(JavaElement parent, String name) {
38                 super(parent, name);
39         }
40 /**
41  * This element is being closed.  Do any necessary cleanup.
42  */
43 protected void closing(Object info) throws JavaModelException {
44         // Do any necessary cleanup
45 }
46 /**
47  * Returns a new element info for this element.
48  */
49 protected Object createElementInfo() {
50         return null; // not used for source ref elements
51 }
52 /**
53  * @see ISourceManipulation
54  */
55 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
56 //      if (container == null) {
57 //              throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
58 //      }
59 //      IJavaElement[] elements= new IJavaElement[] {this};
60 //      IJavaElement[] containers= new IJavaElement[] {container};
61 //      IJavaElement[] siblings= null;
62 //      if (sibling != null) {
63 //              siblings= new IJavaElement[] {sibling};
64 //      }
65 //      String[] renamings= null;
66 //      if (rename != null) {
67 //              renamings= new String[] {rename};
68 //      }
69 //      getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
70 //}
71 /**
72  * @see ISourceManipulation
73  */
74 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
75 //      IJavaElement[] elements = new IJavaElement[] {this};
76 //      getJavaModel().delete(elements, force, monitor);
77 //}
78 /*
79  * @see JavaElement#generateInfos
80  */
81 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
82         Openable openableParent = (Openable)getOpenableParent();
83         if (openableParent == null) return;
84
85         JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo(openableParent);
86         if (openableParentInfo == null) {
87                 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
88         }
89 }
90 /**
91  * @see IMember
92  */
93 public ICompilationUnit getCompilationUnit() {
94         return ((JavaElement)getParent()).getCompilationUnit();
95 }
96 /**
97  * Elements within compilation units and class files have no
98  * corresponding resource.
99  *
100  * @see IJavaElement
101  */
102 public IResource getCorrespondingResource() throws JavaModelException {
103         if (!exists()) throw newNotPresentException();
104         return null;
105 }
106 /*
107  * @see JavaElement
108  */
109 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
110         switch (token.charAt(0)) {
111                 case JEM_COUNT:
112                         return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
113         }
114         return this;
115 }
116 /**
117  * Return the first instance of IOpenable in the hierarchy of this
118  * type (going up the hierarchy from this type);
119  */
120 public IOpenable getOpenableParent() {
121         IJavaElement current = getParent();
122         while (current != null){
123                 if (current instanceof IOpenable){
124                         return (IOpenable) current;
125                 }
126                 current = current.getParent();
127         }
128         return null;
129 }
130
131 /*
132  * @see IJavaElement
133  */
134 public IPath getPath() {
135         return this.getParent().getPath();
136 }
137 /*
138  * @see IJavaElement
139  */
140 public IResource getResource() {
141         return this.getParent().getResource();
142 }
143 /**
144  * @see ISourceReference
145  */
146 public String getSource() throws JavaModelException {
147         IOpenable openable = getOpenableParent();
148         IBuffer buffer = openable.getBuffer();
149         if (buffer == null) {
150                 return null;
151         }
152         ISourceRange range = getSourceRange();
153         int offset = range.getOffset();
154         int length = range.getLength();
155         if (offset == -1 || length == 0 ) {
156                 return null;
157         }
158         try {
159           return buffer.getText(offset, length);
160           // jsurfer insert start
161         } catch (ArrayIndexOutOfBoundsException e) {
162                 
163         }
164         return null;
165 //       jsurfer insert end
166 }
167 /**
168  * @see ISourceReference
169  */
170 public ISourceRange getSourceRange() throws JavaModelException {
171         SourceRefElementInfo info = (SourceRefElementInfo) getElementInfo();
172         return info.getSourceRange();
173 }
174 /**
175  * @see IJavaElement
176  */
177 public IResource getUnderlyingResource() throws JavaModelException {
178         if (!exists()) throw newNotPresentException();
179         return getParent().getUnderlyingResource();
180 }
181 /**
182  * @see ISourceManipulation
183  */
184 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
185 //      if (container == null) {
186 //              throw new IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer")); //$NON-NLS-1$
187 //      }
188 //      IJavaElement[] elements= new IJavaElement[] {this};
189 //      IJavaElement[] containers= new IJavaElement[] {container};
190 //      IJavaElement[] siblings= null;
191 //      if (sibling != null) {
192 //              siblings= new IJavaElement[] {sibling};
193 //      }
194 //      String[] renamings= null;
195 //      if (rename != null) {
196 //              renamings= new String[] {rename};
197 //      }
198 //      getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
199 //}
200 /**
201  * @see ISourceManipulation
202  */
203 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
204 //      if (name == null) {
205 //              throw new IllegalArgumentException(ProjectPrefUtil.bind("element.nullName")); //$NON-NLS-1$
206 //      }
207 //      IJavaElement[] elements= new IJavaElement[] {this};
208 //      IJavaElement[] dests= new IJavaElement[] {this.getParent()};
209 //      String[] renamings= new String[] {name};
210 //      getJavaModel().rename(elements, dests, renamings, force, monitor);
211 //}
212 /*
213  * @see JavaElement#rootedAt(IJavaProject)
214  */
215 public IJavaElement rootedAt(IJavaProject project) {
216         // not needed
217         return null;
218 }
219 }