KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > formbean > ContentPreviewForm


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.formbean;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.htmlparser.Node;
20 import org.htmlparser.NodeFilter;
21 import org.htmlparser.Parser;
22 import org.htmlparser.tags.TableTag;
23 import org.htmlparser.util.NodeList;
24
25 /**
26  * 支持内容预览的基类
27  * @author Liudong
28  */

29 public class ContentPreviewForm extends DlogActionForm {
30
31     public final static int MAX_COUNT = 400;
32     public final static int MAX_COUNT2 = 1000;
33     public static int BRIEF_LENGTH = 25;
34     
35     String JavaDoc content = null;
36     
37     public String JavaDoc getBrief() {
38         //去除所有尖括号对
39
if (content == null)
40             return "无内容";
41         String JavaDoc ct = StringUtils.replace(content," ","");
42         StringBuffer JavaDoc brief = new StringBuffer JavaDoc(256);
43         int cur = 0;
44         do {
45             int idx = ct.indexOf('<',cur);
46             if(idx==-1) {
47                 brief.append(ct.substring(cur));
48                 break;
49             }
50             else {
51                 brief.append(ct.substring(cur,idx));
52                 cur = ct.indexOf('>',idx);
53                 if(cur==-1)
54                     break;
55                 cur++;
56             }
57         }while(true);
58         String JavaDoc text = StringUtils.left(brief.toString().trim(),BRIEF_LENGTH);
59         if(text==null||text.length()==0)
60             text = "[无文本信息]";
61         else
62             text = StringUtils.replace(text,"&nbsp;"," ");
63         
64         return text.trim();
65     }
66     
67     /**
68      * 获取HTML的预览信息
69      * @return
70      */

71     public String JavaDoc getPreviewContent(){
72         String JavaDoc ct = StringUtils.left(content,MAX_COUNT);
73         try {
74             //截取前N个字符
75
if(ct!=null&&content!=null) {
76             int idx2 = ct.lastIndexOf('>');
77             int idx1 = ct.lastIndexOf('<');
78             if((idx2==-1 && idx1>=0) || idx1 > idx2) {
79                 String JavaDoc ct2 = content.substring(ct.length());
80                 int idx3 = ct2.indexOf('>');
81                 if(idx3!=-1 && idx3<(MAX_COUNT2-MAX_COUNT)) {
82                     ct += content.substring(ct.length(),ct.length()+idx3+1);
83                 }
84             }
85         }
86         if(ct!=null&&content!=null) {
87             int idx2 = ct.toLowerCase().lastIndexOf("</object>");
88             int idx1 = ct.toLowerCase().lastIndexOf("<object");
89             if((idx2==-1 && idx1>=0) || idx1 > idx2) {
90                 String JavaDoc ct2 = content.substring(ct.length()).toLowerCase();
91                 int idx3 = ct2.indexOf("</object>");
92                 if(idx3!=-1)
93                     ct += content.substring(ct.length(),ct.length()+idx3+9);
94                 else
95                     ct = ct.substring(0,idx1);
96             }
97         }
98         if(ct!=null&&content!=null) {
99             //System.out.println(ct);
100
int idx1 = ct.toLowerCase().lastIndexOf("<div align='right'><font color='#cccccc' size='1'>[edit");
101             int idx2 = ct.toLowerCase().lastIndexOf("]</font></div>");
102             //System.out.println("idx1="+idx1+",idx2="+idx2);
103
if((idx1>=0 && idx2==-1) || idx1 > idx2){
104                 String JavaDoc ct2 = content.substring(ct.length());
105                 int idx3 = ct2.toLowerCase().indexOf("]</font></div>");
106                 if(idx3!=-1)
107                     ct += content.substring(ct.length(),ct.length()+idx3+14);
108                 else
109                     ct = ct.substring(0,idx1);
110             }
111         }
112         if(ct!=null&&content!=null) {
113             Parser parser = Parser.createParser(new String JavaDoc(ct.getBytes(),ISO8859_1));
114             Node [] tables = parser.extractAllNodesThatAre (TableTag.class);
115             if(tables!=null&&tables.length>0) {
116                 TableTag tableTag = (TableTag)tables[0];
117                 ct = ct.substring(0,tableTag.getStartPosition()) + new String JavaDoc(tableTag.toHtml().getBytes(ISO8859_1));
118             }
119         }
120         pc_len = ct.length();
121         }catch(NullPointerException JavaDoc e) {
122         }catch(Exception JavaDoc e) {
123             e.printStackTrace();
124         }
125         return ct;
126     }
127     
128     int pc_len = -1;
129     
130     public String JavaDoc getOtherContent() {
131         if(content!=null) {
132             if(pc_len==-1)
133                 pc_len = getPreviewContent().length();
134             int pl = (pc_len==-1)?0:pc_len;
135             int cl = getContent().length()-pl;
136             if(cl>0)
137                 return content.substring(pl);
138         }
139         return null;
140     }
141
142     public String JavaDoc getContent() {
143         return content;
144     }
145     
146     public String JavaDoc getWmlContent(){
147         try{
148             return extractText(content);
149         }catch(Exception JavaDoc e){}
150         return null;
151     }
152     public void setContent(String JavaDoc content) {
153         this.content = content;
154     }
155
156     /**
157       * 抽取纯文本信息
158       *
159       * @param inputHtml
160       * @return
161       */

162     protected static String JavaDoc extractText(String JavaDoc inputHtml) throws Exception JavaDoc {
163         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
164         Parser parser = Parser.createParser(new String JavaDoc(inputHtml.getBytes(),ISO8859_1));
165         //遍历所有的节点
166
NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
167             public boolean accept(Node node) {
168                 return true;
169             }
170         });
171         for(int i=0;i<nodes.size();i++){
172             Node node = nodes.elementAt(i);
173             text.append(new String JavaDoc(node.toPlainTextString().getBytes(ISO8859_1)));
174         }
175         return text.toString();
176     }
177
178     public static void main(String JavaDoc[] args) {
179         String JavaDoc ct = "点击<A HREF=\"http://www.javayou.com/dlog/showlog.asp?log_id=534\" target=_blank><FONT color=#ff0000 size=3><STRONG>这里</STRONG></FONT></A>查看 <DIV align=right><FONT color=#cccccc size=1>[Edit&nbsp;on&nbsp;2004-03-11 14:18:31&nbsp;By&nbsp;管理员]</FONT></DIV>";
180         ContentPreviewForm cpf = new ContentPreviewForm();
181         cpf.setContent(ct);
182         System.out.println(cpf.getPreviewContent());
183     }
184     private final static String JavaDoc ISO8859_1 = "8859_1";
185 }
186
Popular Tags