您可以尝试CSSBox。只需查看软件包中包含的 ComputeStyles 演示(有关运行该演示的信息,请参阅分发软件包中的 doc / examples / README 文件)。它计算所有样式,并使用相应的内联样式定义创建一个新的HTML文档(以DOM表示)。
来源在 src / org / fit / css@R_295_2419@ / demo / ComputeStyles.java中 ,非常简短。实际上,它使用jStyleParser来完成主要工作,CSS@R_295_2419@为此提供了一个更好的接口。
//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();