Skip to content

Commit

Permalink
[error-prone] more @nullable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 23, 2024
1 parent 69c38b6 commit 788befb
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
package org.xhtmlrenderer.context;

import org.w3c.dom.css.CSSPrimitiveValue;
import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.extend.ContentFunction;
import org.xhtmlrenderer.css.parser.FSFunction;
Expand All @@ -35,6 +36,10 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.w3c.dom.css.CSSPrimitiveValue.CSS_IDENT;
import static org.w3c.dom.css.CSSPrimitiveValue.CSS_STRING;

public class ContentFunctionFactory {
private final List<ContentFunction> _functions = new ArrayList<>();
Expand All @@ -46,6 +51,7 @@ public ContentFunctionFactory() {
_functions.add(new LeaderFunction());
}

@Nullable
public ContentFunction lookupFunction(LayoutContext c, FSFunction function) {
for (ContentFunction f : _functions) {
if (f.canHandle(c, function)) {
Expand All @@ -65,6 +71,7 @@ public boolean isStatic() {
return false;
}

@Nullable
@Override
public String calculate(LayoutContext c, FSFunction function) {
return null;
Expand Down Expand Up @@ -95,14 +102,14 @@ protected boolean isCounter(FSFunction function, String counterName) {
List<PropertyValue> parameters = function.getParameters();
if (parameters.size() == 1 || parameters.size() == 2) {
PropertyValue param = parameters.get(0);
if (param.getPrimitiveType() != CSSPrimitiveValue.CSS_IDENT ||
! param.getStringValue().equals(counterName)) {
if (param.getPrimitiveType() != CSS_IDENT ||
!Objects.equals(param.getStringValue(), counterName)) {
return false;
}

if (parameters.size() == 2) {
param = parameters.get(1);
return param.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT;
return param.getPrimitiveType() == CSS_IDENT;
}

return true;
Expand Down Expand Up @@ -163,6 +170,7 @@ public String calculate(RenderingContext c, FSFunction function, InlineText text
return "";
}

@Nullable
@Override
public String calculate(LayoutContext c, FSFunction function) {
return null;
Expand All @@ -181,14 +189,14 @@ public boolean canHandle(LayoutContext c, FSFunction function) {
FSFunction f = parameters.get(0).getFunction();
if (f == null ||
f.getParameters().size() != 1 ||
f.getParameters().get(0).getPrimitiveType() != CSSPrimitiveValue.CSS_IDENT ||
! f.getParameters().get(0).getStringValue().equals("href")) {
f.getParameters().get(0).getPrimitiveType() != CSS_IDENT ||
! "href".equals(f.getParameters().get(0).getStringValue())) {
return false;
}

PropertyValue param = parameters.get(1);
return param.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT &&
param.getStringValue().equals("page");
return param.getPrimitiveType() == CSS_IDENT &&
Objects.equals(param.getStringValue(), "page");
}
}

Expand Down Expand Up @@ -253,10 +261,12 @@ public String calculate(RenderingContext c, FSFunction function, InlineText text
return leaderString;
}

@Nullable
@CheckReturnValue
private String getLeaderValue(FSFunction function) {
final PropertyValue param = function.getParameters().get(0);
final String value = param.getStringValue();
if (param.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
if (param.getPrimitiveType() == CSS_IDENT) {
return switch (value) {
case "dotted" -> ". ";
case "solid" -> "_";
Expand All @@ -267,27 +277,30 @@ private String getLeaderValue(FSFunction function) {
return value;
}

@Nullable
@Override
public String calculate(LayoutContext c, FSFunction function) {
return null;
}

@Override
@CheckReturnValue
public String getLayoutReplacementText() {
return " . ";
}

@Override
@CheckReturnValue
public boolean canHandle(LayoutContext c, FSFunction function) {
if (c.isPrint() && function.is("leader")) {
List<PropertyValue> parameters = function.getParameters();
if (parameters.size() == 1) {
PropertyValue param = parameters.get(0);
return param.getPrimitiveType() == CSSPrimitiveValue.CSS_STRING ||
(param.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT &&
(param.getStringValue().equals("dotted") ||
param.getStringValue().equals("solid") ||
param.getStringValue().equals("space")));
return param.getPrimitiveType() == CSS_STRING ||
(param.getPrimitiveType() == CSS_IDENT &&
("dotted".equals(param.getStringValue()) ||
"solid".equals(param.getStringValue()) ||
"space".equals(param.getStringValue())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,10 @@ public void setSiblingSelector(Selector selector) {
public void setNamespaceURI(String namespaceURI) {
_namespaceURI = namespaceURI;
}

@Override
public String toString() {
return "%s{%s}".formatted(getClass().getSimpleName(), _name);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public CSSParser(CSSErrorHandler errorHandler) {
}

@CheckReturnValue
public Stylesheet parseStylesheet(String uri, Origin origin, Reader reader) throws IOException {
public Stylesheet parseStylesheet(@Nullable String uri, Origin origin, Reader reader) throws IOException {
_uri = uri;
reset(reader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ public Origin getOrigin() {
return _origin;
}

@Override
public String toString() {
return "%s{%s %s %s}".formatted(getClass().getSimpleName(), _origin, _props, _fsSelectors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void paintBackground(
void fillOval(int x, int y, int width, int height);

void clip(Shape s);
@Nullable
Shape getClip();
void setClip(Shape s);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ public class LayoutContext implements CssContext {

private final Map<CalculatedStyle, CounterContext> _counterContextMap = new HashMap<>();

@Nullable
private String _pendingPageName;
@Nullable
private String _pageName;

private int _noPageBreak;

@Nullable
private Layer _rootDocumentLayer;
@Nullable
private PageBox _page;

private boolean _mayCheckKeepTogether = true;

@Nullable
private BreakAtLineContext _breakAtLineContext;

public TextRenderer getTextRenderer() {
Expand Down Expand Up @@ -510,14 +515,18 @@ public boolean isPageBreaksAllowed() {
return _noPageBreak == 0;
}

@Nullable
@CheckReturnValue
public String getPendingPageName() {
return _pendingPageName;
}

public void setPendingPageName(String pendingPageName) {
public void setPendingPageName(@Nullable String pendingPageName) {
_pendingPageName = pendingPageName;
}

@Nullable
@CheckReturnValue
public Layer getRootDocumentLayer() {
return _rootDocumentLayer;
}
Expand All @@ -542,11 +551,13 @@ public void setMayCheckKeepTogether(boolean mayKeepTogether) {
_mayCheckKeepTogether = mayKeepTogether;
}

@Nullable
@CheckReturnValue
public BreakAtLineContext getBreakAtLineContext() {
return _breakAtLineContext;
}

public void setBreakAtLineContext(BreakAtLineContext breakAtLineContext) {
public void setBreakAtLineContext(@Nullable BreakAtLineContext breakAtLineContext) {
_breakAtLineContext = breakAtLineContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.layout;

import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.render.MarkerData;

import java.util.LinkedList;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void setBFCs(LinkedList<BlockFormattingContext> s) {
_BFCs = s;
}

@Nullable
public MarkerData getCurrentMarkerData() {
return _currentMarkerData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ public ChildContextData(Box root, VerticalAlignContext vaContext) {
_verticalAlignContext = vaContext;
}

public Box getRoot() {
return _root;
}

public VerticalAlignContext getVerticalAlignContext() {
return _verticalAlignContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
// to the KHTML developers for making such an amazing piece of software!
public class TableBox extends BlockBox {
private final List<ColumnData> _columns = new ArrayList<>();
private int[] _columnPos;
private int @Nullable [] _columnPos;
@Nullable
private TableLayout _tableLayout;

@Nullable
private List<TableColumn> _styleColumns;

private int _pageClearance;
Expand Down Expand Up @@ -1182,7 +1184,9 @@ public void layout(LayoutContext c) {

private static class AutoTableLayout implements TableLayout {
private final TableBox _table;
@Nullable
private Layout[] _layoutStruct;
@Nullable
private List<TableCellBox> _spanCells;

private AutoTableLayout(TableBox table) {
Expand All @@ -1195,6 +1199,7 @@ public void reset() {
_spanCells = null;
}

@Nullable
protected Layout[] getLayoutStruct() {
return _layoutStruct;
}
Expand Down Expand Up @@ -1515,10 +1520,6 @@ private long calcEffectiveWidth(LayoutContext c) {
return tMaxWidth;
}

private boolean shouldScaleColumns(TableBox table) {
return true;
}

@Override
public void calcMinMaxWidth(LayoutContext c) {
TableBox table = _table;
Expand Down Expand Up @@ -1547,12 +1548,9 @@ public void calcMinMaxWidth(LayoutContext c) {
}
}

if (shouldScaleColumns(table)) {
maxNonPercent = (maxNonPercent * 100 + 50) / Math.max(remainingPercent, 1);
maxWidth = Math.max(maxNonPercent, maxWidth);
maxWidth = Math.max(maxWidth, maxPercent);
}

maxNonPercent = (maxNonPercent * 100 + 50) / Math.max(remainingPercent, 1);
maxWidth = Math.max(maxNonPercent, maxWidth);
maxWidth = Math.max(maxWidth, maxPercent);
maxWidth = Math.max(maxWidth, spanMaxWidth);

int bs = table.marginsBordersPaddingAndSpacing(c, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static Path2D generateBorderShape(Rectangle bounds, int side, BorderPrope
* @param side what side you want
* @param border border props
* @param drawInterior if you want it to be 2d or not, if false it will be just a line
* @param scaledOffset insets the border by multipling border widths by this variable, best use would be 1 or .5, cant see it for much other than that
* @param scaledOffset insets the border by multiplying border widths by this variable, best use would be 1 or .5, cant see it for much other than that
* @return a path for the side chosen!
*/
public static Path2D generateBorderShape(Rectangle bounds, int side, BorderPropertySet border, boolean drawInterior, float scaledOffset) {
Expand All @@ -85,7 +85,7 @@ public static Path2D generateBorderShape(Rectangle bounds, int side, BorderPrope
* @param side what side you want
* @param border border props
* @param drawInterior if you want it to be 2d or not, if false it will be just a line
* @param scaledOffset insets the border by multipling border widths by this variable, best use would be 1 or .5, cant see it for much other than that
* @param scaledOffset insets the border by multiplying border widths by this variable, best use would be 1 or .5, cant see it for much other than that
* @param widthScale scales the border widths by this factor, useful for drawing half borders for border types like groove or double
* @return a path for the side chosen!
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public void setClip(Shape s) {
_graphics.setClip(s);
}

@Nullable
@CheckReturnValue
@Override
public Shape getClip() {
return _graphics.getClip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ public void clip(Shape s) {
}
}

@Nullable
@CheckReturnValue
@Override
public Shape getClip() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public void setClip(Shape s) {
_clippingArea = (s == null ? null : new Area(s));
}

@Nullable
@CheckReturnValue
@Override
public Shape getClip() {
return _clippingArea;
Expand Down

0 comments on commit 788befb

Please sign in to comment.