001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.codemodel.writer;
021:
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import com.sun.codemodel.CodeWriter;
030: import com.sun.codemodel.JPackage;
031:
032: /**
033: * Writes all the source files under the specified file folder.
034: *
035: * @author
036: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
037: */
038: public class FileCodeWriter extends CodeWriter {
039:
040: /** The target directory to put source code. */
041: private final File target;
042:
043: /** specify whether or not to mark the generated files read-only */
044: private final boolean readOnly;
045:
046: /** Files that shall be marked as read only. */
047: private final Set<File> readonlyFiles = new HashSet<File>();
048:
049: public FileCodeWriter(File target) throws IOException {
050: this (target, false);
051: }
052:
053: public FileCodeWriter(File target, boolean readOnly)
054: throws IOException {
055: this .target = target;
056: this .readOnly = readOnly;
057: if (!target.exists() || !target.isDirectory())
058: throw new IOException(target + ": non-existent directory");
059: }
060:
061: public OutputStream openBinary(JPackage pkg, String fileName)
062: throws IOException {
063: return new FileOutputStream(getFile(pkg, fileName));
064: }
065:
066: protected File getFile(JPackage pkg, String fileName)
067: throws IOException {
068: File dir;
069: if (pkg.isUnnamed())
070: dir = target;
071: else
072: dir = new File(target, toDirName(pkg));
073:
074: if (!dir.exists())
075: dir.mkdirs();
076:
077: File fn = new File(dir, fileName);
078:
079: if (fn.exists()) {
080: if (!fn.delete())
081: throw new IOException(fn
082: + ": Can't delete previous version");
083: }
084:
085: if (readOnly)
086: readonlyFiles.add(fn);
087: return fn;
088: }
089:
090: public void close() throws IOException {
091: // mark files as read-onnly if necessary
092: for (File f : readonlyFiles)
093: f.setReadOnly();
094: }
095:
096: /** Converts a package name to the directory name. */
097: private static String toDirName(JPackage pkg) {
098: return pkg.name().replace('.', File.separatorChar);
099: }
100:
101: }
|