src/share/jaxws_classes/com/sun/codemodel/internal/CodeWriter.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    26 package com.sun.codemodel.internal;
    28 import java.io.IOException;
    29 import java.io.OutputStream;
    30 import java.io.OutputStreamWriter;
    31 import java.io.Writer;
    32 import java.nio.charset.CharsetEncoder;
    34 import com.sun.codemodel.internal.util.EncoderFactory;
    35 import com.sun.codemodel.internal.util.UnicodeEscapeWriter;
    37 /**
    38  * Receives generated code and writes to the appropriate storage.
    39  *
    40  * @author
    41  *      Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    42  */
    43 public abstract class CodeWriter {
    45     /**
    46      * Encoding to be used by the writer. Null means platform specific encoding.
    47      *
    48      * @since 2.5
    49      */
    50     protected String encoding = null;
    52     /**
    53      * Called by CodeModel to store the specified file.
    54      * The callee must allocate a storage to store the specified file.
    55      *
    56      * <p>
    57      * The returned stream will be closed before the next file is
    58      * stored. So the callee can assume that only one OutputStream
    59      * is active at any given time.
    60      *
    61      * @param   pkg
    62      *      The package of the file to be written.
    63      * @param   fileName
    64      *      File name without the path. Something like
    65      *      "Foo.java" or "Bar.properties"
    66      */
    67     public abstract OutputStream openBinary( JPackage pkg, String fileName ) throws IOException;
    69     /**
    70      * Called by CodeModel to store the specified file.
    71      * The callee must allocate a storage to store the specified file.
    72      *
    73      * <p>
    74      * The returned stream will be closed before the next file is
    75      * stored. So the callee can assume that only one OutputStream
    76      * is active at any given time.
    77      *
    78      * @param   pkg
    79      *      The package of the file to be written.
    80      * @param   fileName
    81      *      File name without the path. Something like
    82      *      "Foo.java" or "Bar.properties"
    83      */
    84     public Writer openSource( JPackage pkg, String fileName ) throws IOException {
    85         final OutputStreamWriter bw = encoding != null
    86                 ? new OutputStreamWriter(openBinary(pkg,fileName), encoding)
    87                 : new OutputStreamWriter(openBinary(pkg,fileName));
    89         // create writer
    90         try {
    91             return new UnicodeEscapeWriter(bw) {
    92                 // can't change this signature to Encoder because
    93                 // we can't have Encoder in method signature
    94                 private final CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding());
    95                 @Override
    96                 protected boolean requireEscaping(int ch) {
    97                     // control characters
    98                     if( ch<0x20 && " \t\r\n".indexOf(ch)==-1 )  return true;
    99                     // check ASCII chars, for better performance
   100                     if( ch<0x80 )       return false;
   102                     return !encoder.canEncode((char)ch);
   103                 }
   104             };
   105         } catch( Throwable t ) {
   106             return new UnicodeEscapeWriter(bw);
   107         }
   108     }
   110     /**
   111      * Called by CodeModel at the end of the process.
   112      */
   113     public abstract void close() throws IOException;
   114 }

mercurial