MinimumEscapeHandler.java revision 325
1193N/A/*
3793N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1193N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1193N/A *
1193N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1193N/A */
1193N/A
1193N/Apackage com.sun.xml.internal.bind.marshaller;
1193N/A
3793N/Aimport java.io.IOException;
1193N/Aimport java.io.Writer;
1193N/A
1193N/A/**
3793N/A * Performs no character escaping. Usable only when the output encoding
3793N/A * is UTF, but this handler gives the maximum performance.
3793N/A *
3793N/A * @author
1193N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
1193N/A */
1193N/Apublic class MinimumEscapeHandler implements CharacterEscapeHandler {
1436N/A
1436N/A private MinimumEscapeHandler() {} // no instanciation please
1193N/A
1193N/A public static final CharacterEscapeHandler theInstance = new MinimumEscapeHandler();
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
// avoid calling the Writerwrite method too much by assuming
// that the escaping occurs rarely.
// profiling revealed that this is faster than the naive code.
int limit = start+length;
for (int i = start; i < limit; i++) {
char c = ch[i];
if(c == '&' || c == '<' || c == '>' || c == '\r' || (c == '\"' && isAttVal) ) {
if(i!=start)
out.write(ch,start,i-start);
start = i+1;
switch (ch[i]) {
case '&':
out.write("&amp;");
break;
case '<':
out.write("&lt;");
break;
case '>':
out.write("&gt;");
break;
case '\"':
out.write("&quot;");
break;
}
}
}
if( start!=limit )
out.write(ch,start,limit-start);
}
}