我正在寻找java中的CSS解析器。特别是我的要求是,对于HTML文档中给定的节点/元素,能够从解析器中获取/获取该元素的CSS样式。在java中寻找CSS解析器
我知道有W3C SAC接口和基于此的一个或两个实现 - 但turorials /示例显示不存在。
任何帮助/点在正确的方向非常赞赏。
感谢
我正在寻找java中的CSS解析器。特别是我的要求是,对于HTML文档中给定的节点/元素,能够从解析器中获取/获取该元素的CSS样式。在java中寻找CSS解析器
我知道有W3C SAC接口和基于此的一个或两个实现 - 但turorials /示例显示不存在。
任何帮助/点在正确的方向非常赞赏。
感谢
十分感谢,我会检查出来 – 2009-10-11 14:21:12
还采用旧CSSParser项目 – 2011-08-11 12:56:23
我用CSSParser,我喜欢它 - 它给出了错误的反馈良好好。
下面是一些示例代码,我发现和修改:
package com.dlogic;
import com.steadystate.css.parser.CSSOMParser;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSStyleSheet;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSStyleRule;
import org.w3c.dom.css.CSSStyleDeclaration;
import java.io.*;
public class CSSParserTest
{
protected static CSSParserTest oParser;
public static void main(String[] args) {
oParser = new CSSParserTest();
if (oParser.Parse("design.css")) {
System.out.println("Parsing completed OK");
} else {
System.out.println("Unable to parse CSS");
}
}
public boolean Parse(String cssfile)
{
FileOutputStream out = null;
PrintStream ps = null;
boolean rtn = false;
try
{
// cssfile accessed as a resource, so must be in the pkg (in src dir).
InputStream stream = oParser.getClass().getResourceAsStream(cssfile);
// overwrites and existing file contents
out = new FileOutputStream("log.txt");
if (out != null)
{
//log file
ps = new PrintStream(out);
System.setErr(ps); //redirects stderr to the log file as well
} else {
return rtn;
}
InputSource source = new InputSource(new InputStreamReader(stream));
CSSOMParser parser = new CSSOMParser();
// parse and create a stylesheet composition
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
//ANY ERRORS IN THE DOM WILL BE SENT TO STDERR HERE!!
// now iterate through the dom and inspect.
CSSRuleList ruleList = stylesheet.getCssRules();
ps.println("Number of rules: " + ruleList.getLength());
for (int i = 0; i < ruleList.getLength(); i++)
{
CSSRule rule = ruleList.item(i);
if (rule instanceof CSSStyleRule)
{
CSSStyleRule styleRule=(CSSStyleRule)rule;
ps.println("selector:" + i + ": " + styleRule.getSelectorText());
CSSStyleDeclaration styleDeclaration = styleRule.getStyle();
for (int j = 0; j < styleDeclaration.getLength(); j++)
{
String property = styleDeclaration.item(j);
ps.println("property: " + property);
ps.println("value: " + styleDeclaration.getPropertyCSSValue(property).getCssText());
ps.println("priority: " + styleDeclaration.getPropertyPriority(property));
}
}// end of StyleRule instance test
} // end of ruleList loop
if (out != null) out.close();
if (stream != null) stream.close();
rtn = true;
}
catch (IOException ioe)
{
System.err.println ("IO Error: " + ioe);
}
catch (Exception e)
{
System.err.println ("Error: " + e);
}
finally
{
if (ps != null) ps.close();
}
return rtn;
}
}
嗨基因,感谢您的信息。我一直在使用CSSParser,但没有太多的问题 - 除了两个小问题:1)如果存在一个无法识别或格式不正确的规则,解析器似乎在整个文件上都会出现漏洞,而不是放弃这条规则。很明显,用真正的css这个发生了很多。另外,我认为有一条评论错误:多行注释似乎导致所有后续的css被放弃到下一个评论。你有没有注意到这个? – 2009-12-07 10:35:37
嗨理查德,我刚开始使用CSSParser。我还没有注意到这个掉线的评论问题,我正试图找到一个solution来解决目前它消除IE黑客的问题。 你对这个问题有什么经验吗? – 2009-12-07 11:22:02
不是真的 - 虽然可能是我看到一些barfing的原因。不知道你在做什么,我会建议在解析之前对每个css文件进行一些预处理。你可以为IE黑客设置一些正则表达式并将它们去掉? – 2009-12-07 11:36:59
退房SAC及其implementstions这里:http://www.w3.org/Style/CSS/SAC/
CSSParser是一点点过时
我需要一个CSS解析器为自己的项目,但我发现“CSSParser”过于乏味和僵化(但可能只是我),所以我最终编写了我自己的简单但功能丰富的CSS解析器。
随意使用它,如果你想:-)
一个CSS库,用于读取和写入CSS2和CSS3文件Java是PH-CSS从https://github.com/phax/ph-css 它是基于在JavaCC语法中,同时支持CSS2和CSS3,另外还可以解析HTML样式属性。
由于2013年5月21日JDK 1。5版本也是可用的,这使得它对于Android开发更有趣
jStyleParser正好提供了这个功能。它解析所有引用的样式表并将它们映射到DOM树节点。
如果用CSSParser挣扎,因为似乎没有文档的一切,你也许想分析只是一个CSS字符串,如从风格的参数值,这里是我使用的简单示例:
import org.junit.Test;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSValue;
import com.steadystate.css.parser.CSSOMParser;
public class ParseCssTest {
@Test
public void testParseStyleDeclaration() throws IOException {
String cssSample = "margin-top: 0cm; margin-bottom: 0cm; background: #e6e6e6";
CSSOMParser parser = new CSSOMParser();
CSSStyleDeclaration o = parser.parseStyleDeclaration(new InputSource(new StringReader(cssSample)));
assertEquals("margin-top: 0cm; margin-bottom: 0cm; background: rgb(230, 230, 230)", o.toString());
assertEquals("0cm", o.getPropertyCSSValue("margin-bottom").toString());
assertEquals("0cm", o.getPropertyCSSValue("margin-bottom").getCssText());
assertEquals(null, o.getPropertyCSSValue("foo"));
}
@Test
public void testParseARule() throws IOException {
String cssSample = "r1 { margin-top: 0cm; margin-bottom: 0cm; background: #e6e6e6 }";
CSSOMParser parser = new CSSOMParser();
CSSRule o = parser.parseRule(new InputSource(new StringReader(cssSample)));
assertEquals("r1 { margin-top: 0cm; margin-bottom: 0cm; background: rgb(230, 230, 230) }", o.toString());
}
@Test
public void parseStyleDeclarationWithAdvancedTests() throws IOException {
String cssSample = "margin-top: 0 cm; margin-bottom: 0cm; background: #e6e6e6";
CSSOMParser parser = new CSSOMParser();
CSSStyleDeclaration o = parser.parseStyleDeclaration(new InputSource(new StringReader(cssSample)));
assertEquals("margin-top: 0 cm; margin-bottom: 0cm; background: rgb(230, 230, 230)", o.toString());
assertEquals("0cm", o.getPropertyCSSValue("margin-bottom").toString());
assertEquals("0cm", o.getPropertyCSSValue("margin-bottom").getCssText());
assertEquals(CSSValue.CSS_VALUE_LIST, o.getPropertyCSSValue("margin-top").getCssValueType());
assertEquals("0 cm", o.getPropertyCSSValue("margin-top").toString());
assertEquals("0 cm", o.getPropertyCSSValue("margin-top").getCssText());
assertEquals(CSSValue.CSS_VALUE_LIST, o.getPropertyCSSValue("margin-top").getCssValueType());
}
}
CSSParser的巨大优势在于它目前在Maven中。所以,如果你寻找一些相当简单和直接可用的CSSParser似乎是不错的选择。
注意:它会自动将颜色从十六进制格式转换为rgb()格式,但不提供单位大小的帮助,它将它们视为值列表!不太好。
我刚推出了我自己的CSS Stream Parser for Java,可在github上找到。是什么使这个解析器除了包括:
TokenSequence
和处理选择Token
简化流程等
看看 - http://cssparser.sourceforge.net/ – adatapost 2009-10-03 12:43:54
@adatapost:重新发布您的回复为“答案”,而不是“意见” – 2009-10-05 04:38:38