最近为了满足功能研发,动态设置部件属性信息,需要解析前端界面的CSS样式表,但是在网上查了查,目前比较合适的是cssparser,但是不满足获取样式的某个属性,这是我写了个工具类,通过正则表达式和cssparser解析样式文件,获取样式类信息,也可获取某个样式类下的某个属性信息,代码如下,需要引入两个jar包(cssparser-0.9.18.jar和sac.jar,在附件中可下载)
package com.demo; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.w3c.css.sac.InputSource; import org.w3c.dom.css.CSSRule; import org.w3c.dom.css.CSSRuleList; import org.w3c.dom.css.CSSStyleSheet; import com.steadystate.css.parser.CSSOMParser; import com.steadystate.css.parser.SACParserCSS3; public class CSSUtil { private CSSStyleSheet sheet = null; public CSSStyleSheet getSheet() { return sheet; } public void setSheet(CSSStyleSheet sheet) { this.sheet = sheet; } /** * 指定文件流 * @param stream */ public CSSUtil(InputStream stream){ InputSource source = new InputSource(new InputStreamReader(stream)); CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); try { sheet = parser.parseStyleSheet(source, null, null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取样式信息 * @param className 样式名 * @return 返回该样式的信息 */ public String getClass(String className){ if(sheet == null){ return null; } String res = ""; CSSRuleList rules = sheet.getCssRules(); for (int i = 0; i < rules.getLength(); i++) { CSSRule rule = rules.item(i); Pattern pattern = Pattern.compile("\\."+className + ".*([^\\{]\\{.*[^\\}]\\})"); Matcher matcher = pattern.matcher(rule.getCssText()); while(matcher.find()){ res = res + matcher.group(1) + "\n"; } } return res; } /** * 获取某个样式类下的某个属性信息,没有返回空 * @param className 样式类名称 * @param attrName 属性名称 * @return */ public String getCssAttribute(String className, String attrName){ String classContent = this.getClass(className); Pattern pattern = Pattern.compile("[^a-zA-Z\\-]" + attrName + ":\\s*([^;\\}]{1,100})\\s*[;\\}]{1}"); Matcher matcher = pattern.matcher(classContent); String res = ""; while(matcher.find()){ res = matcher.group(1); } return res; } }