Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/GWT_Sandbox/FormEngine/src/com/temp/shared/utils/SafeHtmlizer.java

Diff revisions: vs.
  @@ -25,10 +25,12 @@
25 25 * </ul>
26 26 *
27 27 * @param pText String to be processed.
28 + *
28 29 * @return Processed pText.
29 30 */
30 - public String noEmpty1stLine(String pText) {
31 - return escapeAndSanitize(HtmlEntity.NBSP, insureNotEmpty(only1stLine(eliminateLeadingEmptyLines(tabsToSpaces(normalizeNewLines(StringUtils.deNull(pText)))))));
31 + public String noEmpty1stLine( String pText ) {
32 + return escapeAndSanitize( HtmlEntity.NBSP, insureNotEmpty(
33 + only1stLine( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) ) );
32 34 }
33 35
34 36 /**
  @@ -44,10 +46,12 @@
44 46 * </ul>
45 47 *
46 48 * @param pText String to be processed.
49 + *
47 50 * @return Processed pText.
48 51 */
49 - public String noEmpty(String pText) {
50 - return escapeAndSanitize(HtmlEntity.NBSP, insureNotEmpty(eliminateLeadingEmptyLines(tabsToSpaces(normalizeNewLines(StringUtils.deNull(pText))))));
52 + public String noEmpty( String pText ) {
53 + return escapeAndSanitize( HtmlEntity.NBSP,
54 + insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) );
51 55 }
52 56
53 57 /**
  @@ -63,10 +67,11 @@
63 67 * </ul>
64 68 *
65 69 * @param pText String to be processed.
70 + *
66 71 * @return Processed pText.
67 72 */
68 - public String noEmptyLeaveSomeSpaces(String pText) {
69 - return escapeAndSanitize(" ", insureNotEmpty(eliminateLeadingEmptyLines(tabsToSpaces(normalizeNewLines(StringUtils.deNull(pText))))));
73 + public String noEmptyLeaveSomeSpaces( String pText ) {
74 + return escapeAndSanitize( " ", insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ) );
70 75 }
71 76
72 77 /**
  @@ -85,10 +90,12 @@
85 90 *
86 91 * @param pText String to be processed.
87 92 * @param pMaxCharactersPerLine Will inject new lines if any line exceeds this length
93 + *
88 94 * @return Processed pText.
89 95 */
90 - public String noEmpty(String pText, int pMaxCharactersPerLine) {
91 - return escapeAndSanitize(HtmlEntity.NBSP, enforceMaxCharactersPerLine(insureNotEmpty(eliminateLeadingEmptyLines(tabsToSpaces(normalizeNewLines(StringUtils.deNull(pText))))), pMaxCharactersPerLine));
96 + public String noEmpty( String pText, int pMaxCharactersPerLine ) {
97 + return escapeAndSanitize( HtmlEntity.NBSP, enforceMaxCharactersPerLine(
98 + insureNotEmpty( eliminateLeadingEmptyLines( tabsToSpaces( normalizeNewLines( StringUtils.deNull( pText ) ) ) ) ), pMaxCharactersPerLine ) );
92 99 }
93 100
94 101 /**
  @@ -98,8 +105,8 @@
98 105 * @param pText !null
99 106 * @param pMaxCharactersPerLine Max characters per line, with a floor of 40.
100 107 */
101 - protected String enforceMaxCharactersPerLine(String pText, int pMaxCharactersPerLine) {
102 - return StringUtils.wrap(pText, (pMaxCharactersPerLine < 40) ? 40 : pMaxCharactersPerLine);
108 + protected String enforceMaxCharactersPerLine( String pText, int pMaxCharactersPerLine ) {
109 + return StringUtils.wrap( pText, (pMaxCharactersPerLine < 40) ? 40 : pMaxCharactersPerLine );
103 110 }
104 111
105 112 /**
  @@ -108,9 +115,9 @@
108 115 *
109 116 * @param pText !null
110 117 */
111 - protected String normalizeNewLines(String pText) {
118 + protected String normalizeNewLines( String pText ) {
112 119 // switch "CR LF" -> LF & then switch CR -> LF & then switch FF -> LF
113 - return convertCRNL(pText).replace('\r', '\n').replace('\f', '\n');
120 + return convertCRNL( pText ).replace( '\r', '\n' ).replace( '\f', '\n' );
114 121 }
115 122
116 123 /**
  @@ -118,8 +125,8 @@
118 125 *
119 126 * @param pText !null
120 127 */
121 - protected String tabsToSpaces(String pText) {
122 - return pText.replace('\t', ' ');
128 + protected String tabsToSpaces( String pText ) {
129 + return pText.replace( '\t', ' ' );
123 130 }
124 131
125 132 /**
  @@ -127,20 +134,20 @@
127 134 *
128 135 * @param pText !null
129 136 */
130 - protected String eliminateLeadingEmptyLines(String pText) {
137 + protected String eliminateLeadingEmptyLines( String pText ) {
131 138 Integer justPastNewLine = null;
132 139 int at = 0;
133 - while (at < pText.length()) {
134 - char c = pText.charAt(at++);
135 - if (c != ' ') // Leading whitespace
140 + while ( at < pText.length() ) {
141 + char c = pText.charAt( at++ );
142 + if ( c != ' ' ) // Leading whitespace
136 143 {
137 - if (c != '\n') {
144 + if ( c != '\n' ) {
138 145 break;
139 146 }
140 147 justPastNewLine = at;
141 148 }
142 149 }
143 - return (justPastNewLine == null) ? pText : pText.substring(justPastNewLine);
150 + return (justPastNewLine == null) ? pText : pText.substring( justPastNewLine );
144 151 }
145 152
146 153 /**
  @@ -149,9 +156,9 @@
149 156 *
150 157 * @param pText !null
151 158 */
152 - protected String only1stLine(String pText) {
153 - int at = pText.indexOf('\n');
154 - return (at == -1) ? pText : pText.substring(0, at);
159 + protected String only1stLine( String pText ) {
160 + int at = pText.indexOf( '\n' );
161 + return (at == -1) ? pText : pText.substring( 0, at );
155 162 }
156 163
157 164 /**
  @@ -159,7 +166,7 @@
159 166 *
160 167 * @param pText !null
161 168 */
162 - protected String insureNotEmpty(String pText) {
169 + protected String insureNotEmpty( String pText ) {
163 170 return pText.isEmpty() ? " " : pText;
164 171 }
165 172
  @@ -168,15 +175,15 @@
168 175 *
169 176 * @param pText !null
170 177 */
171 - protected String convertCRNL(String pText) {
172 - int at = pText.indexOf(CRNL);
173 - if (at == -1) {
178 + protected String convertCRNL( String pText ) {
179 + int at = pText.indexOf( CRNL );
180 + if ( at == -1 ) {
174 181 return pText;
175 182 }
176 - StringBuilder sb = new StringBuilder(pText);
183 + StringBuilder sb = new StringBuilder( pText );
177 184 do {
178 - sb.deleteCharAt(at);
179 - } while (-1 != (at = pText.indexOf(CRNL)));
185 + sb.deleteCharAt( at );
186 + } while ( -1 != (at = pText.indexOf( CRNL )) );
180 187 return sb.toString();
181 188 }
182 189
  @@ -190,65 +197,66 @@
190 197 *
191 198 * @param spaceReplacement !null
192 199 * @param pText !null
200 + *
193 201 * @return Processed pText.
194 202 */
195 - protected String escapeAndSanitize(String spaceReplacement, String pText) {
203 + protected String escapeAndSanitize( String spaceReplacement, String pText ) {
196 204 StringBuilder sb = new StringBuilder();
197 - for (int i = 0; i < pText.length();) {
198 - char c = pText.charAt(i++);
199 - switch (c) {
205 + for ( int i = 0; i < pText.length(); ) {
206 + char c = pText.charAt( i++ );
207 + switch ( c ) {
200 208 case 160: // Hi-bit Space
201 209 case ' ':
202 - sb.append(isSpace(getCharacterAt(pText, i)) ? HtmlEntity.NBSP : spaceReplacement);
210 + sb.append( isSpace( getCharacterAt( pText, i ) ) ? HtmlEntity.NBSP : spaceReplacement );
203 211 break;
204 212 case '&':
205 - sb.append(HtmlEntity.AMP.getString());
213 + sb.append( HtmlEntity.AMP.getString() );
206 214 break;
207 215 case '<':
208 - sb.append(HtmlEntity.LT.getString());
216 + sb.append( HtmlEntity.LT.getString() );
209 217 break;
210 218 case '>':
211 - sb.append(HtmlEntity.GT.getString());
219 + sb.append( HtmlEntity.GT.getString() );
212 220 break;
213 221 case '"':
214 - sb.append(HtmlEntity.QUOT.getString());
222 + sb.append( HtmlEntity.QUOT.getString() );
215 223 break;
216 224 case '\n':
217 - sb.append(HTML_BR);
225 + sb.append( HTML_BR );
218 226 break;
219 227 default:
220 - if ((c < ' ') || ((127 <= c) && (c < 160))) // Control
228 + if ( (c < ' ') || ((127 <= c) && (c < 160)) ) // Control
221 229 // Character
222 230 {
223 - sb.append('.');
231 + sb.append( '.' );
224 232 break;
225 233 }
226 - if (c < 127) // Regular 7-bit Ascii
234 + if ( c < 127 ) // Regular 7-bit Ascii
227 235 {
228 - sb.append(c);
236 + sb.append( c );
229 237 break;
230 238 }
231 239 // Decimal Escape It!
232 - sb.append("&#");
233 - sb.append((int) c);
234 - sb.append(';');
240 + sb.append( "&#" );
241 + sb.append( (int) c );
242 + sb.append( ';' );
235 243 break;
236 244 }
237 245 }
238 246 return sb.toString();
239 247 }
240 248
241 - private boolean isSpace(Character character) {
242 - if (character != null) {
249 + private boolean isSpace( Character character ) {
250 + if ( character != null ) {
243 251 char c = character.charValue();
244 252 return (c == ' ') || (c == 160); // Hi-Bit Space
245 253 }
246 254 return false;
247 255 }
248 256
249 - private Character getCharacterAt(String text, int at) {
250 - if (at < text.length()) {
251 - return text.charAt(at);
257 + private Character getCharacterAt( String text, int at ) {
258 + if ( at < text.length() ) {
259 + return text.charAt( at );
252 260 }
253 261 return null;
254 262 }