jjg@57: /* jjg@57: * Copyright 2005-2008 Sun Microsystems, Inc. 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 jjg@57: * published by the Free Software Foundation. Sun designates this jjg@57: * particular file as subject to the "Classpath" exception as provided jjg@57: * by Sun 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: * jjg@57: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@57: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@57: * have any 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@57: import java.net.URI; jjg@57: import java.net.URISyntaxException; 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@57: final File f; 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@57: this.f = f; jjg@57: } jjg@57: jjg@57: public InputStream openInputStream() throws IOException { jjg@57: return new FileInputStream(f); jjg@57: } jjg@57: jjg@57: protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { jjg@57: return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); jjg@57: } jjg@57: jjg@57: public OutputStream openOutputStream() throws IOException { jjg@57: ensureParentDirectoriesExist(); jjg@57: return new FileOutputStream(f); jjg@57: } jjg@57: jjg@57: public Writer openWriter() throws IOException { jjg@57: ensureParentDirectoriesExist(); jjg@57: return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName()); jjg@57: } jjg@57: jjg@57: @Override jjg@57: protected String inferBinaryName(Iterable extends File> path) { jjg@57: String fPath = f.getPath(); jjg@57: //System.err.println("RegularFileObject " + file + " " +r.getPath()); jjg@57: for (File dir: path) { jjg@57: //System.err.println("dir: " + dir); jjg@57: String dPath = dir.getPath(); jjg@144: if (dPath.length() == 0) jjg@144: dPath = System.getProperty("user.dir"); jjg@57: if (!dPath.endsWith(File.separator)) jjg@57: dPath += File.separator; jjg@57: if (fPath.regionMatches(true, 0, dPath, 0, dPath.length()) jjg@57: && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) { jjg@57: String relativeName = fPath.substring(dPath.length()); jjg@57: return removeExtension(relativeName).replace(File.separatorChar, '.'); jjg@57: } jjg@57: } jjg@57: return null; jjg@57: } jjg@57: jjg@57: private void ensureParentDirectoriesExist() throws IOException { jjg@57: if (!hasParents) { jjg@57: File parent = f.getParentFile(); jjg@57: if (parent != null && !parent.exists()) { jjg@57: if (!parent.mkdirs()) { jjg@57: if (!parent.exists() || !parent.isDirectory()) { jjg@57: throw new IOException("could not create parent directories"); jjg@57: } jjg@57: } jjg@57: } jjg@57: hasParents = true; jjg@57: } jjg@57: } jjg@57: jjg@57: @Deprecated jjg@57: public String getName() { jjg@57: return name; jjg@57: } jjg@57: jjg@57: public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) { jjg@57: cn.getClass(); jjg@57: // null check jjg@57: if (kind == Kind.OTHER && getKind() != kind) { jjg@57: return false; jjg@57: } jjg@57: String n = cn + kind.extension; jjg@57: if (name.equals(n)) { jjg@57: return true; jjg@57: } jjg@57: if (name.equalsIgnoreCase(n)) { jjg@57: try { jjg@57: // allow for Windows jjg@57: return f.getCanonicalFile().getName().equals(n); jjg@57: } catch (IOException e) { jjg@57: } jjg@57: } jjg@57: return false; jjg@57: } jjg@57: jjg@57: @Deprecated jjg@57: public String getPath() { jjg@57: return f.getPath(); jjg@57: } jjg@57: jjg@57: public long getLastModified() { jjg@57: return f.lastModified(); jjg@57: } jjg@57: jjg@57: public boolean delete() { jjg@57: return f.delete(); jjg@57: } jjg@57: jjg@57: public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { jjg@57: CharBuffer cb = fileManager.getCachedContent(this); jjg@57: if (cb == null) { jjg@57: InputStream in = new FileInputStream(f); 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@57: public boolean equals(Object other) { jjg@57: if (!(other instanceof RegularFileObject)) { jjg@57: return false; jjg@57: } jjg@57: RegularFileObject o = (RegularFileObject) other; jjg@57: try { jjg@57: return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile()); jjg@57: } catch (IOException e) { jjg@57: return false; jjg@57: } jjg@57: } jjg@57: jjg@57: @Override jjg@57: public int hashCode() { jjg@57: return f.hashCode(); jjg@57: } jjg@57: jjg@57: public URI toUri() { jjg@57: try { jjg@57: String path = f.getAbsolutePath().replace(File.separatorChar, '/'); jjg@57: return new URI("file://" + path).normalize(); jjg@57: } catch (URISyntaxException ex) { jjg@57: return f.toURI(); jjg@57: } jjg@57: } jjg@57: }