001 // Copyright 2004, 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.wml;
016
017 import org.apache.tapestry.util.text.AsciiCharacterMatcher;
018 import org.apache.tapestry.util.text.AsciiCharacterTranslator;
019 import org.apache.tapestry.util.text.ICharacterMatcher;
020 import org.apache.tapestry.util.text.ICharacterTranslator;
021 import org.apache.tapestry.util.text.ICharacterTranslatorSource;
022 import org.apache.tapestry.util.text.MarkupCharacterTranslator;
023
024 /**
025 * The WML implementation of a character translator source.
026 * Returns a WML translator that encodes everything that is non-safe.
027 *
028 * Some code borrowed from WMLWriter (by David Solis)
029 *
030 * @author mb
031 * @since 4.0
032 */
033 public class WMLCharacterTranslatorSource implements ICharacterTranslatorSource
034 {
035 private static final String SAFE_CHARACTERS =
036 "01234567890"
037 + "abcdefghijklmnopqrstuvwxyz"
038 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
039 + "\t\n\r !\"#%'()*+,-./:;=?@[\\]^_`{|}~";
040
041 private static final String[][] ENTITIES = {
042 { "\"", """ },
043 { "<", "<" },
044 { ">", ">" },
045 { "&", "&" },
046 { "$", "$$" }
047 };
048
049 private static final ICharacterMatcher SAFE_MATCHER = new AsciiCharacterMatcher(SAFE_CHARACTERS);
050 private static final ICharacterTranslator ENTITY_TRANSLATOR = new AsciiCharacterTranslator(ENTITIES);
051
052 private static final ICharacterTranslator WML_TRANSLATOR = new MarkupCharacterTranslator(true, SAFE_MATCHER, ENTITY_TRANSLATOR);
053
054 /**
055 * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getDefaultTranslator()
056 */
057 public ICharacterTranslator getDefaultTranslator() {
058 return WML_TRANSLATOR;
059 }
060
061 /**
062 * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getTranslator(java.lang.String)
063 */
064 public ICharacterTranslator getTranslator(String encoding) {
065 return getDefaultTranslator();
066 }
067 }