|
1 /* |
|
2 * Copyright 2006 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.codemodel.internal.writer; |
|
27 |
|
28 import java.io.File; |
|
29 import java.io.FileOutputStream; |
|
30 import java.io.IOException; |
|
31 import java.io.OutputStream; |
|
32 import java.util.HashSet; |
|
33 import java.util.Set; |
|
34 |
|
35 import com.sun.codemodel.internal.CodeWriter; |
|
36 import com.sun.codemodel.internal.JPackage; |
|
37 |
|
38 /** |
|
39 * Writes all the source files under the specified file folder. |
|
40 * |
|
41 * @author |
|
42 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
|
43 */ |
|
44 public class FileCodeWriter extends CodeWriter { |
|
45 |
|
46 /** The target directory to put source code. */ |
|
47 private final File target; |
|
48 |
|
49 /** specify whether or not to mark the generated files read-only */ |
|
50 private final boolean readOnly; |
|
51 |
|
52 /** Files that shall be marked as read only. */ |
|
53 private final Set<File> readonlyFiles = new HashSet<File>(); |
|
54 |
|
55 public FileCodeWriter( File target ) throws IOException { |
|
56 this(target,false); |
|
57 } |
|
58 |
|
59 public FileCodeWriter( File target, boolean readOnly ) throws IOException { |
|
60 this.target = target; |
|
61 this.readOnly = readOnly; |
|
62 if(!target.exists() || !target.isDirectory()) |
|
63 throw new IOException(target + ": non-existent directory"); |
|
64 } |
|
65 |
|
66 |
|
67 public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { |
|
68 return new FileOutputStream(getFile(pkg,fileName)); |
|
69 } |
|
70 |
|
71 protected File getFile(JPackage pkg, String fileName ) throws IOException { |
|
72 File dir; |
|
73 if(pkg.isUnnamed()) |
|
74 dir = target; |
|
75 else |
|
76 dir = new File(target, toDirName(pkg)); |
|
77 |
|
78 if(!dir.exists()) dir.mkdirs(); |
|
79 |
|
80 File fn = new File(dir,fileName); |
|
81 |
|
82 if (fn.exists()) { |
|
83 if (!fn.delete()) |
|
84 throw new IOException(fn + ": Can't delete previous version"); |
|
85 } |
|
86 |
|
87 |
|
88 if(readOnly) readonlyFiles.add(fn); |
|
89 return fn; |
|
90 } |
|
91 |
|
92 public void close() throws IOException { |
|
93 // mark files as read-onnly if necessary |
|
94 for (File f : readonlyFiles) |
|
95 f.setReadOnly(); |
|
96 } |
|
97 |
|
98 /** Converts a package name to the directory name. */ |
|
99 private static String toDirName( JPackage pkg ) { |
|
100 return pkg.name().replace('.',File.separatorChar); |
|
101 } |
|
102 |
|
103 } |