Home > Beyond, Java, Programación > Server Side Java Automatic Css Inliner

Server Side Java Automatic Css Inliner

December 27, 2010 Leave a comment Go to comments

Esta clase sirve, como dice el titulo, para pasar los estilos de una pagina html a sus atributos, utilizando la librería JSoup.

Antes:

<html>
<head></head> 
<body> 
<style> 
body{background:#FFC} p{background:red} 
body, p{font-weight:bold} 
</style> 
<p>...</p> 
</body> 
</html> 

Despues:

<html>
<head></head>
<body style=”background:#FFC;font-weight:bold”>
<p style=”background:red;font-weight:bold”>…</p>
</body>
</html>

 

//---------Empieza Clasee
import java.io.IOException;
import java.util.StringTokenizer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class AutomaticCssInliner {

    /**
    * Hecho por Grekz, https://grekz.wordpress.com
    */
    public static void main(String[] args) throws IOException {
        final String style = "style";
        Document doc = Jsoup.connect("http://mypage.com/inlineme.php").get();
        //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(), delims = "{}";
            StringTokenizer st = new StringTokenizer(styleRules, delims);
            while (st.countTokens() > 1) {
                String selector = st.nextToken(), properties = st.nextToken();
                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();
        }
        System.out.println(doc);// now we have the result html without the
        // styles tags, and the inline css in each
        // element
    }

    private static String concatenateProperties(String oldProp, String newProp) {
        oldProp = oldProp.trim();
        if (!oldProp.endsWith(";"))
            oldProp += ";";
        return oldProp + newProp;
    }
}

Categories: Beyond, Java, Programación
  1. Arturo
    February 22, 2011 at 09:56

    Que onda vato ya publica algo nuevo. jajaja

    • February 23, 2011 at 16:33

      hahjahja tu pide, ke okupas?

  2. Simon
    March 31, 2011 at 15:48

    oh no stringTokenizer nooooooo!

    • March 31, 2011 at 19:08

      jajhahja tu trankilo, no pasa nada!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: