01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: /*
27: * @(#)$Id: MinimumEscapeHandler.java,v 1.1 2005/04/15 20:03:47 kohsuke Exp $
28: */
29:
30: package com.sun.xml.internal.bind.marshaller;
31:
32: import java.io.IOException;
33: import java.io.Writer;
34:
35: /**
36: * Performs no character escaping. Usable only when the output encoding
37: * is UTF, but this handler gives the maximum performance.
38: *
39: * @author
40: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
41: */
42: public class MinimumEscapeHandler implements CharacterEscapeHandler {
43:
44: private MinimumEscapeHandler() {
45: } // no instanciation please
46:
47: public static final CharacterEscapeHandler theInstance = new MinimumEscapeHandler();
48:
49: public void escape(char[] ch, int start, int length,
50: boolean isAttVal, Writer out) throws IOException {
51: // avoid calling the Writerwrite method too much by assuming
52: // that the escaping occurs rarely.
53: // profiling revealed that this is faster than the naive code.
54: int limit = start + length;
55: for (int i = start; i < limit; i++) {
56: char c = ch[i];
57: if (c == '&' || c == '<' || c == '>'
58: || (c == '\"' && isAttVal)) {
59: if (i != start)
60: out.write(ch, start, i - start);
61: start = i + 1;
62: switch (ch[i]) {
63: case '&':
64: out.write("&");
65: break;
66: case '<':
67: out.write("<");
68: break;
69: case '>':
70: out.write(">");
71: break;
72: case '\"':
73: out.write(""");
74: break;
75: }
76: }
77: }
78:
79: if (start != limit)
80: out.write(ch, start, limit - start);
81: }
82:
83: }
|