Advertisement
matteopelucco

HTML Shortener

May 2nd, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4.  
  5. public class HtmlShortener {
  6.  
  7.     private static final String TAGS_TO_SKIP = "br,hr,img,link";
  8.     private static final String[] tagsToSkip = TAGS_TO_SKIP.split(",");
  9.     private static final int STATUS_READY = 0;
  10.  
  11.     private int cutPoint = -1;
  12.     private String htmlString = "";
  13.    
  14.     final List<String> tags = new LinkedList<String>();
  15.    
  16.     StringBuilder sb = new StringBuilder("");
  17.     StringBuilder tagSb = new StringBuilder("");
  18.    
  19.     int charCount = 0;
  20.     int status = STATUS_READY;
  21.    
  22.     public HtmlShortener(String htmlString, int cutPoint){
  23.         this.cutPoint = cutPoint;
  24.         this.htmlString = htmlString;
  25.     }
  26.    
  27.     public String cut(){
  28.        
  29.         // reset
  30.         tags.clear();
  31.         sb = new StringBuilder("");
  32.         tagSb = new StringBuilder("");
  33.         charCount = 0;
  34.         status = STATUS_READY;
  35.        
  36.         String tag = "";
  37.        
  38.         if (cutPoint < 0){
  39.             return htmlString;
  40.         }
  41.        
  42.         if (null != htmlString){
  43.            
  44.             if (cutPoint == 0){
  45.                 return "";
  46.             }
  47.            
  48.             for (int i = 0; i < htmlString.length(); i++){
  49.                
  50.                 String strC = htmlString.substring(i, i+1);
  51.                
  52.                
  53.                 if (strC.equals("<")){
  54.                
  55.                     // new tag or tag closure
  56.                    
  57.                     // previous tag reset
  58.                     tagSb = new StringBuilder("");
  59.                     tag = "";
  60.                    
  61.                     // find tag type and name
  62.                     for (int k = i; k < htmlString.length(); k++){
  63.                        
  64.                         String tagC = htmlString.substring(k, k+1);
  65.                         tagSb.append(tagC);
  66.                        
  67.                         if (tagC.equals(">")){
  68.                             tag = getTag(tagSb.toString());
  69.                             if (tag.startsWith("/")){
  70.                                
  71.                                 // closure
  72.                                 if (!isToSkip(tag)){
  73.                                     sb.append("</").append(tags.get(tags.size() - 1)).append(">");
  74.                                     tags.remove((tags.size() - 1));
  75.                                 }
  76.                                
  77.                             } else {
  78.                                
  79.                                 // new tag
  80.                                 sb.append(tagSb.toString());
  81.                                
  82.                                 if (!isToSkip(tag)){
  83.                                     tags.add(tag); 
  84.                                 }
  85.                                
  86.                             }
  87.                            
  88.                             i = k;
  89.                             break;
  90.                         }
  91.                        
  92.                     }
  93.                    
  94.                 } else {
  95.                    
  96.                     sb.append(strC);
  97.                     charCount++;
  98.                    
  99.                 }
  100.                
  101.                 // cut check
  102.                 if (charCount >= cutPoint){
  103.                    
  104.                     // close previously open tags
  105.                     Collections.reverse(tags);
  106.                     for (String t : tags){
  107.                         sb.append("</").append(t).append(">");
  108.                     }
  109.                     break;
  110.                 }
  111.                
  112.             }
  113.                    
  114.             return sb.toString();
  115.            
  116.         } else {
  117.             return null;
  118.         }
  119.        
  120.     }
  121.  
  122.     private boolean isToSkip(String tag) {
  123.        
  124.         if (tag.startsWith("/")){
  125.             tag = tag.substring(1, tag.length());
  126.         }
  127.        
  128.         for (String tagToSkip : tagsToSkip){
  129.             if (tagToSkip.equals(tag)){
  130.                 return true;
  131.             }
  132.         }
  133.        
  134.         return false;
  135.     }
  136.  
  137.     private String getTag(String tagString) {
  138.        
  139.         if (tagString.contains(" ")){
  140.             // tag with attributes
  141.             return tagString.substring(tagString.indexOf("<") + 1, tagString.indexOf(" "));
  142.         } else {
  143.             // simple tag
  144.             return tagString.substring(tagString.indexOf("<") + 1, tagString.indexOf(">"));
  145.         }
  146.        
  147.        
  148.     }
  149.  
  150.  
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement