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

[refactor] convert constants to enum #416

Merged
merged 19 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6a7e0e8
[refactor] convert constants `PropertyValue.VALUE_TYPE_*` to enum
asolntsev Oct 11, 2024
b2f8329
[refactor] convert constants `Selector.*_AXIS` to enum
asolntsev Oct 11, 2024
3ea356e
[refactor] convert constants `Token.*` to enum `Token.Type`
asolntsev Oct 11, 2024
055e991
[refactor] convert constants `StylesheetInfo.*` to enum `StylesheetIn…
asolntsev Oct 11, 2024
8db01ce
[refactor] make field `StylesheetInfo.type` final
asolntsev Oct 11, 2024
ea5050a
[refactor] make field `StylesheetInfo.content` final
asolntsev Oct 11, 2024
a884466
[refactor] make `StylesheetInfo` fields `title` and `mediaTypes` final
asolntsev Oct 11, 2024
19c2b61
[refactor] make all `StylesheetInfo` fields final
asolntsev Oct 11, 2024
c685afa
[refactor] convert constants LEFT/RIGHT/TOP/BOTTOM to enum `Edge`
asolntsev Oct 11, 2024
76b55bd
[refactor] convert constants VARIABLE/FIXED/PERCENT to enum `Length.L…
asolntsev Oct 11, 2024
752eabe
[refactor] convert constants `BoxBuilder.MARGIN_*` to enum `MarginDir…
asolntsev Oct 11, 2024
6f85152
[refactor] convert constants `BlockBox.POSITION_*` to enum `Position`
asolntsev Oct 11, 2024
85bb516
[refactor] convert constants `BlockBox.CONTENT_*` to enum `ContentType`
asolntsev Oct 11, 2024
31c9726
[refactor] convert Box constants NOTHING/FLUX/CHILDREN_FLUX/DONE to e…
asolntsev Oct 11, 2024
5f2adbb
[refactor] convert constants `Box.DUMP_*` to enum `Dump`
asolntsev Oct 11, 2024
f1b27c6
[refactor] convert constants `FontSizeAction_*` to enum `FontSizeChange`
asolntsev Oct 11, 2024
50df962
[refactor] convert class PageElementPosition to enum
asolntsev Oct 11, 2024
6f4747d
[refactor] make few fields in class `PageBox` final
asolntsev Oct 11, 2024
a6c8bb2
[refactor] replace `FSFunction.getName().equals()` by shorter method …
asolntsev Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected IdentValue getListStyleType(FSFunction function) {
}

protected boolean isCounter(FSFunction function, String counterName) {
if (function.getName().equals("counter")) {
if (function.is("counter")) {
List<PropertyValue> parameters = function.getParameters();
if (parameters.size() == 1 || parameters.size() == 2) {
PropertyValue param = parameters.get(0);
Expand Down Expand Up @@ -175,7 +175,7 @@ public String getLayoutReplacementText() {

@Override
public boolean canHandle(LayoutContext c, FSFunction function) {
if (c.isPrint() && function.getName().equals("target-counter")) {
if (c.isPrint() && function.is("target-counter")) {
List<PropertyValue> parameters = function.getParameters();
if (parameters.size() == 2 || parameters.size() == 3) {
FSFunction f = parameters.get(0).getFunction();
Expand Down Expand Up @@ -279,7 +279,7 @@ public String getLayoutReplacementText() {

@Override
public boolean canHandle(LayoutContext c, FSFunction function) {
if (c.isPrint() && function.getName().equals("leader")) {
if (c.isPrint() && function.is("leader")) {
List<PropertyValue> parameters = function.getParameters();
if (parameters.size() == 1) {
PropertyValue param = parameters.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.xhtmlrenderer.context;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -40,7 +42,6 @@
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.util.XRLog;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -97,11 +98,7 @@ private List<Stylesheet> readAndParseAll(List<StylesheetInfo> infos, String medi
List<Stylesheet> result = new ArrayList<>(infos.size() + 15);
for (StylesheetInfo info : infos) {
if (info.appliesToMedia(medium)) {
Stylesheet sheet = info.getStylesheet();

if (sheet == null) {
sheet = _stylesheetFactory.getStylesheet(info);
}
Stylesheet sheet = _stylesheetFactory.getStylesheet(info);

if (sheet != null) {
if (!sheet.getImportRules().isEmpty()) {
Expand Down Expand Up @@ -170,7 +167,9 @@ public CascadedStyle getCascadedStyle(@Nullable Element e, boolean restyle) {
return _matcher.getCascadedStyle(e, restyle);
}

public PageInfo getPageStyle(String pageName, String pseudoPage) {
@NonNull
@CheckReturnValue
public PageInfo getPageStyle(@Nullable String pageName, String pseudoPage) {
return _matcher.getPageCascadedStyle(pageName, pseudoPage);
}

Expand All @@ -179,15 +178,11 @@ public PageInfo getPageStyle(String pageName, String pseudoPage) {
*/
public void flushStyleSheets() {
String uri = _uac.getBaseURL();
StylesheetInfo info = new StylesheetInfo();
info.setUri(uri);
info.setOrigin(StylesheetInfo.AUTHOR);
if (_stylesheetFactory.containsStylesheet(uri)) {
_stylesheetFactory.removeCachedStylesheet(uri);
XRLog.cssParse("Removing stylesheet '" + uri + "' from cache by request.");
} else {
XRLog.cssParse("Requested removing stylesheet '" + uri + "', but it's not in cache.");

}
}

Expand All @@ -207,35 +202,13 @@ private List<StylesheetInfo> getStylesheets() {
List<StylesheetInfo> infos = new ArrayList<>();
long st = System.currentTimeMillis();

StylesheetInfo defaultStylesheet = _nsh.getDefaultStylesheet(_stylesheetFactory);
if (defaultStylesheet != null) {
infos.add(defaultStylesheet);
}

List<StylesheetInfo> refs = _nsh.getStylesheets(_doc);
int inlineStyleCount = 0;
_nsh.getDefaultStylesheet().ifPresent(defaultStylesheet -> infos.add(defaultStylesheet));

for (StylesheetInfo ref : refs) {
String uri;

if (!ref.isInline()) {
uri = _uac.resolveURI(ref.getUri());
ref.setUri(uri);
} else {
ref.setUri(_uac.getBaseURL() + "#inline_style_" + (++inlineStyleCount));
Stylesheet sheet = _stylesheetFactory.parse(
new StringReader(ref.getContent()), ref);
ref.setStylesheet(sheet);
ref.setUri(null);
}
}
infos.addAll(refs);
infos.addAll(_nsh.getStylesheets(_doc));

// TODO: here we should also get user stylesheet from userAgent

long el = System.currentTimeMillis() - st;
XRLog.load("TIME: parse stylesheets " + el + "ms");

XRLog.load("TIME: parse stylesheets in " + (System.currentTimeMillis() - st) + " ms.");
return infos;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
*/
package org.xhtmlrenderer.context;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.extend.StylesheetFactory;
import org.xhtmlrenderer.css.parser.CSSParser;
import org.xhtmlrenderer.css.sheet.Ruleset;
import org.xhtmlrenderer.css.sheet.Stylesheet;
import org.xhtmlrenderer.css.sheet.StylesheetInfo;
import org.xhtmlrenderer.css.sheet.StylesheetInfo.Origin;
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;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -40,6 +43,7 @@
import java.util.Map;
import java.util.logging.Level;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.synchronizedMap;

/**
Expand Down Expand Up @@ -67,11 +71,15 @@ public StylesheetFactoryImpl(UserAgentCallback userAgentCallback) {
}

public Stylesheet parse(Reader reader, StylesheetInfo info) {
return parse(reader, info.getUri(), info.getOrigin());
}

public Stylesheet parse(Reader reader, String uri, Origin origin) {
try {
return _cssParser.parseStylesheet(info.getUri(), info.getOrigin(), reader);
return _cssParser.parseStylesheet(uri, origin, reader);
} catch (IOException e) {
XRLog.cssParse(Level.WARNING, "Couldn't parse stylesheet at URI " + info.getUri() + ": " + e.getMessage(), e);
return new Stylesheet(info.getUri(), info.getOrigin());
XRLog.cssParse(Level.WARNING, "Couldn't parse stylesheet at URI " + uri + ": " + e.getMessage(), e);
return new Stylesheet(uri, origin);
}
}

Expand All @@ -80,16 +88,19 @@ public Stylesheet parse(Reader reader, StylesheetInfo info) {
*/
@Nullable
private Stylesheet parse(StylesheetInfo info) {
CSSResource cr = _userAgentCallback.getCSSResource(info.getUri());
if (cr==null) return null;
CSSResource cr = info.getContent()
.map(css -> new CSSResource(new ByteArrayInputStream(css.getBytes(UTF_8))))
.orElseGet(() -> _userAgentCallback.getCSSResource(info.getUri()));

// Whether by accident or design, InputStream will never be null
// since the null resource stream is wrapped in a BufferedInputStream
InputSource inputSource=cr.getResourceInputSource();
if (inputSource==null) return null;
InputStream is = inputSource.getByteStream();
if (is==null) return null;
try {
return parse(new InputStreamReader(is, Configuration.valueFor("xr.stylesheets.charset-name", "UTF-8")), info);
String charset = Configuration.valueFor("xr.stylesheets.charset-name", "UTF-8");
return parse(new InputStreamReader(is, charset), info);
} catch (UnsupportedEncodingException e) {
// Shouldn't happen
throw new RuntimeException(e.getMessage(), e);
Expand All @@ -98,7 +109,7 @@ private Stylesheet parse(StylesheetInfo info) {
}
}

public Ruleset parseStyleDeclaration(int origin, String styleDeclaration) {
public Ruleset parseStyleDeclaration(Origin origin, String styleDeclaration) {
return _cssParser.parseDeclaration(origin, styleDeclaration);
}

Expand Down Expand Up @@ -145,6 +156,8 @@ void flushCachedStylesheets() {
* @return The stylesheet
*/
//TODO: this looks a bit odd
@Nullable
@CheckReturnValue
public Stylesheet getStylesheet(StylesheetInfo info) {
XRLog.load("Requesting stylesheet: " + info.getUri());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.xhtmlrenderer.css.constants;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.parser.CSSParser;
import org.xhtmlrenderer.css.parser.PropertyValue;
Expand Down Expand Up @@ -1747,6 +1748,8 @@ public static PropertyBuilder getPropertyBuilder(CSSName cssName) {
/**
* Gets the byPropertyName attribute of the CSSName class
*/
@Nullable
@CheckReturnValue
public static CSSName getByPropertyName(String propName) {
return ALL_PROPERTY_NAMES.get(propName);
}
Expand Down Expand Up @@ -1802,7 +1805,7 @@ private static synchronized CSSName addProperty(
for (CSSName cssName : ALL_PRIMITIVE_PROPERTY_NAMES.values()) {
if (cssName.initialValue.charAt(0) != '=' && cssName.implemented) {
PropertyValue value = parser.parsePropertyValue(
cssName, StylesheetInfo.USER_AGENT, cssName.initialValue);
cssName, StylesheetInfo.Origin.USER_AGENT, cssName.initialValue);

if (value == null) {
XRLog.exception("Unable to derive initial value for " + cssName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,37 @@
*/
package org.xhtmlrenderer.css.constants;

import java.util.HashMap;
import java.util.Map;
import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;

public class PageElementPosition {
private static final Map<String, PageElementPosition> ALL = new HashMap<>();
private static int _maxAssigned;
import java.util.Map;

public final int FS_ID;
public enum PageElementPosition {
START("start"),
FIRST("first"),
LAST("last"),
LAST_EXCEPT("last-except");

private static final Map<String, PageElementPosition> ALL = Map.of(
START._ident, START,
FIRST._ident, FIRST,
LAST._ident, LAST,
LAST_EXCEPT._ident, LAST_EXCEPT
);

@Nullable
@CheckReturnValue
public static PageElementPosition byIdent(String ident) {
return ALL.get(ident);
}

private final String _ident;

public static final PageElementPosition START = addValue("start");
public static final PageElementPosition FIRST = addValue("first");
public static final PageElementPosition LAST = addValue("last");
public static final PageElementPosition LAST_EXCEPT = addValue("last-except");

private PageElementPosition(String ident) {
PageElementPosition(String ident) {
this._ident = ident;
this.FS_ID = _maxAssigned++;
}

private static PageElementPosition addValue(String ident) {
PageElementPosition val = new PageElementPosition(ident);
ALL.put(ident, val);
return val;
}

public String toString() {
return _ident;
}

public static PageElementPosition valueOf(String ident) {
return ALL.get(ident);
}

public int hashCode() {
return FS_ID;
}

public boolean equals(Object o) {
if (!(o instanceof PageElementPosition)) {
return false;
}

return FS_ID == ((PageElementPosition)o).FS_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package org.xhtmlrenderer.css.extend;

import com.google.errorprone.annotations.CheckReturnValue;
import org.xhtmlrenderer.css.sheet.Ruleset;
import org.xhtmlrenderer.css.sheet.Stylesheet;
import org.xhtmlrenderer.css.sheet.StylesheetInfo;
import org.xhtmlrenderer.css.sheet.StylesheetInfo.Origin;

import java.io.Reader;

Expand All @@ -34,8 +36,12 @@
* @author Torbjoern Gannholm
*/
public interface StylesheetFactory {
@CheckReturnValue
Stylesheet parse(Reader reader, StylesheetInfo info);
Ruleset parseStyleDeclaration(int author, String style);

@CheckReturnValue
Stylesheet parse(Reader reader, String uri, Origin origin);
@CheckReturnValue
Ruleset parseStyleDeclaration(Origin origin, String style);
@CheckReturnValue
Stylesheet getStylesheet(StylesheetInfo si);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.parser.PropertyValue;
import org.xhtmlrenderer.css.sheet.PropertyDeclaration;
import org.xhtmlrenderer.css.sheet.StylesheetInfo;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -36,6 +35,7 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.xhtmlrenderer.css.sheet.StylesheetInfo.Origin.USER;


/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public static CascadedStyle createAnonymousStyle(IdentValue display) {
CSSPrimitiveValue val = new PropertyValue(display);

List<PropertyDeclaration> props = singletonList(
new PropertyDeclaration(CSSName.DISPLAY, val, true, StylesheetInfo.USER));
new PropertyDeclaration(CSSName.DISPLAY, val, true, USER));

return new CascadedStyle(props);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public static PropertyDeclaration createLayoutPropertyDeclaration(
CSSName cssName, IdentValue display) {
CSSPrimitiveValue val = new PropertyValue(display);
// Urk... kind of ugly, but we really want this value to be used
return new PropertyDeclaration(cssName, val, true, StylesheetInfo.USER);
return new PropertyDeclaration(cssName, val, true, USER);
}

/**
Expand Down
Loading