A cópia do código é a seguinte:
importar java.util.hashmap;
importar java.util.map;
importar org.apache.commons.lang3.StringUtils;
importar org.apache.commons.logging.log;
importar org.apache.commons.logging.logfactory;
/**
* Alguns serviços convenientes para processamento de arquivos estáticos
* @Author Kettas
* 16:27:25
*/
classe pública htmlservices {
log estático privado log = logFactory.getLog (htmlservices.class);
/**
* Limpar código HTML
* <p> Todos os conteúdos incluídos entre '<' e '' 'serão limpos e devolvidos para </p>
* @param args
* @return string
*/
public static string clearhtmltostring (string args) {
return clearhtmltoString (args, falso);
}
/**
* Limpar código HTML
* <p> Todos os conteúdos incluídos entre '<' e '' 'serão limpos e devolvidos para </p>
* @param args
* @param replacenull se você deve substituir caracteres de guia, como espaços
* @return string
*/
public static string clearhtmltostring (string args, replacenull boolean) {
if (stringutils.isEmpty (args)) {
retornar "";
}
args = args.replaceall ("(? Is) <(.*?)>", "");
if (replacenull) {
args = args.replaceall ("// s*|/t |/r |/n", "");
}
retornar args;
}
/**
* Limpar código HTML
* <p> Todos os conteúdos incluídos entre '<' e '' 'serão limpos e o comprimento retornado é especificado </p>
* @param args
* @return string
*/
public static string clearhtmltostring (string args, int maxsize) {
Retornar ClearHtmlToString (Args, MaxSize, "");
}
/**
* Limpar código HTML
* <p> Todos os conteúdos incluídos entre '<' e '' 'serão limpos e o comprimento retornado é especificado </p>
* @param args
* @return string
*/
public static string clearhtmltoString (string args, int maxsize, string replyber) {
args = clearhtmltoString (args);
if (maxSize <= 0) {
retornar args;
}
if (args.length () <= maxsize) {
retornar args;
}
retornar args.substring (0, maxSize) .CONCAT (substituir);
}
/**
* Interceptar a string no comprimento especificado
* @param args
* @param maxsize
* @param substitua
* @return string
*/
public static string clearhtmltosize (string args, int maxsize, string replyber) {
if (args.length () <= maxsize) {
retornar args;
}
retornar args.substring (0, maxSize) .CONCAT (substituir);
}
/// **
// * leia <estilo> estilo do arquivo de estilo CSS
// * @return string
// */
// public static string getStyleToString (arquivo de arquivo) {
// document doc = null;
// elementos fileList = null;
//tentar{
// doc = jsoup.parse (arquivo, config.default_type);
//filelist=doc.select("style ");
// string css = doc.data ();
//if aCss.Length () <1) {
// retorna CSS;
//}
///*
// * Filtre as declarações de comentários
// */
//css=css.replaceall ("(<[^>]*>)", "");
// retorna CSS;
//} catch (Exceção e) {
//log.error(e);
//} finalmente{
// doc = null;
// filelist = null;
//}
//retornar "";
//}
/// **
// * leia <estilo> estilo do arquivo de estilo CSS
// * @return map <string, string>
// */
// mapa estático público <string, string> getStyleTOMap (arquivo de arquivo) {
// mapa <string, string> css = new hashmap <string, string> ();
//tentar{
// string cssStyle = getStyleToString (arquivo);
//if(cssstyle.trim (). comprimento () <1) {
// retorna CSS;
//}
///*
// *
// */
// string [] style = cssstyle.split ("//}");
// string [] map = new String [2];
// for (string _style: style) {
//map=_style.split ("// {");
//CSs.put(mapP.
//}
// retorna CSS;
//} catch (Exceção e) {
//log.error(e);
//}
// retorna CSS;
//}
/**
* Leia as propriedades do CSS do estilo
* <pre>
* String style = "float: esquerda; margem: 0px; font-size: 12px;";
* String fontSize = htmlServices.getStyleNameValue (estilo, "font-size"); </pre>
* estilo @param
* @param stylename
* @return string
*/
String estática protegida getStyleToString (estilo string, string stylename) {
tentar{
Mapa <string, string> css = csstomap (estilo);
retornar css.get (estilenome);
} catch (Exceção e) {
log.error (e);
}
retornar "";
}
public static string filterchare (string msg, string ... chars) {
para (string _char: chars) {
msg = msg.replace (_char, "");
}
retornar msg;
}
public static string maptocss (map <string, string> css) {
StringBuffer Style = new StringBuffer ();
para (map.entry <string, string> entrada: css.entryset ()) {
style.append (style.length ()> 0? ",": "");
style.Append (Entry.getKey ()). Append (":"). Append (Entry.getValue ());
}
retorno style.toString ();
}
/**
* Encapsular o estilo em pares de valor-chave
* <pre>
* String style = "float: esquerda; margem: 0px; font-size: 12px;";
* Mapa <string, string> css = htmlServices.getStyletomap (style);
* System.out.println ("font-size:"+css.get ("font-size"));
* </pre>
* estilo @param
* @return mapa
*/
mapa estático público <string, string> csstomap (estilo string) {
Mapa <string, string> map = new hashmap <string, string> ();
tentar{
if (style == null || stringutils.isempty (style)) {
mapa de retorno;
}
style = style.tolowercase ();
style = style.split ("//}") [0];
String [] csss = style.split ("//;");
for (int i = 0, iszie = css.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] .Redplace ("", "") .Trim (), cssstyle [j+1] .toString (). TRIM ());
}
}
} catch (Exceção e) {
log.error (e);
}
mapa de retorno;
}
/// **
// * Leia o atributo do CSSName do estilo CSS (observe que o CSS é CSS padrão)
// * @param css
// * @param cssName
// * @param stylename
// * @return string
// */
// public static string getStyleNameValue (arquivo de arquivo, string cssName, string stylename) {
//tentar{
// string style = getStyleTOMap (arquivo) .get (cssName);
// return getStyleNameValue (estilo, nome de estilos);
//} catch (Exceção e) {
//log.error(e);
//}
//retornar "";
//}
}