| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2007, 2009 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; | 
| 12 |   | 
| 13 | import java.util.ArrayList; | 
| 14 | import java.util.Collections; | 
| 15 | import java.util.HashMap; | 
| 16 | import java.util.Iterator; | 
| 17 | import java.util.List; | 
| 18 | import java.util.Map; | 
| 19 |   | 
| 20 | import org.eclipse.core.runtime.IProgressMonitor; | 
| 21 | import org.eclipse.core.runtime.IStatus; | 
| 22 | import org.eclipse.core.runtime.Status; | 
| 23 | import org.eclipse.pde.api.tools.internal.descriptors.ElementDescriptorImpl; | 
| 24 | import org.eclipse.pde.api.tools.internal.provisional.ApiDescriptionVisitor; | 
| 25 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; | 
| 26 | import org.eclipse.pde.api.tools.internal.provisional.IApiAccess; | 
| 27 | import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; | 
| 28 | import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; | 
| 29 | import org.eclipse.pde.api.tools.internal.provisional.RestrictionModifiers; | 
| 30 | import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; | 
| 31 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor; | 
| 32 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor; | 
| 33 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMemberDescriptor; | 
| 34 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMethodDescriptor; | 
| 35 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor; | 
| 36 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 37 | import org.w3c.dom.Document; | 
| 38 | import org.w3c.dom.Element; | 
| 39 |   | 
| 40 | import com.ibm.icu.text.MessageFormat; | 
| 41 |   | 
| 42 | /** | 
| 43 |  * Implementation of an API description. | 
| 44 |  * <p> | 
| 45 |  * Note, the implementation is not thread safe. | 
| 46 |  * </p> | 
| 47 |  * @see IApiDescription | 
| 48 |  * @since 1.0.0 | 
| 49 |  */ | 
| 50 | public class ApiDescription implements IApiDescription { | 
| 51 |          | 
| 52 |         // flag to indicate visibility should be inherited from parent node | 
| 53 |         protected static final int VISIBILITY_INHERITED = 0; | 
| 54 |          | 
| 55 |         /** | 
| 56 |          * Debug flag | 
| 57 |          */ | 
| 58 |         static boolean DEBUG = Util.DEBUG; | 
| 59 |          | 
| 60 |         /** | 
| 61 |          * Method used for initializing tracing for API descriptions | 
| 62 |          */ | 
| 63 |         public static void setDebug(boolean debugValue) { | 
| 64 |                 DEBUG = debugValue || Util.DEBUG; | 
| 65 |         } | 
| 66 |          | 
| 67 |         /** | 
| 68 |          * API component identifier of the API component that owns this | 
| 69 |          * description. All references within a component have no restrictions. | 
| 70 |          * We allow this to be null for testing purposes, but in general | 
| 71 |          * a component description should have a component id. | 
| 72 |          */ | 
| 73 |         protected String fOwningComponentId = null; | 
| 74 |          | 
| 75 |         /** | 
| 76 |          * Whether this description needs saving | 
| 77 |          */ | 
| 78 |         private boolean fModified = false; | 
| 79 |          | 
| 80 |         /** | 
| 81 |          * Represents a single node in the tree of mapped manifest items | 
| 82 |          */ | 
| 83 |         class ManifestNode implements Comparable { | 
| 84 |                 protected IElementDescriptor element = null; | 
| 85 |                 protected int visibility, restrictions; | 
| 86 |                 protected ManifestNode parent = null; | 
| 87 |                 protected HashMap children = new HashMap(1); | 
| 88 |                  | 
| 89 |                 public ManifestNode(ManifestNode parent, IElementDescriptor element, int visibility, int restrictions) { | 
| 90 |                         this.element = element; | 
| 91 |                         this.visibility = visibility; | 
| 92 |                         this.restrictions = restrictions; | 
| 93 |                         this.parent = parent; | 
| 94 |                 } | 
| 95 |          | 
| 96 |                 /* (non-Javadoc) | 
| 97 |                  * @see java.lang.Object#equals(java.lang.Object) | 
| 98 |                  * This method is safe to override, as the name of an element is unique within its branch of the tree | 
| 99 |                  * and does not change over time. | 
| 100 |                  */ | 
| 101 |                 public boolean equals(Object obj) { | 
| 102 |                         if(obj instanceof ManifestNode) { | 
| 103 |                                 return ((ManifestNode)obj).element.equals(element); | 
| 104 |                         } | 
| 105 |                         return false; | 
| 106 |                 } | 
| 107 |                  | 
| 108 |                 /* (non-Javadoc) | 
| 109 |                  * @see java.lang.Object#hashCode() | 
| 110 |                  * This method is safe to override, as the name of an element is unique within its branch of the tree | 
| 111 |                  * and does not change over time. | 
| 112 |                  */ | 
| 113 |                 public int hashCode() { | 
| 114 |                         return element.hashCode(); | 
| 115 |                 } | 
| 116 |                  | 
| 117 |                 /* (non-Javadoc) | 
| 118 |                  * @see java.lang.Object#toString() | 
| 119 |                  */ | 
| 120 |                 public String toString() { | 
| 121 |                         String type = null; | 
| 122 |                         String name = null; | 
| 123 |                         switch(element.getElementType()) { | 
| 124 |                                 case IElementDescriptor.FIELD: { | 
| 125 |                                         type = "Field"; //$NON-NLS-1$ | 
| 126 |                                         name = ((IMemberDescriptor) element).getName(); | 
| 127 |                                         break; | 
| 128 |                                 } | 
| 129 |                                 case IElementDescriptor.METHOD: { | 
| 130 |                                         type = "Method"; //$NON-NLS-1$ | 
| 131 |                                         name = ((IMemberDescriptor) element).getName(); | 
| 132 |                                         break; | 
| 133 |                                 } | 
| 134 |                                 case IElementDescriptor.PACKAGE: { | 
| 135 |                                         type = "Package"; //$NON-NLS-1$ | 
| 136 |                                         name = ((IPackageDescriptor) element).getName(); | 
| 137 |                                         break; | 
| 138 |                                 } | 
| 139 |                                 case IElementDescriptor.TYPE: { | 
| 140 |                                         type = "Type"; //$NON-NLS-1$ | 
| 141 |                                         name = ((IMemberDescriptor) element).getName(); | 
| 142 |                                         break; | 
| 143 |                                 } | 
| 144 |                         } | 
| 145 |                         StringBuffer buffer = new StringBuffer(); | 
| 146 |                         buffer.append(type == null ? "Unknown" : type).append(" Node: ").append(name == null ? "Unknown Name" : name); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | 
| 147 |                         buffer.append("\nVisibility: ").append(VisibilityModifiers.getVisibilityName(visibility)); //$NON-NLS-1$ | 
| 148 |                         buffer.append("\nRestrictions: ").append(RestrictionModifiers.getRestrictionText(restrictions)); //$NON-NLS-1$ | 
| 149 |                         if(parent != null) { | 
| 150 |                                 String pname = parent.element.getElementType() == IElementDescriptor.PACKAGE ?  | 
| 151 |                                                 ((IPackageDescriptor)parent.element).getName() : ((IMemberDescriptor)parent.element).getName(); | 
| 152 |                                 buffer.append("\nParent: ").append(pname); //$NON-NLS-1$ | 
| 153 |                         } | 
| 154 |                         return buffer.toString(); | 
| 155 |                 } | 
| 156 |   | 
| 157 |                 /* (non-Javadoc) | 
| 158 |                  * @see java.lang.Comparable#compareTo(java.lang.Object) | 
| 159 |                  */ | 
| 160 |                 public int compareTo(Object o) { | 
| 161 |                         if (o instanceof ManifestNode) { | 
| 162 |                                 ManifestNode node = (ManifestNode) o; | 
| 163 |                                 return ((ElementDescriptorImpl)element).compareTo(node.element); | 
| 164 |                         } | 
| 165 |                         return -1; | 
| 166 |                 } | 
| 167 |                  | 
| 168 |                 /** | 
| 169 |                  * Returns if the given node has API visibility. If the given node has {@link ApiDescription#VISIBILITY_INHERITED} visibility | 
| 170 |                  * this method recursively asks its' parent nodes if they have API visibility. | 
| 171 |                  * @param node | 
| 172 |                  * @return true if this node has API visibility false otherwise | 
| 173 |                  */ | 
| 174 |                 protected boolean hasApiVisibility(ManifestNode node) { | 
| 175 |                         if(DEBUG) { | 
| 176 |                                 System.out.println("Checking node for API visibility:"+node); //$NON-NLS-1$ | 
| 177 |                         } | 
| 178 |                         if(node != null) { | 
| 179 |                                 if(VisibilityModifiers.isAPI(node.visibility)) { | 
| 180 |                                         return true; | 
| 181 |                                 } | 
| 182 |                                 else if(node.visibility == VISIBILITY_INHERITED) { | 
| 183 |                                         return hasApiVisibility(node.parent); | 
| 184 |                                 } | 
| 185 |                         } | 
| 186 |                         return false; | 
| 187 |                 } | 
| 188 |                  | 
| 189 |                 /** | 
| 190 |                  * Ensure this node is up to date. Default implementation does | 
| 191 |                  * nothing. Subclasses should override as required. | 
| 192 |                  *  | 
| 193 |                  * Returns the resulting node if the node is valid, or <code>null</code> | 
| 194 |                  * if the node no longer exists. | 
| 195 |                  *  | 
| 196 |                  * @return up to date node, or <code>null</code> if no longer exists | 
| 197 |                  */ | 
| 198 |                 protected ManifestNode refresh() { | 
| 199 |                         if(DEBUG) { | 
| 200 |                                 System.out.println("Refreshing manifest node: "+this); //$NON-NLS-1$ | 
| 201 |                         } | 
| 202 |                         return this; | 
| 203 |                 } | 
| 204 |                  | 
| 205 |                 /** | 
| 206 |                  * Persists this node as a child of the given element. | 
| 207 |                  *  | 
| 208 |                  * @param document XML document | 
| 209 |                  * @param parent parent element in the document | 
| 210 |                  * @param component component the description is for or <code>null</code> | 
| 211 |                  */ | 
| 212 |                 void persistXML(Document document, Element parent) { | 
| 213 |                         if(RestrictionModifiers.isUnrestricted(this.restrictions)) {  | 
| 214 |                                 return; | 
| 215 |                         } | 
| 216 |                         switch (element.getElementType()) { | 
| 217 |                                 case IElementDescriptor.METHOD: { | 
| 218 |                                         IMethodDescriptor md = (IMethodDescriptor) element; | 
| 219 |                                         Element method = document.createElement(IApiXmlConstants.ELEMENT_METHOD); | 
| 220 |                                         method.setAttribute(IApiXmlConstants.ATTR_NAME, md.getName()); | 
| 221 |                                         method.setAttribute(IApiXmlConstants.ATTR_SIGNATURE, md.getSignature()); | 
| 222 |                                         persistAnnotations(method); | 
| 223 |                                         parent.appendChild(method); | 
| 224 |                                         break; | 
| 225 |                                 } | 
| 226 |                                 case IElementDescriptor.FIELD: { | 
| 227 |                                         IFieldDescriptor fd = (IFieldDescriptor) element; | 
| 228 |                                         Element field = document.createElement(IApiXmlConstants.ELEMENT_FIELD); | 
| 229 |                                         field.setAttribute(IApiXmlConstants.ATTR_NAME, fd.getName()); | 
| 230 |                                         persistAnnotations(field); | 
| 231 |                                         parent.appendChild(field); | 
| 232 |                                         break; | 
| 233 |                                 } | 
| 234 |                         } | 
| 235 |                 } | 
| 236 |                  | 
| 237 |                 /** | 
| 238 |                  * Adds visibility and restrictions to the XML element. | 
| 239 |                  *  | 
| 240 |                  * @param element XML element to annotate | 
| 241 |                  * @param component the component the description is for or <code>null</code> | 
| 242 |                  */ | 
| 243 |                 void persistAnnotations(Element element) { | 
| 244 |                         element.setAttribute(IApiXmlConstants.ATTR_VISIBILITY, Integer.toString(this.visibility)); | 
| 245 |                         element.setAttribute(IApiXmlConstants.ATTR_RESTRICTIONS, Integer.toString(this.restrictions)); | 
| 246 |                 } | 
| 247 |         } | 
| 248 |                  | 
| 249 |         /** | 
| 250 |          * This is a map of component names to a map of package names to package node objects represented as: | 
| 251 |          * <pre> | 
| 252 |          * HashMap<IElementDescriptor(package), ManifestNode(package)> | 
| 253 |          * </pre> | 
| 254 |          */ | 
| 255 |         protected HashMap fPackageMap = new HashMap(); | 
| 256 |   | 
| 257 |         /** | 
| 258 |          * This map holds the mapping of special access kinds for packages and has the  | 
| 259 |          * form: | 
| 260 |          * <pre> | 
| 261 |          * HashMap<IPackageDescriptor(package), HashMap<IElementDescriptor(component), IApiAccess>> | 
| 262 |          * </pre> | 
| 263 |          */ | 
| 264 |         protected HashMap fAccessMap = new HashMap(); | 
| 265 |          | 
| 266 |         private float fEmbeddedVersion = 0.0f; | 
| 267 |          | 
| 268 |         /** | 
| 269 |          * Constructs an API description owned by the specified component. | 
| 270 |          *  | 
| 271 |          * @param owningComponentId API component identifier or <code>null</code> if there | 
| 272 |          * is no specific owner. | 
| 273 |          */ | 
| 274 |         public ApiDescription(String owningComponentId) { | 
| 275 |                 fOwningComponentId = owningComponentId; | 
| 276 |         } | 
| 277 |   | 
| 278 |         /* (non-Javadoc) | 
| 279 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiDescription#accept(org.eclipse.pde.api.tools.internal.provisional.ApiDescriptionVisitor, org.eclipse.core.runtime.IProgressMonitor) | 
| 280 |          */ | 
| 281 |         public void accept(ApiDescriptionVisitor visitor, IProgressMonitor monitor) { | 
| 282 |                 visitChildren(visitor, fPackageMap, monitor); | 
| 283 |         } | 
| 284 |         /** | 
| 285 |          * Visits all children nodes in the given children map. | 
| 286 |          *  | 
| 287 |          * @param visitor visitor to visit | 
| 288 |          * @param childrenMap map of element name to manifest nodes | 
| 289 |          * @param monitor | 
| 290 |          */ | 
| 291 |         protected void visitChildren(ApiDescriptionVisitor visitor, Map childrenMap, IProgressMonitor monitor) { | 
| 292 |                 List elements = new ArrayList(childrenMap.keySet()); | 
| 293 |                 Collections.sort(elements); | 
| 294 |                 Iterator iterator = elements.iterator(); | 
| 295 |                 while (iterator.hasNext()) { | 
| 296 |                         Util.updateMonitor(monitor); | 
| 297 |                         IElementDescriptor element = (IElementDescriptor) iterator.next(); | 
| 298 |                         ManifestNode node = (ManifestNode) childrenMap.get(element); | 
| 299 |                         visitNode(visitor, node); | 
| 300 |                 } | 
| 301 |         } | 
| 302 |          | 
| 303 |         /** | 
| 304 |          * Compares the given version against the embedded version that has been  | 
| 305 |          * read from the API description | 
| 306 |          * @param version | 
| 307 |          * @return returns the same values as a compareTo call: | 
| 308 |          * <ul> | 
| 309 |          * <li> -1 if the given version is less than the embedded version</li> | 
| 310 |          * <li> 0 if the given version is equal to the embedded version</li> | 
| 311 |          * <li> 1 if the given version is greater than the embedded version</li> | 
| 312 |          * </ul> | 
| 313 |          */ | 
| 314 |         public int compareEmbeddedVersionTo(String version) { | 
| 315 |                 float lversion = Float.parseFloat(version); | 
| 316 |                 if(fEmbeddedVersion < lversion) { | 
| 317 |                         return 1; | 
| 318 |                 } | 
| 319 |                 if(fEmbeddedVersion == lversion) { | 
| 320 |                         return 0; | 
| 321 |                 } | 
| 322 |                 return -1; | 
| 323 |         } | 
| 324 |          | 
| 325 |         /** | 
| 326 |          * Allows the embedded version of this API description to be set. If the  | 
| 327 |          * given version string cannot be parsed to a valid version, the embedded version | 
| 328 |          * will default to the current version, as specified in {@link IApiXmlConstants#API_DESCRIPTION_CURRENT_VERSION} | 
| 329 |          *  | 
| 330 |          * @param version the version to set on this description | 
| 331 |          */ | 
| 332 |         public void setEmbeddedVersion(String version) { | 
| 333 |                 try { | 
| 334 |                         fEmbeddedVersion = Float.parseFloat(version); | 
| 335 |                 } | 
| 336 |                 catch(NumberFormatException nfe) { | 
| 337 |                         fEmbeddedVersion = Float.parseFloat(IApiXmlConstants.API_DESCRIPTION_CURRENT_VERSION); | 
| 338 |                 } | 
| 339 |         } | 
| 340 |          | 
| 341 |         /** | 
| 342 |          * Visits a node and its children. | 
| 343 |          *  | 
| 344 |          * @param visitor visitor to visit | 
| 345 |          * @param node node to visit | 
| 346 |          */ | 
| 347 |         private void visitNode(ApiDescriptionVisitor visitor, ManifestNode node) { | 
| 348 |                 int vis = node.visibility; | 
| 349 |                 ManifestNode tmp = node; | 
| 350 |                 while (tmp != null) { | 
| 351 |                         vis = tmp.visibility; | 
| 352 |                         if(tmp.visibility == VISIBILITY_INHERITED) { | 
| 353 |                                 tmp = tmp.parent; | 
| 354 |                         } | 
| 355 |                         else { | 
| 356 |                                 tmp = null; | 
| 357 |                         } | 
| 358 |                 } | 
| 359 |                 IApiAnnotations desc = new ApiAnnotations(vis, node.restrictions); | 
| 360 |                 boolean visitChildren = visitor.visitElement(node.element, desc); | 
| 361 |                 if (visitChildren && !node.children.isEmpty()) { | 
| 362 |                         visitChildren(visitor, node.children, null); | 
| 363 |                 } | 
| 364 |                 visitor.endVisitElement(node.element, desc); | 
| 365 |         } | 
| 366 |          | 
| 367 |         /** | 
| 368 |          * Returns the node in the manifest for specified element and context, closest node, or <code>null</code>. | 
| 369 |          * Creates a new node with default visibility and no restrictions if insert is <code>true</code> | 
| 370 |          * and a node is not present. Default visibility for packages is API, and for types is inherited. | 
| 371 |          *  | 
| 372 |          * @param element element | 
| 373 |          * @param write <code>true</code> if setting a node, <code>false</code> if getting a node | 
| 374 |          * @return manifest node or <code>null</code> | 
| 375 |          */ | 
| 376 |         protected ManifestNode findNode(IElementDescriptor element, boolean write) { | 
| 377 |                 if(DEBUG) { | 
| 378 |                         StringBuffer buffer = new StringBuffer(); | 
| 379 |                         buffer.append("Looking up manifest node for element: "); //$NON-NLS-1$ | 
| 380 |                         buffer.append(element); | 
| 381 |                         System.out.println(buffer.toString()); | 
| 382 |                 } | 
| 383 |                 IElementDescriptor[] path = element.getPath(); | 
| 384 |                 Map map = fPackageMap; | 
| 385 |                 ManifestNode parentNode = null; | 
| 386 |                 ManifestNode node = null; | 
| 387 |                 for (int i = 0 ; i < path.length; i++) { | 
| 388 |                         IElementDescriptor current = path[i]; | 
| 389 |                         parentNode = node; | 
| 390 |                         node = (ManifestNode) map.get(current); | 
| 391 |                         if (node == null) { | 
| 392 |                                 if (write || (isInsertOnResolve(current))) { | 
| 393 |                                         node = createNode(parentNode, current); | 
| 394 |                                         if (node != null) { | 
| 395 |                                                 map.put(current, node); | 
| 396 |                                         } else { | 
| 397 |                                                 return null; | 
| 398 |                                         } | 
| 399 |                                 } else { | 
| 400 |                                         if(DEBUG) { | 
| 401 |                                                 StringBuffer buffer = new StringBuffer(); | 
| 402 |                                                 buffer.append("Returning parent manifest node: "); //$NON-NLS-1$ | 
| 403 |                                                 buffer.append(parentNode); | 
| 404 |                                                 buffer.append(" when looking for element"); //$NON-NLS-1$ | 
| 405 |                                                 buffer.append(element); | 
| 406 |                                                 System.out.println(buffer.toString());  | 
| 407 |                                         } | 
| 408 |                                         return parentNode; | 
| 409 |                                 } | 
| 410 |                         } | 
| 411 |                         node = node.refresh(); | 
| 412 |                         if (node != null) { | 
| 413 |                                 map = node.children; | 
| 414 |                         } | 
| 415 |                 } | 
| 416 |                 if(DEBUG) { | 
| 417 |                         StringBuffer buffer = new StringBuffer(); | 
| 418 |                         buffer.append("Manifest node found: "); //$NON-NLS-1$ | 
| 419 |                         buffer.append(node); | 
| 420 |                         buffer.append(" when looking for element"); //$NON-NLS-1$ | 
| 421 |                         buffer.append(element); | 
| 422 |                         System.out.println(buffer.toString()); | 
| 423 |                 } | 
| 424 |                 return node; | 
| 425 |         } | 
| 426 |           | 
| 427 |         /* (non-Javadoc) | 
| 428 |          * @see org.eclipse.pde.api.tools.model.component.IApiDescription#resolveAPIDescription(java.lang.String, org.eclipse.pde.api.tools.model.component.IElementDescriptor) | 
| 429 |          */ | 
| 430 |         public IApiAnnotations resolveAnnotations(IElementDescriptor element) { | 
| 431 |                 ManifestNode node = findNode(element, false); | 
| 432 |                 if (node != null) { | 
| 433 |                         return resolveAnnotations(node, element); | 
| 434 |                 } | 
| 435 |                 else if(DEBUG){ | 
| 436 |                         StringBuffer buffer = new StringBuffer(); | 
| 437 |                         buffer.append("Tried to resolve annotations for manifest node: "); //$NON-NLS-1$ | 
| 438 |                         buffer.append(node); | 
| 439 |                         buffer.append(" but the node could not be found."); //$NON-NLS-1$ | 
| 440 |                         System.out.println(buffer.toString()); | 
| 441 |                 } | 
| 442 |                 return null; | 
| 443 |         } | 
| 444 |          | 
| 445 |         /** | 
| 446 |          * Resolves annotations based on inheritance for the given node and element. | 
| 447 |          *  | 
| 448 |          * @param node manifest node | 
| 449 |          * @param element the element annotations are being resolved for | 
| 450 |          * @return annotations | 
| 451 |          */ | 
| 452 |         protected IApiAnnotations resolveAnnotations(ManifestNode node, IElementDescriptor element) { | 
| 453 |                 ManifestNode visNode = node; | 
| 454 |                 int vis = visNode.visibility; | 
| 455 |                 while (vis == VISIBILITY_INHERITED) { | 
| 456 |                         visNode = visNode.parent; | 
| 457 |                         vis = visNode.visibility; | 
| 458 |                 } | 
| 459 |                 int res = RestrictionModifiers.NO_RESTRICTIONS; | 
| 460 |                 if (node.element.equals(element)) { | 
| 461 |                         res = node.restrictions; | 
| 462 |                 } | 
| 463 |                 if(DEBUG) { | 
| 464 |                         StringBuffer stringBuffer = new StringBuffer(); | 
| 465 |                         stringBuffer.append("Resolved annotations for manifest node: "); //$NON-NLS-1$ | 
| 466 |                         stringBuffer.append(node); | 
| 467 |                         stringBuffer.append(" to be: "); //$NON-NLS-1$ | 
| 468 |                         stringBuffer.append(VisibilityModifiers.getVisibilityName(vis)); | 
| 469 |                         stringBuffer.append(" "); //$NON-NLS-1$ | 
| 470 |                         stringBuffer.append(RestrictionModifiers.getRestrictionText(res)); | 
| 471 |                         System.out.println(stringBuffer.toString()); | 
| 472 |                 } | 
| 473 |                 return new ApiAnnotations(vis, res); | 
| 474 |         } | 
| 475 |          | 
| 476 |         /** | 
| 477 |          * Internal hook to clear the package map to remove stale data | 
| 478 |          */ | 
| 479 |         protected void clearPackages() { | 
| 480 |                 if(fPackageMap != null) { | 
| 481 |                         if(DEBUG) { | 
| 482 |                                 System.out.println("Clearing package map"); //$NON-NLS-1$ | 
| 483 |                         } | 
| 484 |                         fPackageMap.clear(); | 
| 485 |                 } | 
| 486 |         } | 
| 487 |          | 
| 488 |         /** | 
| 489 |          * Creates and returns a new manifest node to be inserted into the tree | 
| 490 |          * or <code>null</code> if the node does not exist. | 
| 491 |          *  | 
| 492 |          * <p> | 
| 493 |          * Subclasses should override this method as required. | 
| 494 |          * </p> | 
| 495 |          * @param parentNode parent node | 
| 496 |          * @param element element the node is to be created for | 
| 497 |          * @return new manifest node or <code>null</code> if none | 
| 498 |          */ | 
| 499 |         protected ManifestNode createNode(ManifestNode parentNode, IElementDescriptor element) { | 
| 500 |                 int vis = VISIBILITY_INHERITED; | 
| 501 |                 if (element.getElementType() == IElementDescriptor.PACKAGE) { | 
| 502 |                         vis = VisibilityModifiers.API; | 
| 503 |                 } | 
| 504 |                 if(DEBUG) { | 
| 505 |                         StringBuffer buffer = new StringBuffer(); | 
| 506 |                         buffer.append("Creating new manifest node for element: "); //$NON-NLS-1$ | 
| 507 |                         buffer.append(element); | 
| 508 |                         buffer.append(" and adding it to parent node: "); //$NON-NLS-1$ | 
| 509 |                         buffer.append(parentNode); | 
| 510 |                         System.out.println(buffer.toString()); | 
| 511 |                 } | 
| 512 |                 return new ManifestNode(parentNode, element, vis, RestrictionModifiers.NO_RESTRICTIONS); | 
| 513 |         } | 
| 514 |          | 
| 515 |         /* (non-Javadoc) | 
| 516 |          * @see org.eclipse.pde.api.tools.model.component.IApiDescription#setRestrictions(java.lang.String, org.eclipse.pde.api.tools.model.component.IElementDescriptor, int) | 
| 517 |          */ | 
| 518 |         public IStatus setRestrictions(IElementDescriptor element, int restrictions) { | 
| 519 |                 ManifestNode node = findNode(element, true); | 
| 520 |                 if(node != null) { | 
| 521 |                         if(DEBUG) { | 
| 522 |                                 StringBuffer buffer = new StringBuffer(); | 
| 523 |                                 buffer.append("Setting restrictions for manifest node: "); //$NON-NLS-1$ | 
| 524 |                                 buffer.append(node); | 
| 525 |                                 buffer.append(" to be "); //$NON-NLS-1$ | 
| 526 |                                 buffer.append(RestrictionModifiers.getRestrictionText(restrictions)); | 
| 527 |                                 System.out.println(buffer.toString()); | 
| 528 |                         } | 
| 529 |                         modified(); | 
| 530 |                         node.restrictions = restrictions; | 
| 531 |                         return Status.OK_STATUS; | 
| 532 |                 } | 
| 533 |                 return new Status(IStatus.ERROR, ApiPlugin.PLUGIN_ID, ELEMENT_NOT_FOUND, | 
| 534 |                                 MessageFormat.format("Failed to set API restriction: {0} not found in {1}", //$NON-NLS-1$ | 
| 535 |                                                 new String[]{element.toString(), fOwningComponentId}), null); | 
| 536 |         } | 
| 537 |   | 
| 538 |         /* (non-Javadoc) | 
| 539 |          * @see org.eclipse.pde.api.tools.model.component.IApiDescription#setVisibility(java.lang.String, org.eclipse.pde.api.tools.model.component.IElementDescriptor, int) | 
| 540 |          */ | 
| 541 |         public IStatus setVisibility(IElementDescriptor element, int visibility) { | 
| 542 |                 ManifestNode node = findNode(element, true); | 
| 543 |                 if(node != null) { | 
| 544 |                         if(DEBUG) { | 
| 545 |                                 StringBuffer buffer = new StringBuffer(); | 
| 546 |                                 buffer.append("Setting visibility for manifest node: "); //$NON-NLS-1$ | 
| 547 |                                 buffer.append(node); | 
| 548 |                                 buffer.append(" to be "); //$NON-NLS-1$ | 
| 549 |                                 buffer.append(VisibilityModifiers.getVisibilityName(visibility)); | 
| 550 |                                 System.out.println(buffer.toString()); | 
| 551 |                         } | 
| 552 |                         modified(); | 
| 553 |                         node.visibility = visibility; | 
| 554 |                         return Status.OK_STATUS; | 
| 555 |                 } | 
| 556 |                 return new Status(IStatus.ERROR, ApiPlugin.PLUGIN_ID, ELEMENT_NOT_FOUND, | 
| 557 |                                 MessageFormat.format("Failed to set API visibility: {0} not found in {1}", //$NON-NLS-1$ | 
| 558 |                                                 new String[]{element.toString(), fOwningComponentId}), null); | 
| 559 |         } | 
| 560 |          | 
| 561 |         public IStatus setAddedProfile(IElementDescriptor element, int addedProfile) { | 
| 562 |                 return Status.OK_STATUS; | 
| 563 |         } | 
| 564 |         public IStatus setRemovedProfile(IElementDescriptor element, | 
| 565 |                         int removedProfile) { | 
| 566 |                 return Status.OK_STATUS; | 
| 567 |         } | 
| 568 |         public IStatus setSuperclass(IElementDescriptor element, String superclass) { | 
| 569 |                 return Status.OK_STATUS; | 
| 570 |         } | 
| 571 |         public IStatus setSuperinterfaces(IElementDescriptor element, | 
| 572 |                         String superinterfaces) { | 
| 573 |                 return Status.OK_STATUS; | 
| 574 |         } | 
| 575 |         public IStatus setInterface(IElementDescriptor element, | 
| 576 |                         boolean interfaceFlag) { | 
| 577 |                 return Status.OK_STATUS; | 
| 578 |         } | 
| 579 |         /* (non-Javadoc) | 
| 580 |          * @see java.lang.Object#toString() | 
| 581 |          */ | 
| 582 |         public String toString() { | 
| 583 |                 StringBuffer buffer = new StringBuffer(); | 
| 584 |                 buffer.append("Api description for component: ").append(fOwningComponentId); //$NON-NLS-1$ | 
| 585 |                 return buffer.toString(); | 
| 586 |         } | 
| 587 |   | 
| 588 |         /** | 
| 589 |          * Returns whether a new node should be inserted into the API description | 
| 590 |          * when resolving the annotations for an element if a node is not already | 
| 591 |          * present, in the context of the given component. | 
| 592 |          * <p> | 
| 593 |          * Default implementation returns <code>false</code>. Subclasses should | 
| 594 |          * override this method as required. | 
| 595 |          * </p> | 
| 596 |          * @param elementDescriptor | 
| 597 |          * @return whether a new node should be inserted into the API description | 
| 598 |          * when resolving the annotations for an element if a node is not already | 
| 599 |          * present | 
| 600 |          */ | 
| 601 |         protected boolean isInsertOnResolve(IElementDescriptor elementDescriptor) { | 
| 602 |                 return false; | 
| 603 |         } | 
| 604 |          | 
| 605 |         /** | 
| 606 |          * Marks the description as modified | 
| 607 |          */ | 
| 608 |         protected synchronized void modified() { | 
| 609 |                 fModified = true; | 
| 610 |         } | 
| 611 |          | 
| 612 |         /** | 
| 613 |          * Returns whether this description has been modified. | 
| 614 |          *  | 
| 615 |          * @return | 
| 616 |          */ | 
| 617 |         protected synchronized boolean isModified() { | 
| 618 |                 return fModified; | 
| 619 |         } | 
| 620 |   | 
| 621 |         /* (non-Javadoc) | 
| 622 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiDescription#resolveAccessLevel(org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor, org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor) | 
| 623 |          */ | 
| 624 |         public IApiAccess resolveAccessLevel(IElementDescriptor element, IPackageDescriptor pelement) { | 
| 625 |                 if(fAccessMap != null) { | 
| 626 |                         HashMap map = (HashMap) fAccessMap.get(pelement); | 
| 627 |                         if(map != null) { | 
| 628 |                                 return (IApiAccess) map.get(element); | 
| 629 |                         } | 
| 630 |                 }  | 
| 631 |                 return null; | 
| 632 |         } | 
| 633 |          | 
| 634 |         /* (non-Javadoc) | 
| 635 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiDescription#setAccessLevel(org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor, org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor, int) | 
| 636 |          */ | 
| 637 |         public void setAccessLevel(IElementDescriptor element, IPackageDescriptor pelement, int access) { | 
| 638 |                 if(element != null && pelement != null && access != IApiAccess.NORMAL) { | 
| 639 |                         if(fAccessMap == null) { | 
| 640 |                                 fAccessMap = new HashMap(); | 
| 641 |                         } | 
| 642 |                         HashMap map = (HashMap) fAccessMap.get(pelement); | 
| 643 |                         if(map == null) { | 
| 644 |                                 map = new HashMap(); | 
| 645 |                                 fAccessMap.put(pelement, map); | 
| 646 |                         } | 
| 647 |                         map.put(element, new ApiAccess(access)); | 
| 648 |                 } | 
| 649 |         } | 
| 650 | } |