java解析css⽂件⼯具类CssParserUtil.java CSS Parser 是⼀个⽤Java编写的⽤来解析 CSS 的解析库
项⽬源码路径:svn://de.sf/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.HttpURLConnection;
import java.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(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 = CssRules();
Length() == 0 ){
return false;
}
for (int i = 0; i < Length(); 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();
<("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 = CssRules();
Length() == 0 ){
return false;
}
for (int i = 0; i < Length(); 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();
<("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 = CssRules();
Length() == 0 ){
return false;
}
for (int i = 0; i < Length(); 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();
<("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 = CssRules();
Length() == 0 ){
return null;
}
sheet.insertRule(rule, Length());
} catch (Exception e) {
/
/ TODO: handle exception
e.printStackTrace();
<("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 = CssRules();
Length() == 0 ){
return null;
}
for (int i = 0; i < Length(); 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();
<("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 = CssRules();
Length() == 0 ){
return null;
}
for (int i = 0; i < Length(); i++) {
error parse newfinal 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();
<("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 = CssRules();
Length() == 0 ){
return null;
}
for (int i = 0; i < Length(); i++) {
final CSSRule rule = rules.item(i);
//获取选择器名称
String selectorText_ = ((CSSStyleRule) rule).getSelectorText();
//获取样式内容
String cssText = ((CSSStyleRule)rule).getCssText();
if(selectorText.equals(selectorText_)){

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。