Skip to content

Commit 25e1fc7

Browse files
committed
Javascript RegExp screams.
1 parent 734181e commit 25e1fc7

File tree

12 files changed

+245
-35
lines changed

12 files changed

+245
-35
lines changed

sources/net.sf.j2s.java.core/src/java/io/File.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ public File getAbsoluteFile() {
604604
* @since JDK1.1
605605
*/
606606
public String getCanonicalPath() throws IOException {
607-
return this.path.replace('\\', '/');
607+
return this.path.replaceAll("\\\\", "/");
608608
// return fs.canonicalize(fs.resolve(this));
609609
}
610610

@@ -636,7 +636,7 @@ public File getCanonicalFile() throws IOException {
636636

637637
private static String slashify(String path, boolean isDirectory) {
638638
String p = path;
639-
p = p.replace('\\','/');
639+
p = p.replaceAll("\\\\", "/");
640640
if (!p.startsWith("/"))
641641
p = "/" + p;
642642
if (!p.endsWith("/") && isDirectory)

sources/net.sf.j2s.java.core/src/java/io/FileSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public boolean isAbsolute(File f) {
259259
* normal form then it is simply returned.
260260
*/
261261
public String normalize(String path) {
262-
return path.replace('\\', '/');
262+
return path.replaceAll("\\\\", "/");
263263
}
264264

265265
public long getLength(File file) {

sources/net.sf.j2s.java.core/src/swingjs/JSHTMLHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private String getCSS(String id) {
216216
css += s + "\n";
217217
}
218218
}
219-
return PT.rep(css, "NamedStyle:", "#" + id + " ").replace('=',':').replace(',',';');
219+
return css.replaceAll("NamedStyle:", "#" + id + " ").replaceAll("=",":").replaceAll(",", ";");
220220
}
221221

222222

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ protected void setMnemonic(int newValue) {
14831483
*/
14841484
protected boolean isUIDisabled;
14851485

1486-
protected boolean setUIDisabled(boolean b) {
1486+
public boolean setUIDisabled(boolean b) {
14871487
return isUIDisabled = b;
14881488
}
14891489

@@ -1808,7 +1808,7 @@ protected Dimension setHTMLSize1(DOMNode node, boolean addCSS, boolean usePrefer
18081808
}
18091809
}
18101810
// allow a UI to slightly adjust its dimension
1811-
Dimension dim = getCSSAdjustment(addCSS);
1811+
Dimension dim = getCSSAdjustment(addCSS, true);
18121812
dim.width += w;
18131813
dim.height += h;
18141814
DOMNode.setStyles(node, "position", null);
@@ -1833,16 +1833,19 @@ protected Dimension setHTMLSize1(DOMNode node, boolean addCSS, boolean usePrefer
18331833
}
18341834

18351835
/**
1836-
* allows for can be overloaded to allow some special adjustments
1836+
* allows for can be overloaded to allow some special adjustments;
1837+
* must be mutable
18371838
*
1838-
* @param addingCSS TODO
1839+
* @param addingCSS see subclasses
1840+
* @param mutable TODO
18391841
*
18401842
* @return
18411843
*/
1842-
protected Dimension getCSSAdjustment(boolean addingCSS) {
1843-
return new Dimension(0, 0);
1844+
protected Dimension getCSSAdjustment(boolean addingCSS, boolean mutable) {
1845+
return mutable ? new Dimension(0, 0) : ZERO_SIZE;
18441846
}
18451847

1848+
protected static Dimension ZERO_SIZE = new Dimension(0, 0);
18461849
protected static Dimension ANY_SIZE = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
18471850

18481851
/**
@@ -2372,7 +2375,7 @@ private void setOuterLocationFromComponent() {
23722375
private void setSizeFromComponent(int width, int height, int op) {
23732376
// allow for special adjustments
23742377
// currently MenuItem, TextField, and TextArea
2375-
Dimension size = getCSSAdjustment(true);
2378+
Dimension size = getCSSAdjustment(true, false);
23762379
// if (this.width != width || this.height != height) {
23772380
this.width = width;
23782381
this.height = height;
@@ -2405,6 +2408,14 @@ private ImageIcon getIcon(JSComponent c, Icon icon) {
24052408
: (icon instanceof ImageIcon) ? (ImageIcon) icon : JSToolkit.createImageIcon(jc, icon, id + "tmpIcon"));
24062409
}
24072410

2411+
@SuppressWarnings("unused")
2412+
private static Object re0 = /** @j2sNative new RegExp("\u0000","gm") || */null;
2413+
@SuppressWarnings("unused")
2414+
private static Object reSpace = /** @j2sNative new RegExp(" ","gm") || */null;
2415+
@SuppressWarnings("unused")
2416+
private static Object reLT = /** @j2sNative new RegExp("<","gm") || */null;
2417+
2418+
24082419
/**
24092420
* remove 0x0000 and replace space with nonbreaking space if not a textarea
24102421
*
@@ -2419,9 +2430,9 @@ protected String fixText(String t) {
24192430
// file://testing -> swingjs/j2s/testing
24202431
// file:/testing --> swintjs/j2s/testing
24212432
String rp = J2S.getResourcePath("", true);
2422-
t = PT.rep(t, "file:/", t.indexOf(rp) >= 0 ? "" : rp);
2433+
t = t.replaceAll("file:/", t.indexOf(rp) >= 0 ? "" : rp);
24232434
} else if (valueNode == null) {
2424-
t = PT.rep(t, "\u0000", "").replace(' ', '\u00A0');
2435+
/** @j2sNative t = t.replace(C$.re0, "").replace(C$.reSpace, "\u00A0"); */
24252436
}
24262437
}
24272438
return t;
@@ -2508,9 +2519,9 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
25082519
if (text.indexOf("<html>") == 0) {
25092520
isHTML = true;
25102521
// PhET uses <html> in labels and uses </br>
2511-
text = PT.rep(text.substring(6), "</br>", "");
2512-
text = PT.rep(text, "</html>", "");
2513-
text = PT.rep(text, "href=", "target=_blank href=");
2522+
text = text.substring(6).replaceAll("</br>", "");
2523+
text = text.replaceAll("</html>", "");
2524+
text = text.replaceAll("href=", "target=_blank href=");
25142525
} else if (jc.getClientProperty("html") != null) {
25152526
isHTML = true;
25162527
} else if (mnemonicIndex >= 0) {
@@ -2530,8 +2541,9 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
25302541

25312542
setCssFont(domNode, getFont()); // for vertical centering
25322543
setCssFont(textNode, getFont());
2533-
if (!isHTML)
2534-
text = PT.rep(text, "<", "&lt;").replace(' ', '\u00A0');
2544+
if (!isHTML) {
2545+
/** @j2sNative text = text.replace(C$.reLT, "&lt;").replace(C$.reSpace, "\u00A0");*/
2546+
}
25352547
} else if (valueNode != null) {
25362548
prop = "value";
25372549
obj = valueNode;

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSEditorPaneUI.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,12 @@ else if (isSup)
498498
} else {
499499
String t = text.substring(start, isDiv ? end - 1 : end);
500500
if (t.indexOf(' ') >= 0)
501-
t = t.replace(' ', '\u00A0');
501+
t = t.replaceAll(" ", "\u00A0");
502502
if (t.indexOf('\t') >= 0) {
503-
t = PT.rep(t, "\t", JSTAB);
503+
t = t.replaceAll("\t", JSTAB);
504504
}
505505
if (t.indexOf('<') >= 0) {
506-
t = PT.rep(PT.rep(t, "<", "&lt;"), ">", "&gt;");
506+
t = t.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
507507
}
508508
sb.append(t);
509509
}
@@ -603,10 +603,10 @@ private boolean checkAttr(int attr, AttributeSet a, AttributeSet currAttr) {
603603
return false;
604604
}
605605

606-
@Override
607-
protected Dimension getCSSAdjustment(boolean addingCSS) {
608-
return new Dimension(0, 0);
609-
}
606+
// @Override
607+
// protected Dimension getCSSAdjustment(boolean addingCSS, boolean mutable) {
608+
// return mutable ? new Dimension(0, 0) : ZERO_SIZE;
609+
// }
610610

611611
@Override
612612
protected String getPropertyPrefix() {
@@ -704,7 +704,7 @@ protected Object[] getJSNodePt(DOMNode node, int pt, boolean isRoot) {
704704

705705
@Override
706706
public String getJSTextValue() {
707-
String s = getInnerTextSafely(domNode, false, null).toString().replace('\u00A0',' '); // &nbsp;
707+
String s = getInnerTextSafely(domNode, false, null).toString().replaceAll("\u00A0"," "); // &nbsp;
708708
// System.out.println("getjSTextValue " + s);
709709
return s;
710710
}
@@ -887,12 +887,17 @@ boolean getJSMarkAndDot(Point pt, int keycode) {
887887
*
888888
//System.out.println("getJSMandD " + [toEnd,toStart]);
889889
*
890-
* var s = window.getSelection(); anode = s.anchorNode; apt =
890+
* var s = window.getSelection(); anode = s.anchorNode;
891+
* if (anode) {
892+
*
893+
* apt =
891894
* s.anchorOffset; if (anode.tagName) { anode =
892895
* anode.childNodes[apt]; apt = 0; } else { alen = anode.length; apar
893896
* = anode.parentElement; } fnode = s.focusNode; fpt = s.focusOffset;
894897
* if (fnode.tagName) { fnode = fnode.childNodes[fpt]; fpt = 0; }
895898
* else { flen = fnode.length; fpar = fnode.parentElement; }
899+
*
900+
* }
896901
*/
897902

898903
if (anode == null || fnode == null) {

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSMenuItemUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected int getContainerHeight() {
4949
}
5050

5151
@Override
52-
protected Dimension getCSSAdjustment(boolean addingCSS) {
53-
return new Dimension(isMenu && containerNode == null ? 0 : 5, 0);
52+
protected Dimension getCSSAdjustment(boolean addingCSS, boolean mutable) {
53+
return mutable || !isMenu || containerNode != null ? new Dimension(isMenu && containerNode == null ? 0 : 5, 0) : ZERO_SIZE;
5454
}
5555

5656
@Override

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSSplitPaneUI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,21 +1418,21 @@ protected int[] getSizes() {
14181418
* Returns the width of the passed in Components preferred size.
14191419
*/
14201420
protected int getPreferredSizeOfComponent(Component c) {
1421-
return getSizeForPrimaryAxis(c.getPreferredSize());
1421+
return c == null ? 0 : getSizeForPrimaryAxis(c.getPreferredSize());
14221422
}
14231423

14241424
/**
14251425
* Returns the width of the passed in Components minimum size.
14261426
*/
14271427
int getMinimumSizeOfComponent(Component c) {
1428-
return getSizeForPrimaryAxis(c.getMinimumSize());
1428+
return c == null ? 0 : getSizeForPrimaryAxis(c.getMinimumSize());
14291429
}
14301430

14311431
/**
14321432
* Returns the width of the passed in component.
14331433
*/
14341434
protected int getSizeOfComponent(Component c) {
1435-
return getSizeForPrimaryAxis(c.getSize());
1435+
return c == null ? 0 : getSizeForPrimaryAxis(c.getSize());
14361436
}
14371437

14381438
/**

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSTextFieldUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public DOMNode updateDOMNode() {
4646
}
4747

4848
@Override
49-
protected Dimension getCSSAdjustment(boolean addingCSS) {
50-
return new Dimension(0, addingCSS ? 0 : -2);
49+
protected Dimension getCSSAdjustment(boolean addingCSS, boolean mutable) {
50+
return mutable || !addingCSS ? new Dimension(0, addingCSS ? 0 : -2) : ZERO_SIZE;
5151
}
5252

5353
@Override

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ private QName getName(String tag, Map<String, String> attr) {
505505
* @param attr
506506
*/
507507
private void addXMLAttributes(String tag, String data, Map<String, String> attr) {
508-
data = "<__ " + data.replace('{', '\'').replace('}', '\'') + " />";
508+
data = "<__ " + data.replaceAll("\\{", "'").replaceAll("\\}", "'") + " />";
509509
// System.out.println(data);
510510
DOMNode doc = JSUtil.jQuery.parseXML(data);
511511
DOMNode node = DOMNode.firstChild(doc);

sources/net.sf.j2s.java.core/src/test/Test_Regex.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public static void main(String[] args) {
1818
String s = Arrays.toString(splitUnquoted("this; is \"a;test\"", ";"));
1919
System.out.println(s);
2020
assert(s.equals("[this, is \"a;test\"]"));
21+
22+
s = "x/2 is a very excellent 1/x or x";
23+
24+
assert("y/2 is a very excellent 1/y or y".equals((" " +s + " ").replaceAll("(\\W)x(\\W)", "$1y$2").trim()));
25+
26+
System.out.println(Pattern.compile("(\\W)y(\\W)").matcher("(m*(@^2+~^2)/r+m*g*(1-y/r))*(1-y/r)" ).replaceAll("$1`$2").trim());
27+
28+
System.out.println(Pattern.compile("(\\W)y(\\W)").matcher(" y+ y*2 " ).replaceAll("$1`$2").trim());
29+
2130
System.out.println("Test_Regex OK");
2231
}
2332

@@ -63,4 +72,17 @@ public static String eval(String s, Map<String, Double> vars) {
6372
}
6473

6574

75+
class Matcher1 {
76+
77+
int grN = 0;
78+
79+
public String toString() {
80+
return group(grN);
81+
}
82+
83+
}
84+
85+
String group(int n) {
86+
return "" + n;
87+
}
6688
}

0 commit comments

Comments
 (0)