01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel.writer;
21:
22: import java.io.IOException;
23: import java.io.PrintWriter;
24: import java.io.Writer;
25:
26: import com.sun.codemodel.CodeWriter;
27: import com.sun.codemodel.JPackage;
28:
29: /**
30: * Writes all the source files under the specified file folder and
31: * inserts a file prolog comment in each java source file.
32: *
33: * @author
34: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
35: */
36: public class PrologCodeWriter extends FilterCodeWriter {
37:
38: /** prolog comment */
39: private final String prolog;
40:
41: /**
42: * @param core
43: * This CodeWriter will be used to actually create a storage for files.
44: * PrologCodeWriter simply decorates this underlying CodeWriter by
45: * adding prolog comments.
46: * @param prolog
47: * Strings that will be added as comments.
48: * This string may contain newlines to produce multi-line comments.
49: * '//' will be inserted at the beginning of each line to make it
50: * a valid Java comment, so the caller can just pass strings like
51: * "abc\ndef"
52: */
53: public PrologCodeWriter(CodeWriter core, String prolog) {
54: super (core);
55: this .prolog = prolog;
56: }
57:
58: public Writer openSource(JPackage pkg, String fileName)
59: throws IOException {
60: Writer w = super .openSource(pkg, fileName);
61:
62: PrintWriter out = new PrintWriter(w);
63:
64: // write prolog if this is a java source file
65: if (prolog != null) {
66: out.println("//");
67:
68: String s = prolog;
69: int idx;
70: while ((idx = s.indexOf('\n')) != -1) {
71: out.println("// " + s.substring(0, idx));
72: s = s.substring(idx + 1);
73: }
74: out.println("//");
75: out.println();
76: }
77: out.flush(); // we can't close the stream for that would close the undelying stream.
78:
79: return w;
80: }
81: }
|