1 /** 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. 5 * Portions Copyright 2013-2018 Philip Helger + contributors 6 * 7 * The contents of this file are subject to the terms of either the GNU 8 * General Public License Version 2 only ("GPL") or the Common Development 9 * and Distribution License("CDDL") (collectively, the "License"). You 10 * may not use this file except in compliance with the License. You can 11 * obtain a copy of the License at 12 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html 13 * or packager/legal/LICENSE.txt. See the License for the specific 14 * language governing permissions and limitations under the License. 15 * 16 * When distributing the software, include this License Header Notice in each 17 * file and include the License file at packager/legal/LICENSE.txt. 18 * 19 * GPL Classpath Exception: 20 * Oracle designates this particular file as subject to the "Classpath" 21 * exception as provided by Oracle in the GPL Version 2 section of the License 22 * file that accompanied this code. 23 * 24 * Modifications: 25 * If applicable, add the following below the License Header, with the fields 26 * enclosed by brackets [] replaced by your own identifying information: 27 * "Portions Copyright [year] [name of copyright owner]" 28 * 29 * Contributor(s): 30 * If you wish your version of this file to be governed by only the CDDL or 31 * only the GPL Version 2, indicate your decision by adding "[Contributor] 32 * elects to include this software in this distribution under the [CDDL or GPL 33 * Version 2] license." If you don't indicate a single choice of license, a 34 * recipient has the option to distribute your version of this file under 35 * either the CDDL, the GPL Version 2 or to extend the choice of license to 36 * its licensees as provided above. However, if you add GPL Version 2 code 37 * and therefore, elected the GPL Version 2 license, then the option applies 38 * only if the new code is made subject to such option by the copyright 39 * holder. 40 */ 41 package com.helger.jcodemodel.writer; 42 43 import java.io.FilterOutputStream; 44 import java.io.IOException; 45 import java.io.OutputStream; 46 import java.io.PrintStream; 47 48 import javax.annotation.Nonnull; 49 50 import com.helger.jcodemodel.JPackage; 51 52 /** 53 * Output all source files into a single stream with a little formatting header 54 * in front of each file. This is primarily for human consumption of the 55 * generated source code, such as to debug/test CodeModel or to quickly inspect 56 * the result. 57 * 58 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 59 */ 60 public class SingleStreamCodeWriter extends AbstractCodeWriter 61 { 62 private final PrintStream m_aPS; 63 private final boolean m_bDoClose; 64 65 /** 66 * @param aOS 67 * This stream will be closed at the end of the code generation. Except 68 * it is System.out or System.err 69 */ 70 public SingleStreamCodeWriter (@Nonnull final OutputStream aOS) 71 { 72 this (aOS, JCMWriter.getDefaultNewLine ()); 73 } 74 75 /** 76 * @param aOS 77 * This stream will be closed at the end of the code generation. Except 78 * it is System.out or System.err 79 * @param sNewLine 80 * The new line string to be used for source files 81 */ 82 public SingleStreamCodeWriter (@Nonnull final OutputStream aOS, @Nonnull final String sNewLine) 83 { 84 super (null, sNewLine); 85 // Do not close System.out or System.err 86 m_bDoClose = aOS != System.out && aOS != System.err; 87 m_aPS = aOS instanceof PrintStream ? (PrintStream) aOS : new PrintStream (aOS); 88 } 89 90 @Override 91 public OutputStream openBinary (@Nonnull final JPackage aPkg, @Nonnull final String sFilename) throws IOException 92 { 93 String sPkgName = aPkg.name (); 94 if (sPkgName.length () > 0) 95 sPkgName += '.'; 96 97 m_aPS.println ("-----------------------------------" + 98 sPkgName + 99 sFilename + 100 "-----------------------------------"); 101 102 return new FilterOutputStream (m_aPS) 103 { 104 @Override 105 public void close () 106 { 107 // don't let this stream close 108 } 109 }; 110 } 111 112 @Override 113 public void close () throws IOException 114 { 115 m_aPS.flush (); 116 if (m_bDoClose) 117 m_aPS.close (); 118 } 119 }