La copia del código es la siguiente:
import java.util.hashmap;
import java.util.map;
importar org.apache.commons.lang3.stringutils;
importar org.apache.commons.logging.log;
importar org.apache.commons.logging.logFactory;
/**
* Algunos servicios convenientes para el procesamiento de archivos estáticos
* @author kettas
* 4:27:25 PM
*/
clase pública HTMLServices {
Log de registro estático privado = logFactory.getLog (htmlservices.class);
/**
* Borrar código HTML
* <p> Todos los contenidos incluidos entre '<' y '>' se borrarán y devolverán a </p>
* @param args
* @return cadena
*/
public static string clearhtmlToString (string args) {
return clearhtmlToString (args, falso);
}
/**
* Borrar código HTML
* <p> Todos los contenidos incluidos entre '<' y '>' se borrarán y devolverán a </p>
* @param args
* @param reemplazar si reemplazar caracteres de pestaña como espacios
* @return cadena
*/
public static string clearhtmlToString (String args, boolean reemplazo) {
if (stringUtils.isempty (args)) {
devolver "";
}
args = args.replacealll ("(? es) <(.*?)>", "");
if (reemplazar) {
args = args.replacealll ("// s*|/t |/r |/n", "");
}
devolver args;
}
/**
* Borrar código HTML
* <p> Todos los contenidos incluidos entre '<' y '>' se borrarán y se especifica la longitud devuelta </p>
* @param args
* @return cadena
*/
public static string clearhtmlToString (string args, int maxSize) {
devolver ClearHtmlToString (args, maxSize, "");
}
/**
* Borrar código HTML
* <p> Todos los contenidos incluidos entre '<' y '>' se borrarán y se especifica la longitud devuelta </p>
* @param args
* @return cadena
*/
public static string clearhtmlToString (string args, int maxSize, string reemplazar) {
args = clearhtmlToString (args);
if (maxSize <= 0) {
devolver args;
}
if (args.length () <= maxSize) {
devolver args;
}
return args.substring (0, maxSize) .concat (reemplazar);
}
/**
* Interceptar la cadena a la longitud especificada
* @param args
* @param maxsize
* @param reemplazar
* @return cadena
*/
public static string clearHtmlTosize (string args, int maxSize, string reemplazar) {
if (args.length () <= maxSize) {
devolver args;
}
return args.substring (0, maxSize) .concat (reemplazar);
}
/// **
// * Leer <estilo> Estilo del archivo de estilo CSS
// * @@return cadena
// */
// public static string getStyletoString (archivo de archivo) {
// documento doc = null;
// Elements filelist = null;
//intentar{
// doc = jsoup.parse (archivo, config.default_type);
//filelist=doc.select("style ");
// cadena css = doc.data ();
//if(css.length () <1) {
// devolver CSS;
//}
///*
// * Filtrar declaraciones de comentarios
// */
//css=css.replaceAll ("(<[^>]*>)", "");
// devolver CSS;
//} Catch (Exception e) {
//log.error(e);
//} finalmente{
// doc = null;
// filelist = nulo;
//}
//devolver "";
//}
/// **
// * Leer <estilo> Estilo del archivo de estilo CSS
// * @@return map <string, string>
// */
// public static map <String, String> getStyletomap (archivo de archivo) {
// map <string, string> css = new Hashmap <String, String> ();
//intentar{
// string cssstyle = getStyletoString (archivo);
//if(cssstyle.trim (). Longitud () <1) {
// devolver CSS;
//}
///*
// *
// */
// string [] style = cssstyle.split ("//}");
// cadena [] map = nueva cadena [2];
// for (string _style: style) {
//map=_style.split ("// {");
//css.put( mapchefont>, mapche1]);
//}
// devolver CSS;
//} Catch (Exception e) {
//log.error(e);
//}
// devolver CSS;
//}
/**
* Lea las propiedades de CSS del estilo
* <PRE>
* String style = "float: izquierda; margen: 0px; font-size: 12px;";
* String fontSize = htmlservices.getStylenamEvalue (estilo, "Font-Size"); </pre>
* @Param Style
* @param stylename
* @return cadena
*/
Cadena estática protegida getStyletoString (estilo de cadena, stylename) {
intentar{
Map <string, string> css = csstomap (estilo);
return css.get (stylename);
} Catch (Exception e) {
log.error (e);
}
devolver "";
}
Public static string filtreChare (string msg, string ... chars) {
for (string _char: chars) {
msg = msg.replace (_char, "");
}
regresar 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 ();
}
/**
* Estilo encapsular en pares de valores clave
* <PRE>
* String style = "float: izquierda; margen: 0px; font-size: 12px;";
* Map <string, string> css = htmlservices.getStyletomap (estilo);
* System.out.println ("Font-Size:"+css.get ("font-size"));
* </pre>
* @Param Style
* @@return mapa
*/
Public Static Map <String, String> csstomap (string style) {
Map <string, string> map = new HashMap <String, String> ();
intentar{
if (style == null || stringUtils.isempty (style)) {
mapa de retorno;
}
style = style.tolowercase ();
style = style.split ("//}") [0];
Cadena [] csss = style.split ("//;");
para (int i = 0, iszie = csss.length; i <iszie; i ++) {
Cadena [] cssstyle = csss [i] .split ("//:");
para (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);
}
mapa de retorno;
}
/// **
// * Lea el atributo de CSSName desde el estilo CSS (tenga en cuenta que CSS es CSS estándar)
// * @param CSS
// * @param cssname
// * @param stylename
// * @@return cadena
// */
// public static string getStylenEvalue (archivo de archivo, cadena cssname, string stylename) {
//intentar{
// string style = getstyletomap (archivo) .get (cssname);
// return getStylenamEvalue (estilo, stylename);
//} Catch (Exception e) {
//log.error(e);
//}
//devolver "";
//}
}