c78a9eef9e2262e794fa2eb623fd2900047785c3
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / PackageFragment.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.ArrayList;
14 import java.util.HashSet;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.WorkingCopyOwner;
23 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
24 import net.sourceforge.phpdt.internal.core.util.Util;
25 import net.sourceforge.phpdt.internal.core.util.PHPFileUtil;
26
27 import org.eclipse.core.resources.IContainer;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.Path;
33
34 /**
35  * @see IPackageFragment
36  */
37 public class PackageFragment extends Openable implements IPackageFragment {
38         /**
39          * Constant empty list of compilation units
40          */
41         protected static ICompilationUnit[] fgEmptyCompilationUnitList = new ICompilationUnit[] {};
42
43         /**
44          * Constructs a handle for a package fragment
45          * 
46          * @see IPackageFragment
47          */
48         protected PackageFragment(PackageFragmentRoot root, String name) {
49                 super(root, name);
50         }
51
52         /**
53          * @see Openable
54          */
55         protected boolean buildStructure(OpenableElementInfo info,
56                         IProgressMonitor pm, Map newElements, IResource underlyingResource)
57                         throws JavaModelException {
58
59                 // check whether this pkg can be opened
60                 if (!underlyingResource.isAccessible())
61                         throw newNotPresentException();
62
63                 int kind = getKind();
64                 String extType;
65                 // if (kind == IPackageFragmentRoot.K_SOURCE) {
66                 extType = "php"; // EXTENSION_java;
67                 // } else {
68                 // extType = EXTENSION_class;
69                 // }
70
71                 // add compilation units/class files from resources
72                 HashSet vChildren = new HashSet();
73                 try {
74                         PackageFragmentRoot root = getPackageFragmentRoot();
75                         // char[][] inclusionPatterns = root.fullInclusionPatternChars();
76                         char[][] exclusionPatterns = root.fullExclusionPatternChars();
77                         IResource[] members = ((IContainer) underlyingResource).members();
78                         for (int i = 0, max = members.length; i < max; i++) {
79                                 IResource child = members[i];
80                                 if (child.getType() != IResource.FOLDER
81                                                 && !Util.isExcluded(child, exclusionPatterns)) {
82                                         String extension = child.getProjectRelativePath()
83                                                         .getFileExtension();
84                                         if (extension != null) {
85                                                 if (extension.equalsIgnoreCase(extType)) {
86                                                         IJavaElement childElement;
87                                                         // if (kind == IPackageFragmentRoot.K_SOURCE &&
88                                                         // ProjectPrefUtil.isValidCompilationUnitName(child.getName()))
89                                                         // {
90                                                         // childElement = new CompilationUnit(this,
91                                                         // child.getName(),
92                                                         // DefaultWorkingCopyOwner.PRIMARY);
93                                                         // vChildren.add(childElement);
94                                                         // } else if
95                                                         // (ProjectPrefUtil.isValidClassFileName(child.getName()))
96                                                         // {
97                                                         // childElement = getClassFile(child.getName());
98                                                         // vChildren.add(childElement);
99                                                         // }
100                                                 }
101                                         }
102                                 }
103                         }
104                 } catch (CoreException e) {
105                         throw new JavaModelException(e);
106                 }
107
108                 // if (kind == IPackageFragmentRoot.K_SOURCE) {
109                 // // add primary compilation units
110                 // ICompilationUnit[] primaryCompilationUnits =
111                 // getCompilationUnits(DefaultWorkingCopyOwner.PRIMARY);
112                 // for (int i = 0, length = primaryCompilationUnits.length; i < length;
113                 // i++) {
114                 // ICompilationUnit primary = primaryCompilationUnits[i];
115                 // vChildren.add(primary);
116                 // }
117                 // }
118
119                 IJavaElement[] children = new IJavaElement[vChildren.size()];
120                 vChildren.toArray(children);
121                 info.setChildren(children);
122                 return true;
123         }
124
125         // /**
126         // * Compute the children of this package fragment.
127         // *
128         // * <p>Package fragments which are folders recognize files based on the
129         // * type of the fragment
130         // * <p>Package fragments which are in a jar only recognize .class files (
131         // * @see JarPackageFragment).
132         // */
133         // protected boolean computeChildren(OpenableElementInfo info, IResource
134         // resource) throws JavaModelException {
135         // ArrayList vChildren = new ArrayList();
136         // // int kind = getKind();
137         // String extType;
138         // // if (kind == IPackageFragmentRoot.K_SOURCE) {
139         // extType = "php"; //$NON-NLS-1$
140         // // } else {
141         // // extType = "class"; //$NON-NLS-1$
142         // // }
143         // try {
144         // char[][] exclusionPatterns =
145         // ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
146         // IResource[] members = ((IContainer) resource).members();
147         // for (int i = 0, max = members.length; i < max; i++) {
148         // IResource child = members[i];
149         // if (child.getType() != IResource.FOLDER
150         // && !ProjectPrefUtil.isExcluded(child, exclusionPatterns)) {
151         // String extension = child.getProjectRelativePath().getFileExtension();
152         // if (extension != null) {
153         // if (extension.equalsIgnoreCase(extType)) {
154         // IJavaElement childElement;
155         // // if (kind == IPackageFragmentRoot.K_SOURCE &&
156         // ProjectPrefUtil.isValidCompilationUnitName(child.getName())) {
157         // // childElement = getCompilationUnit(child.getName());
158         // // vChildren.add(childElement);
159         // // } else if (ProjectPrefUtil.isValidClassFileName(child.getName())) {
160         // // childElement = getClassFile(child.getName());
161         // // vChildren.add(childElement);
162         // // }
163         // }
164         // }
165         // }
166         // }
167         // } catch (CoreException e) {
168         // throw new JavaModelException(e);
169         // }
170         // IJavaElement[] children = new IJavaElement[vChildren.size()];
171         // vChildren.toArray(children);
172         // info.setChildren(children);
173         // return true;
174         // }
175         /**
176          * Returns true if this fragment contains at least one java resource.
177          * Returns false otherwise.
178          */
179         // public boolean containsJavaResources() throws JavaModelException {
180         // return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
181         // }
182         /**
183          * @see ISourceManipulation
184          */
185         // public void copy(IJavaElement container, IJavaElement sibling, String
186         // rename, boolean force, IProgressMonitor monitor) throws
187         // JavaModelException {
188         // if (container == null) {
189         // throw new
190         // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
191         // //$NON-NLS-1$
192         // }
193         // IJavaElement[] elements= new IJavaElement[] {this};
194         // IJavaElement[] containers= new IJavaElement[] {container};
195         // IJavaElement[] siblings= null;
196         // if (sibling != null) {
197         // siblings= new IJavaElement[] {sibling};
198         // }
199         // String[] renamings= null;
200         // if (rename != null) {
201         // renamings= new String[] {rename};
202         // }
203         // getJavaModel().copy(elements, containers, siblings, renamings, force,
204         // monitor);
205         // }
206         /**
207          * @see IPackageFragment
208          */
209         public ICompilationUnit createCompilationUnit(String name, String contents,
210                         boolean force, IProgressMonitor monitor) throws JavaModelException {
211                 // CreateCompilationUnitOperation op= new
212                 // CreateCompilationUnitOperation(this, name, contents, force);
213                 // runOperation(op, monitor);
214                 // return getCompilationUnit(name);
215                 return null;
216         }
217
218         /**
219          * @see JavaElement
220          */
221         protected Object createElementInfo() {
222                 return new PackageFragmentInfo();
223         }
224
225         /**
226          * @see ISourceManipulation
227          */
228         // public void delete(boolean force, IProgressMonitor monitor) throws
229         // JavaModelException {
230         // IJavaElement[] elements = new IJavaElement[] {this};
231         // getJavaModel().delete(elements, force, monitor);
232         // }
233         // /**
234         // * @see Openable
235         // */
236         // protected boolean generateInfos(OpenableElementInfo info,
237         // IProgressMonitor pm, Map newElements, IResource underlyingResource)
238         // throws JavaModelException {
239         //      
240         // return computeChildren(info, underlyingResource);
241         // }
242         // /**
243         // * @see IPackageFragment#getClassFile(String)
244         // */
245         // public IClassFile getClassFile(String name) {
246         // return new ClassFile(this, name);
247         // }
248         /**
249          * Returns a the collection of class files in this - a folder package
250          * fragment which has a root that has its kind set to
251          * <code>IPackageFragmentRoot.K_Source</code> does not recognize class
252          * files.
253          * 
254          * @see IPackageFragment#getClassFiles()
255          */
256         // public IClassFile[] getClassFiles() throws JavaModelException {
257         // if (getKind() == IPackageFragmentRoot.K_SOURCE) {
258         // return fgEmptyClassFileList;
259         // }
260         //      
261         // ArrayList list = getChildrenOfType(CLASS_FILE);
262         // IClassFile[] array= new IClassFile[list.size()];
263         // list.toArray(array);
264         // return array;
265         // }
266         /**
267          * @see IPackageFragment#getCompilationUnit(String)
268          * @exception IllegalArgumentExcpetion
269          *                if the name does not end with a php file extension
270          */
271         public ICompilationUnit getCompilationUnit(String cuName) {
272                 if (!PHPFileUtil.isValidPHPUnitName(cuName)) {
273                         throw new IllegalArgumentException(Util
274                                         .bind("convention.unit.notJavaName")); //$NON-NLS-1$
275                 }
276                 return new CompilationUnit(this, cuName,
277                                 DefaultWorkingCopyOwner.PRIMARY);
278         }
279
280         /**
281          * @see IPackageFragment#getCompilationUnits()
282          */
283         public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
284                 if (getKind() == IPackageFragmentRoot.K_BINARY) {
285                         return fgEmptyCompilationUnitList;
286                 }
287
288                 ArrayList list = getChildrenOfType(COMPILATION_UNIT);
289                 ICompilationUnit[] array = new ICompilationUnit[list.size()];
290                 list.toArray(array);
291                 return array;
292         }
293
294         /**
295          * @see IJavaElement
296          */
297         public int getElementType() {
298                 return PACKAGE_FRAGMENT;
299         }
300
301         /*
302          * @see JavaElement
303          */
304         public IJavaElement getHandleFromMemento(String token,
305                         MementoTokenizer memento, WorkingCopyOwner owner) {
306                 switch (token.charAt(0)) {
307                 case JEM_COUNT:
308                         return getHandleUpdatingCountFromMemento(memento, owner);
309                         // case JEM_CLASSFILE:
310                         // String classFileName = memento.nextToken();
311                         // JavaElement classFile = (JavaElement)getClassFile(classFileName);
312                         // return classFile.getHandleFromMemento(memento, owner);
313                 case JEM_COMPILATIONUNIT:
314                         String cuName = memento.nextToken();
315                         JavaElement cu = new CompilationUnit(this, cuName, owner);
316                         return cu.getHandleFromMemento(memento, owner);
317                 }
318                 return null;
319         }
320
321         /**
322          * @see JavaElement#getHandleMementoDelimiter()
323          */
324         protected char getHandleMementoDelimiter() {
325                 return JavaElement.JEM_PACKAGEFRAGMENT;
326         }
327
328         /**
329          * @see IPackageFragment#getKind()
330          */
331         public int getKind() throws JavaModelException {
332                 return ((IPackageFragmentRoot) getParent()).getKind();
333         }
334
335         /**
336          * Returns an array of non-java resources contained in the receiver.
337          */
338         // public Object[] getNonJavaResources() throws JavaModelException {
339         // if (this.isDefaultPackage()) {
340         // // We don't want to show non java resources of the default package (see
341         // PR #1G58NB8)
342         // return JavaElementInfo.NO_NON_JAVA_RESOURCES;
343         // } else {
344         // return ((PackageFragmentInfo)
345         // getElementInfo()).getNonJavaResources(getResource(),
346         // (PackageFragmentRoot)getPackageFragmentRoot());
347         // }
348         // }
349         /**
350          * @see IJavaElement#getPath()
351          */
352         public IPath getPath() {
353                 PackageFragmentRoot root = this.getPackageFragmentRoot();
354                 if (root.isArchive()) {
355                         return root.getPath();
356                 } else {
357                         // return root.getPath().append(this.getElementName().replace('.',
358                         // '/'));
359                         return root.getPath().append(this.getElementName());
360                 }
361         }
362
363         /**
364          * @see IJavaElement#getResource()
365          */
366         public IResource getResource() {
367                 PackageFragmentRoot root = this.getPackageFragmentRoot();
368                 if (root.isArchive()) {
369                         return root.getResource();
370                 } else {
371                         String elementName = this.getElementName();
372                         if (elementName.length() == 0) {
373                                 return root.getResource();
374                         } else {
375                                 // return ((IContainer)root.getResource()).getFolder(new
376                                 // Path(this.getElementName().replace('.', '/')));
377                                 return ((IContainer) root.getResource()).getFolder(new Path(
378                                                 this.getElementName()));
379                         }
380                 }
381         }
382
383         /**
384          * @see IJavaElement#getUnderlyingResource()
385          */
386         public IResource getUnderlyingResource() throws JavaModelException {
387                 IResource rootResource = parent.getUnderlyingResource();
388                 if (rootResource == null) {
389                         // jar package fragment root that has no associated resource
390                         return null;
391                 }
392                 // the underlying resource may be a folder or a project (in the case
393                 // that the project folder
394                 // is atually the package fragment root)
395                 // if (rootResource.getType() == IResource.FOLDER ||
396                 // rootResource.getType() == IResource.PROJECT) {
397                 // IContainer folder = (IContainer) rootResource;
398                 // String[] segs = Signature.getSimpleNames(fName);
399                 // for (int i = 0; i < segs.length; ++i) {
400                 // IResource child = folder.findMember(segs[i]);
401                 // if (child == null || child.getType() != IResource.FOLDER) {
402                 // throw newNotPresentException();
403                 // }
404                 // folder = (IFolder) child;
405                 // }
406                 // return folder;
407                 // } else {
408                 return rootResource;
409                 // }
410         }
411
412         /**
413          * @see IPackageFragment#hasSubpackages()
414          */
415         // public boolean hasSubpackages() throws JavaModelException {
416         // IJavaElement[] packages=
417         // ((IPackageFragmentRoot)getParent()).getChildren();
418         // String name = getElementName();
419         // int nameLength = name.length();
420         // String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
421         // for (int i= 0; i < packages.length; i++) {
422         // String otherName = packages[i].getElementName();
423         // if (otherName.length() > nameLength && otherName.startsWith(packageName))
424         // {
425         // return true;
426         // }
427         // }
428         // return false;
429         // }
430         /**
431          * @see IPackageFragment#isDefaultPackage()
432          */
433         public boolean isDefaultPackage() {
434                 return this.getElementName().length() == 0;
435         }
436
437         /**
438          * @see ISourceManipulation#move(IJavaElement, IJavaElement, String,
439          *      boolean, IProgressMonitor)
440          */
441         // public void move(IJavaElement container, IJavaElement sibling, String
442         // rename, boolean force, IProgressMonitor monitor) throws
443         // JavaModelException {
444         // if (container == null) {
445         // throw new
446         // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
447         // //$NON-NLS-1$
448         // }
449         // IJavaElement[] elements= new IJavaElement[] {this};
450         // IJavaElement[] containers= new IJavaElement[] {container};
451         // IJavaElement[] siblings= null;
452         // if (sibling != null) {
453         // siblings= new IJavaElement[] {sibling};
454         // }
455         // String[] renamings= null;
456         // if (rename != null) {
457         // renamings= new String[] {rename};
458         // }
459         // getJavaModel().move(elements, containers, siblings, renamings, force,
460         // monitor);
461         // }
462         // protected void openWhenClosed(IProgressMonitor pm) throws
463         // JavaModelException {
464         // if (!this.resourceExists()) throw newNotPresentException();
465         // super.openWhenClosed(pm);
466         // }
467         /**
468          * Recomputes the children of this element, based on the current state of
469          * the workbench.
470          */
471         // public void refreshChildren() {
472         // try {
473         // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
474         // computeChildren(info, getResource());
475         // } catch (JavaModelException e) {
476         // // do nothing.
477         // }
478         // }
479         /**
480          * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
481          */
482         // public void rename(String name, boolean force, IProgressMonitor monitor)
483         // throws JavaModelException {
484         // if (name == null) {
485         // throw new
486         // IllegalArgumentException(ProjectPrefUtil.bind("element.nullName"));
487         // //$NON-NLS-1$
488         // }
489         // IJavaElement[] elements= new IJavaElement[] {this};
490         // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
491         // String[] renamings= new String[] {name};
492         // getJavaModel().rename(elements, dests, renamings, force, monitor);
493         // }
494         // /*
495         // * @see JavaElement#rootedAt(IJavaProject)
496         // */
497         // public IJavaElement rootedAt(IJavaProject project) {
498         // return
499         // new PackageFragment(
500         // (IPackageFragmentRoot)((JavaElement)parent).rootedAt(project),
501         // name);
502         // }
503         /**
504          * Debugging purposes
505          */
506         protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
507                 if (tab == 0) {
508                         super.toStringChildren(tab, buffer, info);
509                 }
510         }
511
512         /**
513          * Debugging purposes
514          */
515         protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
516                 buffer.append(this.tabString(tab));
517                 if (getElementName().length() == 0) {
518                         buffer.append("[default]"); //$NON-NLS-1$
519                 } else {
520                         buffer.append(getElementName());
521                 }
522                 if (info == null) {
523                         buffer.append(" (not open)"); //$NON-NLS-1$
524                 } else {
525                         if (tab > 0) {
526                                 buffer.append(" (...)"); //$NON-NLS-1$
527                         }
528                 }
529         }
530 }