1 19 20 package org.netbeans.modules.xml.text.navigator; 21 22 26 public class HTMLTextEncoder { 27 28 29 private static final Object [] entities = new Object [] { 30 new char[] { 'g', 't' }, new char[] { 'l', 't' }, new char[] { 'q', 'u', 'o', 't' }, new char[] { 'a', 'm', 'p' }, new char[] { 'l', 's', 'q', 'u', 'o' }, new char[] { 'r', 's', 'q', 'u', 'o' }, new char[] { 'l', 'd', 'q', 'u', 'o' }, new char[] { 'r', 'd', 'q', 'u', 'o' }, new char[] { 'n', 'd', 'a', 's', 'h' }, new char[] { 'm', 'd', 'a', 's', 'h' }, new char[] { 'n', 'e' }, new char[] { 'l', 'e' }, new char[] { 'g', 'e' }, new char[] { 'c', 'o', 'p', 'y' }, new char[] { 'r', 'e', 'g' }, new char[] { 't', 'r', 'a', 'd', 'e' }, new char[] { 'n', 'b', 's', 'p' } 46 }; 48 49 private static final char[] entitySubstitutions = new char[] { 50 '>', '<', '"', '&', 8216, 8217, 8220, 8221, 8211, 8212, 8800, 8804, 8805, 169, 174, 8482, ' ' 52 }; 53 54 55 static String encodeHTMLText(String content) { 56 StringBuffer encoded = new StringBuffer (); 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 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 char[] _chs = new char[chs.length + 2]; 72 _chs[0] = '&';_chs[_chs.length - 1] = ';'; 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 |