Skip to content

Commit 82d874d

Browse files
committed
merged checkstyle changes
2 parents 85e972b + 789a380 commit 82d874d

File tree

13 files changed

+765
-566
lines changed

13 files changed

+765
-566
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ maven-eclipse.xml
1313
.externalToolBuilders
1414
# Netbeans configuration
1515
nb-configuration.xml
16-
*/nbproject/*
16+
*/nbproject/*
17+

core/src/main/java/org/owasp/encoder/CDATAEncoder.java

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
import java.nio.charset.CoderResult;
3838

3939
/**
40-
* CDATAEncoder -- encoder for CDATA sections. CDATA sections are generally good for including large blocks of text that contain
41-
* characters that normally require encoding (ampersand, quotes, less-than, etc...). The CDATA context however still does not
42-
* allow invalid characters, and can be closed by the sequence "]]>". This encoder removes invalid XML characters, and encodes
43-
* "]]>" (to "]]]]><![CDATA[>"). The result is that the data integrity is maintained, but the code receiving the output will
44-
* have to handle multiple CDATA events. As an alternate approach, the caller could pre-encode "]]>" to something of their
45-
* choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use this encoder to remove any invalid XML characters.
40+
* CDATAEncoder -- encoder for CDATA sections. CDATA sections are generally good
41+
* for including large blocks of text that contain characters that normally
42+
* require encoding (ampersand, quotes, less-than, etc...). The CDATA context
43+
* however still does not allow invalid characters, and can be closed by the
44+
* sequence "]]>". This encoder removes invalid XML characters, and encodes
45+
* "]]>" (to "]]]]><![CDATA[>"). The result is that the data integrity is
46+
* maintained, but the code receiving the output will have to handle multiple
47+
* CDATA events. As an alternate approach, the caller could pre-encode "]]>" to
48+
* something of their choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then
49+
* use this encoder to remove any invalid XML characters.
4650
*
4751
* @author Jeff Ichnowski
4852
*/
@@ -94,33 +98,31 @@ protected int firstEncodedOffset(String input, int off, int len) {
9498
// // valid
9599
}
96100

97-
} else {
98-
if (i + 1 < n) {
99-
if (input.charAt(i + 1) != ']') {
100-
// "]x" (next character is safe for this to be ']')
101-
} else {
102-
// "]]?"
103-
// keep looping through ']'
104-
for (; i + 2 < n && input.charAt(i + 2) == ']'; ++i) {
105-
// valid
106-
}
107-
// at this point we've looped through a sequence
108-
// of 2 or more "]", if the next character is ">"
109-
// we need to encode "]]>".
110-
if (i + 2 < n) {
111-
if (input.charAt(i + 2) == '>') {
112-
return i;
101+
} else if (i + 1 < n) {
102+
if (input.charAt(i + 1) != ']') {
103+
// "]x" (next character is safe for this to be ']')
104+
} else {
105+
// "]]?"
106+
// keep looping through ']'
107+
for (; i + 2 < n && input.charAt(i + 2) == ']'; ++i) {
108+
// valid
109+
}
110+
// at this point we've looped through a sequence
111+
// of 2 or more "]", if the next character is ">"
112+
// we need to encode "]]>".
113+
if (i + 2 < n) {
114+
if (input.charAt(i + 2) == '>') {
115+
return i;
113116
// } else {
114117
// // valid
115-
}
116-
117-
} else {
118-
return n;
119118
}
119+
120+
} else {
121+
return n;
120122
}
121-
} else {
122-
return n;
123123
}
124+
} else {
125+
return n;
124126
}
125127
} else if (ch < Character.MIN_HIGH_SURROGATE) {
126128
if (ch <= Unicode.MAX_C1_CTRL_CHAR && ch != Unicode.NEL) {
@@ -145,11 +147,12 @@ protected int firstEncodedOffset(String input, int off, int len) {
145147
// end of input, high without low = invalid
146148
return i;
147149
}
148-
} else if ( // low surrogate without preceding high surrogate
150+
} else if (// low surrogate without preceding high surrogate
149151
ch <= Character.MAX_LOW_SURROGATE
150-
|| // non characters
151-
ch > '\ufffd'
152-
|| ('\ufdd0' <= ch && ch <= '\ufdef')) {
152+
// or non-characters
153+
|| ch > '\ufffd'
154+
|| ('\ufdd0' <= ch && ch <= '\ufdef'))
155+
{
153156
return i;
154157
// } else {
155158
// // valid
@@ -180,63 +183,61 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
180183
} else {
181184
out[j++] = XMLEncoder.INVALID_CHARACTER_REPLACEMENT;
182185
}
183-
} else {
184-
if (i + 1 < n) {
185-
if (in[i + 1] != ']') {
186-
// "]x" (next character is safe for this to be ']')
186+
} else if (i + 1 < n) {
187+
if (in[i + 1] != ']') {
188+
// "]x" (next character is safe for this to be ']')
189+
if (j >= m) {
190+
return overflow(input, i, output, j);
191+
}
192+
out[j++] = ']';
193+
} else {
194+
// "]]?"
195+
// keep looping through ']'
196+
for (; i + 2 < n && in[i + 2] == ']'; ++i) {
187197
if (j >= m) {
188198
return overflow(input, i, output, j);
189199
}
190200
out[j++] = ']';
191-
} else {
192-
// "]]?"
193-
// keep looping through ']'
194-
for (; i + 2 < n && in[i + 2] == ']'; ++i) {
195-
if (j >= m) {
201+
}
202+
// at this point we've looped through a sequence
203+
// of 2 or more "]", if the next character is ">"
204+
// we need to encode "]]>".
205+
if (i + 2 < n) {
206+
if (in[i + 2] == '>') {
207+
if (j + CDATA_END_ENCODED_LENGTH > m) {
196208
return overflow(input, i, output, j);
197209
}
198-
out[j++] = ']';
199-
}
200-
// at this point we've looped through a sequence
201-
// of 2 or more "]", if the next character is ">"
202-
// we need to encode "]]>".
203-
if (i + 2 < n) {
204-
if (in[i + 2] == '>') {
205-
if (j + CDATA_END_ENCODED_LENGTH > m) {
206-
return overflow(input, i, output, j);
207-
}
208-
System.arraycopy(CDATA_END_ENCODED, 0, out, j, CDATA_END_ENCODED_LENGTH);
209-
j += CDATA_END_ENCODED_LENGTH;
210-
i += 2;
211-
} else {
212-
if (j >= m) {
213-
return overflow(input, i, output, j);
214-
}
215-
out[j++] = ']';
216-
}
217-
} else if (endOfInput) {
218-
if (j + 2 > m) {
210+
System.arraycopy(CDATA_END_ENCODED, 0, out, j, CDATA_END_ENCODED_LENGTH);
211+
j += CDATA_END_ENCODED_LENGTH;
212+
i += 2;
213+
} else {
214+
if (j >= m) {
219215
return overflow(input, i, output, j);
220216
}
221217
out[j++] = ']';
222-
out[j++] = ']';
223-
i = n;
224-
break;
225-
} else {
226-
break;
227218
}
219+
} else if (endOfInput) {
220+
if (j + 2 > m) {
221+
return overflow(input, i, output, j);
222+
}
223+
out[j++] = ']';
224+
out[j++] = ']';
225+
i = n;
226+
break;
227+
} else {
228+
break;
228229
}
229-
} else if (endOfInput) {
230-
// seen "]", then end of input.
231-
if (j >= m) {
232-
return overflow(input, i, output, j);
233-
}
234-
out[j++] = ']';
235-
i++;
236-
break;
237-
} else {
238-
break;
239230
}
231+
} else if (endOfInput) {
232+
// seen "]", then end of input.
233+
if (j >= m) {
234+
return overflow(input, i, output, j);
235+
}
236+
out[j++] = ']';
237+
i++;
238+
break;
239+
} else {
240+
break;
240241
}
241242
} else if (ch < Character.MIN_HIGH_SURROGATE) {
242243
if (ch > Unicode.MAX_C1_CTRL_CHAR || ch == Unicode.NEL) {
@@ -284,11 +285,12 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
284285
} else {
285286
break;
286287
}
287-
} else if ( // low surrogate without preceding high surrogate
288+
} else if (// low surrogate without preceding high surrogate
288289
ch <= Character.MAX_LOW_SURROGATE
289-
|| // non characters
290-
ch > '\ufffd'
291-
|| ('\ufdd0' <= ch && ch <= '\ufdef')) {
290+
// or non-characters
291+
|| ch > '\ufffd'
292+
|| ('\ufdd0' <= ch && ch <= '\ufdef'))
293+
{
292294
if (j >= m) {
293295
return overflow(input, i, output, j);
294296
}
@@ -299,9 +301,7 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
299301
}
300302
out[j++] = ch;
301303
}
302-
303304
}
304-
305305
return underflow(input, i, output, j);
306306
}
307307

core/src/main/java/org/owasp/encoder/CSSEncoder.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CSSEncoder extends Encoder {
5656
* Encoding mode of operation--specified the set of characters that
5757
* required encoding.
5858
*/
59-
static enum Mode {
59+
enum Mode {
6060
/**
6161
* String contexts. Characters between quotes.
6262
*
@@ -136,7 +136,7 @@ static enum Mode {
136136
*
137137
* @param mode the mode of the encoder.
138138
*/
139-
public CSSEncoder(Mode mode) {
139+
CSSEncoder(Mode mode) {
140140
_mode = mode;
141141
_lowMask = mode.lowMask();
142142
_highMask = mode.highMask();
@@ -256,10 +256,14 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
256256
boolean needsSpace = false;
257257
if (i+1 < n) {
258258
char la = in[i + 1];
259-
if ('0' <= la && la <= '9' ||
260-
'a' <= la && la <= 'f' ||
261-
'A' <= la && la <= 'F' ||
262-
la == ' ' || la == '\n' || la == '\r' || la == '\t' || la == '\f')
259+
if ('0' <= la && la <= '9'
260+
|| 'a' <= la && la <= 'f'
261+
|| 'A' <= la && la <= 'F'
262+
|| la == ' '
263+
|| la == '\n'
264+
|| la == '\r'
265+
|| la == '\t'
266+
|| la == '\f')
263267
{
264268
needsSpace = true;
265269
k++;

core/src/main/java/org/owasp/encoder/ChainedEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ChainedEncoder extends Encoder {
6868
* @param first the first encoder to apply
6969
* @param last the second/last encoder to apply.
7070
*/
71-
public ChainedEncoder(Encoder first, Encoder last) {
71+
ChainedEncoder(Encoder first, Encoder last) {
7272
_first = first;
7373
_last = last;
7474
}

core/src/main/java/org/owasp/encoder/Encode.java

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,54 @@ public static void forHtmlAttribute(Writer out, String input)
347347
* </tr>
348348
* </thead>
349349
* <tbody>
350-
* <tr class="altColor"><td class="colFirst">{@code U+0009} (horizontal tab)</td><td class="colLast">{@code &#9;}</td></tr>
351-
* <tr class="rowColor"><td class="colFirst">{@code U+000A} (line feed)</td><td class="colLast">{@code &#10;}</td></tr>
352-
* <tr class="altColor"><td class="colFirst">{@code U+000C} (form feed)</td><td class="colLast">{@code &#12;}</td></tr>
353-
* <tr class="rowColor"><td class="colFirst">{@code U+000D} (carriage return)</td><td class="colLast">{@code &#13;}</td></tr>
354-
* <tr class="altColor"><td class="colFirst">{@code U+0020} (space)</td><td class="colLast">{@code &#32;}</td></tr>
355-
* <tr class="rowColor"><td class="colFirst">{@code &}</td><td class="colLast">{@code &amp;}</td></tr>
356-
* <tr class="altColor"><td class="colFirst">{@code <}</td><td class="colLast">{@code &lt;}</td></tr>
357-
* <tr class="rowColor"><td class="colFirst">{@code >}</td><td class="colLast">{@code &gt;}</td></tr>
358-
* <tr class="altColor"><td class="colFirst">{@code "}</td><td class="colLast">{@code &#34;}</td></tr>
359-
* <tr class="rowColor"><td class="colFirst">{@code '}</td><td class="colLast">{@code &#39;}</td></tr>
360-
* <tr class="altColor"><td class="colFirst">{@code /}</td><td class="colLast">{@code &#47;}</td></tr>
361-
* <tr class="rowColor"><td class="colFirst">{@code =}</td><td class="colLast">{@code &#61;}</td></tr>
362-
* <tr class="altColor"><td class="colFirst">{@code `}</td><td class="colLast">{@code &#96;}</td></tr>
363-
* <tr class="rowColor"><td class="colFirst">{@code U+0085} (next line)</td><td class="colLast">{@code &#133;}</td></tr>
364-
* <tr class="altColor"><td class="colFirst">{@code U+2028} (line separator)</td><td class="colLast">{@code &#8232;}</td></tr>
365-
* <tr class="rowColor"><td class="colFirst">{@code U+2029} (paragraph separator)</td><td class="colLast">{@code &#8233;}</td></tr>
350+
* <tr class="altColor">
351+
* <td class="colFirst">{@code U+0009} (horizontal tab)</td>
352+
* <td class="colLast">{@code &#9;}</td></tr>
353+
* <tr class="rowColor">
354+
* <td class="colFirst">{@code U+000A} (line feed)</td>
355+
* <td class="colLast">{@code &#10;}</td></tr>
356+
* <tr class="altColor">
357+
* <td class="colFirst">{@code U+000C} (form feed)</td>
358+
* <td class="colLast">{@code &#12;}</td></tr>
359+
* <tr class="rowColor">
360+
* <td class="colFirst">{@code U+000D} (carriage return)</td>
361+
* <td class="colLast">{@code &#13;}</td></tr>
362+
* <tr class="altColor">
363+
* <td class="colFirst">{@code U+0020} (space)</td>
364+
* <td class="colLast">{@code &#32;}</td></tr>
365+
* <tr class="rowColor">
366+
* <td class="colFirst">{@code &}</td>
367+
* <td class="colLast">{@code &amp;}</td></tr>
368+
* <tr class="altColor">
369+
* <td class="colFirst">{@code <}</td>
370+
* <td class="colLast">{@code &lt;}</td></tr>
371+
* <tr class="rowColor">
372+
* <td class="colFirst">{@code >}</td>
373+
* <td class="colLast">{@code &gt;}</td></tr>
374+
* <tr class="altColor">
375+
* <td class="colFirst">{@code "}</td>
376+
* <td class="colLast">{@code &#34;}</td></tr>
377+
* <tr class="rowColor">
378+
* <td class="colFirst">{@code '}</td>
379+
* <td class="colLast">{@code &#39;}</td></tr>
380+
* <tr class="altColor">
381+
* <td class="colFirst">{@code /}</td>
382+
* <td class="colLast">{@code &#47;}</td></tr>
383+
* <tr class="rowColor">
384+
* <td class="colFirst">{@code =}</td>
385+
* <td class="colLast">{@code &#61;}</td></tr>
386+
* <tr class="altColor">
387+
* <td class="colFirst">{@code `}</td>
388+
* <td class="colLast">{@code &#96;}</td></tr>
389+
* <tr class="rowColor">
390+
* <td class="colFirst">{@code U+0085} (next line)</td>
391+
* <td class="colLast">{@code &#133;}</td></tr>
392+
* <tr class="altColor">
393+
* <td class="colFirst">{@code U+2028} (line separator)</td>
394+
* <td class="colLast">{@code &#8232;}</td></tr>
395+
* <tr class="rowColor">
396+
* <td class="colFirst">{@code U+2029} (paragraph separator)</td>
397+
* <td class="colLast">{@code &#8233;}</td></tr>
366398
* </tbody>
367399
* </table>
368400
*
@@ -1294,9 +1326,8 @@ String encode(Encoder encoder, String str, int j) {
12941326
// else, it's an overflow, we need to use a new output buffer
12951327
// we'll allocate this buffer to be the exact size of the worst
12961328
// case, guaranteeing a second overflow would not be possible.
1297-
CharBuffer tmp = CharBuffer.allocate(
1298-
_output.position() +
1299-
encoder.maxEncodedLength(_input.remaining()));
1329+
CharBuffer tmp = CharBuffer.allocate(_output.position()
1330+
+ encoder.maxEncodedLength(_input.remaining()));
13001331

13011332
// copy over everything that has been encoded so far
13021333
tmp.put(_output.array(), 0, _output.position());

0 commit comments

Comments
 (0)