jjg@57: /* ohair@554: * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved. jjg@57: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@57: * jjg@57: * This code is free software; you can redistribute it and/or modify it jjg@57: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@57: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@57: * jjg@57: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@57: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@57: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@57: * version 2 for more details (a copy is included in the LICENSE file that jjg@57: * accompanied this code). jjg@57: * jjg@57: * You should have received a copy of the GNU General Public License version jjg@57: * 2 along with this work; if not, write to the Free Software Foundation, jjg@57: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@57: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@57: */ jjg@57: jjg@57: package com.sun.tools.javac.file; jjg@57: jjg@57: import java.io.File; jjg@57: import java.io.FileInputStream; jjg@57: import java.io.FileOutputStream; jjg@57: import java.io.IOException; jjg@57: import java.io.InputStream; jjg@57: import java.io.OutputStream; jjg@57: import java.io.OutputStreamWriter; jjg@57: import java.io.Writer; jjg@424: import java.lang.ref.Reference; jjg@424: import java.lang.ref.SoftReference; jjg@57: import java.net.URI; jjg@57: import java.nio.ByteBuffer; jjg@57: import java.nio.CharBuffer; jjg@57: import java.nio.charset.CharsetDecoder; jjg@57: import javax.tools.JavaFileObject; jjg@57: jjg@57: /** jjg@57: * A subclass of JavaFileObject representing regular files. jjg@333: * jjg@333: *

This is NOT part of any API supported by Sun Microsystems. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. jjg@57: */ jjg@57: class RegularFileObject extends BaseFileObject { jjg@57: jjg@57: /** Have the parent directories been created? jjg@57: */ jjg@57: private boolean hasParents = false; jjg@57: private String name; jjg@424: final File file; jjg@424: private Reference absFileRef; jjg@57: jjg@57: public RegularFileObject(JavacFileManager fileManager, File f) { jjg@57: this(fileManager, f.getName(), f); jjg@57: } jjg@57: jjg@57: public RegularFileObject(JavacFileManager fileManager, String name, File f) { jjg@57: super(fileManager); jjg@57: if (f.isDirectory()) { jjg@57: throw new IllegalArgumentException("directories not supported"); jjg@57: } jjg@57: this.name = name; jjg@424: this.file = f; jjg@57: } jjg@57: jjg@415: @Override jjg@415: public URI toUri() { jjg@424: return file.toURI().normalize(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getName() { jjg@424: return file.getPath(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getShortName() { jjg@415: return name; jjg@415: } jjg@415: jjg@415: @Override jjg@415: public JavaFileObject.Kind getKind() { jjg@415: return getKind(name); jjg@415: } jjg@415: jjg@415: @Override jjg@57: public InputStream openInputStream() throws IOException { jjg@424: return new FileInputStream(file); jjg@57: } jjg@57: jjg@400: @Override jjg@57: public OutputStream openOutputStream() throws IOException { jjg@57: ensureParentDirectoriesExist(); jjg@424: return new FileOutputStream(file); jjg@57: } jjg@57: jjg@57: @Override jjg@57: public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { jjg@57: CharBuffer cb = fileManager.getCachedContent(this); jjg@57: if (cb == null) { jjg@424: InputStream in = new FileInputStream(file); jjg@57: try { jjg@57: ByteBuffer bb = fileManager.makeByteBuffer(in); jjg@57: JavaFileObject prev = fileManager.log.useSource(this); jjg@57: try { jjg@57: cb = fileManager.decode(bb, ignoreEncodingErrors); jjg@57: } finally { jjg@57: fileManager.log.useSource(prev); jjg@57: } jjg@57: fileManager.recycleByteBuffer(bb); jjg@57: if (!ignoreEncodingErrors) { jjg@57: fileManager.cache(this, cb); jjg@57: } jjg@57: } finally { jjg@57: in.close(); jjg@57: } jjg@57: } jjg@57: return cb; jjg@57: } jjg@57: jjg@57: @Override jjg@415: public Writer openWriter() throws IOException { jjg@415: ensureParentDirectoriesExist(); jjg@424: return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName()); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public long getLastModified() { jjg@424: return file.lastModified(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public boolean delete() { jjg@424: return file.delete(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { jjg@415: return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); jjg@415: } jjg@415: jjg@415: @Override jjg@415: protected String inferBinaryName(Iterable path) { jjg@424: String fPath = file.getPath(); jjg@415: //System.err.println("RegularFileObject " + file + " " +r.getPath()); jjg@415: for (File dir: path) { jjg@415: //System.err.println("dir: " + dir); jjg@415: String dPath = dir.getPath(); jjg@415: if (dPath.length() == 0) jjg@415: dPath = System.getProperty("user.dir"); jjg@415: if (!dPath.endsWith(File.separator)) jjg@415: dPath += File.separator; jjg@415: if (fPath.regionMatches(true, 0, dPath, 0, dPath.length()) jjg@415: && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) { jjg@415: String relativeName = fPath.substring(dPath.length()); jjg@415: return removeExtension(relativeName).replace(File.separatorChar, '.'); jjg@415: } jjg@415: } jjg@415: return null; jjg@415: } jjg@415: jjg@415: @Override jjg@415: public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) { jjg@415: cn.getClass(); jjg@415: // null check jjg@415: if (kind == Kind.OTHER && getKind() != kind) { jjg@415: return false; jjg@415: } jjg@415: String n = cn + kind.extension; jjg@415: if (name.equals(n)) { jjg@415: return true; jjg@415: } jjg@415: if (name.equalsIgnoreCase(n)) { jjg@415: try { jjg@415: // allow for Windows jjg@424: return file.getCanonicalFile().getName().equals(n); jjg@415: } catch (IOException e) { jjg@415: } jjg@415: } jjg@415: return false; jjg@415: } jjg@415: jjg@415: private void ensureParentDirectoriesExist() throws IOException { jjg@415: if (!hasParents) { jjg@424: File parent = file.getParentFile(); jjg@415: if (parent != null && !parent.exists()) { jjg@415: if (!parent.mkdirs()) { jjg@415: if (!parent.exists() || !parent.isDirectory()) { jjg@415: throw new IOException("could not create parent directories"); jjg@415: } jjg@415: } jjg@415: } jjg@415: hasParents = true; jjg@415: } jjg@415: } jjg@415: jjg@424: /** jjg@424: * Check if two file objects are equal. jjg@424: * Two RegularFileObjects are equal if the absolute paths of the underlying jjg@424: * files are equal. jjg@424: */ jjg@415: @Override jjg@57: public boolean equals(Object other) { jjg@424: if (this == other) jjg@424: return true; jjg@424: jjg@424: if (!(other instanceof RegularFileObject)) jjg@57: return false; jjg@424: jjg@57: RegularFileObject o = (RegularFileObject) other; jjg@424: return getAbsoluteFile().equals(o.getAbsoluteFile()); jjg@57: } jjg@57: jjg@57: @Override jjg@57: public int hashCode() { jjg@424: return getAbsoluteFile().hashCode(); jjg@424: } jjg@424: jjg@424: private File getAbsoluteFile() { jjg@424: File absFile = (absFileRef == null ? null : absFileRef.get()); jjg@424: if (absFile == null) { jjg@424: absFile = file.getAbsoluteFile(); jjg@424: absFileRef = new SoftReference(absFile); jjg@424: } jjg@424: return absFile; jjg@57: } jjg@57: }