Skip to content

Commit 8c7d5f4

Browse files
authored
Pair replacement (#329)
* removing old code, found 20+yo bug (resulting in a lesser optimization only) in Util.compile_search * remove unused code * use more generic types
1 parent dd6f057 commit 8c7d5f4

File tree

6 files changed

+49
-192
lines changed

6 files changed

+49
-192
lines changed

org/w3c/css/parser/CssSelectors.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public CssSelectors(ApplContext ac) {
9090
super(ac);
9191
style = ac.getCssSelectorsStyle();
9292
try {
93-
properties = (CssStyle) style.newInstance();
93+
properties = (CssStyle) style.getConstructor().newInstance();
9494
} catch (Exception e) {
9595
e.printStackTrace();
9696
}
@@ -101,7 +101,7 @@ private CssSelectors(Class style) {
101101
super();
102102
CssSelectors.style = style;
103103
try {
104-
properties = (CssStyle) style.newInstance();
104+
properties = (CssStyle) style.getConstructor().newInstance();
105105
} catch (Exception e) {
106106
e.printStackTrace();
107107
}
@@ -484,7 +484,7 @@ void Invalidate() {
484484
if (Init) {
485485
// yes I invalidate all properties too !
486486
try {
487-
properties = (CssStyle) style.newInstance();
487+
properties = (CssStyle) style.getConstructor().newInstance();
488488
} catch (Exception e) {
489489
e.printStackTrace();
490490
}

org/w3c/css/properties/css2/font/FontFamily.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.w3c.css.properties.css.CssProperty;
1414
import org.w3c.css.util.ApplContext;
1515
import org.w3c.css.util.InvalidParamException;
16-
import org.w3c.css.util.Util;
1716
import org.w3c.css.values.CssExpression;
1817
import org.w3c.css.values.CssIdent;
1918
import org.w3c.css.values.CssOperator;
@@ -199,7 +198,7 @@ public CssProperty getPropertyInStyle(CssStyle style, boolean resolve) {
199198
/**
200199
* Compares two properties for equality.
201200
*
202-
* @param value The other property.
201+
* @param property The other property.
203202
*/
204203
public boolean equals(CssProperty property) {
205204
return false; //@@ FIXME
@@ -213,9 +212,9 @@ private static String trimToOneSpace(String name) {
213212

214213
name.getChars(0, count, src, 0);
215214
for (int i = 0; i < count; i++)
216-
if (i == 0 || !Util.isWhiteSpace(src[i]) ||
217-
(Util.isWhiteSpace(src[i]) &&
218-
!Util.isWhiteSpace(dst[index])))
215+
if (i == 0 || !Character.isWhitespace(src[i]) ||
216+
(Character.isWhitespace(src[i]) &&
217+
!Character.isWhitespace(dst[index])))
219218
dst[++index] = src[i];
220219

221220
return new String(dst, 0, index + 1);

org/w3c/css/servlet/CssValidator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.w3c.css.servlet;
99

10+
import org.apache.commons.lang3.tuple.Pair;
1011
import org.apache.commons.validator.routines.EmailValidator;
1112
import org.w3c.css.css.CssParser;
1213
import org.w3c.css.css.DocumentParser;
@@ -26,7 +27,6 @@
2627
import org.w3c.css.util.FakeFile;
2728
import org.w3c.css.util.HTTPURL;
2829
import org.w3c.css.util.InvalidParamException;
29-
import org.w3c.css.util.NVPair;
3030
import org.w3c.css.util.Utf8Properties;
3131
import org.w3c.css.util.Util;
3232
import org.w3c.www.mime.MimeType;
@@ -576,8 +576,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse res)
576576
try {
577577
buf = new byte[count];
578578
System.arraycopy(general, 0, buf, 0, count);
579-
for (NVPair pair : Codecs.mpFormDataDecode(buf, req.getContentType())) {
580-
switch (pair.getName()) {
579+
for (Pair<String, ?> pair : Codecs.mpFormDataDecode(buf, req.getContentType())) {
580+
switch (pair.getKey()) {
581581
case opt_file:
582582
file = (FakeFile) pair.getValue();
583583
break;

org/w3c/css/util/Codecs.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838

3939
package org.w3c.css.util;
4040

41+
import org.apache.commons.lang3.tuple.ImmutablePair;
42+
import org.apache.commons.lang3.tuple.Pair;
43+
4144
import java.io.IOException;
45+
import java.nio.charset.StandardCharsets;
46+
import java.util.ArrayList;
4247

4348
/**
4449
* This class collects various encoders and decoders.
@@ -98,16 +103,18 @@ private Codecs() {
98103
* @param cont_type the content type header (must contain the
99104
* boundary string).
100105
* @return an array of name/value pairs, one for each part;
101-
* the name is the 'name' attribute given in the
102-
* Content-Disposition header; the value is either
103-
* the name of the file if a filename attribute was
104-
* found, or the contents of the part.
106+
* the name is the 'name' attribute given in the
107+
* Content-Disposition header; the value is either
108+
* the name of the file if a filename attribute was
109+
* found, or the contents of the part.
105110
* @throws IOException If any file operation fails.
106111
*/
107-
public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
108-
String cont_type)
112+
public final static synchronized ArrayList<Pair<String, ?>> mpFormDataDecode(byte[] data,
113+
String cont_type)
109114
throws IOException {
110115

116+
ArrayList<Pair<String, ?>> pList = new ArrayList<>();
117+
111118
// Find and extract boundary string
112119
String bndstr = getParameter("boundary", cont_type);
113120
if (bndstr == null) {
@@ -119,9 +126,9 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
119126
boundary = new byte[bndstr.length() + 6],
120127
endbndry = new byte[bndstr.length() + 6];
121128

122-
srtbndry = ("--" + bndstr + "\n").getBytes();
123-
boundary = ("\n--" + bndstr + "\n").getBytes();
124-
endbndry = ("\n--" + bndstr + "--").getBytes();
129+
srtbndry = ("--" + bndstr + "\n").getBytes(StandardCharsets.ISO_8859_1);
130+
boundary = ("\n--" + bndstr + "\n").getBytes(StandardCharsets.ISO_8859_1);
131+
endbndry = ("\n--" + bndstr + "--").getBytes(StandardCharsets.ISO_8859_1);
125132

126133
if (debugMode) {
127134
System.err.println("[START OF DATA]");
@@ -163,7 +170,6 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
163170

164171
start += srtbndry.length;
165172

166-
NVPair[] res = new NVPair[10];
167173
boolean done = false;
168174
int idx;
169175

@@ -314,20 +320,14 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
314320
} else { // It's simple data
315321
value = new String(data, start, end - start);
316322
}
317-
318-
if (idx >= res.length) {
319-
res = Util.resizeArray(res, idx + 10);
320-
}
321-
322-
res[idx] = new NVPair(name, value);
323+
pList.add(new ImmutablePair<>(name, value));
323324
if (debugMode) {
324325
System.err.println("[ADD " + name + ',' + value + ','
325326
+ value.getClass() + ']');
326327
}
327328
start = end + boundary.length;
328329
}
329-
330-
return Util.resizeArray(res, idx);
330+
return pList;
331331
}
332332

333333

@@ -364,22 +364,22 @@ public final static String getParameter(String param, String hdr) {
364364
pbeg = vend + 1;
365365
}
366366
while (pbeg < len - 1
367-
&& (Util.isWhiteSpace(hdr.charAt(pbeg)) || (hdr.charAt(pbeg) == ';'))) {
367+
&& (Character.isWhitespace(hdr.charAt(pbeg)) || (hdr.charAt(pbeg) == ';'))) {
368368
pbeg++;
369369
}
370370
if (pbeg == len - 1) return null;
371371
pend = hdr.indexOf('=', pbeg + 1); // get '='
372372
if (pend == -1) return null;
373373
vbeg = pend + 1;
374-
while (Util.isWhiteSpace(hdr.charAt(--pend))) ;
374+
while (Character.isWhitespace(hdr.charAt(--pend))) ;
375375
pend++;
376376

377377
if (debugMode) {
378378
System.err.println("[DEBUG] p is [" + hdr.substring(pbeg, pend) + "]");
379379
}
380380
// mark parameter value
381381

382-
while (vbeg < len && Util.isWhiteSpace(hdr.charAt(vbeg))) vbeg++;
382+
while (vbeg < len && Character.isWhitespace(hdr.charAt(vbeg))) vbeg++;
383383
if (vbeg == len) return null;
384384

385385
vend = vbeg;
@@ -390,7 +390,7 @@ public final static String getParameter(String param, String hdr) {
390390
} else { // is a simple token
391391
vend = hdr.indexOf(';', vbeg);
392392
if (vend == -1) vend = hdr.length();
393-
while (Util.isWhiteSpace(hdr.charAt(--vend))) ;
393+
while (Character.isWhitespace(hdr.charAt(--vend))) ;
394394
vend++;
395395
}
396396
if (hdr.regionMatches(true, pbeg, param, 0, pend - pbeg)) {

org/w3c/css/util/NVPair.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)