Java clear HTML escape character _java

Source: Internet
Author: User
Tags log log trim stringbuffer

Copy Code code as follows:

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;
/**
* Some convenient services for static file processing
* @author Kettas
* 4:27:25 PM
*/
public class Htmlservices {
private static Log Log=logfactory.getlog (Htmlservices.class);

/**
* Clear HTML code
* <p> all content included between ' < ' and ' > ' will be erased and returned to </P>
* @param args
* @return String
*/
public static string Clearhtmltostring (String args) {
Return clearhtmltostring (Args,false);
}
/**
* Clear HTML code
* <p> all content included between ' < ' and ' > ' will be erased and returned to </P>
* @param args
* @param replacenull whether to replace tabs such as spaces
* @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;
}
/**
* Clear HTML code
* <p> all content included between ' < ' and ' > ' will be erased and the return length </P>
* @param args
* @return String
*/
public static string Clearhtmltostring (String Args,int maxSize) {
Return clearhtmltostring (args, MaxSize, "");
}
/**
* Clear HTML code
* <p> all content included between ' < ' and ' > ' will be erased and the return length </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);
}
/**
* Intercept a string for a specified length
* @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);
}
// /**
* Read <Style> style from CSS style files
* @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;
//   }
//   /*
* Filter out Comment statements
//    */
Css=css.replaceall ("(<[^>]*>)", "");
return CSS;
}catch (Exception e) {
Log.error (e);
}finally{
Doc=null;
Filelist=null;
//  }
Return "";
// }
// /**
* Read <Style> style from CSS style files
* @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;
// }
/**
* Read the properties of the CSS from the style style
* <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 ();
}
/**
* Encapsulate style as a key value pair
* <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;
}
// /**
* Read Cssname properties from CSS styles (note that CSS is standard 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 "";
// }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.