La copie de code est la suivante:
import java.util.hashmap;
importation java.util.map;
import org.apache.commons.lang3.stringutils;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
/ **
* Quelques services pratiques pour le traitement de fichiers statique
* @author kettas
* 16:27:25
* /
classe publique htmlServices {
journal statique privé = logfactory.getLog (htmlServices.class);
/ **
* Netter le code HTML
* <p> Tous les contenus inclus entre '<' et '>' seront effacés et retournés à </p>
* @param args
* String @return
* /
String statique public clearhtmltoString (String args) {
return clearhtmltoString (args, false);
}
/ **
* Netter le code HTML
* <p> Tous les contenus inclus entre '<' et '>' seront effacés et retournés à </p>
* @param args
* @Param Rempacenull s'il faut remplacer les caractères d'onglet tels que les espaces
* String @return
* /
String statique public clearhtmltoString (String args, booléen remplacenull) {
if (stringUtils.isempty (args)) {
retour "";
}
args = args.replaceAll ("(? is) <(. *?)>", "");
if (remplacenull) {
args = args.replaceAll ("// s * | / t | / r | / n", "");
}
retour args;
}
/ **
* Netter le code HTML
* <p> Tous les contenus inclus entre '<' et '>' seront effacés et la longueur renvoyée est spécifiée </p>
* @param args
* String @return
* /
String statique public clearhtmltoString (String args, int maxsize) {
return ClearHtmlToString (args, maxsize, "");
}
/ **
* Netter le code HTML
* <p> Tous les contenus inclus entre '<' et '>' seront effacés et la longueur renvoyée est spécifiée </p>
* @param args
* String @return
* /
String statique public clearhtmltoString (String args, int maxsize, string remphal
args = clearhtmltoString (args);
if (maxSize <= 0) {
retour args;
}
if (args.length () <= maxsize) {
retour args;
}
return args.substring (0, maxSize) .Concat (remplacer);
}
/ **
* Intercepter la chaîne à la longueur spécifiée
* @param args
* @param maxsize
* @param remplacer
* String @return
* /
String statique public clearhtmltoSize (String args, int maxsize, string remplace) {
if (args.length () <= maxsize) {
retour args;
}
return args.substring (0, maxSize) .Concat (remplacer);
}
/// **
// * Lire <style> Style à partir du fichier de style CSS
// * @return String
// * /
// String statique public getStyletOstring (fichier de fichier) {
// document doc = null;
// Éléments FileList = null;
//essayer{
// doc = jsoup.parse (fichier, config.default_type);
//filelist=doc.select("Style ");
// String css = doc.data ();
//if(css.length () <1) {
// retourne CSS;
//}
/// *
// * Filtrez des instructions de commentaires
// * /
//css=css.replaceALL ("(<[^>] *>)", "");
// retourne CSS;
//} catch (exception e) {
//log.error(e);
//} enfin{
// doc = null;
// FileList = null;
//}
//retour "";
//}
/// **
// * Lire <style> Style à partir du fichier de style CSS
// * @return map <string, string>
// * /
// Public Static Map <String, String> getSTyletoMap (fichier de fichier) {
// map <string, string> css = new hashmap <string, string> ();
//essayer{
// String cssStyle = getSTyletOstring (fichier);
//if(cssstyle.trim (). Length () <1) {
// retourne CSS;
//}
/// *
// *
// * /
// string [] style = cssStyle.split ("//}");
// String [] map = new String [2];
// pour (String _Style: style) {
//map=_style.split ("// {");
//css.put(MAP&ABLAGE,MAP[1]);
//}
// retourne CSS;
//} catch (exception e) {
//log.error(e);
//}
// retourne CSS;
//}
/ **
* Lire les propriétés CSS à partir du style
* <pre>
* String Style = "Float: Left; margin: 0px; font-size: 12px;";
* String FontSize = htmlServices.getStyLenameValue (style, "Font-Size"); </ Pre>
* style @param
* @param stylename
* String @return
* /
String statique protégé getSTyletOstring (Style Style, String Stylename) {
essayer{
Map <string, string> css = csstoMap (style);
return css.get (stylename);
} catch (exception e) {
Log.Error (e);
}
retour "";
}
Public Static String FilterChare (String Msg, String ... Chars) {
pour (String _Char: Chars) {
msg = msg.replace (_char, "");
}
retour msg;
}
MAPTOCSS STATIQUE STATIQUE publique (map <string, string> css) {
StringBuffer style = new StringBuffer ();
pour (map.entry <string, string> entrée: css.entryset ()) {
style.append (style.length ()> 0? ",": "");
style.append (entry.getKey ()). APPEND (":"). APPEND (entrée.getValue ());
}
return style.toString ();
}
/ **
* Encapsuler le style en paires de valeurs clés
* <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>
* style @param
* @return map
* /
Map statique publique <String, String> csstoMap (String Style) {
Map <string, string> map = new hashmap <string, string> ();
essayer{
if (style == null || stringUtils.isempty (style)) {
carte de retour;
}
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 ("//:");
pour (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);
}
carte de retour;
}
/// **
// * Lisez l'attribut de CSSName à partir du style CSS (notez que CSS est CSS standard)
// * @param css
// * @param cssname
// * @param stylename
// * @return String
// * /
// String statique public getStyLeNameValue (fichier de fichier, chaîne cssName, String Stylename) {
//essayer{
// String style = getSTyletoMap (file) .get (cssName);
// return getStyLenameValue (style, stylename);
//} catch (exception e) {
//log.error(e);
//}
//retour "";
//}
}