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;
13 import java.util.ArrayList;
14 import java.util.HashSet;
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;
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;
35 * @see IPackageFragment
37 public class PackageFragment extends Openable implements IPackageFragment {
39 * Constant empty list of compilation units
41 protected static ICompilationUnit[] fgEmptyCompilationUnitList = new ICompilationUnit[] {};
44 * Constructs a handle for a package fragment
46 * @see IPackageFragment
48 protected PackageFragment(PackageFragmentRoot root, String name) {
55 protected boolean buildStructure(OpenableElementInfo info,
56 IProgressMonitor pm, Map newElements, IResource underlyingResource)
57 throws JavaModelException {
59 // check whether this pkg can be opened
60 if (!underlyingResource.isAccessible())
61 throw newNotPresentException();
65 // if (kind == IPackageFragmentRoot.K_SOURCE) {
66 extType = "php"; // EXTENSION_java;
68 // extType = EXTENSION_class;
71 // add compilation units/class files from resources
72 HashSet vChildren = new HashSet();
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()
84 if (extension != null) {
85 if (extension.equalsIgnoreCase(extType)) {
86 IJavaElement childElement;
87 // if (kind == IPackageFragmentRoot.K_SOURCE &&
88 // ProjectPrefUtil.isValidCompilationUnitName(child.getName()))
90 // childElement = new CompilationUnit(this,
92 // DefaultWorkingCopyOwner.PRIMARY);
93 // vChildren.add(childElement);
95 // (ProjectPrefUtil.isValidClassFileName(child.getName()))
97 // childElement = getClassFile(child.getName());
98 // vChildren.add(childElement);
104 } catch (CoreException e) {
105 throw new JavaModelException(e);
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;
114 // ICompilationUnit primary = primaryCompilationUnits[i];
115 // vChildren.add(primary);
119 IJavaElement[] children = new IJavaElement[vChildren.size()];
120 vChildren.toArray(children);
121 info.setChildren(children);
126 // * Compute the children of this package fragment.
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).
133 // protected boolean computeChildren(OpenableElementInfo info, IResource
134 // resource) throws JavaModelException {
135 // ArrayList vChildren = new ArrayList();
136 // // int kind = getKind();
138 // // if (kind == IPackageFragmentRoot.K_SOURCE) {
139 // extType = "php"; //$NON-NLS-1$
141 // // extType = "class"; //$NON-NLS-1$
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);
167 // } catch (CoreException e) {
168 // throw new JavaModelException(e);
170 // IJavaElement[] children = new IJavaElement[vChildren.size()];
171 // vChildren.toArray(children);
172 // info.setChildren(children);
176 * Returns true if this fragment contains at least one java resource.
177 * Returns false otherwise.
179 // public boolean containsJavaResources() throws JavaModelException {
180 // return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
183 * @see ISourceManipulation
185 // public void copy(IJavaElement container, IJavaElement sibling, String
186 // rename, boolean force, IProgressMonitor monitor) throws
187 // JavaModelException {
188 // if (container == null) {
190 // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
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};
199 // String[] renamings= null;
200 // if (rename != null) {
201 // renamings= new String[] {rename};
203 // getJavaModel().copy(elements, containers, siblings, renamings, force,
207 * @see IPackageFragment
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);
221 protected Object createElementInfo() {
222 return new PackageFragmentInfo();
226 * @see ISourceManipulation
228 // public void delete(boolean force, IProgressMonitor monitor) throws
229 // JavaModelException {
230 // IJavaElement[] elements = new IJavaElement[] {this};
231 // getJavaModel().delete(elements, force, monitor);
236 // protected boolean generateInfos(OpenableElementInfo info,
237 // IProgressMonitor pm, Map newElements, IResource underlyingResource)
238 // throws JavaModelException {
240 // return computeChildren(info, underlyingResource);
243 // * @see IPackageFragment#getClassFile(String)
245 // public IClassFile getClassFile(String name) {
246 // return new ClassFile(this, name);
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
254 * @see IPackageFragment#getClassFiles()
256 // public IClassFile[] getClassFiles() throws JavaModelException {
257 // if (getKind() == IPackageFragmentRoot.K_SOURCE) {
258 // return fgEmptyClassFileList;
261 // ArrayList list = getChildrenOfType(CLASS_FILE);
262 // IClassFile[] array= new IClassFile[list.size()];
263 // list.toArray(array);
267 * @see IPackageFragment#getCompilationUnit(String)
268 * @exception IllegalArgumentExcpetion
269 * if the name does not end with a php file extension
271 public ICompilationUnit getCompilationUnit(String cuName) {
272 if (!PHPFileUtil.isValidPHPUnitName(cuName)) {
273 throw new IllegalArgumentException(Util
274 .bind("convention.unit.notJavaName")); //$NON-NLS-1$
276 return new CompilationUnit(this, cuName,
277 DefaultWorkingCopyOwner.PRIMARY);
281 * @see IPackageFragment#getCompilationUnits()
283 public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
284 if (getKind() == IPackageFragmentRoot.K_BINARY) {
285 return fgEmptyCompilationUnitList;
288 ArrayList list = getChildrenOfType(COMPILATION_UNIT);
289 ICompilationUnit[] array = new ICompilationUnit[list.size()];
297 public int getElementType() {
298 return PACKAGE_FRAGMENT;
304 public IJavaElement getHandleFromMemento(String token,
305 MementoTokenizer memento, WorkingCopyOwner owner) {
306 switch (token.charAt(0)) {
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);
322 * @see JavaElement#getHandleMementoDelimiter()
324 protected char getHandleMementoDelimiter() {
325 return JavaElement.JEM_PACKAGEFRAGMENT;
329 * @see IPackageFragment#getKind()
331 public int getKind() throws JavaModelException {
332 return ((IPackageFragmentRoot) getParent()).getKind();
336 * Returns an array of non-java resources contained in the receiver.
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
342 // return JavaElementInfo.NO_NON_JAVA_RESOURCES;
344 // return ((PackageFragmentInfo)
345 // getElementInfo()).getNonJavaResources(getResource(),
346 // (PackageFragmentRoot)getPackageFragmentRoot());
350 * @see IJavaElement#getPath()
352 public IPath getPath() {
353 PackageFragmentRoot root = this.getPackageFragmentRoot();
354 if (root.isArchive()) {
355 return root.getPath();
357 // return root.getPath().append(this.getElementName().replace('.',
359 return root.getPath().append(this.getElementName());
364 * @see IJavaElement#getResource()
366 public IResource getResource() {
367 PackageFragmentRoot root = this.getPackageFragmentRoot();
368 if (root.isArchive()) {
369 return root.getResource();
371 String elementName = this.getElementName();
372 if (elementName.length() == 0) {
373 return root.getResource();
375 // return ((IContainer)root.getResource()).getFolder(new
376 // Path(this.getElementName().replace('.', '/')));
377 return ((IContainer) root.getResource()).getFolder(new Path(
378 this.getElementName()));
384 * @see IJavaElement#getUnderlyingResource()
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
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();
404 // folder = (IFolder) child;
413 * @see IPackageFragment#hasSubpackages()
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))
431 * @see IPackageFragment#isDefaultPackage()
433 public boolean isDefaultPackage() {
434 return this.getElementName().length() == 0;
438 * @see ISourceManipulation#move(IJavaElement, IJavaElement, String,
439 * boolean, IProgressMonitor)
441 // public void move(IJavaElement container, IJavaElement sibling, String
442 // rename, boolean force, IProgressMonitor monitor) throws
443 // JavaModelException {
444 // if (container == null) {
446 // IllegalArgumentException(ProjectPrefUtil.bind("operation.nullContainer"));
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};
455 // String[] renamings= null;
456 // if (rename != null) {
457 // renamings= new String[] {rename};
459 // getJavaModel().move(elements, containers, siblings, renamings, force,
462 // protected void openWhenClosed(IProgressMonitor pm) throws
463 // JavaModelException {
464 // if (!this.resourceExists()) throw newNotPresentException();
465 // super.openWhenClosed(pm);
468 * Recomputes the children of this element, based on the current state of
471 // public void refreshChildren() {
473 // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
474 // computeChildren(info, getResource());
475 // } catch (JavaModelException e) {
480 * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
482 // public void rename(String name, boolean force, IProgressMonitor monitor)
483 // throws JavaModelException {
484 // if (name == null) {
486 // IllegalArgumentException(ProjectPrefUtil.bind("element.nullName"));
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);
495 // * @see JavaElement#rootedAt(IJavaProject)
497 // public IJavaElement rootedAt(IJavaProject project) {
499 // new PackageFragment(
500 // (IPackageFragmentRoot)((JavaElement)parent).rootedAt(project),
506 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
508 super.toStringChildren(tab, buffer, info);
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$
520 buffer.append(getElementName());
523 buffer.append(" (not open)"); //$NON-NLS-1$
526 buffer.append(" (...)"); //$NON-NLS-1$