package com.neroarc.blog.myblog.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author: fjx
* @date: 2019/4/15 10:33
* Descripe:
*/
public class HtmlUtil {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<(?!pre|/pre)[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
private static final String regEx_summary = "<[^>]+>";
private static final String regEx_pre = "(?<=<pre>).+?(?=</pre>)";
public static String delHTMLTag(String htmlStr) {
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
return htmlStr.trim(); // 返回文本字符串
}
public static String getTextFromHtml(String htmlStr){
htmlStr = delHTMLTag(htmlStr);
htmlStr = htmlStr.replaceAll(" ", "");
//htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
return htmlStr;
}
public static String getSummary(String preStr){
Pattern p_pre = Pattern.compile(regEx_pre,Pattern.CASE_INSENSITIVE);
Matcher m_pre = p_pre.matcher(preStr);
preStr = m_pre.replaceAll("");
Pattern p_summary = Pattern.compile(regEx_summary,Pattern.CASE_INSENSITIVE);
Matcher m_summary = p_summary.matcher(preStr);
preStr = m_summary.replaceAll("");
return preStr;
}
public static void main(String[] args) {
String str = "\n" +
"<h4 id=\"h4-java-\"><a name=\"Java设计模式\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>Java设计模式</h4><h4 id=\"h4-u88C5u9970u8005u6A21u5F0F\"><a name=\"装饰者模式\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>装饰者模式</h4><h6 id=\"h6-component\"><a name=\"Component\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>Component</h6><pre><code class=\"lang-java\">public interface Component {\n" +
" void method();\n" +
"}\n" +
"</code></pre>\n" +
"<h6 id=\"h6-concrete\"><a name=\"Concrete\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>Concrete</h6><pre><code class=\"lang-java\">public class ConcreteComponent implements Component {\n" +
" @Override\n" +
" public void method() {\n" +
" System.out.println(\"ConcreteComponent方法\");\n" +
" }\n" +
"}\n" +
"</code></pre>\n" +
"<h6 id=\"h6-decorator\"><a name=\"Decorator\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>Decorator</h6><pre><code class=\"lang-java\">public class Decorator implements Component{\n" +
"\n" +
" private Component component;\n" +
"\n" +
" public Decorator(Component component){\n" +
" this.component = component;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public void method() {\n" +
" component.method();\n" +
" }\n" +
"}\n" +
"</code></pre>\n" +
"<h6 id=\"h6-decoratora\"><a name=\"DecoratorA\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>DecoratorA</h6><pre><code class=\"lang-java\">public class DecoratorA extends Decorator {\n" +
"\n" +
" public DecoratorA(Component component){\n" +
" super(component);\n" +
" }\n" +
"\n" +
" @Override\n" +
" public void method() {\n" +
" super.method();\n" +
" methodA();\n" +
" }\n" +
"\n" +
" public void methodA(){\n" +
" System.out.println(\"DecoratorA方法\");\n" +
" }\n" +
"}\n" +
"</code></pre>\n" +
"<h6 id=\"h6-decoratorb\"><a name=\"DecoratorB\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>DecoratorB</h6><pre><code class=\"lang-java\">public class DecoratorB extends Decorator {\n" +
"\n" +
" public DecoratorB(Component component){\n" +
" super(component);\n" +
" }\n" +
"\n" +
" @Override\n" +
" public void method() {\n" +
" super.method();\n" +
" methodB();\n" +
" }\n" +
"\n" +
" public void methodB(){\n" +
" System.out.println(\"DecoratorB方法\");\n" +
" }\n" +
"}\n" +
"</code></pre>\n" +
"<h6 id=\"h6-u6D4Bu8BD5u7C7B\"><a name=\"测试类\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>测试类</h6><pre><code class=\"lang-java\">public class Test {\n" +
"\n" +
" public static void main(String[] args) {\n" +
" Component component = new DecoratorB(new DecoratorA(new ConcreteComponent()));\n" +
"\n" +
" component.method();\n" +
" }\n" +
"}\n" +
"</code></pre>\n" +
"<p>B调用A的method,A调用ConceteCompant的method,链式。</p>\n" +
"<p>装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式和装饰对象交互</p>\n" +
"<p>装饰对象接收所有来自客户端的请求,把这些请求转发给真实对象。</p>\n" +
"<p>装饰对象可以在转发这些请求前或者后增加一些附加功能。</p>\n" +
"<h4 id=\"h4-u5355u4F8Bu8BBEu8BA1u6A21u5F0F\"><a name=\"单例设计模式\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>单例设计模式</h4><h5 id=\"h5--\"><a name=\"懒汉模式(调用时才创建实例):\" class=\"reference-link\"></a><span class=\"header-link octicon octicon-link\"></span>懒汉模式(调用时才创建实例):</h5><p>原型:</p>\n" +
"<pre><code class=\"lang-java\">public class LanHanMode {\n" +
"\n" +
" private static LanHanMode lanHanMode= null;\n" +
" private LanHanMode(){}\n" +
"\n" +
" public static LanHanMode getInstance(){\n" +
" if(lanHanMode==null){\n" +
" lanHanMode = new LanHanMode();//懒汉模式,线程不安全\n"