JavadocEscapeWriter.java revision 524:dcaa586ab756
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
23 * questions.
24 */
25
26package com.sun.codemodel.internal.util;
27
28import java.io.FilterWriter;
29import java.io.IOException;
30import 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, &#x611B; (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 */
52public 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            out.write(ch);
66    }
67
68    public void write(char[] buf, int off, int len) throws IOException {
69        for( int i=0; i<len; i++ )
70            write(buf[off+i]);
71    }
72
73    public void write(char[] buf) throws IOException {
74        write(buf,0,buf.length);
75    }
76
77    public void write(String buf, int off, int len) throws IOException {
78        write( buf.toCharArray(), off, len );
79    }
80
81    public void write(String buf) throws IOException {
82        write( buf.toCharArray(), 0, buf.length() );
83    }
84
85}
86