How can I convert an external CSS style sheet into the 'style' section of my HTML using Java?
I want to be able to have my external CSS exist in the 'style' section so that I can parse it to inline styles rather than external and the easiest way I have seen to do this is to have the CSS directly in the HTML rather than linked.
I would like to change this in my HTML:
<link rel="stylesheet" href="smartdoc.css" />
To this (Ideally by passing the CSS file as an argument for a method):
<style>
Content from external CSS.
</style>
So that I can apply JSoup to convert it to inline style using something like this:
public static String inlineCss(String html) {
final String style = "style";
Document doc = Jsoup.parse(html);
Elements els = doc.select(style);// to get all the style elements
for (Element e : els) {
String styleRules = e.getAllElements().get(0).data().replaceAll("\n", "").trim();
String delims = "{}";
StringTokenizer st = new StringTokenizer(styleRules, delims);
while (st.countTokens() > 1) {
String selector = st.nextToken(), properties = st.nextToken();
if (!selector.contains(":")) { // skip a:hover rules, etc.
Elements selectedElements = doc.select(selector);
for (Element selElem : selectedElements) {
String oldProperties = selElem.attr(style);
selElem.attr(style,
oldProperties.length() > 0 ? concatenateProperties(
oldProperties, properties) : properties);
}
}
}
e.remove();
}
return doc.toString();
}
private static String concatenateProperties(String oldProp, @NotNull String newProp) {
oldProp = oldProp.trim();
if (!oldProp.endsWith(";"))
oldProp += ";";
return oldProp + newProp.replaceAll("\\s{2,}", " ");
}
Any suggestions for this?