1 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 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 content = null; 36 37 public String getBrief() { 38 if (content == null) 40 return "无内容"; 41 String ct = StringUtils.replace(content," ",""); 42 StringBuffer brief = new StringBuffer (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 text = StringUtils.left(brief.toString().trim(),BRIEF_LENGTH); 59 if(text==null||text.length()==0) 60 text = "[无文本信息]"; 61 else 62 text = StringUtils.replace(text," "," "); 63 64 return text.trim(); 65 } 66 67 71 public String getPreviewContent(){ 72 String ct = StringUtils.left(content,MAX_COUNT); 73 try { 74 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 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 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 int idx1 = ct.toLowerCase().lastIndexOf("<div align='right'><font color='#cccccc' size='1'>[edit"); 101 int idx2 = ct.toLowerCase().lastIndexOf("]</font></div>"); 102 if((idx1>=0 && idx2==-1) || idx1 > idx2){ 104 String 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 (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 (tableTag.toHtml().getBytes(ISO8859_1)); 118 } 119 } 120 pc_len = ct.length(); 121 }catch(NullPointerException e) { 122 }catch(Exception e) { 123 e.printStackTrace(); 124 } 125 return ct; 126 } 127 128 int pc_len = -1; 129 130 public String 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 getContent() { 143 return content; 144 } 145 146 public String getWmlContent(){ 147 try{ 148 return extractText(content); 149 }catch(Exception e){} 150 return null; 151 } 152 public void setContent(String content) { 153 this.content = content; 154 } 155 156 162 protected static String extractText(String inputHtml) throws Exception { 163 StringBuffer text = new StringBuffer (); 164 Parser parser = Parser.createParser(new String (inputHtml.getBytes(),ISO8859_1)); 165 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 (node.toPlainTextString().getBytes(ISO8859_1))); 174 } 175 return text.toString(); 176 } 177 178 public static void main(String [] args) { 179 String 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 on 2004-03-11 14:18:31 By 管理员]</FONT></DIV>"; 180 ContentPreviewForm cpf = new ContentPreviewForm(); 181 cpf.setContent(ct); 182 System.out.println(cpf.getPreviewContent()); 183 } 184 private final static String ISO8859_1 = "8859_1"; 185 } 186 | Popular Tags |