KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > text > MarkupCharacterTranslator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.util.text;
16
17 /**
18  * An object that encodes a character according to rules of the HTML specification,
19  * so that it will be properly parsed by a browser irrespectively of the character
20  * encoding used in the HTML output.
21  *
22  * @author mb
23  * @since 4.0
24  */

25 public class MarkupCharacterTranslator implements ICharacterTranslator
26 {
27     private static final String JavaDoc SAFE_CHARACTERS =
28         "01234567890"
29             + "abcdefghijklmnopqrstuvwxyz"
30             + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31             + "\t\n\r !#$%'()*+,-./:;=?@[\\]^_`{|}~";
32
33     private static final String JavaDoc[][] ENTITIES = {
34         { "\"", """ },
35         { "<", "&lt;" },
36         { ">", "&gt;" },
37         { "&", "&amp;" }
38     };
39     
40     private static final ICharacterMatcher SAFE_MATCHER = new AsciiCharacterMatcher(SAFE_CHARACTERS);
41     private static final ICharacterTranslator ENTITY_TRANSLATOR = new AsciiCharacterTranslator(ENTITIES);
42     
43     private boolean _encodeNonAscii;
44     private ICharacterMatcher _safeMatcher;
45     private ICharacterTranslator _entityTranslator;
46     
47     public MarkupCharacterTranslator()
48     {
49         this(true);
50     }
51     
52     public MarkupCharacterTranslator(boolean encodeNonAscii)
53     {
54         this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR);
55     }
56     
57     public MarkupCharacterTranslator(boolean encodeNonAscii, ICharacterMatcher safeMatcher, ICharacterTranslator entityTranslator)
58     {
59         _encodeNonAscii = encodeNonAscii;
60         _safeMatcher = safeMatcher;
61         _entityTranslator = entityTranslator;
62     }
63
64     public MarkupCharacterTranslator(boolean encodeNonAscii, String JavaDoc safeCharacters, String JavaDoc[][] entities)
65     {
66         _encodeNonAscii = encodeNonAscii;
67         _safeMatcher = new AsciiCharacterMatcher(safeCharacters);
68         _entityTranslator = new AsciiCharacterTranslator(entities);
69     }
70     
71     /**
72      * @see org.apache.tapestry.util.text.IMarkupCharacterTranslator#translateAttribute(char)
73      */

74     public String JavaDoc translate(char ch) {
75         if (ch >= 128 && !_encodeNonAscii)
76             return null;
77         
78         if (_safeMatcher.matches(ch))
79             return null;
80
81         String JavaDoc entity = _entityTranslator.translate(ch);
82         if (entity != null)
83             return entity;
84         
85         // needs to use a NumberFormat here to be fully compliant,
86
// but this is accepted fine by the browsers
87
return "&#" + (int) ch + ";";
88     }
89 }
90
Popular Tags