java解析css文件工具类CssParserUtil.java

CSS Parser 是一个用Java编写的用来解析 CSS 的解析库

项目源码路径:svn://svn.code.sf.net/p/cssparser/code/trunk/cssparser

在实际项目开发中,只需如引入cssparser-0.9.20.jar即可

maven依赖:

       <dependency>
            <groupId>net.sourceforge.cssparser</groupId>
            <artifactId>cssparser</artifactId>
            <version>0.9.20</version>
        </dependency>


自己写的工具类:CssParserUtil.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.log4j.Logger;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleRule;
import org.w3c.dom.css.CSSStyleSheet;

import com.steadystate.css.parser.CSSOMParser;
    
public class CssParserUtil {
    private final static Logger log = Logger.getLogger(CssParserUtil.class);
    /**
     * 打印样式文件内容
     * @param filePath 样式本地文件路径
     * @param selectorText  属性名称
     * @return
     */
    public static boolean showCssText(InputStream inStream){
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return false;
            }
            
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取样式名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                System.out.println(cssText);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("showCssText***************************************"+e.getMessage());
            return false;
        }
         return false;
    }
    /**
     * 检查样式属性是否存在
     * @param filePath 样式本地文件路径
     * @param selectorText  属性名称
     * @return
     */
    public static boolean checkSelectorText(InputStream inStream,String selectorText){
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return false;
            }
            
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取样式名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                
                if(selectorText.equals(selectorText_)){
                    return true;
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("checkSelectorText***************************************"+e.getMessage());
            return false;
        }
         return false;
    }
    /**
     * 查询样式文件中选择器名称下面的属性是否存在
     * @param inStream css文件输入流
     * @param selectorText 选择器名称
     * @param property  属性名称
     * @return
     */
    public static boolean checkCssProperty(InputStream inStream,String selectorText,String property){
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return false;
            }
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取选择器名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                
                if(selectorText.equals(selectorText_)){
                    CSSStyleDeclaration ss =  ((CSSStyleRule)rule).getStyle();
                    String propertyValue = ss.getPropertyValue(property);
                    if("".equals(propertyValue) || propertyValue == null){
                        return false;
                    }
                    return true;
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            log.error("checkCssProperty***************************************"+e.getMessage());
            return false;
        }
        return false;
    }
    
    /**
     * 添加新样式
     * @param inStream css文件输入流
     * @param selectorText 选择器名称
     * @param property  属性名称
     * @return
     */
    public static CSSStyleSheet insertRule(InputStream inStream,String rule){
        CSSStyleSheet sheet = null;
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return null;
            }
            sheet.insertRule(rule, rules.getLength());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            log.error("insertRule***************************************"+e.getMessage());
            return null;
        }
        return sheet;
    }
    /**
     * 更新选择器样式
     * @param inStream css文件输入流
     * @param selectorText 选择器名称
     * @param property  属性名称
     * @return
     */
    public static CSSStyleSheet updateRule(InputStream inStream,String selectorText,String ruleText){
        CSSStyleSheet sheet = null;
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return null;
            }
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取选择器名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                if(selectorText.equals(selectorText_)){
                    ((CSSStyleRule)rule).getStyle().setCssText(ruleText);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            log.error("updateRule***************************************"+e.getMessage());
            return null;
        }
        return sheet;
    }
    /**
     * 删除选择器样式
     * @param inStream css文件输入流
     * @param selectorText 选择器名称
     * @param property  属性名称
     * @return
     */
    public static CSSStyleSheet deleteRule(InputStream inStream,String selectorText){
        CSSStyleSheet sheet = null;
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return null;
            }
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取选择器名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                
                if(selectorText.equals(selectorText_)){
                    sheet.deleteRule(i);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            log.error("deleteRule***************************************"+e.getMessage());
            return null;
        }
        return sheet;
    }
    /**
     * 添加或者更新选择器属性
     * @param inStream  文件输入流
     * @param selectorText 选择器名称
     * @param propertyName 添加或者更新的属性名称
     * @param propertyValue 属性值
     * @param priority  优先级   例如 "important",或者空
     * @return
     */
    public static CSSStyleSheet addRuleProperty(InputStream inStream,String selectorText,String propertyName,String propertyValue,String priority){
        CSSStyleSheet sheet = null;
        try {
            InputSource source = new InputSource();
            source.setByteStream(inStream);
            source.setEncoding("UTF-8");
            final CSSOMParser parser = new CSSOMParser();
            sheet = parser.parseStyleSheet(source, null, null);
            CSSRuleList rules = sheet.getCssRules();
            if(rules.getLength() == 0 ){
                return null;
            }
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                //获取选择器名称
                String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
                //获取样式内容
                String cssText = ((CSSStyleRule)rule).getCssText();
                
                if(selectorText.equals(selectorText_)){
                    CSSStyleDeclaration cd =  ((CSSStyleRule)rule).getStyle();
                    cd.setProperty(propertyName, propertyValue, priority);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            log.error("addRuleProperty***************************************"+e.getMessage());
            return null;
        }
        return sheet;
    }
    public static void main(String[] args) throws IOException {
//        URL url = new URL("https://g.alicdn.com/tm/login/3.0.5/login_pc.css");
//        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//      InputStream inStream = connection.getInputStream();
        
        InputStream inStream = new FileInputStream(new File("D://test.css"));
//        CSSStyleSheet sheet = addRuleProperty(inStream,".icon-cut","height","100%","important");
        
        
        CSSStyleSheet sheet = insertRule(inStream,".div{width:100px;height:200px;}");
        
//        CSSStyleSheet sheet = updateRule(inStream,".div","height:100px;");
        
        String cssRule = sheet.getCssRules().toString();
        FileOutputStream out = new FileOutputStream("D://test.css");
        out.write(cssRule.getBytes());
        out.close();
        
//        checkSelectorText(inStream,".table");
//        checkCssProperty(inStream,".table","height");
          
        
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值