KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > navigator > HTMLTextEncoder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.text.navigator;
21
22 /**
23  *
24  * @author mfukala@netbeans.org
25  */

26 public class HTMLTextEncoder {
27     
28      /** Definitions for a limited subset of SGML character entities */
29     private static final Object JavaDoc[] entities = new Object JavaDoc[] {
30             new char[] { 'g', 't' }, new char[] { 'l', 't' }, //NOI18N
31
new char[] { 'q', 'u', 'o', 't' }, new char[] { 'a', 'm', 'p' }, //NOI18N
32
new char[] { 'l', 's', 'q', 'u', 'o' }, //NOI18N
33
new char[] { 'r', 's', 'q', 'u', 'o' }, //NOI18N
34
new char[] { 'l', 'd', 'q', 'u', 'o' }, //NOI18N
35
new char[] { 'r', 'd', 'q', 'u', 'o' }, //NOI18N
36
new char[] { 'n', 'd', 'a', 's', 'h' }, //NOI18N
37
new char[] { 'm', 'd', 'a', 's', 'h' }, //NOI18N
38
new char[] { 'n', 'e' }, //NOI18N
39
new char[] { 'l', 'e' }, //NOI18N
40
new char[] { 'g', 'e' }, //NOI18N
41
new char[] { 'c', 'o', 'p', 'y' }, //NOI18N
42
new char[] { 'r', 'e', 'g' }, //NOI18N
43
new char[] { 't', 'r', 'a', 'd', 'e' }, //NOI18N
44
new char[] { 'n', 'b', 's', 'p' //NOI18N
45
}
46         }; //NOI18N
47

48     /** Mappings for the array of SGML character entities to characters */
49     private static final char[] entitySubstitutions = new char[] {
50             '>', '<', '"', '&', 8216, 8217, 8220, 8221, 8211, 8212, 8800, 8804, 8805, //NOI18N
51
169, 174, 8482, ' '
52         };
53     
54     /** Encodes the given string using the built-in translation table.*/
55     static String JavaDoc encodeHTMLText(String JavaDoc content) {
56         StringBuffer JavaDoc encoded = new StringBuffer JavaDoc();
57         for(int i = 0; i < content.length(); i++) {
58             char ch = content.charAt(i);
59             encoded.append(encode(ch));
60         }
61         return encoded.toString();
62     }
63     
64     /** Encodes the given character using the translation table.
65      * It returns the encoded HTML entities in the &ent; form.*/

66     private static char[] encode(char ch) {
67         for(int i=0; i < entitySubstitutions.length; i++) {
68             if(entitySubstitutions[i] == ch) {
69                 char[] chs = (char[])entities[i];
70                 //add the ampersand char before and semicolon after the text
71
char[] _chs = new char[chs.length + 2];
72                 _chs[0] = '&';_chs[_chs.length - 1] = ';'; //NOI18N
73
System.arraycopy(chs, 0, _chs, 1, chs.length);
74
75                 return _chs;
76             }
77         }
78         return new char[]{ch};
79     }
80     
81 }
82
Popular Tags