< prev index next >

jaxws/src/jdk.xml.bind/share/classes/com/sun/codemodel/internal/writer/PrologCodeWriter.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, 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


  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     public Writer openSource(JPackage pkg, String fileName) throws IOException {
  66         Writer w = super.openSource(pkg,fileName);
  67 
  68         PrintWriter out = new PrintWriter(w);
  69 
  70         // write prolog if this is a java source file
  71         if( prolog != null ) {
  72             out.println( "//" );
  73 
  74             String s = prolog;
  75             int idx;
  76             while( (idx=s.indexOf('\n'))!=-1 ) {
  77                 out.println("// "+ s.substring(0,idx) );
  78                 s = s.substring(idx+1);
  79             }
  80             out.println("//");
  81             out.println();
  82         }
  83         out.flush();    // we can't close the stream for that would close the undelying stream.
  84 
   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


  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 
< prev index next >