litesoft
@ 950
litesoft / trunk / GWT_Sandbox / MultiModule / common / src / org / litesoft / sandbox / infrastructure / client / 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 |
package org.litesoft.sandbox.infrastructure.client; import org.litesoft.sandbox.anywhere.util.*; import com.google.gwt.safehtml.shared.*; public class SafeHtmlizer { public static SafeHtmlizer getInstance() { SafeHtmlizer zInstance = sInstance; // Snag the reference return (zInstance != null) ? zInstance : new SafeHtmlizer(); // Force Registration } /** * HTML Encode the first line of the input (guaranteed to contains at least a single character). * <p/> * Line Breaks are normalized to NewLines. (@see normalizeNewLines) * Tabs are converted to a space. (@see tabsToSpaces) * Leading blank lines are removed. (@see eliminateLeadingEmptyLines) * Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * * @param pText String to be processed. * * @return Processed pText as a SafeHtml object. */ public SafeHtml noEmpty1stLine( String pText ) { return escapeAndSanitize( insureNotEmpty( only1stLine( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( deNull( pText ) ) ) ) ) ) ); } /** * HTML Encode the input (guaranteed to contains at least a single character) with New Lines converted to <br />s. * <p/> * Line Breaks are normalized to NewLines. (@see normalizeNewLines) * Tabs are converted to a space. (@see tabsToSpaces) * Leading blank lines are removed. (@see eliminateLeadingEmptyLines) * Escape the HTML Markup and sanitize the text (@see escapeAndSanitize) * * @param pText String to be processed. * * @return Processed pText as a SafeHtml object. */ public SafeHtml noEmpty( String pText ) { return escapeAndSanitize( insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( deNull( pText ) ) ) ) ) ); } protected String deNull( String pText ) { return (pText == null) ? "" : pText; } /** * 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( Constants.CRNL ); if ( at == -1 ) { return pText; } StringBuilder sb = new StringBuilder( pText ); do { sb.deleteCharAt( at ); } while ( -1 != (at = pText.indexOf( Constants.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 ('"', '<', '>', '&'), * bah 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 pText !null * * @return Processed pText as a SafeHtml object. */ protected SafeHtml escapeAndSanitize( String pText ) { StringBuilder sb = new StringBuilder(); for ( int i = 0; i < pText.length(); i++ ) { char c = pText.charAt( i ); switch ( c ) { case 160: // Hi-bit Space case ' ': sb.append( Constants.NBSP ); break; case '&': sb.append( Constants.AMPERSAND ); break; case '<': sb.append( Constants.LESS_THAN ); break; case '>': sb.append( Constants.GREATER_THAN ); break; case '"': sb.append( Constants.DOUBLE_QUOTE ); break; case '\n': sb.append( Constants.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 SafeHtmlUtils.fromTrustedString( sb.toString() ); } private static volatile SafeHtmlizer sInstance; protected SafeHtmlizer() { sInstance = this; } } |