使用HTML内联外部CSS [英] Inline external CSS with HTML
问题描述
我正在寻找一个java库,它可以内置一个外部 CSS
文件与 HTML
ID /类属性。
I am looking for a java library which can inline an external CSS
file with a HTML
document based on its ID/class attributes.
我找到了 jStyleParser ,但我不知道这是否适合我的图书馆。我似乎不明白,如果它可以做的工作,在HTML中的元素内联 CSS
。
有没有人可以回答这个问题,或者还有其他的库吗?
Is there anyone who can answer that question or does there exist another library for this?
感谢
推荐答案
您可以尝试 CSSBox 。只需查看包中包含的 ComputeStyles 演示(有关运行演示的信息,请参阅发行包中的 doc / examples / README 文件)。它计算所有的样式并创建一个带有相应内联样式定义的新HTML文档(由DOM表示)。
You may try CSSBox. Just take a look at the ComputeStyles demo contained in the package (see the doc/examples/README file in the distribution package for information about running the demo). It computes all the styles and creates a new HTML document (represented by a DOM) with the corresponding inline style definitions.
源代码位于 src / org / fit / cssbox / demo / ComputeStyles.java ,它很短。实际上,它使用jStyleParser做主要工作,CSSBox只是为此提供了更好的界面。
The source is in src/org/fit/cssbox/demo/ComputeStyles.java and it's pretty short. Actually, it uses jStyleParser for doing the main job, CSSBox just provides a nicer interface for this.
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(args[0]);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.getStyleSheets(); //load the author style sheets
//Compute the styles
System.err.println("Computing style...");
da.stylesToDomInherited();
//Save the output
PrintStream os = new PrintStream(new FileOutputStream(args[1]));
Output out = new NormalOutput(doc);
out.dumpTo(os);
os.close();
docSource.close();
这篇关于使用HTML内联外部CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!