Reading the content of a css file using Java

I am trying to read a css file, find out the css classes and their definition and then save it in a csv file with its class name and description. Using Java, the following I have css file, common.css.

/* CSS Document */

.Page
{
    background-color: #F4EEE0;
    background-image: none;
    margin: 0px 0px 0px 0px;
    scrollbar-face-color: #DEAC64; 
    scrollbar-highlight-color: #FFFFFF; 
    scrollbar-shadow-color: #805822; 
    scrollbar-3dlight-color: #B47F36; 
    scrollbar-arrow-color: #805822; 
    scrollbar-darkshadow-color: #7188AA; 
    scrollbar-base-color: #F4EEE0; 
    scrollbar-track-color: #E8C490; 

}
a.PageLinkTrail 
{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    font-weight: bold;
    color: #805822;
    text-decoration:none;
}

a.PageLinkTrail:hover
{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    font-weight: bold;
    color: #805822;
    text-decoration:underline;
}
.IconSpacing a:hover
{
    padding: 3px 3px 3px 3px;
    text-align:center;
    width:15px;
    height:15px;
    border-top: 1px solid #FFFFFF;
    border-right: 1px solid #C99349;
    border-bottom: 1px solid #C99349;
    border-left: 1px solid #FFFFFF;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    font-style: normal;
    font-weight: normal;
    color: #333333;
    text-decoration:normal;
    vertical-align:Top;
    white-space:nowrap;
    cursor:hand;
}

I want class name

.Page
a.PageLinkTrail 
a.PageLinkTrail:hover

Here is its definition.

{
    background-color: #F4EEE0;
    background-image: none;
    margin: 0px 0px 0px 0px;
    scrollbar-face-color: #DEAC64; 
    scrollbar-highlight-color: #FFFFFF; 
    scrollbar-shadow-color: #805822; 
    scrollbar-3dlight-color: #B47F36; 
    scrollbar-arrow-color: #805822; 
    scrollbar-darkshadow-color: #7188AA; 
    scrollbar-base-color: #F4EEE0; 
    scrollbar-track-color: #E8C490; 

}
{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    font-weight: bold;
    color: #805822;
    text-decoration:none;
}

{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-style: normal;
    font-weight: bold;
    color: #805822;
    text-decoration:underline;
}

I want to save it on a csv file. How should I use Java to get the CSS content such as name and definition? This is the part of the solution I am having most trouble completing at the moment. I have written a flowing code

package com.tufan.digite.Count;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 public class GetAllCssFiles {
public static void main(String args[]) throws IOException {
    try {       
        FileInputStream fstream = new FileInputStream("D:/digite/work/digite/WEBUI/common/theme1/common.css");

        DataInputStream dis = new DataInputStream(fstream);
        FileChannel fc = fstream.getChannel();
        ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,(int) fc.size());
        Charset cs = Charset.forName("8859_1");
        CharsetDecoder cd = cs.newDecoder();
        CharBuffer cb = cd.decode(bb);          
        String strLine;                     
        String content = ".MainNav a:hover{ float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; }";
        Pattern p = Pattern.compile("([a-zA-Z_0-9 | -|:|;|nt]*)({[nt]*[a-zA-Z_0-9 | -|:|;|nt]*})");
        Matcher matcher = p.matcher(cb);
        while (matcher.find()) {
            String selector = matcher.group(1);
            String definition = matcher.group(2);
            System.out.println("selector:" + selector + "Definition"
                    + definition);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } }}

it will not give me group values. if i pass content to Matcher it will give me ans ".MainNav a:hover" in group one and { float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; } as definition as group2

but that content is hardcode. i am trying on cb cb contents whole css file content.


找到第一个开口大括号的位置,找到第一个大括号的位置,从开始到开始的大括号获取文本,从开始大括号到关闭大括号获取文本,在关闭大括号之后用文本重复,直到没有剩下文本。

链接地址: http://www.djcxy.com/p/87566.html

上一篇: Print.css打印表格给其他行上的行

下一篇: 使用Java读取一个css文件的内容