2009-10-03 58 views
28

我正在寻找java中的CSS解析器。特别是我的要求是,对于HTML文档中给定的节点/元素,能够从解析器中获取/获取该元素的CSS样式。在java中寻找CSS解析器

我知道有W3C SAC接口和基于此的一个或两个实现 - 但turorials /示例显示不存在。

任何帮助/点在正确的方向非常赞赏。

感谢

+5

看看 - http://cssparser.sourceforge.net/ – adatapost 2009-10-03 12:43:54

+1

@adatapost:重新发布您的回复为“答案”,而不是“意见” – 2009-10-05 04:38:38

回答

4

的除了cssparser.sourcefourge.net,

眼镜蛇:

http://lobobrowser.org/cobra.jsp

+0

十分感谢,我会检查出来 – 2009-10-11 14:21:12

+0

还采用旧CSSParser项目 – 2011-08-11 12:56:23

16

我用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; 

    } 

} 
+0

嗨基因,感谢您的信息。我一直在使用CSSParser,但没有太多的问题 - 除了两个小问题:1)如果存在一个无法识别或格式不正确的规则,解析器似乎在整个文件上都会出现漏洞,而不是放弃这条规则。很明显,用真正的css这个发生了很多。另外,我认为有一条评论错误:多行注释似乎导致所有后续的css被放弃到下一个评论。你有没有注意到这个? – 2009-12-07 10:35:37

+0

嗨理查德,我刚开始使用CSSParser。我还没有注意到这个掉线的评论问题,我正试图找到一个solution来解决目前它消除IE黑客的问题。 你对这个问题有什么经验吗? – 2009-12-07 11:22:02

+0

不是真的 - 虽然可能是我看到一些barfing的原因。不知道你在做什么,我会建议在解析之前对每个css文件进行一些预处理。你可以为IE黑客设置一些正则表达式并将它们去掉? – 2009-12-07 11:36:59

11

我需要一个CSS解析器为自己的项目,但我发现“CSSParser”过于乏味和僵化(但可能只是我),所以我最终编写了我自己的简单但功能丰富的CSS解析器。

随意使用它,如果你想:-)

OSBCP CSS Parser

+0

我很快查看了你的项目,看起来你的解析器功能非常强大。我正在认真考虑在我的一个项目中使用它。我需要了解哪些问题?另外,CSS规则可以在内存中修改,然后再次保存到CSS文件中?谢谢。 – stepanian 2012-03-19 21:57:24

+0

是的。它将规则解析成规则列表,可以修改规则列表,然后使用toString()写回到字符串/文件。我希望能回答你的问题。 – corgrath 2012-04-01 18:48:33

+0

谢谢。我正在使用它,它对我来说非常合适。 – stepanian 2012-04-11 00:32:39

13

一个CSS库,用于读取和写入CSS2和CSS3文件Java是PH-CSShttps://github.com/phax/ph-css 它是基于在JavaCC语法中,同时支持CSS2和CSS3,另外还可以解析HTML样式属性。

  • 它支持最常见的黑客“*”,“_”和“$”未遵循规范的
  • 它支持CSS的数学 - 的计算()表达
  • 它支持@page规则
  • 它支持CSS3媒体查询
  • 它支持@viewport规则
  • 它支持@keyframes规则
  • 它支持@supports规则 - 很新
  • 它支持@namespace规则
  • 你可以得到不同的元素(起始和终止行+列数 - 既为标签以及为完整的结构)源位置信息

由于2013年5月21日JDK 1。5版本也是可用的,这使得它对于Android开发更有趣

+1

我确实使用了它,并且自问题被问到,它确实演变得很好。 Nice API,访问者只需获取所需的元素,只是缺少一些文档(但API文档相当不错)。推荐的。 – Martin 2013-04-22 12:18:04

+0

这个好项目的文档很差,这太浪费了。通过一点工作,这可能会变成一个更受欢迎的项目。 – Gili 2014-03-08 06:05:22

+0

这看起来像是我最好的一个 – 2016-06-21 23:53:56

1

jStyleParser正好提供了这个功能。它解析所有引用的样式表并将它们映射到DOM树节点。

1

如果用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()格式,但不提供单位大小的帮助,它将它们视为值列表!不太好。

1

我刚推出了我自己的CSS Stream Parser for Java,可在github上找到。是什么使这个解析器除了包括:

  • 它是一种流解析器,所以解析器处理器将接收后立即每个项目已经被解析为所有当前记录在规则
  • 全面支持所有新内容的通知
  • 自定义类TokenSequence和处理选择Token简化流程等
  • 易于使用和理解
  • 有用的验证或更高级的应用
  • 可伸缩:设计用于处理对CSS定义的更改。