htmlutil.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 128 行

JAVA
128
字号
package cn.js.fan.util;import javax.servlet.http.*;import cn.js.fan.web.*;import com.cloudwebsoft.framework.util.*;import com.redmoon.forum.*;import org.htmlparser.*;import org.htmlparser.filters.*;import org.htmlparser.nodes.*;import org.htmlparser.tags.*;import org.htmlparser.util.*;public class HtmlUtil {    public static final int MAX_LEN2 = 3000;    public HtmlUtil() {    }        public static boolean isTrimEmpty(String astr) {        if ((null == astr) || (astr.length() == 0)) {            return true;        }        if (isBlank(astr.trim())) {            return true;        }        return false;    }        public static boolean isBlank(String astr) {        if ((null == astr) || (astr.length() == 0)) {            return true;        } else {            return false;        }    }    public static String getAbstract(HttpServletRequest request, String htmlStr, int len) {        String content = StrUtil.getLeft(htmlStr, len);                int idx1 = content.lastIndexOf('<');        int idx2 = content.lastIndexOf('>');                        if ((idx2 == -1 && idx1 >= 0) || (idx1 > idx2)) {            String ct3 = htmlStr;            int idx3 = ct3.indexOf('>', idx1);            if (idx3!=-1) {                if (idx3 < MAX_LEN2) {                    content = ct3.substring(0, idx3 + 1);                }            }        }                idx2 = content.toLowerCase().lastIndexOf("</object>");        idx1 = content.toLowerCase().lastIndexOf("<object");        if ((idx2 == -1 && idx1 >= 0) || idx1 > idx2) {            String ct2 = htmlStr.toLowerCase();            int idx3 = ct2.indexOf("</object>");            if (idx3 != -1)                content += htmlStr.substring(content.length(), content.length() + idx3 + 9);            else                content = htmlStr.substring(0, idx1);        }        String str = "";        try {            Parser myParser;            NodeList nodeList = null;            myParser = Parser.createParser(content, "utf-8");            NodeFilter textFilter = new NodeClassFilter(TextNode.class);            NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);            NodeFilter imgFilter = new NodeClassFilter(ImageTag.class);                                    OrFilter lastFilter = new OrFilter();            lastFilter.setPredicates(new NodeFilter[] {textFilter, linkFilter,                                     imgFilter});            nodeList = myParser.parse(lastFilter);            Node[] nodes = nodeList.toNodeArray();            for (int i = 0; i < nodes.length; i++) {                Node anode = (Node) nodes[i];                String line = "";                if (anode instanceof TextNode) {                    TextNode textnode = (TextNode) anode;                                        line = textnode.getText();                } else if (anode instanceof ImageTag) {                    ImageTag imagenode = (ImageTag) anode;                    String url = imagenode.getImageURL();                    String ext = StrUtil.getFileExt(url).toLowerCase();                                        if (ext.equals("gif") || ext.equals("png") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("bmp")) {                                                if (imagenode.getImageURL().startsWith("http"))                            ;                         else if (imagenode.getImageURL().startsWith("/")) {                            ;                         }                        else {                                                         url = request.getContextPath() + "/forum/" + imagenode.getImageURL();                        }                        line = "<div align=center><a onfocus=this.blur() href=\"" + url + "\" target=_blank><IMG SRC=\"" + url + "\" border=0 alt=" +                                SkinUtil.LoadString(request,                                 "res.cn.js.fan.util.StrUtil",                                    "click_open_win") + " onload=\"javascript:if(this.width>screen.width-333) this.width=screen.width-333\"></a></div><BR>";                                            }                }                if (isTrimEmpty(line))                    continue;                str += "<p>" + line + "</p>";            }        }        catch (ParserException e) {            LogUtil.getLog(MsgUtil.class.getName()).error("getAbstract:" + e.getMessage());        }        return str;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?