01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel.util;
21:
22: import java.io.FilterWriter;
23: import java.io.IOException;
24: import java.io.Writer;
25:
26: /**
27: * {@link Writer} that escapes characters that are unsafe
28: * as Javadoc comments.
29: *
30: * Such characters include '<' and '&'.
31: *
32: * <p>
33: * Note that this class doesn't escape other Unicode characters
34: * that are typically unsafe. For example, 愛 (A kanji
35: * that means "love") can be considered as unsafe because
36: * javac with English Windows cannot accept this character in the
37: * source code.
38: *
39: * <p>
40: * If the application needs to escape such characters as well, then
41: * they are on their own.
42: *
43: * @author
44: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
45: */
46: public class JavadocEscapeWriter extends FilterWriter {
47:
48: public JavadocEscapeWriter(Writer next) {
49: super (next);
50: }
51:
52: public void write(int ch) throws IOException {
53: if (ch == '<')
54: out.write("<");
55: else if (ch == '&')
56: out.write("&");
57: else
58: out.write(ch);
59: }
60:
61: public void write(char[] buf, int off, int len) throws IOException {
62: for (int i = 0; i < len; i++)
63: write(buf[off + i]);
64: }
65:
66: public void write(char[] buf) throws IOException {
67: write(buf, 0, buf.length);
68: }
69:
70: public void write(String buf, int off, int len) throws IOException {
71: write(buf.toCharArray(), off, len);
72: }
73:
74: public void write(String buf) throws IOException {
75: write(buf.toCharArray(), 0, buf.length());
76: }
77:
78: }
|