1 /*
   2  * Copyright (c) 1997, 2014, 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.util;
  27 
  28 import java.io.FilterWriter;
  29 import java.io.IOException;
  30 import java.io.Writer;
  31 
  32 /**
  33  * {@link Writer} that escapes characters that are unsafe
  34  * as Javadoc comments.
  35  *
  36  * Such characters include '<' and '&'.
  37  *
  38  * <p>
  39  * Note that this class doesn't escape other Unicode characters
  40  * that are typically unsafe. For example, 愛 (A kanji
  41  * that means "love") can be considered as unsafe because
  42  * javac with English Windows cannot accept this character in the
  43  * source code.
  44  *
  45  * <p>
  46  * If the application needs to escape such characters as well, then
  47  * they are on their own.
  48  *
  49  * @author
  50  *      Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  51  */
  52 public class JavadocEscapeWriter extends FilterWriter {
  53 
  54     public JavadocEscapeWriter( Writer next ) {
  55         super(next);
  56     }
  57 
  58     public void write(int ch) throws IOException {
  59         if(ch=='<')
  60             out.write("&lt;");
  61         else
  62         if(ch=='&')
  63             out.write("&amp;");
  64         else
  65         if(ch=='>')
  66             out.write("&gt;");
  67         else
  68             out.write(ch);
  69     }
  70 
  71     public void write(char[] buf, int off, int len) throws IOException {
  72         for( int i=0; i<len; i++ )
  73             write(buf[off+i]);
  74     }
  75 
  76     public void write(char[] buf) throws IOException {
  77         write(buf,0,buf.length);
  78     }
  79 
  80     public void write(String buf, int off, int len) throws IOException {
  81         write( buf.toCharArray(), off, len );
  82     }
  83 
  84     public void write(String buf) throws IOException {
  85         write( buf.toCharArray(), 0, buf.length() );
  86     }
  87 
  88 }
--- EOF ---