alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.api.tools.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;

/**
 * A compilation unit in the API context acts as a proxy to the stream of file contents. It holds 
 * meta-data about the underlying file, but does not hold on to the actual contents of the file.
 * 
 * @since 1.0.0
 */
public class CompilationUnit {

	private String name = null;
	private String filepath = null;
	private ICompilationUnit unit = null;
	
	/**
	 * The full path to the file
	 * Constructor
	 * @param filepath the absolute path to the file. If the path points to a file that does
	 * not exist an {@link IllegalArgumentException} is thrown
	 */
	public CompilationUnit(String filepath) {
		File file = new File(filepath);
		if(!file.exists()) {
			throw new IllegalArgumentException("The specified path is not an existing file"); //$NON-NLS-1$
		}
		this.filepath = filepath;
		name = file.getName();
	}
	
	public CompilationUnit(ICompilationUnit compilationUnit) {
		unit = compilationUnit;
		name = compilationUnit.getElementName();
	}
	
	/**
	 * @return the name of the file
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Returns the input stream of the file
	 * @return the input stream of the files' contents
	 * @throws FileNotFoundException if the input stream could not connect
	 * to the actual file
	 */
	public InputStream getInputStream() throws FileNotFoundException {
		if (unit != null) {
			try {
				return ((IFile)(unit.getCorrespondingResource())).getContents();
			} catch (CoreException e) {
				// TODO: should throw CoreException
				throw new FileNotFoundException(e.getStatus().getMessage());
			}
		}
		return new FileInputStream(new File(filepath));
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.