当前位置: 移动技术网 > IT编程>开发语言>Java > java清除html转义字符

java清除html转义字符

2019年07月22日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

import java.util.hashmap;
import java.util.map;

import org.apache.commons.lang3.stringutils;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
/**
 * 静态文件处理的一些便捷服务
 * @author kettas
 * 4:27:25 pm
 */
public class htmlservices {
 private static log log=logfactory.getlog(htmlservices.class);

 /**
  * 清除html代码
  * <p>所有包括在'<'与'>'之间的内容全部都会被清除掉,并返回</p>
  * @param args
  * @return string
  */
 public static string clearhtmltostring(string args){
  return clearhtmltostring(args,false);
 }
 /**
  * 清除html代码
  * <p>所有包括在'<'与'>'之间的内容全部都会被清除掉,并返回</p>
  * @param args
  * @param replacenull 是否替换空格等制表符
  * @return string
  */
 public static string clearhtmltostring(string args,boolean replacenull){
  if(stringutils.isempty(args)){
   return "";
  }
  args= args.replaceall("(?is)<(.*?)>","");
  if(replacenull){
   args = args.replaceall("\\s*|\t|\r|\n","");
  }
  return args;
 }
 /**
  * 清除html代码
  * <p>所有包括在'<'与'>'之间的内容全部都会被清除掉,并指定返回的长度</p>
  * @param args
  * @return string
  */
 public static string clearhtmltostring(string args,int maxsize){
  return clearhtmltostring(args, maxsize, "");
 }
 /**
  * 清除html代码
  * <p>所有包括在'<'与'>'之间的内容全部都会被清除掉,并指定返回的长度</p>
  * @param args
  * @return string
  */
 public static string clearhtmltostring(string args,int maxsize,string replace){
  args=clearhtmltostring(args);
  if(maxsize<=0){
   return args;
  }
  if(args.length()<=maxsize){
   return args;
  }
  return args.substring(0,maxsize).concat(replace);
 }
 /**
  * 将字符串截取指定长度
  * @param args
  * @param maxsize
  * @param replace
  * @return string
  */
 public static string clearhtmltosize(string args,int maxsize,string replace){
  if(args.length()<=maxsize){
   return args;
  }
  return args.substring(0,maxsize).concat(replace);
 }
// /**
//  * 从css样式文件中读取<style>样式
//  * @return string
//  */
// public static  string getstyletostring(file file){
//  document doc=null;
//  elements filelist=null;
//  try{
//   doc = jsoup.parse(file, config.default_type);
//   filelist=doc.select("style");
//   string css= doc.data();
//   if(css.length()<1){
//    return css;
//   }
//   /*
//    * 过滤掉注释语句
//    */
//   css=css.replaceall("(<[^>]*>)", "");
//   return css;
//  }catch (exception e) {
//   log.error(e);
//  }finally{
//   doc=null;
//   filelist=null;
//  }
//  return "";
// }
// /**
//  * 从css样式文件中读取<style>样式
//  * @return map<string,string>
//  */
// public static  map<string,string> getstyletomap(file file){
//  map<string, string> css=new hashmap<string, string>();
//  try{
//   string cssstyle=getstyletostring(file);
//   if(cssstyle.trim().length()<1){
//    return css;
//   }
//   /*
//    *
//    */
//   string[] style=cssstyle.split("\\}");
//   string[] map=new string[2];
//   for (string _style : style) {
//    map=_style.split("\\{");
//    css.put(map[0],map[1]);
//   }
//   return css;
//  }catch (exception e) {
//   log.error(e);
//  }
//  return css;
// }
 /**
  * 从style样式中读取css的属性
  * <pre>
  * string style="float:left;margin:0px;font-size:12px;";
  * string fontsize=htmlservices.getstylenamevalue(style,"font-size");</pre>
  * @param style
  * @param stylename
  * @return string
  */
 protected static  string getstyletostring(string style,string stylename){
  try{
   map<string,string> css=csstomap(style);
   return css.get(stylename);
  }catch (exception e) {
   log.error(e);
  }
  return "";
 }
 public static string filterchare(string msg,string ...chars){
  for(string _char:chars){
   msg=msg.replace(_char,"");
  }
  return msg;
 }
 public static string maptocss(map<string, string> css){
  stringbuffer style=new stringbuffer();
  for(map.entry <string,string> entry : css.entryset()){
   style.append(style.length()>0?",":"");
   style.append(entry.getkey()).append(":").append(entry.getvalue());
  }
  return style.tostring();
 }
 /**
  * 将style封装成键值对
  * <pre>
  * string style="float:left;margin:0px;font-size:12px;";
  * map<string,string> css=htmlservices.getstyletomap(style);
  * system.out.println("font-size:"+css.get("font-size"));
  * </pre>
  * @param style
  * @return map
  */
 public static  map<string, string> csstomap(string style){
  map<string, string> map=new hashmap<string, string>();
  try{
   if(style==null||stringutils.isempty(style)){
    return map;
   }
   style=style.tolowercase();
   style=style.split("\\}")[0];
   string[] csss=style.split("\\;");
   for(int i=0,iszie=csss.length;i<iszie;i++){
    string [] cssstyle=csss[i].split("\\:");
    for (int j = 0,jsize=cssstyle.length; (j+1) < jsize; j+=2) {
     map.put(cssstyle[j].replace(" ", "").trim(), cssstyle[j+1].tostring().trim());
    }
   }
  }catch (exception e) {
   log.error(e);
  }
  return map;
 }
// /**
//  * 从css样式中读取cssname的属性(注意,css是标准css)
//  * @param css
//  * @param cssname
//  * @param stylename
//  * @return string
//  */
// public static  string getstylenamevalue(file file,string cssname,string stylename){
//  try{
//   string style=getstyletomap(file).get(cssname);
//   return getstylenamevalue(style, stylename);
//  }catch (exception e) {
//   log.error(e);
//  }
//  return "";
// }

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网