| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2008 IBM Corporation and others. | 
| 3 |  * All rights reserved. This program and the accompanying materials | 
| 4 |  * are made available under the terms of the Eclipse Public License v1.0 | 
| 5 |  * which accompanies this distribution, and is available at | 
| 6 |  * http://www.eclipse.org/legal/epl-v10.html | 
| 7 |  *  | 
| 8 |  * Contributors: | 
| 9 |  *     IBM Corporation - initial API and implementation | 
| 10 |  *******************************************************************************/ | 
| 11 | package org.eclipse.pde.api.tools.internal.builder; | 
| 12 |   | 
| 13 | import java.util.Arrays; | 
| 14 | import java.util.HashMap; | 
| 15 | import java.util.HashSet; | 
| 16 | import java.util.Iterator; | 
| 17 | import java.util.Map; | 
| 18 | import java.util.Set; | 
| 19 | import java.util.Map.Entry; | 
| 20 |   | 
| 21 | import org.eclipse.core.runtime.CoreException; | 
| 22 | import org.eclipse.pde.api.tools.internal.model.ApiElement; | 
| 23 | import org.eclipse.pde.api.tools.internal.provisional.Factory; | 
| 24 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor; | 
| 25 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; | 
| 26 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; | 
| 27 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 28 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; | 
| 29 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; | 
| 30 |   | 
| 31 | /** | 
| 32 |  * A search scope containing only types from one component. More efficient than a general purpose | 
| 33 |  * scope. | 
| 34 |  *  | 
| 35 |  * @since 1.0 | 
| 36 |  */ | 
| 37 | public class TypeScope extends ApiElement implements IApiTypeContainer { | 
| 38 |   | 
| 39 |         /** | 
| 40 |          * Associated component | 
| 41 |          */ | 
| 42 |         private IApiComponent fComponent; | 
| 43 |          | 
| 44 |         /** | 
| 45 |          * Map of package names to associated type descriptors | 
| 46 |          */ | 
| 47 |         private Map fPackageToTypes; | 
| 48 |                  | 
| 49 |         /** | 
| 50 |          * Constructs a new class file container/search scope on the given types. | 
| 51 |          *  | 
| 52 |          * @param component API component | 
| 53 |          * @param types types within the component | 
| 54 |          */ | 
| 55 |         public TypeScope(IApiComponent component, IReferenceTypeDescriptor[] types) { | 
| 56 |                 super(component, IApiElement.API_TYPE_CONTAINER, null); | 
| 57 |                 fComponent = component; | 
| 58 |                 fPackageToTypes = new HashMap(); | 
| 59 |                 for (int i = 0; i < types.length; i++) { | 
| 60 |                         IReferenceTypeDescriptor type = types[i]; | 
| 61 |                         String name = type.getPackage().getName(); | 
| 62 |                         Set set = (Set) fPackageToTypes.get(name); | 
| 63 |                         if (set == null) { | 
| 64 |                                 set = new HashSet(); | 
| 65 |                                 fPackageToTypes.put(name, set); | 
| 66 |                         } | 
| 67 |                         set.add(type); | 
| 68 |                 } | 
| 69 |         } | 
| 70 |   | 
| 71 |         /* (non-Javadoc) | 
| 72 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#getPackageNames() | 
| 73 |          */ | 
| 74 |         public String[] getPackageNames() throws CoreException { | 
| 75 |                 Set pkgs = fPackageToTypes.keySet(); | 
| 76 |                 String[] result = new String[pkgs.size()]; | 
| 77 |                 pkgs.toArray(result); | 
| 78 |                 Arrays.sort(result); | 
| 79 |                 return result; | 
| 80 |         } | 
| 81 |   | 
| 82 |         /* (non-Javadoc) | 
| 83 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#accept(org.eclipse.pde.api.tools.internal.provisional.ApiTypeContainerVisitor) | 
| 84 |          */ | 
| 85 |         public void accept(ApiTypeContainerVisitor visitor) throws CoreException { | 
| 86 |                 if (visitor.visit(fComponent)) { | 
| 87 |                         Set entrySet = fPackageToTypes.entrySet(); | 
| 88 |                         Iterator iterator = entrySet.iterator(); | 
| 89 |                         while (iterator.hasNext()) { | 
| 90 |                                 Entry entry = (Entry) iterator.next(); | 
| 91 |                                 String pkg = (String)entry.getKey(); | 
| 92 |                                 if (visitor.visitPackage(pkg)) { | 
| 93 |                                         Set types = (Set) entry.getValue(); | 
| 94 |                                         Iterator typeIter = types.iterator(); | 
| 95 |                                         while (typeIter.hasNext()) { | 
| 96 |                                                 IReferenceTypeDescriptor type = (IReferenceTypeDescriptor) typeIter.next(); | 
| 97 |                                                 IApiTypeRoot classFile = fComponent.findTypeRoot(type.getQualifiedName()); | 
| 98 |                                                 if (classFile != null) { | 
| 99 |                                                         visitor.visit(pkg, classFile); | 
| 100 |                                                         visitor.end(pkg, classFile); | 
| 101 |                                                 } | 
| 102 |                                         } | 
| 103 |                                 } | 
| 104 |                                 visitor.endVisitPackage(pkg); | 
| 105 |                         } | 
| 106 |                 } | 
| 107 |                 visitor.end(fComponent); | 
| 108 |         } | 
| 109 |   | 
| 110 |         /* (non-Javadoc) | 
| 111 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#close() | 
| 112 |          */ | 
| 113 |         public void close() throws CoreException { | 
| 114 |         } | 
| 115 |   | 
| 116 |         /* (non-Javadoc) | 
| 117 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findClassFile(java.lang.String) | 
| 118 |          */ | 
| 119 |         public IApiTypeRoot findTypeRoot(String qualifiedName) throws CoreException { | 
| 120 |                 IReferenceTypeDescriptor descriptor = Factory.typeDescriptor(qualifiedName); | 
| 121 |                 Set types = (Set) fPackageToTypes.get(descriptor.getPackage().getName()); | 
| 122 |                 if (types != null && types.contains(descriptor)) { | 
| 123 |                         return fComponent.findTypeRoot(qualifiedName); | 
| 124 |                 } | 
| 125 |                 return null; | 
| 126 |         } | 
| 127 |   | 
| 128 |   | 
| 129 |         /* (non-Javadoc) | 
| 130 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findClassFile(java.lang.String, java.lang.String) | 
| 131 |          */ | 
| 132 |         public IApiTypeRoot findTypeRoot(String qualifiedName, String id) throws CoreException { | 
| 133 |                 if (fComponent.getId().equals(id)) { | 
| 134 |                         return findTypeRoot(qualifiedName); | 
| 135 |                 } | 
| 136 |                 return null; | 
| 137 |         } | 
| 138 |   | 
| 139 |         /* (non-Javadoc) | 
| 140 |          * @see java.lang.Object#toString() | 
| 141 |          */ | 
| 142 |         public String toString() { | 
| 143 |                 StringBuffer buffer = new StringBuffer(); | 
| 144 |                 buffer.append("*** Type Search Scope ***\n"); //$NON-NLS-1$ | 
| 145 |                 buffer.append("Component: ").append(fComponent); //$NON-NLS-1$ | 
| 146 |                 if(fPackageToTypes != null) { | 
| 147 |                         String pack = null; | 
| 148 |                         for(Iterator iter = fPackageToTypes.keySet().iterator(); iter.hasNext();) { | 
| 149 |                                 pack = (String) iter.next(); | 
| 150 |                                 buffer.append("Package: ").append(pack).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ | 
| 151 |                                 buffer.append("Types: ").append(fPackageToTypes.get(pack).toString()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ | 
| 152 |                         } | 
| 153 |                 } | 
| 154 |                 return buffer.toString(); | 
| 155 |         } | 
| 156 |          | 
| 157 |         /* (non-Javadoc) | 
| 158 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType() | 
| 159 |          */ | 
| 160 |         public int getContainerType() { | 
| 161 |                 return IApiTypeContainer.COMPONENT; | 
| 162 |         } | 
| 163 | } |