Skip to content

Commit 9796d9e

Browse files
committed
changes to remove empty if statements identified by checkstyle
1 parent 016a773 commit 9796d9e

File tree

5 files changed

+298
-354
lines changed

5 files changed

+298
-354
lines changed

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

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,38 @@
3131
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3232
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3333
// OF THE POSSIBILITY OF SUCH DAMAGE.
34-
3534
package org.owasp.encoder;
3635

3736
import java.nio.CharBuffer;
3837
import java.nio.charset.CoderResult;
3938

4039
/**
41-
* CDATAEncoder -- encoder for CDATA sections. CDATA sections are generally
42-
* good for including large blocks of text that contain characters that
43-
* normally require encoding (ampersand, quotes, less-than, etc...). The
44-
* CDATA context however still does not allow invalid characters, and can
45-
* be closed by the sequence "]]>". This encoder removes invalid XML
46-
* characters, and encodes "]]>" (to "]]>]]<![CDATA[>"). The result is
47-
* that the data integrity is maintained, but the code receiving the output
48-
* will have to handle multiple CDATA events with character events between.
49-
* As an alternate approach, the caller could pre-encode "]]>" to something
50-
* of their choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use
51-
* this encoder to remove any invalid XML characters.
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 with character events between. As an alternate approach, the caller could pre-encode "]]>"
45+
* to something of their choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use this encoder to remove any invalid XML
46+
* characters.
5247
*
5348
* @author Jeff Ichnowski
5449
*/
5550
class CDATAEncoder extends Encoder {
5651

57-
/** The encoding of @{code "]]>"}. */
58-
private static final char[] CDATA_END_ENCODED =
59-
"]]>]]<![CDATA[>".toCharArray();
52+
/**
53+
* The encoding of @{code "]]>"}.
54+
*/
55+
private static final char[] CDATA_END_ENCODED
56+
= "]]>]]<![CDATA[>".toCharArray();
6057

61-
/** Length of {@code "]]>]]<![CDATA[>"}. */
58+
/**
59+
* Length of {@code "]]>]]<![CDATA[>"}.
60+
*/
6261
private static final int CDATA_END_ENCODED_LENGTH = 15;
6362

64-
/** Length of {@code "]]>"}. */
63+
/**
64+
* Length of {@code "]]>"}.
65+
*/
6566
private static final int CDATA_END_LENGTH = 3;
6667

6768
@Override
@@ -83,35 +84,37 @@ protected int maxEncodedLength(int n) {
8384
@Override
8485
protected int firstEncodedOffset(String input, int off, int len) {
8586
final int n = off + len;
86-
int closeCount = 0;
87-
for (int i=off ; i<n ; ++i) {
87+
//int closeCount = 0; //unused...
88+
for (int i = off; i < n; ++i) {
8889
char ch = input.charAt(i);
8990
if (ch <= Unicode.MAX_ASCII) {
9091
if (ch != ']') {
91-
if (ch >= ' ' || ch == '\n' || ch == '\r' || ch == '\t') {
92-
// valid
93-
} else {
92+
if (ch < ' ' && ch != '\n' && ch != '\r' && ch != '\t') {
9493
return i;
94+
// } else {
95+
// // valid
9596
}
97+
9698
} else {
97-
if (i+1 < n) {
98-
if (input.charAt(i+1) != ']') {
99+
if (i + 1 < n) {
100+
if (input.charAt(i + 1) != ']') {
99101
// "]x" (next character is safe for this to be ']')
100102
} else {
101103
// "]]?"
102104
// keep looping through ']'
103-
for ( ; i+2 < n && input.charAt(i+2) == ']' ; ++i) {
105+
for (; i + 2 < n && input.charAt(i + 2) == ']'; ++i) {
104106
// valid
105107
}
106108
// at this point we've looped through a sequence
107109
// of 2 or more "]", if the next character is ">"
108110
// we need to encode "]]>".
109-
if (i+2 < n) {
110-
if (input.charAt(i+2) == '>') {
111+
if (i + 2 < n) {
112+
if (input.charAt(i + 2) == '>') {
111113
return i;
112-
} else {
113-
// valid
114+
// } else {
115+
// // valid
114116
}
117+
115118
} else {
116119
return n;
117120
}
@@ -121,15 +124,15 @@ protected int firstEncodedOffset(String input, int off, int len) {
121124
}
122125
}
123126
} else if (ch < Character.MIN_HIGH_SURROGATE) {
124-
if (ch > Unicode.MAX_C1_CTRL_CHAR || ch == Unicode.NEL) {
125-
// valid
126-
} else {
127+
if (ch <= Unicode.MAX_C1_CTRL_CHAR && ch != Unicode.NEL) {
127128
return i;
129+
// } else {
130+
// // valid
128131
}
129132
} else if (ch <= Character.MAX_HIGH_SURROGATE) {
130-
if (i+1 < n) {
131-
if (Character.isLowSurrogate(input.charAt(i+1))) {
132-
int cp = Character.toCodePoint(ch, input.charAt(i+1));
133+
if (i + 1 < n) {
134+
if (Character.isLowSurrogate(input.charAt(i + 1))) {
135+
int cp = Character.toCodePoint(ch, input.charAt(i + 1));
133136
if (Unicode.isNonCharacter(cp)) {
134137
return i;
135138
} else {
@@ -143,16 +146,14 @@ protected int firstEncodedOffset(String input, int off, int len) {
143146
// end of input, high without low = invalid
144147
return i;
145148
}
146-
} else if (
147-
// low surrogate without preceding high surrogate
148-
ch <= Character.MAX_LOW_SURROGATE ||
149-
// non characters
150-
ch > '\ufffd' ||
151-
('\ufdd0' <= ch && ch <= '\ufdef'))
152-
{
149+
} else if ( // low surrogate without preceding high surrogate
150+
ch <= Character.MAX_LOW_SURROGATE
151+
|| // non characters
152+
ch > '\ufffd'
153+
|| ('\ufdd0' <= ch && ch <= '\ufdef')) {
153154
return i;
154-
} else {
155-
// valid
155+
// } else {
156+
// // valid
156157
}
157158

158159
}
@@ -168,7 +169,7 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
168169
int j = output.arrayOffset() + output.position();
169170
final int m = output.arrayOffset() + output.limit();
170171

171-
for ( ; i<n ; ++i) {
172+
for (; i < n; ++i) {
172173
char ch = in[i];
173174
if (ch <= Unicode.MAX_ASCII) {
174175
if (ch != ']') {
@@ -181,8 +182,8 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
181182
out[j++] = XMLEncoder.INVALID_CHARACTER_REPLACEMENT;
182183
}
183184
} else {
184-
if (i+1 < n) {
185-
if (in[i+1] != ']') {
185+
if (i + 1 < n) {
186+
if (in[i + 1] != ']') {
186187
// "]x" (next character is safe for this to be ']')
187188
if (j >= m) {
188189
return overflow(input, i, output, j);
@@ -191,7 +192,7 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
191192
} else {
192193
// "]]?"
193194
// keep looping through ']'
194-
for ( ; i+2 < n && in[i+2] == ']' ; ++i) {
195+
for (; i + 2 < n && in[i + 2] == ']'; ++i) {
195196
if (j >= m) {
196197
return overflow(input, i, output, j);
197198
}
@@ -200,9 +201,9 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
200201
// at this point we've looped through a sequence
201202
// of 2 or more "]", if the next character is ">"
202203
// we need to encode "]]>".
203-
if (i+2 < n) {
204-
if (in[i+2] == '>') {
205-
if (j+CDATA_END_ENCODED_LENGTH > m) {
204+
if (i + 2 < n) {
205+
if (in[i + 2] == '>') {
206+
if (j + CDATA_END_ENCODED_LENGTH > m) {
206207
return overflow(input, i, output, j);
207208
}
208209
System.arraycopy(CDATA_END_ENCODED, 0, out, j, CDATA_END_ENCODED_LENGTH);
@@ -215,7 +216,7 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
215216
out[j++] = ']';
216217
}
217218
} else if (endOfInput) {
218-
if (j+2 > m) {
219+
if (j + 2 > m) {
219220
return overflow(input, i, output, j);
220221
}
221222
out[j++] = ']';
@@ -252,17 +253,17 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
252253
out[j++] = XMLEncoder.INVALID_CHARACTER_REPLACEMENT;
253254
}
254255
} else if (ch <= Character.MAX_HIGH_SURROGATE) {
255-
if (i+1 < n) {
256-
if (Character.isLowSurrogate(in[i+1])) {
257-
int cp = Character.toCodePoint(ch, in[i+1]);
256+
if (i + 1 < n) {
257+
if (Character.isLowSurrogate(in[i + 1])) {
258+
int cp = Character.toCodePoint(ch, in[i + 1]);
258259
if (Unicode.isNonCharacter(cp)) {
259260
if (j >= m) {
260261
return overflow(input, i, output, j);
261262
}
262263
out[j++] = XMLEncoder.INVALID_CHARACTER_REPLACEMENT;
263264
++i;
264265
} else {
265-
if (j+1 >= m) {
266+
if (j + 1 >= m) {
266267
return overflow(input, i, output, j);
267268
}
268269
out[j++] = ch;
@@ -284,13 +285,11 @@ protected CoderResult encodeArrays(CharBuffer input, CharBuffer output, boolean
284285
} else {
285286
break;
286287
}
287-
} else if (
288-
// low surrogate without preceding high surrogate
289-
ch <= Character.MAX_LOW_SURROGATE ||
290-
// non characters
291-
ch > '\ufffd' ||
292-
('\ufdd0' <= ch && ch <= '\ufdef'))
293-
{
288+
} else if ( // low surrogate without preceding high surrogate
289+
ch <= Character.MAX_LOW_SURROGATE
290+
|| // non characters
291+
ch > '\ufffd'
292+
|| ('\ufdd0' <= ch && ch <= '\ufdef')) {
294293
if (j >= m) {
295294
return overflow(input, i, output, j);
296295
}

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

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3232
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3333
// OF THE POSSIBILITY OF SUCH DAMAGE.
34-
3534
package org.owasp.encoder;
3635

3736
import java.io.IOException;
@@ -40,20 +39,19 @@
4039
import java.nio.charset.CoderResult;
4140

4241
/**
43-
* EncodedWriter -- A writer the encodes all input for a specific
44-
* context and writes the encoded output to another writer.
42+
* EncodedWriter -- A writer the encodes all input for a specific context and writes the encoded output to another writer.
4543
*
4644
* @author Jeff Ichnowski
4745
*/
4846
public class EncodedWriter extends Writer {
4947

5048
/**
5149
* Buffer size to allocate.
52-
* */
50+
*
51+
*/
5352
static final int BUFFER_SIZE = 1024;
5453
/**
55-
* Buffer to use for handling characters remaining in the input
56-
* buffer after an encode. The value is set high enough to handle
54+
* Buffer to use for handling characters remaining in the input buffer after an encode. The value is set high enough to handle
5755
* the lookaheads of all the encoders in the package.
5856
*/
5957
static final int LEFT_OVER_BUFFER = 16;
@@ -69,44 +67,37 @@ public class EncodedWriter extends Writer {
6967
private Encoder _encoder;
7068

7169
/**
72-
* Where encoded output is buffered before sending on to the
73-
* output writer.
70+
* Where encoded output is buffered before sending on to the output writer.
7471
*/
7572
private CharBuffer _buffer = CharBuffer.allocate(BUFFER_SIZE);
7673

7774
/**
78-
* Some encoders require more input or an explicit end-of-input
79-
* flag before they will process the remaining characters of an
80-
* input buffer. Because the writer API cannot pass this
81-
* information on to the caller (e.g. by returning how many bytes
82-
* were actually written), this writer implementation must buffer
83-
* up the remaining characters between calls. The
84-
* <code>_hasLeftOver</code> boolean is a flag used to indicate
85-
* that there are left over characters in the buffer.
75+
* Some encoders require more input or an explicit end-of-input flag before they will process the remaining characters of an
76+
* input buffer. Because the writer API cannot pass this information on to the caller (e.g. by returning how many bytes were
77+
* actually written), this writer implementation must buffer up the remaining characters between calls. The
78+
* <code>_hasLeftOver</code> boolean is a flag used to indicate that there are left over characters in the buffer.
8679
*/
8780
private boolean _hasLeftOver;
8881

8982
/**
90-
* See comment on _hasLeftOver. This buffer is created on-demand
91-
* once. Whether it has anything to flush is determined by the
83+
* See comment on _hasLeftOver. This buffer is created on-demand once. Whether it has anything to flush is determined by the
9284
* _hasLeftOver flag.
9385
*/
9486
private CharBuffer _leftOverBuffer;
9587

9688
/**
97-
* Creates an EncodedWriter that uses the specified encoder to
98-
* encode all input before sending it to the wrapped writer.
89+
* Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer.
9990
*
10091
* @param out the target for all writes
10192
* @param encoder the encoder to use
10293
*/
10394
public EncodedWriter(Writer out, Encoder encoder) {
10495
super(out);
10596

106-
if (out == null) {
107-
throw new NullPointerException("writer must not be null");
108-
}
109-
97+
// Reduntant null check, super(out) checks for null and throws NPE.
98+
// if (out == null) {
99+
// throw new NullPointerException("writer must not be null");
100+
// }
110101
if (encoder == null) {
111102
throw new NullPointerException("encoder must not be null");
112103
}
@@ -117,28 +108,26 @@ public EncodedWriter(Writer out, Encoder encoder) {
117108
}
118109

119110
/**
120-
* Creates an EncodedWriter that uses the specified encoder to
121-
* encode all input before sending it to the wrapped writer.
122-
* This method is equivalent to calling:
111+
* Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer. This
112+
* method is equivalent to calling:
123113
* <pre>
124114
* new EncodedWriter(out, Encoders.forName(contextName));
125115
* </pre>
116+
*
126117
* @param out the target for all writes
127118
* @param contextName the encoding context name.
128-
* @throws UnsupportedContextException if the contextName is
129-
* unrecognized or not supported.
119+
* @throws UnsupportedContextException if the contextName is unrecognized or not supported.
130120
*/
131121
public EncodedWriter(Writer out, String contextName)
132-
throws UnsupportedContextException
133-
{
122+
throws UnsupportedContextException {
134123
this(out, Encoders.forName(contextName));
135124
}
136125

137126
@Override
138127
public void write(char[] cbuf, int off, int len) throws IOException {
139128
synchronized (lock) {
140129
CharBuffer input = CharBuffer.wrap(cbuf);
141-
input.limit(off+len).position(off);
130+
input.limit(off + len).position(off);
142131

143132
flushLeftOver(input);
144133

@@ -163,8 +152,7 @@ public void write(char[] cbuf, int off, int len) throws IOException {
163152
}
164153

165154
/**
166-
* Flushes the contents of the buffer to the writer and resets the
167-
* buffer to make room for more input.
155+
* Flushes the contents of the buffer to the writer and resets the buffer to make room for more input.
168156
*
169157
* @throws IOException thrown by the wrapped output.
170158
*/
@@ -174,17 +162,14 @@ private void flushBufferToWriter() throws IOException {
174162
}
175163

176164
/**
177-
* Flushes the left-over buffer. Characters from the input buffer
178-
* are used to add more data to the _leftOverBuffer in order to
165+
* Flushes the left-over buffer. Characters from the input buffer are used to add more data to the _leftOverBuffer in order to
179166
* make the flush happen.
180167
*
181-
* @param input the next input to encode, or null if at end of
182-
* file.
168+
* @param input the next input to encode, or null if at end of file.
183169
* @throws IOException from the underlying writer.
184170
*/
185171
private void flushLeftOver(CharBuffer input)
186-
throws IOException
187-
{
172+
throws IOException {
188173
if (!_hasLeftOver) {
189174
return;
190175
}

0 commit comments

Comments
 (0)