【转】java截取带html标签的字符串,再把标签补全(保证页面显示效果)

【转】http://blog.csdn.net/zdtwyjp/article/details/5736430

Java截取带HTML标签的字符串,一般情况下有三种实现方式:

1、在截取字符串函数中对HTML标签进行闭合(对标签作入栈出栈式处理即可)。

2、过滤掉HTML。

3、如果需要保留样式的话,可以按照格式提取各节内容,然后分别截取之后再重新用HTML做出表现样式。

 

1和2的方法不够好。因为1在截取长度时,是边所有的HTML标签都计算在内的,如果有的内容包含HTML标签较多,哪它实际的正文内容就得少了。所以不同内容可能表现长度不一,视觉效果不好。而2,没有格式,当然不可取。所以相对来说, 3比较好些。先提取内容然后再将内容补全,具体实现代码如下:

 

[java]  view plain  copy
 
  1. package string;  
  2. public class TagsList {  
  3.     private String[] data;  
  4.     private int size = 0;  
  5.     public TagsList(int size) {  
  6.         data = new String[size];  
  7.     }  
  8.     public TagsList() {  
  9.         this(10);  
  10.     }  
  11.     public void add(String str) {  
  12.         ensureCapacity(size + 1);  
  13.         data[size++] = str;  
  14.     }  
  15.     public String get(int index) {  
  16.         if(index < size)  
  17.             return data[index];  
  18.         else  
  19.             return null;  
  20.     }  
  21.     // 为了提高效率,只将其置为null  
  22.     public boolean remove(String str) {  
  23.         for(int index = 0; index < size; index++) {  
  24.             if(str.equals(data[index])) {  
  25.                 data[index] = null;  
  26.                 return true;  
  27.             }  
  28.         }  
  29.         return false;  
  30.     }  
  31.     public boolean remove(int index) {  
  32.         if(index < data.length) {  
  33.             data[index] = null;  
  34.             return true;  
  35.         }  
  36.         return false;  
  37.     }  
  38.     public int size() {  
  39.         return this.size;  
  40.     }  
  41.     // 扩展容量  
  42.     public void ensureCapacity(int minSize) {  
  43.         int oldCapacity = data.length;  
  44.         if(minSize > oldCapacity) {  
  45.             int newCapacity = (oldCapacity * 3 / 2 + 1) > minSize ? oldCapacity * 3 / 2 + 1 : minSize;  
  46.             String[] newArray = new String[newCapacity];  
  47.             for(int i = 0; i < data.length; i++) {  
  48.                 newArray[i] = data[i];  
  49.             }  
  50.             data = newArray;  
  51.         }  
  52.     }  
  53. }  
  54.   
  55. package string;  
  56.   
  57. /** 
  58.  * java截取带html标签的字符串,再把标签补全(保证页面显示效果)<br> 
  59.  * 一般是用在字符串中有html标签的截取.如: 后台发布用了在线编辑器, 前台显示内容要截取的情况.<br> 
  60.  *  
  61.  * @author YangJunping 
  62.  * @date 2010-7-15 
  63.  */  
  64. public class SubStringHTML {  
  65.     public static void main(String[] args) {  
  66.         String htmlCode = "<h1><span style="/" mce_style="/""font-size: xx-large; color: #000000;/">新华网北京7月13,.</span></h1><h1><span>北京7——月13</span></h1>";  
  67.         System.out.println(subStringHTML(htmlCode, 5));  
  68.         // 测试结果:<h1><span style="font-size: xx-large; color: #000000;" mce_style="font-size: xx-large; color: #000000;">新华网</span></h1>  
  69.     }  
  70.     /** 
  71.      * 按子节长度截取字符串(支持截取带HTML代码样式的字符串)<br> 
  72.      * 如:<span>中国人发在线</span> 当截取2个字节得到的结果是:<span>中国 
  73.      *  
  74.      * @param param 
  75.      *            将要截取的含html代码的字符串参数 
  76.      * @param length 
  77.      *            截取的字节长度 
  78.      * @return 返回截取后的字符串 
  79.      * @author YangJunping 
  80.      * @date 2010-7-15 
  81.      */  
  82.     public static String subStringHTML(String param, int length) {  
  83.         StringBuffer result = new StringBuffer();  
  84.         int n = 0;  
  85.         char temp;  
  86.         boolean isCode = false// 是不是HTML代码  
  87.         boolean isHTML = false// 是不是HTML特殊字符,如   
  88.         for(int i = 0; i < param.length(); i++) {  
  89.             temp = param.charAt(i);  
  90.             if(temp == '<') {  
  91.                 isCode = true;  
  92.             }else if(temp == '&') {  
  93.                 isHTML = true;  
  94.             }else if(temp == '>' && isCode) {  
  95.                 n = n - 1;  
  96.                 isCode = false;  
  97.             }else if(temp == ';' && isHTML) {  
  98.                 isHTML = false;  
  99.             }  
  100.             if(!isCode && !isHTML) {  
  101.                 n = n + 1;  
  102.                 // UNICODE码字符占两个字节  
  103.                 if((temp + "").getBytes().length > 1) {  
  104.                     n = n + 1;  
  105.                 }  
  106.             }  
  107.             result.append(temp);  
  108.             if(n >= length) {  
  109.                 break;  
  110.             }  
  111.         }  
  112.         return fix(result.toString());  
  113.     }  
  114.     /** 
  115.      * 补全HTML代码<br> 
  116.      * 如:<span>中国 ---> <span>中国</span> 
  117.      *  
  118.      * @param str 
  119.      * @return 
  120.      * @author YangJunping 
  121.      * @date 2010-7-15 
  122.      */  
  123.     private static String fix(String str) {  
  124.         StringBuffer fixed = new StringBuffer(); // 存放修复后的字符串  
  125.         TagsList[] unclosedTags = getUnclosedTags(str);  
  126.         // 生成新字符串  
  127.         for(int i = unclosedTags[0].size() - 1; i > -1; i--) {  
  128.             fixed.append("<" + unclosedTags[0].get(i) + ">");  
  129.         }  
  130.         fixed.append(str);  
  131.         for(int i = unclosedTags[1].size() - 1; i > -1; i--) {  
  132.             String s = null;  
  133.             if((s = unclosedTags[1].get(i)) != null) {  
  134.                 fixed.append("</" + s + ">");  
  135.             }  
  136.         }  
  137.         return fixed.toString();  
  138.     }  
  139.     private static TagsList[] getUnclosedTags(String str) {  
  140.         StringBuffer temp = new StringBuffer(); // 存放标签  
  141.         TagsList[] unclosedTags = new TagsList[2];  
  142.         unclosedTags[0] = new TagsList(); // 前不闭合,如有</div>而前面没有<div>  
  143.         unclosedTags[1] = new TagsList(); // 后不闭合,如有<div>而后面没有</div>  
  144.         boolean flag = false// 记录双引号"或单引号'  
  145.         char currentJump = ' '// 记录需要跳过''还是""  
  146.         char current = ' ', last = ' '// 当前 & 上一个  
  147.         // 开始判断  
  148.         for(int i = 0; i < str.length();) {  
  149.             current = str.charAt(i++); // 读取一个字符  
  150.             if(current == '"' || current == '/'') {  
  151.                 flag = flag ? false : true// 若为引号,flag翻转  
  152.                 currentJump = current;  
  153.             }  
  154.             if(!flag) {  
  155.                 if(current == '<') { // 开始提取标签  
  156.                     current = str.charAt(i++);  
  157.                     if(current == '/') { // 标签的闭合部分,如</div>  
  158.                         current = str.charAt(i++);  
  159.                         // 读取标签  
  160.                         while(i < str.length() && current != '>') {  
  161.                             temp.append(current);  
  162.                             current = str.charAt(i++);  
  163.                         }  
  164.                         // 从tags_bottom移除一个闭合的标签  
  165.                         if(!unclosedTags[1].remove(temp.toString())) { // 若移除失败,说明前面没有需要闭合的标签  
  166.                             unclosedTags[0].add(temp.toString()); // 此标签需要前闭合  
  167.                         }  
  168.                         temp.delete(0, temp.length()); // 清空temp  
  169.                     }else { // 标签的前部分,如<div>  
  170.                         last = current;  
  171.                         while(i < str.length() && current != ' ' && current != ' ' && current != '>') {  
  172.                             temp.append(current);  
  173.                             last = current;  
  174.                             current = str.charAt(i++);  
  175.                         }  
  176.                         // 已经读取到标签,跳过其他内容,如<div id=test>跳过id=test  
  177.                         while(i < str.length() && current != '>') {  
  178.                             last = current;  
  179.                             current = str.charAt(i++);  
  180.                             if(current == '"' || current == '/'') { // 判断引号  
  181.                                 flag = flag ? false : true;  
  182.                                 currentJump = current;  
  183.                                 if(flag) { // 若引号不闭合,跳过到下一个引号之间的内容  
  184.                                     while(i < str.length() && str.charAt(i++) != currentJump)  
  185.                                         ;  
  186.                                     current = str.charAt(i++);  
  187.                                     flag = false;  
  188.                                 }  
  189.                             }  
  190.                         }  
  191.                         if(last != '/' && current == '>'// 判断这种类型:<TagName />  
  192.                             unclosedTags[1].add(temp.toString());  
  193.                         temp.delete(0, temp.length());  
  194.                     }  
  195.                 }  
  196.             }else {  
  197.                 while(i < str.length() && str.charAt(i++) != currentJump)  
  198.                     ; // 跳过引号之间的部分  
  199.                 flag = false;  
  200.             }  
  201.         }  
  202.         return unclosedTags;  
  203.     }  
  204. }  

猜你喜欢

转载自ln-software.iteye.com/blog/2331389