Skip to content

Commit

Permalink
refactor exception handling & closing I/O resources
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Dec 29, 2023
1 parent 802e8a9 commit 1dd13bd
Show file tree
Hide file tree
Showing 40 changed files with 359 additions and 710 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.resource.CSSResource;
import org.xhtmlrenderer.util.Configuration;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.XRLog;
import org.xml.sax.InputSource;

Expand Down Expand Up @@ -91,11 +92,7 @@ private Stylesheet parse(StylesheetInfo info) {
// Shouldn't happen
throw new RuntimeException(e.getMessage(), e);
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
IOUtil.close(is);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private static TableCellBox createMarginBox(
stripAllWhitespace(children);
}

if (children.size() == 0 && style.isAutoWidth() && ! alwaysCreate) {
if (children.isEmpty() && style.isAutoWidth() && ! alwaysCreate) {
return null;
}

Expand All @@ -291,14 +291,14 @@ private static TableCellBox createMarginBox(

private static void resolveChildren(
LayoutContext c, BlockBox owner, List<Styleable> children, ChildBoxInfo info) {
if (children.size() > 0) {
if (!children.isEmpty()) {
if (info.isContainsBlockLevelContent()) {
insertAnonymousBlocks(
c.getSharedContext(), owner, children, info.isLayoutRunningBlocks());
owner.setChildrenContentType(BlockBox.CONTENT_BLOCK);
} else {
WhitespaceStripper.stripInlineContent(children);
if (children.size() > 0) {
if (!children.isEmpty()) {
owner.setInlineContent(children);
owner.setChildrenContentType(BlockBox.CONTENT_INLINE);
} else {
Expand Down Expand Up @@ -339,7 +339,7 @@ private static void resolveChildTableContent(
if (matchesTableLevel(target, styleable.getStyle().getIdent(CSSName.DISPLAY))) {
childrenForAnonymous.add(styleable);
} else {
if (childrenForAnonymous.size() > 0) {
if (!childrenForAnonymous.isEmpty()) {
createAnonymousTableContent(c, (BlockBox) childrenForAnonymous.get(0), nextUp,
childrenForAnonymous, childrenWithAnonymous);

Expand All @@ -349,7 +349,7 @@ private static void resolveChildTableContent(
}
}

if (childrenForAnonymous.size() > 0) {
if (!childrenForAnonymous.isEmpty()) {
createAnonymousTableContent(c, (BlockBox) childrenForAnonymous.get(0), nextUp,
childrenForAnonymous, childrenWithAnonymous);
}
Expand Down Expand Up @@ -449,7 +449,7 @@ private static void resolveTableContent(
IdentValue childDisplay = child.getStyle().getIdent(CSSName.DISPLAY);

if (isProperTableNesting(parentDisplay, childDisplay)) {
if (childrenForAnonymous.size() > 0) {
if (!childrenForAnonymous.isEmpty()) {
createAnonymousTableContent(c, parent, next, childrenForAnonymous,
childrenWithAnonymous);

Expand All @@ -461,7 +461,7 @@ private static void resolveTableContent(
}
}

if (childrenForAnonymous.size() > 0) {
if (!childrenForAnonymous.isEmpty()) {
createAnonymousTableContent(c, parent, next, childrenForAnonymous,
childrenWithAnonymous);
}
Expand Down Expand Up @@ -558,7 +558,7 @@ private static BlockBox reorderTableContent(LayoutContext c, TableBox table) {
table.addChild(footer);
}

if (topCaptions.size() == 0 && bottomCaptions.size() == 0) {
if (topCaptions.isEmpty() && bottomCaptions.isEmpty()) {
return table;
} else {
// If we have a floated table with a caption, we need to float the
Expand Down Expand Up @@ -680,7 +680,7 @@ private static boolean isAttrFunction(FSFunction function) {
public static boolean isElementFunction(FSFunction function) {
if (function.getName().equals("element")) {
List<PropertyValue> params = function.getParameters();
if (params.size() < 1 || params.size() > 2) {
if (params.isEmpty() || params.size() > 2) {
return false;
}
PropertyValue value1 = params.get(0);
Expand All @@ -699,7 +699,7 @@ public static boolean isElementFunction(FSFunction function) {
private static CounterFunction makeCounterFunction(FSFunction function, LayoutContext c, CalculatedStyle style) {
if (function.getName().equals("counter")) {
List<PropertyValue> params = function.getParameters();
if (params.size() < 1 || params.size() > 2) {
if (params.isEmpty() || params.size() > 2) {
return null;
}

Expand Down Expand Up @@ -1259,7 +1259,7 @@ private static void insertAnonymousBlocks(
}
}
} else {
if (inline.size() > 0) {
if (!inline.isEmpty()) {
createAnonymousBlock(parent, inline, savedParents);
inline = new ArrayList<>();
savedParents = new ArrayList<>(parents);
Expand All @@ -1273,11 +1273,11 @@ private static void insertAnonymousBlocks(

private static void createAnonymousBlock(Box parent, List<Styleable> inline, List<InlineBox> savedParents) {
WhitespaceStripper.stripInlineContent(inline);
if (inline.size() > 0) {
if (!inline.isEmpty()) {
AnonymousBlockBox anon = new AnonymousBlockBox(parent.getElement());
anon.setStyle(parent.getStyle().createAnonymousStyle(IdentValue.BLOCK));
anon.setAnonymous(true);
if (savedParents != null && savedParents.size() > 0) {
if (savedParents != null && !savedParents.isEmpty()) {
anon.setOpenInlineBoxes(savedParents);
}
parent.addChild(anon);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.xhtmlrenderer.swing.Java2DRenderer;
import org.xhtmlrenderer.util.FSImageWriter;
import org.xhtmlrenderer.util.IOUtil;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -138,13 +139,7 @@ public static BufferedImage renderImageToOutput(String url, FSImageWriter fsw, S

return image;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
IOUtil.close(os);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,30 +378,16 @@ public StylesheetInfo getDefaultStylesheet(StylesheetFactory factory) {
info.setMedia("all");
info.setType("text/css");

InputStream is = null;
try {
is = getDefaultStylesheetStream();

try (InputStream is = getDefaultStylesheetStream()) {
if (_defaultStylesheetError) {
return null;
}

Stylesheet sheet = factory.parse(new InputStreamReader(is), info);
info.setStylesheet(sheet);

is.close();
is = null;
} catch (IOException e) {
_defaultStylesheetError = true;
XRLog.exception("Could not parse default stylesheet", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
}

_defaultStylesheet = info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,11 @@ public void reloadDocument(Document doc) {
}

public URL getURL() {
URL base = null;
try {
base = new URL(getSharedContext().getUac().getBaseURL());
return new URL(getSharedContext().getUac().getBaseURL());
} catch (MalformedURLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
throw new RuntimeException(e);
}
return base;
}

public Document getDocument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -126,38 +125,24 @@ public ImageResource getImageResource(String uri) {
*/
public XMLResource getXMLResource(String uri) {
String ruri = _uriResolver.resolve(uri);
StreamResource sr = new StreamResource(ruri);
try {
try (StreamResource sr = new StreamResource(ruri)) {
sr.connect();
BufferedInputStream bis = sr.bufferedStream();
return XMLResource.load(bis);
} catch (IOException e) {
return null;
} finally {
sr.close();
}
}

@Nullable
@CheckReturnValue
public byte[] getBinaryResource(String uri) {
String ruri = _uriResolver.resolve(uri);
StreamResource sr = new StreamResource(ruri);
try {
try (StreamResource sr = new StreamResource(ruri)) {
sr.connect();
BufferedInputStream bis = sr.bufferedStream();
ByteArrayOutputStream result = new ByteArrayOutputStream(sr.hasStreamLength() ? sr.streamLength() : 4 * 1024);
byte[] buf = new byte[10240];
int i;
while ((i = bis.read(buf)) != -1) {
result.write(buf, 0, i);
}

return result.toByteArray();
return IOUtil.readBytes(sr.bufferedStream());
} catch (IOException e) {
return null;
} finally {
sr.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.xhtmlrenderer.resource.ImageResource;
import org.xhtmlrenderer.resource.XMLResource;
import org.xhtmlrenderer.util.FontUtil;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.ImageUtil;
import org.xhtmlrenderer.util.XRLog;

Expand All @@ -34,7 +35,6 @@
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -256,11 +256,7 @@ public ImageResource getImageResource(String uri) {
} catch (IOException e) {
XRLog.exception("Can't read image file; unexpected problem for URI '" + uri + "'", e);
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
IOUtil.close(is);
}
}
}
Expand Down Expand Up @@ -293,49 +289,21 @@ protected ImageResource createImageResource(String uri, @Nullable Image img) {
*/
@Override
public XMLResource getXMLResource(String uri) {
InputStream inputStream = resolveAndOpenStream(uri);
XMLResource xmlResource;
try {
xmlResource = XMLResource.load(inputStream);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// swallow
}
}
try (InputStream inputStream = resolveAndOpenStream(uri)) {
return XMLResource.load(inputStream);
} catch (IOException ignore) {
return null;
}
return xmlResource;
}

@Override
@Nullable
@CheckReturnValue
public byte[] getBinaryResource(String uri) {
InputStream is = resolveAndOpenStream(uri);
if (is==null) return null;
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buf = new byte[10240];
int i;
while ((i = is.read(buf)) != -1) {
result.write(buf, 0, i);
}
is.close();
is = null;

return result.toByteArray();
try (InputStream is = resolveAndOpenStream(uri)) {
return is == null ? null : IOUtil.readBytes(is);
} catch (IOException e) {
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public String resolveUri(final String uri) {
} catch (MalformedURLException e) {
try {
setBaseUri(new File(".").toURI().toURL().toExternalForm());
} catch (Exception e1) {
XRLog.exception("The default NaiveUserAgent doesn't know how to resolve the base URL for " + uri);
return null;
} catch (MalformedURLException e1) {
throw new IllegalStateException("The default NaiveUserAgent doesn't know how to resolve the base URL for " + uri, e1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,18 @@ private void loadOverrideProperties(String uri) {
return;
}
} else {
InputStream in = null;
try {
URL url = new URL(uri);
in = new BufferedInputStream(url.openStream());
info("Found config override URI " + uri);
temp.load(in);
try (InputStream in = new BufferedInputStream(url.openStream())) {
info("Found config override URI " + uri);
temp.load(in);
}
} catch (MalformedURLException e) {
warning("URI for override properties is malformed, skipping: " + uri);
return;
} catch (IOException e) {
warning("Overridden properties could not be loaded from URI: " + uri, e);
return;
} finally {
if (in != null ) try {
in.close();
} catch (IOException e) {
// swallow
}
}
}

Expand Down Expand Up @@ -552,7 +546,7 @@ public static int valueAsInt(String key, int defaultVal) {
*
* @param key Name of the property
* @param defaultVal Default value to return
* @returnValue assigned to the key, as a character
* @return Value assigned to the key, as a character
*/
public static char valueAsChar(String key, char defaultVal) {
String val = valueFor(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void write(BufferedImage image, String filePath) throws IOException {
try (OutputStream fos = new BufferedOutputStream(newOutputStream(file.toPath()))) {
write(image, fos);
}
// ignore
}

/**
Expand Down
Loading

0 comments on commit 1dd13bd

Please sign in to comment.