001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.markup; 016 017 import java.io.PrintWriter; 018 019 import org.apache.tapestry.util.text.ICharacterTranslator; 020 021 /** 022 * For the meantime, implemenatations of {@link org.apache.tapestry.markup.MarkupFilter} are 023 * wrappers around {@link org.apache.tapestry.util.text.ICharacterTranslator}. This class provides 024 * handy methods for doing the grunt work. 025 * 026 * @author Howard M. Lewis Ship 027 * @since 4.0 028 */ 029 public final class MarkupFilterUtils 030 { 031 /* defeat instantiation */ 032 private MarkupFilterUtils() { } 033 034 public static void print(PrintWriter writer, char[] data, int offset, int length, 035 boolean escapeQuotes, ICharacterTranslator translator) 036 { 037 StringBuffer buffer = new StringBuffer(length); 038 039 for (int i = 0; i < length; i++) 040 { 041 char ch = data[offset + i]; 042 043 if (ch == '"' && !escapeQuotes) 044 { 045 buffer.append(ch); 046 continue; 047 } 048 049 String translated = translator.translate(ch); 050 051 if (translated == null) 052 { 053 buffer.append(ch); 054 continue; 055 } 056 057 buffer.append(translated); 058 } 059 060 // We'll have to see if building up a buffer and then printing it in one go is the 061 // most efficient route. It's hard to predict what will give the best performance, 062 // but there's almost certainly a buffered writer between this code and the 063 // character set encoder. 064 065 writer.print(buffer.toString()); 066 } 067 }