Skip to content

Commit

Permalink
[refactor] make field StylesheetInfo.type final
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 11, 2024
1 parent 055e991 commit 8db01ce
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public PageInfo getPageStyle(String pageName, String pseudoPage) {
*/
public void flushStyleSheets() {
String uri = _uac.getBaseURL();
StylesheetInfo info = new StylesheetInfo(AUTHOR);
StylesheetInfo info = new StylesheetInfo(AUTHOR, null);
info.setUri(uri);
if (_stylesheetFactory.containsStylesheet(uri)) {
_stylesheetFactory.removeCachedStylesheet(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ private void import_rule(Stylesheet stylesheet) throws IOException {
try {
Token t = next();
if (t == Token.TK_IMPORT_SYM) {
StylesheetInfo info = new StylesheetInfo(stylesheet.getOrigin());
info.setType("text/css");
StylesheetInfo info = new StylesheetInfo(stylesheet.getOrigin(), "text/css");

skip_whitespace();
t = next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.xhtmlrenderer.css.sheet;

import org.jspecify.annotations.Nullable;

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

Expand All @@ -39,7 +41,8 @@ public class StylesheetInfo {
private String title;
private String uri;
private final Origin origin;
private String type;
@Nullable
private final String type;
private List<String> mediaTypes = new ArrayList<>();
private String content;

Expand All @@ -52,8 +55,9 @@ public enum Origin {
AUTHOR
}

public StylesheetInfo(Origin origin) {
public StylesheetInfo(Origin origin, @Nullable String type) {
this.origin = origin;
this.type = type;
}

/**
Expand Down Expand Up @@ -97,15 +101,6 @@ public void addMedium(String medium) {
mediaTypes.add(medium);
}

/**
* Sets the type attribute of the StylesheetInfo object
*
* @param type The new type value
*/
public void setType( String type ) {
this.type = type;
}

/**
* Sets the title attribute of the StylesheetInfo object
*
Expand Down Expand Up @@ -151,11 +146,7 @@ public Origin getOrigin() {
return origin;
}

/**
* Gets the type attribute of the StylesheetInfo object
*
* @return The type value
*/
@Nullable
public String getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -179,8 +180,10 @@ public List<StylesheetInfo> getStylesheets(Document doc) {
Node node = nl.item(i);
if (node.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE) continue;
ProcessingInstruction piNode = (ProcessingInstruction) node;
if (!piNode.getTarget().equals("xml-stylesheet")) continue;
StylesheetInfo info = new StylesheetInfo(AUTHOR);
if (!piNode.getTarget().equals("xml-stylesheet")) {
continue;
}

String pi = piNode.getData();
Matcher m = _alternatePattern.matcher(pi);
if (m.matches()) {
Expand All @@ -189,14 +192,12 @@ public List<StylesheetInfo> getStylesheets(Document doc) {
//TODO: handle alternate stylesheets
if (alternate.equals("yes")) continue;//DON'T get alternate stylesheets for now
}
m = _typePattern.matcher(pi);
if (m.find()) {
int start = m.end();
String type = pi.substring(start + 1, pi.indexOf(pi.charAt(start), start + 1));
//TODO: handle other stylesheet types
if (!type.equals("text/css")) continue;//for now
info.setType(type);
}
String type = detectType(pi);
//TODO: handle other stylesheet types
if (!Objects.equals(type, "text/css")) continue; // for now

StylesheetInfo info = new StylesheetInfo(AUTHOR, type);

m = _hrefPattern.matcher(pi);
if (m.find()) {
int start = m.end();
Expand All @@ -223,6 +224,17 @@ public List<StylesheetInfo> getStylesheets(Document doc) {
return list;
}

@Nullable
@CheckReturnValue
private String detectType(String pi) {
Matcher m = _typePattern.matcher(pi);
if (m.find()) {
int start = m.end();
return pi.substring(start + 1, pi.indexOf(pi.charAt(start), start + 1));
}
return null;
}

@Override
@Nullable
public StylesheetInfo getDefaultStylesheet(StylesheetFactory factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,8 @@ protected StylesheetInfo readStyleElement(Element style) {
if (media.isEmpty()) {
media = "all";
}//default for HTML is "screen", but that is silly and firefox seems to assume "all"
StylesheetInfo info = new StylesheetInfo(AUTHOR);
StylesheetInfo info = new StylesheetInfo(AUTHOR, style.getAttribute("type"));
info.setMedia(media);
info.setType(style.getAttribute("type"));
info.setTitle(style.getAttribute("title"));

StringBuilder buf = new StringBuilder();
Expand Down Expand Up @@ -290,17 +289,9 @@ protected StylesheetInfo readLinkElement(Element link) {
return null;
}

String type = link.getAttribute("type");
if (!(type.isEmpty() || type.equals("text/css"))) {
return null;
}
String type = detectType(link);
StylesheetInfo info = new StylesheetInfo(AUTHOR, type);

StylesheetInfo info = new StylesheetInfo(AUTHOR);

if (type.isEmpty()) {
type = "text/css";
} // HACK is not entirely correct because default may be set by META tag or HTTP headers
info.setType(type);
info.setUri(link.getAttribute("href"));
String media = link.getAttribute("media");
if (media.isEmpty()) {
Expand All @@ -314,6 +305,17 @@ protected StylesheetInfo readLinkElement(Element link) {
return info;
}

@Nullable
@CheckReturnValue
private String detectType(Element link) {
String type = link.getAttribute("type");
return switch (type) {
case "text/css" -> type;
case "" -> "text/css"; // HACK is not entirely correct because default may be set by META tag or HTTP headers
default -> null;
};
}

/**
* Gets the stylesheetLinks attribute of the XhtmlNamespaceHandler object
*/
Expand Down Expand Up @@ -370,10 +372,9 @@ public StylesheetInfo getDefaultStylesheet(StylesheetFactory factory) {
return null;
}

StylesheetInfo info = new StylesheetInfo(USER_AGENT);
StylesheetInfo info = new StylesheetInfo(USER_AGENT, "text/css");
info.setUri(getNamespace());
info.setMedia("all");
info.setType("text/css");

try (InputStream is = getDefaultStylesheetStream()) {
if (_defaultStylesheetError) {
Expand Down

0 comments on commit 8db01ce

Please sign in to comment.