001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.codemodel.internal.writer;
027:
028: import java.io.File;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.io.OutputStream;
032: import java.util.HashSet;
033: import java.util.Set;
034:
035: import com.sun.codemodel.internal.CodeWriter;
036: import com.sun.codemodel.internal.JPackage;
037:
038: /**
039: * Writes all the source files under the specified file folder.
040: *
041: * @author
042: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
043: */
044: public class FileCodeWriter extends CodeWriter {
045:
046: /** The target directory to put source code. */
047: private final File target;
048:
049: /** specify whether or not to mark the generated files read-only */
050: private final boolean readOnly;
051:
052: /** Files that shall be marked as read only. */
053: private final Set<File> readonlyFiles = new HashSet<File>();
054:
055: public FileCodeWriter(File target) throws IOException {
056: this (target, false);
057: }
058:
059: public FileCodeWriter(File target, boolean readOnly)
060: throws IOException {
061: this .target = target;
062: this .readOnly = readOnly;
063: if (!target.exists() || !target.isDirectory())
064: throw new IOException(target + ": non-existent directory");
065: }
066:
067: public OutputStream openBinary(JPackage pkg, String fileName)
068: throws IOException {
069: return new FileOutputStream(getFile(pkg, fileName));
070: }
071:
072: protected File getFile(JPackage pkg, String fileName)
073: throws IOException {
074: File dir;
075: if (pkg.isUnnamed())
076: dir = target;
077: else
078: dir = new File(target, toDirName(pkg));
079:
080: if (!dir.exists())
081: dir.mkdirs();
082:
083: File fn = new File(dir, fileName);
084:
085: if (fn.exists()) {
086: if (!fn.delete())
087: throw new IOException(fn
088: + ": Can't delete previous version");
089: }
090:
091: if (readOnly)
092: readonlyFiles.add(fn);
093: return fn;
094: }
095:
096: public void close() throws IOException {
097: // mark files as read-onnly if necessary
098: for (File f : readonlyFiles)
099: f.setReadOnly();
100: }
101:
102: /** Converts a package name to the directory name. */
103: private static String toDirName(JPackage pkg) {
104: return pkg.name().replace('.', File.separatorChar);
105: }
106:
107: }
|