2012-11-09 72 views
7

我在寻找一个java库,它可以根据ID/class属性将内部CSS文件与HTML文档内联。我发现了jStyleParser,但我不确定这是否适合我。我似乎无法理解它是否可以完成内联元素CSS的工作。文档和示例不是我所期望的。HTML内嵌外部CSS

有没有人可以回答这个问题,或者是否存在另一个库?

谢谢

回答

9

您可以尝试CSSBox。只需查看包中包含的ComputeStyles演示(有关运行演示的信息,请参阅发行包中的doc/examples/README文件)。它计算所有样式,并用相应的内联样式定义创建一个新的HTML文档(由DOM表示)。

来源是src/org/fit/cssbox/demo/ComputeStyles.java它很短。实际上,它使用jStyleParser来完成主要工作,CSSBox只是为此提供了一个更好的接口。

 //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(); 
+0

Ack!它在AppEngine中不起作用!诅咒你的谷歌! – Chloe

+0

@radkovo如果源是String input =“ ...”(而不是URL),这个示例如何修改?很好的工作顺便! – athspk

+0

@athspk您将不得不编写自己的DocumentSource实现,该实现从字符串而不是URL创建输入流。这应该很简单,只需看看最初的DefaultDocumentSource实现。 [Here](http://stackoverflow.com/questions/247161/how-do-i-turn-a-string-into-a-stream-in-java)你可能会发现如何从一个字符串创建一个输入流。 – radkovo

4

我一直很高兴与JSoup(v1.5.2)。我有这样的方法:

public static String inlineCss(String html) { 
    final String style = "style"; 
    Document doc = Jsoup.parse(html); 
    Elements els = doc.select(style);// to get all the style elements 
    for (Element e : els) { 
     String styleRules = e.getAllElements().get(0).data().replaceAll("\n", "").trim(); 
     String delims = "{}"; 
     StringTokenizer st = new StringTokenizer(styleRules, delims); 
     while (st.countTokens() > 1) { 
     String selector = st.nextToken(), properties = st.nextToken(); 
     if (!selector.contains(":")) { // skip a:hover rules, etc. 
      Elements selectedElements = doc.select(selector); 
      for (Element selElem : selectedElements) { 
      String oldProperties = selElem.attr(style); 
      selElem.attr(style, 
       oldProperties.length() > 0 ? concatenateProperties(
        oldProperties, properties) : properties); 
      } 
     } 
     } 
     e.remove(); 
    } 
    return doc.toString(); 
    } 

    private static String concatenateProperties(String oldProp, @NotNull String newProp) { 
    oldProp = oldProp.trim(); 
    if (!oldProp.endsWith(";")) 
     oldProp += ";"; 
    return oldProp + newProp.replaceAll("\\s{2,}", " "); 
    } 
+0

我认为你缺少'concatenateProperties'功能:) – zozelfelfo

+0

感谢@zozelfelfo - 我说从山魈缺少的功能 –

+0

切换到亚马逊SES - 你救了我的一天:) 山魈这是否内联本身 – jang00