| 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.model; | 
| 12 |   | 
| 13 | import org.eclipse.core.runtime.CoreException; | 
| 14 | import org.eclipse.pde.api.tools.internal.problems.ApiProblemFilter; | 
| 15 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; | 
| 16 | import org.eclipse.pde.api.tools.internal.provisional.Factory; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor; | 
| 20 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; | 
| 21 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; | 
| 22 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; | 
| 23 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 24 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; | 
| 25 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem; | 
| 26 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter; | 
| 27 |   | 
| 28 | /** | 
| 29 |  * Common implementation of an API component as a composite class file container. | 
| 30 |  *  | 
| 31 |  * @since 1.0.0 | 
| 32 |  */ | 
| 33 | public abstract class AbstractApiComponent extends AbstractApiTypeContainer implements IApiComponent { | 
| 34 |         /** | 
| 35 |          * API description | 
| 36 |          */ | 
| 37 |         private IApiDescription fApiDescription = null; | 
| 38 |                  | 
| 39 |         /** | 
| 40 |          * Api Filter store | 
| 41 |          */ | 
| 42 |         private IApiFilterStore fFilterStore = null; | 
| 43 |          | 
| 44 |         /** | 
| 45 |          * Constructs an API component in the given {@link IApiBaseline}. | 
| 46 |          *  | 
| 47 |          * @param baseline the parent {@link IApiBaseline} | 
| 48 |          */ | 
| 49 |         public AbstractApiComponent(IApiBaseline baseline) { | 
| 50 |                 super(baseline, IApiElement.COMPONENT, null); | 
| 51 |         } | 
| 52 |          | 
| 53 |         /* (non-Javadoc) | 
| 54 |          * @see org.eclipse.pde.api.tools.model.component.IClassFileContainer#accept(org.eclipse.pde.api.tools.model.component.ClassFileContainerVisitor) | 
| 55 |          */ | 
| 56 |         public void accept(ApiTypeContainerVisitor visitor) throws CoreException { | 
| 57 |                 if (visitor.visit(this)) { | 
| 58 |                         super.accept(visitor); | 
| 59 |                 } | 
| 60 |                 visitor.end(this); | 
| 61 |         }         | 
| 62 |                  | 
| 63 |         /** | 
| 64 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent#getHost() | 
| 65 |          */ | 
| 66 |         public IApiComponent getHost() throws CoreException { | 
| 67 |                 return null; | 
| 68 |         } | 
| 69 |          | 
| 70 |         /** | 
| 71 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiComponent#getBaseline() | 
| 72 |          */ | 
| 73 |         public IApiBaseline getBaseline() { | 
| 74 |                 return (IApiBaseline) getAncestor(IApiElement.BASELINE); | 
| 75 |         } | 
| 76 |   | 
| 77 |         /* (non-Javadoc) | 
| 78 |          * @see org.eclipse.pde.api.tools.model.component.IApiComponent#dispose() | 
| 79 |          */ | 
| 80 |         public void dispose() { | 
| 81 |                 try { | 
| 82 |                         close(); | 
| 83 |                 } catch (CoreException e) { | 
| 84 |                         ApiPlugin.log(e); | 
| 85 |                 } | 
| 86 |                 fApiDescription = null; | 
| 87 |         } | 
| 88 |   | 
| 89 |         /* (non-Javadoc) | 
| 90 |          * @see org.eclipse.pde.api.tools.internal.model.ApiElement#getApiComponent() | 
| 91 |          */ | 
| 92 |         public IApiComponent getApiComponent() { | 
| 93 |                 return this; | 
| 94 |         } | 
| 95 |          | 
| 96 |         /* (non-Javadoc) | 
| 97 |          * @see org.eclipse.pde.api.tools.model.component.IApiComponent#getApiDescription() | 
| 98 |          */ | 
| 99 |         public synchronized IApiDescription getApiDescription() throws CoreException { | 
| 100 |                 if (fApiDescription == null) { | 
| 101 |                         fApiDescription = createApiDescription(); | 
| 102 |                 } | 
| 103 |                 return fApiDescription; | 
| 104 |         } | 
| 105 |          | 
| 106 |         /** | 
| 107 |          * Returns whether this component has created an API description. | 
| 108 |          *  | 
| 109 |          * @return whether this component has created an API description | 
| 110 |          */ | 
| 111 |         protected synchronized boolean isApiDescriptionInitialized() { | 
| 112 |                 return fApiDescription != null; | 
| 113 |         } | 
| 114 |   | 
| 115 |         /** | 
| 116 |          * Returns if this component has created an API filter store | 
| 117 |          *  | 
| 118 |          * @return true if a store has been created, false other wise | 
| 119 |          */ | 
| 120 |         protected synchronized boolean hasApiFilterStore() { | 
| 121 |                 return fFilterStore != null; | 
| 122 |         } | 
| 123 |          | 
| 124 |         /* (non-Javadoc) | 
| 125 |          * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeContainer#getApiTypeContainers() | 
| 126 |          */ | 
| 127 |         public synchronized IApiTypeContainer[] getApiTypeContainers() throws CoreException { | 
| 128 |                 return super.getApiTypeContainers(); | 
| 129 |         } | 
| 130 |   | 
| 131 |         /* (non-Javadoc) | 
| 132 |          * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeContainer#getApiTypeContainers() | 
| 133 |          */ | 
| 134 |         public synchronized IApiTypeContainer[] getApiTypeContainers(String id) throws CoreException { | 
| 135 |                 if (this.hasFragments()) { | 
| 136 |                         return super.getApiTypeContainers(id); | 
| 137 |                 } else { | 
| 138 |                         return super.getApiTypeContainers(); | 
| 139 |                 } | 
| 140 |         } | 
| 141 |          | 
| 142 |         /** | 
| 143 |          * Creates and returns the API description for this component. | 
| 144 |          *  | 
| 145 |          * @return newly created API description for this component | 
| 146 |          */ | 
| 147 |         protected abstract IApiDescription createApiDescription() throws CoreException; | 
| 148 |   | 
| 149 |         /* (non-Javadoc) | 
| 150 |          * @see org.eclipse.pde.api.tools.IApiComponent#getFilterStore() | 
| 151 |          */ | 
| 152 |         public IApiFilterStore getFilterStore() throws CoreException { | 
| 153 |                 if(fFilterStore == null) { | 
| 154 |                         fFilterStore = createApiFilterStore(); | 
| 155 |                 } | 
| 156 |                 return fFilterStore; | 
| 157 |         } | 
| 158 |   | 
| 159 |         /* (non-Javadoc) | 
| 160 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiComponent#newProblemFilter(org.eclipse.pde.api.tools.internal.provisional.IApiProblem) | 
| 161 |          */ | 
| 162 |         public IApiProblemFilter newProblemFilter(IApiProblem problem) throws CoreException { | 
| 163 |                 //TODO either expose a way to make problems or change the method to accept the parts of a problem | 
| 164 |                 return new ApiProblemFilter(getId(), problem); | 
| 165 |         } | 
| 166 |          | 
| 167 |         /* (non-Javadoc) | 
| 168 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent#getHandle() | 
| 169 |          */ | 
| 170 |         public IElementDescriptor getHandle() { | 
| 171 |                 return Factory.componentDescriptor(this.getId(), this.getVersion()); | 
| 172 |         } | 
| 173 |          | 
| 174 |         /* (non-Javadoc) | 
| 175 |          * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType() | 
| 176 |          */ | 
| 177 |         public int getContainerType() { | 
| 178 |                 return IApiTypeContainer.COMPONENT; | 
| 179 |         } | 
| 180 |          | 
| 181 |         /** | 
| 182 |          * Lazily creates a new {@link IApiFilterStore} when it is requested | 
| 183 |          *  | 
| 184 |          * @return the current {@link IApiFilterStore} for this component | 
| 185 |          * @throws CoreException | 
| 186 |          */ | 
| 187 |         protected abstract IApiFilterStore createApiFilterStore() throws CoreException;         | 
| 188 | } |