| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2008, 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 java.io.IOException; | 
| 14 | import java.io.InputStream; | 
| 15 |   | 
| 16 | import org.eclipse.core.resources.IFile; | 
| 17 | import org.eclipse.core.runtime.CoreException; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 20 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; | 
| 21 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 22 |   | 
| 23 | /** | 
| 24 |  * A class file corresponding to a resource in the workspace. | 
| 25 |  *  | 
| 26 |  * @since 1.0 | 
| 27 |  */ | 
| 28 | public class ResourceApiTypeRoot extends AbstractApiTypeRoot { | 
| 29 |          | 
| 30 |         /** | 
| 31 |          * Corresponding file | 
| 32 |          */ | 
| 33 |         private IFile fFile; | 
| 34 |   | 
| 35 |         /** | 
| 36 |          * Constructs an {@link IApiTypeRoot} on the underlying file. | 
| 37 |          *  | 
| 38 |          * @param parent the {@link IApiElement} parent or <code>null</code> if none | 
| 39 |          * @param file underlying resource | 
| 40 |          * @param component API component the class file originates from | 
| 41 |          */ | 
| 42 |         public ResourceApiTypeRoot(IApiElement parent, IFile file, String typeName) { | 
| 43 |                 super(parent, typeName); | 
| 44 |                 fFile = file; | 
| 45 |         } | 
| 46 |   | 
| 47 |         /* (non-Javadoc) | 
| 48 |          * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeRoot#getContents() | 
| 49 |          */ | 
| 50 |         public byte[] getContents() throws CoreException { | 
| 51 |                 InputStream stream = fFile.getContents(); | 
| 52 |                 try { | 
| 53 |                         return Util.getInputStreamAsByteArray(stream, -1); | 
| 54 |                 } | 
| 55 |                 catch (IOException ioe) { | 
| 56 |                         abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$ | 
| 57 |                         return null; | 
| 58 |                 } | 
| 59 |                 finally { | 
| 60 |                         try { | 
| 61 |                                 stream.close(); | 
| 62 |                         } catch (IOException e) { | 
| 63 |                                 ApiPlugin.log(e); | 
| 64 |                         } | 
| 65 |                 } | 
| 66 |         } | 
| 67 |          | 
| 68 |         /* (non-Javadoc) | 
| 69 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeRoot#getTypeName() | 
| 70 |          */ | 
| 71 |         public String getTypeName() { | 
| 72 |                 return getName(); | 
| 73 |         } | 
| 74 |   | 
| 75 |         /* (non-Javadoc) | 
| 76 |          * @see java.lang.Object#toString() | 
| 77 |          */ | 
| 78 |         public String toString() { | 
| 79 |                 return getTypeName(); | 
| 80 |         } | 
| 81 |          | 
| 82 |         /* (non-Javadoc) | 
| 83 |          * @see java.lang.Object#hashCode() | 
| 84 |          */ | 
| 85 |         public int hashCode() { | 
| 86 |                 return getName().hashCode(); | 
| 87 |         } | 
| 88 |          | 
| 89 |         /* (non-Javadoc) | 
| 90 |          * @see java.lang.Object#equals(java.lang.Object) | 
| 91 |          */ | 
| 92 |         public boolean equals(Object obj) { | 
| 93 |                 if(obj instanceof IApiTypeRoot) { | 
| 94 |                         IApiTypeRoot file = (IApiTypeRoot) obj; | 
| 95 |                         return getName().equals(file.getTypeName()); | 
| 96 |                 } | 
| 97 |                 return super.equals(obj); | 
| 98 |         } | 
| 99 | } |