1 /* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.codemodel.internal.writer; 27 28 import java.io.IOException; 29 import java.io.PrintWriter; 30 import java.io.Writer; 31 32 import com.sun.codemodel.internal.CodeWriter; 33 import com.sun.codemodel.internal.JPackage; 34 35 /** 36 * Writes all the source files under the specified file folder and 37 * inserts a file prolog comment in each java source file. 38 * 39 * @author 40 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 41 */ 42 public class PrologCodeWriter extends FilterCodeWriter { 43 44 /** prolog comment */ 45 private final String prolog; 46 47 /** 48 * @param core 49 * This CodeWriter will be used to actually create a storage for files. 50 * PrologCodeWriter simply decorates this underlying CodeWriter by 51 * adding prolog comments. 52 * @param prolog 53 * Strings that will be added as comments. 54 * This string may contain newlines to produce multi-line comments. 55 * '//' will be inserted at the beginning of each line to make it 56 * a valid Java comment, so the caller can just pass strings like 57 * "abc\ndef" 58 */ 59 public PrologCodeWriter( CodeWriter core, String prolog ) { 60 super(core); 61 this.prolog = prolog; 62 } 63 64 65 @Override 66 public Writer openSource(JPackage pkg, String fileName) throws IOException { 67 Writer w = super.openSource(pkg,fileName); 68 69 PrintWriter out = new PrintWriter(w); 70 71 // write prolog if this is a java source file 72 if( prolog != null ) { 73 out.println( "//" ); 74 75 String s = prolog; 76 int idx; 77 while( (idx=s.indexOf('\n'))!=-1 ) { 78 out.println("// "+ s.substring(0,idx) ); 79 s = s.substring(idx+1); 80 } 81 out.println("//"); 82 out.println(); 83 } 84 out.flush(); // we can't close the stream for that would close the undelying stream. 85 86 return w; 87 } 88 }