在java中寻找CSS解析器
我正在寻找java中的CSS解析器。特别是我的要求是,对于HTML文档中的给定节点/元素,能够从解析器中请求/获取该元素的css样式 我知道有W3C SAC接口和一个或两个基于此的实现——但是教程/示例似乎不存在 非常感谢您的帮助/指出正确的方向在java中寻找CSS解析器,java,css,parsing,Java,Css,Parsing,我正在寻找java中的CSS解析器。特别是我的要求是,对于HTML文档中的给定节点/元素,能够从解析器中请求/获取该元素的css样式 我知道有W3C SAC接口和一个或两个基于此的实现——但是教程/示例似乎不存在 非常感谢您的帮助/指出正确的方向 感谢添加到cssparser.sourcefourge.net 眼镜蛇: 我用过,我喜欢它——它也能很好地反馈错误 以下是我找到并修改的一些示例代码: package com.dlogic; import com.steadystate.css.pa
感谢添加到cssparser.sourcefourge.net 眼镜蛇: 我用过,我喜欢它——它也能很好地反馈错误 以下是我找到并修改的一些示例代码:
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;
}
}
package com.dlogic;
导入com.steadystate.css.parser.CSSOMParser;
导入org.w3c.css.sac.InputSource;
导入org.w3c.dom.css.CSSStyleSheet;
导入org.w3c.dom.css.CSSRuleList;
导入org.w3c.dom.css.CSSRule;
导入org.w3c.dom.css.CSSStyleRule;
导入org.w3c.dom.css.CSSStyleDeclaration;
导入java.io.*;
公共类CSSParserTest
{
受保护的静态CSSParserTest混浊器;
公共静态void main(字符串[]args){
oParser=new CSSParserTest();
if(oParser.Parse(“design.css”)){
System.out.println(“解析完成OK”);
}否则{
System.out.println(“无法解析CSS”);
}
}
公共布尔解析(字符串cssfile)
{
FileOutputStream out=null;
PrintStream ps=null;
布尔rtn=假;
尝试
{
//cssfile作为资源访问,因此必须在pkg中(在src dir中)。
InputStream=oParser.getClass().getResourceAsStream(cssfile);
//覆盖和删除现有文件内容
out=新文件输出流(“log.txt”);
if(out!=null)
{
//日志文件
ps=新打印流(输出);
System.setErr(ps);//也将stderr重定向到日志文件
}否则{
返回rtn;
}
InputSource source=新的InputSource(新的InputStreamReader(stream));
CSSOMParser parser=新的CSSOMParser();
//解析并创建样式表组合
CSSStyleSheet stylesheet=parser.parseStyleSheet(source,null,null);
//DOM中的任何错误都将发送到这里的STDERR!!
//现在遍历dom并检查。
CSSRuleList ruleList=stylesheet.getCssRules();
ps.println(“规则数:+ruleList.getLength());
对于(int i=0;i
在此处查看SAC及其实施:
CSSParser有点过时了我自己的项目需要一个CSS解析器,但我发现“CSSParser”太单调和不灵活,无法使用(但那可能就是我),所以我最终编写了自己的简单但功能强大的CSS解析器 如果愿意,请随意使用:-)
用于在Java中读取和写入CSS2和CSS3文件的CSS库是 它基于JavaCC语法,支持CSS2和CSS3,还允许解析HTML样式属性
- 它支持最常见的不符合规范的黑客“*”、“u”和“$”
- 它支持CSS数学-calc()表达式
- 它支持@page规则
- 它支持CSS3媒体查询
- 它支持@viewport规则
- 它支持@keyframes规则
- 它支持@supports规则-非常新
- 它支持@namespace规则
- 您可以获得不同元素的源位置信息(开始和结束的行号+列号-标记和完整构造的行号和列号)
自2013年5月21日起,JDK1.5版本也将面世,这使得Android开发更加有趣恰恰提供了这一功能。它解析所有引用的样式表,并将它们映射到DOM树节点。如果您在使用CSSParser时遇到困难,因为似乎根本没有任何文档,您可能只想解析CSS字符串,如style参数的值,下面是我的简单使用示例:
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中。所以,如果你想找一些简单且直接可用的东西,CSSpaser似乎是个不错的选择
注:它可以从十六进制格式自动转换颜色