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