01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.util;
11:
12: import java.net.URI;
13:
14: import org.eclipse.core.resources.IFile;
15: import org.eclipse.core.runtime.CoreException;
16: import org.eclipse.jdt.core.compiler.CharOperation;
17: import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
18:
19: /**
20: * An ICompilationUnit that retrieves its contents using an IFile
21: */
22: public class ResourceCompilationUnit extends CompilationUnit {
23:
24: private IFile file;
25:
26: public ResourceCompilationUnit(IFile file, URI location) {
27: super (null/*no contents*/, location == null ? file
28: .getFullPath().toString() : location.getPath(), null/*encoding is used only when retrieving the contents*/);
29: this .file = file;
30: }
31:
32: public char[] getContents() {
33: if (this .contents != null)
34: return this .contents; // answer the cached source
35:
36: // otherwise retrieve it
37: try {
38: return Util.getResourceContentsAsCharArray(this .file);
39: } catch (CoreException e) {
40: return CharOperation.NO_CHAR;
41: }
42: }
43: }
|