Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some performace-related idea warnings #353

Merged
merged 8 commits into from
Jul 19, 2024
27 changes: 27 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ static NthChildCondition fromString(String number) {
Matcher m = pattern.matcher(number);

if (!m.matches()) {
throw new CSSParseException("Invalid nth-child selector: " + number, -1);
throw new CSSParseException("Invalid nth-child selector: " + number, -1, e);
} else {
int a = m.group(2).isEmpty() ? 1 : Integer.parseInt(m.group(2));
int b = (m.group(5) == null) ? 0 : Integer.parseInt(m.group(5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.xhtmlrenderer.css.parser;

public class CSSParseException extends RuntimeException {
private static final long serialVersionUID = 1L;

private final Token _found;
private final Token[] _expected;
private int _line;
Expand All @@ -31,6 +29,11 @@ public class CSSParseException extends RuntimeException {
private boolean _callerNotified;

public CSSParseException(String message, int line) {
this(message, line, null);
}

public CSSParseException(String message, int line, Throwable cause) {
super(message, cause);
_found = null;
_expected = null;
_line = line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void import_rule(Stylesheet stylesheet) throws IOException {
System.out.println("Token: " + tokenValue + " resolved " + resolvedUri);
info.setUri(resolvedUri);
} catch (URISyntaxException use) {
throw new CSSParseException("Invalid URL, " + use.getMessage(), getCurrentLine());
throw new CSSParseException("Invalid URL, " + use.getMessage(), getCurrentLine(), use);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,31 @@ public String evaluate() {
}

public static String createCounterText(IdentValue listStyle, int listCounter) {
String text;
if (listStyle == IdentValue.LOWER_LATIN || listStyle == IdentValue.LOWER_ALPHA) {
text = toLatin(listCounter).toLowerCase();
return toLatin(listCounter).toLowerCase();
} else if (listStyle == IdentValue.UPPER_LATIN || listStyle == IdentValue.UPPER_ALPHA) {
text = toLatin(listCounter).toUpperCase();
return toLatin(listCounter).toUpperCase();
} else if (listStyle == IdentValue.LOWER_ROMAN) {
text = toRoman(listCounter).toLowerCase();
return toRoman(listCounter).toLowerCase();
} else if (listStyle == IdentValue.UPPER_ROMAN) {
text = toRoman(listCounter).toUpperCase();
return toRoman(listCounter).toUpperCase();
} else if (listStyle == IdentValue.DECIMAL_LEADING_ZERO) {
text = (listCounter >= 10 ? "" : "0") + listCounter;
return (listCounter >= 10 ? "" : "0") + listCounter;
} else { // listStyle == IdentValue.DECIMAL or anything else
text = Integer.toString(listCounter);
return Integer.toString(listCounter);
}
return text;
}


private static String toLatin(int val) {
String result = "";
val -= 1;
private static String toLatin(int index) {
StringBuilder result = new StringBuilder(5);
int val = index - 1;
while (val >= 0) {
int letter = val % 26;
val = val / 26 - 1;
result = ((char) (letter + 65)) + result;
result.insert(0, (char) (letter + 65));
}
return result;
return result.toString();
}

private static String toRoman(int val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.charset.UnsupportedCharsetException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class DataURLConnection extends URLConnection {

Expand Down Expand Up @@ -176,13 +177,14 @@ class Base64 {

private static final byte[] EMPTY_BYTE_ARRAY = {};
private static final String _map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private static final Pattern RE_FORBIDDEN_CHARACTERS = Pattern.compile("[^" + _map + "]+");

public static byte [] decode(String s) {
public static byte[] decode(String s) {

if (s == null || s.length() < 4)
return EMPTY_BYTE_ARRAY;

s = s.replaceAll("[^A-Za-z0-9+/=]+", "");
s = RE_FORBIDDEN_CHARACTERS.matcher(s).replaceAll("");

if (s.length() < 4)
return EMPTY_BYTE_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void setFeature(XMLReader xmlReader) {
xmlReader.setFeature("http://xml.org/sax/features/validation", false);

XRLog.xmlEntities(Level.FINE, "SAX Parser feature: " +
"http://xml.org/sax/features/validation".substring("http://xml.org/sax/features/validation".lastIndexOf("/")) +
"http://xml.org/sax/features/validation".substring("http://xml.org/sax/features/validation".lastIndexOf('/')) +
" set to " +
xmlReader.getFeature("http://xml.org/sax/features/validation"));
} catch (SAXNotSupportedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private XMLResource createXMLResource(XMLResource target) {

target.setElapsedLoadTime(end - start);
XRLog.load("Loaded document in ~" + target.getElapsedLoadTime() + "ms");

target.setDocument(document);
return target;
}
Expand Down Expand Up @@ -331,7 +331,7 @@ private void setFeature(XMLReader xmlReader, String featureUri, boolean value) {
xmlReader.setFeature(featureUri, value);

XRLog.load(Level.FINE, "SAX Parser feature: " +
featureUri.substring(featureUri.lastIndexOf("/")) +
featureUri.substring(featureUri.lastIndexOf('/')) +
" set to " +
xmlReader.getFeature(featureUri));
} catch (SAXNotSupportedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static BufferedImage renderToImage(File inFile, String path, int width, i
*/
private static BufferedImage renderImageToOutput(String url, String path, int width)
throws IOException {

try (OutputStream os = new BufferedOutputStream(newOutputStream(Paths.get(path)))) {
Java2DRenderer renderer = new Java2DRenderer(url, url, width);
BufferedImage image = renderer.getImage();
Expand All @@ -148,7 +148,7 @@ public static void main(String[] args) throws IOException {
File f = new File(url);
if (f.exists()) {
String output = f.getAbsolutePath();
output = output.substring(0, output.lastIndexOf(".")) + ".png";
output = output.substring(0, output.lastIndexOf('.')) + ".png";
System.out.println("Saving image to " + output);
renderToImage(f, output, DEFAULT_WIDTH);
} else {
Expand All @@ -171,4 +171,4 @@ private static void usage(String err) {
System.err.println("Usage: ... [url]");
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,11 @@ private void traverseOptions(Element e, String prefix) {
traverseOptions(child, prefix + child.getAttribute("label")
+ " ");
} else if (child.getNodeName().equalsIgnoreCase("option")) {
String value = child.getAttribute("value");
String label = child.getAttribute("label");
String valueAttribute = child.getAttribute("value");
String labelAttribute = child.getAttribute("label");
String content = collectText(child);
if (value.isEmpty()) {
value = content;
}
if (label.isEmpty()) {
label = content;
} else {
label = prefix + label;
}
String value = valueAttribute.isEmpty() ? content : valueAttribute;
String label = labelAttribute.isEmpty() ? content : prefix + labelAttribute;
_options.put(value, label);
if (!child.getAttribute("selected").isEmpty()) {
if (isMultiple()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private boolean verbose() {
}

private Iterable<File> listSourceFiles(File sourceDirectory) {
File[] files = sourceDirectory.listFiles((dir, name) -> EXTENSIONS.contains(name.substring(name.lastIndexOf(".") + 1)));
File[] files = sourceDirectory.listFiles((dir, name) -> EXTENSIONS.contains(name.substring(name.lastIndexOf('.') + 1)));
return files == null ? emptyList() : asList(files);
}

Expand Down Expand Up @@ -270,12 +270,12 @@ private void checking(File source) {

private void report() {
int failed = 0;
for (File file : files.keySet()) {
Result result = files.get(file);
for (Map.Entry<File, Result> entry : files.entrySet()) {
Result result = entry.getValue();

if (result instanceof FailedResult) {
failed++;
System.out.println(result.describe(file));
System.out.println(result.describe(entry.getKey()));
}
}
System.out.println("Checked " + files.keySet().size() + " files, " + (failed > 0 ? failed + " failed." : "all OK."));
Expand Down Expand Up @@ -332,4 +332,4 @@ public String describe(File file) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
*/
public class Configuration {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(Configuration.class);

/**
* Our backing data store of properties.
*/
Expand Down Expand Up @@ -671,7 +671,7 @@ public static Iterator<String> keysByPrefix(String prefix) {
.iterator();
}


/**
* Returns true if the value is "true" (ignores case), or the default
* provided value if not found or if the value is not a valid boolean (true
Expand Down Expand Up @@ -734,7 +734,7 @@ public static Object valueFromClassConstant(String key, Object defaultValue) {
if ( val == null ) {
return defaultValue;
}
int idx = val.lastIndexOf(".");
int idx = val.lastIndexOf('.');
String klassname;
String cnst;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class FontUtil {

public static Boolean isEmbeddedBase64Font(String uri) {
public static boolean isEmbeddedBase64Font(String uri) {
return uri != null && uri.startsWith("data:font/");
}

Expand Down
8 changes: 4 additions & 4 deletions flying-saucer-core/src/test/java/eeze/Eeze.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
*/
public class Eeze {
private static final Logger log = LoggerFactory.getLogger(Eeze.class);

private List<File> testFiles;
private JFrame eezeFrame;
private File currentDisplayed;
Expand All @@ -77,7 +77,7 @@ public class Eeze {
private ImagePanel imagePanel;
private boolean comparingWithImage;
private File directory;

private static final FileFilter HTML_FILE_FILTER = f ->
f.getName().endsWith(".html") ||
f.getName().endsWith(".htm") ||
Expand Down Expand Up @@ -311,7 +311,7 @@ private Image loadImageForPage() {
File parent = file.getAbsoluteFile().getParentFile();
if (parent == null) parent = file;
File imgDir = new File(parent.getAbsolutePath() + File.separator + "ref-img");
String name = file.getName().substring(0, file.getName().lastIndexOf(".")) + ".png";
String name = file.getName().substring(0, file.getName().lastIndexOf('.')) + ".png";
File target = new File(imgDir.getAbsolutePath() + File.separator + name);
if (target.exists()) {
img = new ImageIcon(target.toURI().toURL()).getImage();
Expand Down Expand Up @@ -619,7 +619,7 @@ public void actionPerformed(ActionEvent e) {
return;
}
}
String name = file.getName().substring(0, file.getName().lastIndexOf(".")) + ".png";
String name = file.getName().substring(0, file.getName().lastIndexOf('.')) + ".png";
File target = new File(imgDir.getAbsolutePath() + File.separator + name);
if (target.exists() && JOptionPane.showConfirmDialog(
Eeze.this.eezeFrame,
Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-examples/src/main/java/ImageRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void main(String[] args) throws IOException {

FSImageWriter imageWriter = new FSImageWriter();
String path = f.getAbsolutePath();
path = path.substring(0, path.lastIndexOf("."));
path = path.substring(0, path.lastIndexOf('.'));
imageWriter.write(image, path + ".png");

// compare to old
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ private BaseFont createFont(String name, String encoding, boolean embedded) {
return BaseFont.createFont(name, encoding, embedded);
}
catch (DocumentException | IOException e) {
throw new RuntimeException("Failed to load font " + name + " and encoding " + encoding);
throw new RuntimeException("Failed to load font " + name + " and encoding " + encoding, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ private void parseHtmlMetaTags( Document doc ) {
*/
private void addPdfMetaValuesToPdfDocument( ITextRenderer renderer ) {

for (PdfName pdfName : this.pdfInfoValues.keySet()) {
PdfString pdfString = pdfInfoValues.get(pdfName);
for (Map.Entry<PdfName, PdfString> entry : pdfInfoValues.entrySet()) {
PdfName pdfName = entry.getKey();
PdfString pdfString = entry.getValue();
XRLog.render(Level.FINEST, "pdfName=" + pdfName + ", pdfString=" + pdfString);
renderer.getOutputDevice().getWriter().getInfo().put(pdfName, pdfString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
import org.xhtmlrenderer.simple.xhtml.controls.TextControl;
import org.xhtmlrenderer.swt.BasicRenderer;

import java.util.regex.Pattern;

public class SWTTextControl extends SWTXhtmlControl {

private static final Pattern RE_NEWLINE = Pattern.compile("\n");
private String _sizeText;
private boolean _noChangeText = false;

Expand Down Expand Up @@ -108,7 +111,7 @@ public int getIdealHeight() {
}

private static String encodeDelimiter(String text) {
return text.replaceAll("\n", Text.DELIMITER);
return RE_NEWLINE.matcher(text).replaceAll(Text.DELIMITER);
}

private static String decodeDelimiter(String text) {
Expand Down