Coverage Report - org.webmacro.util.HTMLEscaper
 
Classes in this File Line Coverage Branch Coverage Complexity
HTMLEscaper
42%
9/21
0%
0/6
3
 
 1  
 /*
 2  
 
 3  
 HTML Escaping code
 4  
 Copyright (C) 2000 Marc Palmer / AnyWare Ltd.
 5  
 
 6  
 Acording to Marc, this code may be used under any license.
 7  
 Thanks to Jason Hunter for his great book "JAVA Servlet Programming" used as
 8  
 the source for the entity mappings. Thanks to CARDIACS for making the
 9  
 most joyful music (www.cardiacs.com/button.html)
 10  
 
 11  
 http://www.anyware.co.uk/java for updates and other stuff
 12  
 
 13  
 @author Marc Palmer
 14  
 @author Marcel Huijkman
 15  
 
 16  
 @version        15-07-2002
 17  
 */
 18  
 
 19  
 package org.webmacro.util;
 20  
 
 21  
 /**
 22  
  * This class will escape characters that have an HTML entity
 23  
  * representation. It will not escape standard ASCII characters unless
 24  
  * it is necessary (i.e. will not escape ASCI #32 but will escape "&").
 25  
  * Any characters with no corresponding entity are passed through unchanged.
 26  
  * It uses a quick string -> array mapping to avoid creating thousands of
 27  
  * temporary objects.
 28  
  */
 29  
 public class HTMLEscaper
 30  
 {
 31  
     /** Utility classes should not have a public or default constructor. */
 32  0
     private HTMLEscaper() {}
 33  
     
 34  
     /**
 35  
      * This method will take the input and escape characters that have
 36  
      * an HTML entity representation.
 37  
      * It uses a quick string -> array mapping to avoid creating thousands of
 38  
      * temporary objects.
 39  
      * @param nonHTMLsrc String containing the text to make HTML-safe
 40  
      * @return String containing new copy of string with ENTITIES escaped
 41  
      */
 42  
     public static final String escape (String nonHTMLsrc)
 43  
     {
 44  0
         if (nonHTMLsrc == null)
 45  0
             return null;
 46  
 
 47  0
         StringBuffer res = new StringBuffer();
 48  0
         int l = nonHTMLsrc.length();
 49  
         int idx;
 50  
         char c;
 51  0
         for (int i = 0; i < l; i++)
 52  
         {
 53  0
             c = nonHTMLsrc.charAt(i);
 54  0
             idx = entityMap.indexOf(c);
 55  0
             if (idx == -1)
 56  
             {
 57  0
                 res.append(c);
 58  
             }
 59  
             else
 60  
             {
 61  0
                 res.append(quickEntities[idx]);
 62  
             }
 63  
         }
 64  0
         return res.toString();
 65  
     }
 66  
 
 67  
     /**
 68  
      * These are probably HTML 3.2 level... as it looks like some HTML 4 entities
 69  
      * are not present.
 70  
      */
 71  2
     private static final String[][] ENTITIES = {
 72  
         /* We probably don't want to filter regular ASCII chars so we leave them out */
 73  
         {"&", "amp"},
 74  
         {"<", "lt"},
 75  
         {">", "gt"},
 76  
         {"\"", "quot"},
 77  
 
 78  
         {"\u0083", "#131"},
 79  
         {"\u0084", "#132"},
 80  
         {"\u0085", "#133"},
 81  
         {"\u0086", "#134"},
 82  
         {"\u0087", "#135"},
 83  
         {"\u0089", "#137"},
 84  
         {"\u008A", "#138"},
 85  
         {"\u008B", "#139"},
 86  
         {"\u008C", "#140"},
 87  
         {"\u0091", "#145"},
 88  
         {"\u0092", "#146"},
 89  
         {"\u0093", "#147"},
 90  
         {"\u0094", "#148"},
 91  
         {"\u0095", "#149"},
 92  
         {"\u0096", "#150"},
 93  
         {"\u0097", "#151"},
 94  
         {"\u0099", "#153"},
 95  
         {"\u009A", "#154"},
 96  
         {"\u009B", "#155"},
 97  
         {"\u009C", "#156"},
 98  
         {"\u009F", "#159"},
 99  
 
 100  
         {"\u00A0", "nbsp"},
 101  
         {"\u00A1", "iexcl"},
 102  
         {"\u00A2", "cent"},
 103  
         {"\u00A3", "pound"},
 104  
         {"\u00A4", "curren"},
 105  
         {"\u00A5", "yen"},
 106  
         {"\u00A6", "brvbar"},
 107  
         {"\u00A7", "sect"},
 108  
         {"\u00A8", "uml"},
 109  
         {"\u00A9", "copy"},
 110  
         {"\u00AA", "ordf"},
 111  
         {"\u00AB", "laquo"},
 112  
         {"\u00AC", "not"},
 113  
         {"\u00AD", "shy"},
 114  
         {"\u00AE", "reg"},
 115  
         {"\u00AF", "macr"},
 116  
         {"\u00B0", "deg"},
 117  
         {"\u00B1", "plusmn"},
 118  
         {"\u00B2", "sup2"},
 119  
         {"\u00B3", "sup3"},
 120  
 
 121  
         {"\u00B4", "acute"},
 122  
         {"\u00B5", "micro"},
 123  
         {"\u00B6", "para"},
 124  
         {"\u00B7", "middot"},
 125  
         {"\u00B8", "cedil"},
 126  
         {"\u00B9", "sup1"},
 127  
         {"\u00BA", "ordm"},
 128  
         {"\u00BB", "raquo"},
 129  
         {"\u00BC", "frac14"},
 130  
         {"\u00BD", "frac12"},
 131  
         {"\u00BE", "frac34"},
 132  
         {"\u00BF", "iquest"},
 133  
 
 134  
         {"\u00C0", "Agrave"},
 135  
         {"\u00C1", "Aacute"},
 136  
         {"\u00C2", "Acirc"},
 137  
         {"\u00C3", "Atilde"},
 138  
         {"\u00C4", "Auml"},
 139  
         {"\u00C5", "Aring"},
 140  
         {"\u00C6", "AElig"},
 141  
         {"\u00C7", "Ccedil"},
 142  
         {"\u00C8", "Egrave"},
 143  
         {"\u00C9", "Eacute"},
 144  
         {"\u00CA", "Ecirc"},
 145  
         {"\u00CB", "Euml"},
 146  
         {"\u00CC", "Igrave"},
 147  
         {"\u00CD", "Iacute"},
 148  
         {"\u00CE", "Icirc"},
 149  
         {"\u00CF", "Iuml"},
 150  
 
 151  
         {"\u00D0", "ETH"},
 152  
         {"\u00D1", "Ntilde"},
 153  
         {"\u00D2", "Ograve"},
 154  
         {"\u00D3", "Oacute"},
 155  
         {"\u00D4", "Ocirc"},
 156  
         {"\u00D5", "Otilde"},
 157  
         {"\u00D6", "Ouml"},
 158  
         {"\u00D7", "times"},
 159  
         {"\u00D8", "Oslash"},
 160  
         {"\u00D9", "Ugrave"},
 161  
         {"\u00DA", "Uacute"},
 162  
         {"\u00DB", "Ucirc"},
 163  
         {"\u00DC", "Uuml"},
 164  
         {"\u00DD", "Yacute"},
 165  
         {"\u00DE", "THORN"},
 166  
         {"\u00DF", "szlig"},
 167  
 
 168  
         {"\u00E0", "agrave"},
 169  
         {"\u00E1", "aacute"},
 170  
         {"\u00E2", "acirc"},
 171  
         {"\u00E3", "atilde"},
 172  
         {"\u00E4", "auml"},
 173  
         {"\u00E5", "aring"},
 174  
         {"\u00E6", "aelig"},
 175  
         {"\u00E7", "ccedil"},
 176  
         {"\u00E8", "egrave"},
 177  
         {"\u00E9", "eacute"},
 178  
         {"\u00EA", "ecirc"},
 179  
         {"\u00EB", "euml"},
 180  
         {"\u00EC", "igrave"},
 181  
         {"\u00ED", "iacute"},
 182  
         {"\u00EE", "icirc"},
 183  
         {"\u00EF", "iuml"},
 184  
 
 185  
         {"\u00F0", "eth"},
 186  
         {"\u00F1", "ntilde"},
 187  
         {"\u00F2", "ograve"},
 188  
         {"\u00F3", "oacute"},
 189  
         {"\u00F4", "ocirc"},
 190  
         {"\u00F5", "otilde"},
 191  
         {"\u00F6", "ouml"},
 192  
         {"\u00F7", "divide"},
 193  
         {"\u00F8", "oslash"},
 194  
         {"\u00F9", "ugrave"},
 195  
         {"\u00FA", "uacute"},
 196  
         {"\u00FB", "ucirc"},
 197  
         {"\u00FC", "uuml"},
 198  
         {"\u00FD", "yacute"},
 199  
         {"\u00FE", "thorn"},
 200  
         {"\u00FF", "yuml"},
 201  
         {"\u0080", "euro"}
 202  
     };
 203  
 
 204  
     private static String entityMap;
 205  
     private static String[] quickEntities;
 206  
 
 207  
     static
 208  
     {
 209  
         // Initialize some local mappings to speed it all up
 210  2
         int l = ENTITIES.length;
 211  2
         StringBuffer temp = new StringBuffer();
 212  
 
 213  2
         quickEntities = new String[l];
 214  246
         for (int i = 0; i < l; i++)
 215  
         {
 216  244
             temp.append(ENTITIES[i][0]);
 217  244
             quickEntities[i] = "&" + ENTITIES[i][1] + ";";
 218  
         }
 219  2
         entityMap = temp.toString();
 220  2
     }
 221  
 }