| 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.io.File; | 
| 14 | import java.io.FileInputStream; | 
| 15 | import java.io.FileNotFoundException; | 
| 16 | import java.io.InputStream; | 
| 17 |   | 
| 18 | import org.eclipse.core.resources.IFile; | 
| 19 | import org.eclipse.core.runtime.CoreException; | 
| 20 | import org.eclipse.jdt.core.ICompilationUnit; | 
| 21 |   | 
| 22 | /** | 
| 23 |  * A compilation unit in the API context acts as a proxy to the stream of file contents. It holds  | 
| 24 |  * meta-data about the underlying file, but does not hold on to the actual contents of the file. | 
| 25 |  *  | 
| 26 |  * @since 1.0.0 | 
| 27 |  */ | 
| 28 | public class CompilationUnit { | 
| 29 |   | 
| 30 |         private String name = null; | 
| 31 |         private String filepath = null; | 
| 32 |         private ICompilationUnit unit = null; | 
| 33 |          | 
| 34 |         /** | 
| 35 |          * The full path to the file | 
| 36 |          * Constructor | 
| 37 |          * @param filepath the absolute path to the file. If the path points to a file that does | 
| 38 |          * not exist an {@link IllegalArgumentException} is thrown | 
| 39 |          */ | 
| 40 |         public CompilationUnit(String filepath) { | 
| 41 |                 File file = new File(filepath); | 
| 42 |                 if(!file.exists()) { | 
| 43 |                         throw new IllegalArgumentException("The specified path is not an existing file"); //$NON-NLS-1$ | 
| 44 |                 } | 
| 45 |                 this.filepath = filepath; | 
| 46 |                 name = file.getName(); | 
| 47 |         } | 
| 48 |          | 
| 49 |         public CompilationUnit(ICompilationUnit compilationUnit) { | 
| 50 |                 unit = compilationUnit; | 
| 51 |                 name = compilationUnit.getElementName(); | 
| 52 |         } | 
| 53 |          | 
| 54 |         /** | 
| 55 |          * @return the name of the file | 
| 56 |          */ | 
| 57 |         public String getName() { | 
| 58 |                 return name; | 
| 59 |         } | 
| 60 |          | 
| 61 |         /** | 
| 62 |          * Returns the input stream of the file | 
| 63 |          * @return the input stream of the files' contents | 
| 64 |          * @throws FileNotFoundException if the input stream could not connect | 
| 65 |          * to the actual file | 
| 66 |          */ | 
| 67 |         public InputStream getInputStream() throws FileNotFoundException { | 
| 68 |                 if (unit != null) { | 
| 69 |                         try { | 
| 70 |                                 return ((IFile)(unit.getCorrespondingResource())).getContents(); | 
| 71 |                         } catch (CoreException e) { | 
| 72 |                                 // TODO: should throw CoreException | 
| 73 |                                 throw new FileNotFoundException(e.getStatus().getMessage()); | 
| 74 |                         } | 
| 75 |                 } | 
| 76 |                 return new FileInputStream(new File(filepath)); | 
| 77 |         } | 
| 78 |          | 
| 79 |         /* (non-Javadoc) | 
| 80 |          * @see java.lang.Object#toString() | 
| 81 |          */ | 
| 82 |         public String toString() { | 
| 83 |                 return getName(); | 
| 84 |         } | 
| 85 | } |