|
1 /* |
|
2 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javac.file; |
|
27 |
|
28 import java.io.File; |
|
29 import java.io.FileInputStream; |
|
30 import java.io.FileOutputStream; |
|
31 import java.io.IOException; |
|
32 import java.io.InputStream; |
|
33 import java.io.OutputStream; |
|
34 import java.io.OutputStreamWriter; |
|
35 import java.io.Writer; |
|
36 import java.net.URI; |
|
37 import java.net.URISyntaxException; |
|
38 import java.nio.ByteBuffer; |
|
39 import java.nio.CharBuffer; |
|
40 import java.nio.charset.CharsetDecoder; |
|
41 import javax.tools.JavaFileObject; |
|
42 |
|
43 /** |
|
44 * A subclass of JavaFileObject representing regular files. |
|
45 */ |
|
46 class RegularFileObject extends BaseFileObject { |
|
47 |
|
48 /** Have the parent directories been created? |
|
49 */ |
|
50 private boolean hasParents = false; |
|
51 private String name; |
|
52 final File f; |
|
53 |
|
54 public RegularFileObject(JavacFileManager fileManager, File f) { |
|
55 this(fileManager, f.getName(), f); |
|
56 } |
|
57 |
|
58 public RegularFileObject(JavacFileManager fileManager, String name, File f) { |
|
59 super(fileManager); |
|
60 if (f.isDirectory()) { |
|
61 throw new IllegalArgumentException("directories not supported"); |
|
62 } |
|
63 this.name = name; |
|
64 this.f = f; |
|
65 } |
|
66 |
|
67 public InputStream openInputStream() throws IOException { |
|
68 return new FileInputStream(f); |
|
69 } |
|
70 |
|
71 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
|
72 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
|
73 } |
|
74 |
|
75 public OutputStream openOutputStream() throws IOException { |
|
76 ensureParentDirectoriesExist(); |
|
77 return new FileOutputStream(f); |
|
78 } |
|
79 |
|
80 public Writer openWriter() throws IOException { |
|
81 ensureParentDirectoriesExist(); |
|
82 return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName()); |
|
83 } |
|
84 |
|
85 @Override |
|
86 protected String inferBinaryName(Iterable<? extends File> path) { |
|
87 String fPath = f.getPath(); |
|
88 //System.err.println("RegularFileObject " + file + " " +r.getPath()); |
|
89 for (File dir: path) { |
|
90 //System.err.println("dir: " + dir); |
|
91 String dPath = dir.getPath(); |
|
92 if (!dPath.endsWith(File.separator)) |
|
93 dPath += File.separator; |
|
94 if (fPath.regionMatches(true, 0, dPath, 0, dPath.length()) |
|
95 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) { |
|
96 String relativeName = fPath.substring(dPath.length()); |
|
97 return removeExtension(relativeName).replace(File.separatorChar, '.'); |
|
98 } |
|
99 } |
|
100 return null; |
|
101 } |
|
102 |
|
103 private void ensureParentDirectoriesExist() throws IOException { |
|
104 if (!hasParents) { |
|
105 File parent = f.getParentFile(); |
|
106 if (parent != null && !parent.exists()) { |
|
107 if (!parent.mkdirs()) { |
|
108 if (!parent.exists() || !parent.isDirectory()) { |
|
109 throw new IOException("could not create parent directories"); |
|
110 } |
|
111 } |
|
112 } |
|
113 hasParents = true; |
|
114 } |
|
115 } |
|
116 |
|
117 @Deprecated |
|
118 public String getName() { |
|
119 return name; |
|
120 } |
|
121 |
|
122 public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) { |
|
123 cn.getClass(); |
|
124 // null check |
|
125 if (kind == Kind.OTHER && getKind() != kind) { |
|
126 return false; |
|
127 } |
|
128 String n = cn + kind.extension; |
|
129 if (name.equals(n)) { |
|
130 return true; |
|
131 } |
|
132 if (name.equalsIgnoreCase(n)) { |
|
133 try { |
|
134 // allow for Windows |
|
135 return f.getCanonicalFile().getName().equals(n); |
|
136 } catch (IOException e) { |
|
137 } |
|
138 } |
|
139 return false; |
|
140 } |
|
141 |
|
142 @Deprecated |
|
143 public String getPath() { |
|
144 return f.getPath(); |
|
145 } |
|
146 |
|
147 public long getLastModified() { |
|
148 return f.lastModified(); |
|
149 } |
|
150 |
|
151 public boolean delete() { |
|
152 return f.delete(); |
|
153 } |
|
154 |
|
155 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
156 CharBuffer cb = fileManager.getCachedContent(this); |
|
157 if (cb == null) { |
|
158 InputStream in = new FileInputStream(f); |
|
159 try { |
|
160 ByteBuffer bb = fileManager.makeByteBuffer(in); |
|
161 JavaFileObject prev = fileManager.log.useSource(this); |
|
162 try { |
|
163 cb = fileManager.decode(bb, ignoreEncodingErrors); |
|
164 } finally { |
|
165 fileManager.log.useSource(prev); |
|
166 } |
|
167 fileManager.recycleByteBuffer(bb); |
|
168 if (!ignoreEncodingErrors) { |
|
169 fileManager.cache(this, cb); |
|
170 } |
|
171 } finally { |
|
172 in.close(); |
|
173 } |
|
174 } |
|
175 return cb; |
|
176 } |
|
177 |
|
178 @Override |
|
179 public boolean equals(Object other) { |
|
180 if (!(other instanceof RegularFileObject)) { |
|
181 return false; |
|
182 } |
|
183 RegularFileObject o = (RegularFileObject) other; |
|
184 try { |
|
185 return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile()); |
|
186 } catch (IOException e) { |
|
187 return false; |
|
188 } |
|
189 } |
|
190 |
|
191 @Override |
|
192 public int hashCode() { |
|
193 return f.hashCode(); |
|
194 } |
|
195 |
|
196 public URI toUri() { |
|
197 try { |
|
198 String path = f.getAbsolutePath().replace(File.separatorChar, '/'); |
|
199 return new URI("file://" + path).normalize(); |
|
200 } catch (URISyntaxException ex) { |
|
201 return f.toURI(); |
|
202 } |
|
203 } |
|
204 } |