litesoft
@ 964
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / common / shared / utils / SafeHtmlizer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
package com.temp.common.shared.utils; public class SafeHtmlizer { public static SafeHtmlizer getInstance() { SafeHtmlizer zInstance = sInstance; // Snag the reference // If null then Force Registration return (zInstance != null) ? zInstance : new SafeHtmlizer(); } public static final String HTML_BR = "<br />"; public static final String CRNL = "\r\n"; /** * HTML Encode the first line of the input (guaranteed to contains at least * a single character). * <p/> * <ul> * <li>Line Breaks are normalized to NewLines. (@see normalizeNewLines)</li> * <li>Tabs are converted to a space. (@see tabsToSpaces)</li> * <li>Leading blank lines are removed. (@see eliminateLeadingEmptyLines)</li> * <li>Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * </li> * </ul> * * @param pText String to be processed. * * @return Processed pText. */ public String noEmpty1stLine( String pText ) { return escapeAndSanitize( HtmlEntity.NBSP, insureNotEmpty( only1stLine( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) ) ); } /** * HTML Encode the input (guaranteed to contains at least a single * character) with New Lines converted to <br />s. * <p/> * <ul> * <li>Line Breaks are normalized to NewLines. (@see normalizeNewLines)</li> * <li>Tabs are converted to a space. (@see tabsToSpaces)</li> * <li>Leading blank lines are removed. (@see eliminateLeadingEmptyLines)</li> * <li>Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * </li> * </ul> * * @param pText String to be processed. * * @return Processed pText. */ public String noEmpty( String pText ) { return escapeAndSanitize( HtmlEntity.NBSP, insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) ); } /** * HTML Encode the input (guaranteed to contains at least a single * character) with New Lines converted to <br />s. * <p/> * <ul> * <li>Line Breaks are normalized to NewLines. (@see normalizeNewLines)</li> * <li>Tabs are converted to a space. (@see tabsToSpaces)</li> * <li>Leading blank lines are removed. (@see eliminateLeadingEmptyLines)</li> * <li>Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * </li> * </ul> * * @param pText String to be processed. * * @return Processed pText. */ public String noEmptyLeaveSomeSpaces( String pText ) { return escapeAndSanitize( " ", insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) ); } /** * HTML Encode the input (guaranteed to contains at least a single * character) with New Lines converted to <br />s. * <p/> * <ul> * <li>Line Breaks are normalized to NewLines. (@see normalizeNewLines)</li> * <li>Tabs are converted to a space. (@see tabsToSpaces)</li> * <li>Leading blank lines are removed. (@see eliminateLeadingEmptyLines)</li> * <li>Lines are Force Wrapped if they exceed the MaxCharactersPerLine. * (@see enforceMaxCharactersPerLine)</li> * <li>Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * </li> * </ul> * * @param pText String to be processed. * @param pMaxCharactersPerLine Will inject new lines if any line exceeds this length * * @return Processed pText. */ public String noEmpty( String pText, int pMaxCharactersPerLine ) { return escapeAndSanitize( HtmlEntity.NBSP, enforceMaxCharactersPerLine( insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ), pMaxCharactersPerLine ) ); } /** * Inject NewLines where ever the line length exceeds the * MaxCharactersPerLine. * * @param pText !null * @param pMaxCharactersPerLine Max characters per line, with a floor of 40. */ protected String enforceMaxCharactersPerLine( String pText, int pMaxCharactersPerLine ) { return StringUtils.wrap( pText, (pMaxCharactersPerLine < 40) ? 40 : pMaxCharactersPerLine ); } /** * Line Breaks are normalized to NewLines. (after CR LF combinations are * converted to a LF, then both FFs & CRs are converted to LFs). * * @param pText !null */ protected String normalizeNewLines( String pText ) { // switch "CR LF" -> LF & then switch CR -> LF & then switch FF -> LF return convertCRNL( pText ).replace( '\r', '\n' ).replace( '\f', '\n' ); } /** * Tabs are converted to a space. * * @param pText !null */ protected String tabsToSpaces( String pText ) { return pText.replace( '\t', ' ' ); } /** * Leading blank lines are removed. * * @param pText !null */ protected String eliminateLeadingEmptyLines( String pText ) { Integer justPastNewLine = null; int at = 0; while ( at < pText.length() ) { char c = pText.charAt( at++ ); if ( c != ' ' ) // Leading whitespace { if ( c != '\n' ) { break; } justPastNewLine = at; } } return (justPastNewLine == null) ? pText : pText.substring( justPastNewLine ); } /** * Return the Text if no New Lines, otherwise return the Text upto the first * New Line. * * @param pText !null */ protected String only1stLine( String pText ) { int at = pText.indexOf( '\n' ); return (at == -1) ? pText : pText.substring( 0, at ); } /** * Return the text unless it is empty, then return a space. * * @param pText !null */ protected String insureNotEmpty( String pText ) { return pText.isEmpty() ? " " : pText; } /** * Convert CR LF (New Line) combinations into just a LF. * * @param pText !null */ protected String convertCRNL( String pText ) { int at = pText.indexOf( CRNL ); if ( at == -1 ) { return pText; } StringBuilder sb = new StringBuilder( pText ); do { sb.deleteCharAt( at ); } while ( -1 != (at = pText.indexOf( CRNL )) ); return sb.toString(); } /** * Escape and sanitize the text to make if reasonably safe to inject as HTML for display purposes. * <p/> * This means that in addition to escaping the standard html special characters ('"', '<', '>', '&'), * blah blah blah are decimal escaped to eliminate "cdata" sections, * spaces are converted to " ", New Lines are converted to "<br />", all other control characters * are converted to a period ('.'), and any remaining non-7Bit ascii is turned into a decimal entity. * * @param spaceReplacement !null * @param pText !null * * @return Processed pText. */ protected String escapeAndSanitize( String spaceReplacement, String pText ) { StringBuilder sb = new StringBuilder(); for ( int i = 0; i < pText.length(); ) { char c = pText.charAt( i++ ); switch ( c ) { case 160: // Hi-bit Space case ' ': sb.append( isSpace( getCharacterAt( pText, i ) ) ? HtmlEntity.NBSP : spaceReplacement ); break; case '&': sb.append( HtmlEntity.AMP.getString() ); break; case '<': sb.append( HtmlEntity.LT.getString() ); break; case '>': sb.append( HtmlEntity.GT.getString() ); break; case '"': sb.append( HtmlEntity.QUOT.getString() ); break; case '\n': sb.append( HTML_BR ); break; default: if ( (c < ' ') || ((127 <= c) && (c < 160)) ) // Control // Character { sb.append( '.' ); break; } if ( c < 127 ) // Regular 7-bit Ascii { sb.append( c ); break; } // Decimal Escape It! sb.append( "&#" ); sb.append( (int) c ); sb.append( ';' ); break; } } return sb.toString(); } private boolean isSpace( Character character ) { if ( character != null ) { char c = character.charValue(); return (c == ' ') || (c == 160); // Hi-Bit Space } return false; } private Character getCharacterAt( String text, int at ) { if ( at < text.length() ) { return text.charAt( at ); } return null; } private static volatile SafeHtmlizer sInstance; protected SafeHtmlizer() { sInstance = this; } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/common/shared/utils/SafeHtmlizer.java
Revision | Author | Commited | Message |
---|---|---|---|
964 | GeorgeS | Fri 01 Aug, 2014 03:18:23 +0000 | ! |