package ru.org.linux.site.tags;

import ru.org.linux.tag.TagService;
import ru.org.linux.topic.TopicListController;
import ru.org.linux.util.StringUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.List;

/**
 * tags tag
 */
public class TagsTag extends TagSupport {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }


    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        if(list != null) {
            try {
                StringBuilder buf = new StringBuilder();
                for(String el : list) {
                    if(buf.length() > 0) {
                        buf.append(", ");
                    }
                    if(TagService.isGoodTag(el)) {
                        buf
                            .append("<a class=tag rel=tag href=\"")
                            .append(TopicListController.tagListUrl(el))
                            .append("\">")
                            .append(StringUtil.escapeHtml(el))
                            .append("</a>");
                    } else {
                        buf.append(StringUtil.escapeHtml(el));
                    }
                }
                out
                    .append("<p class=\"tags\">Метки: ")
                    .append(buf)
                    .append("</p>");
            } catch (IOException e) {
                throw new JspException("Error:" + e.getMessage());
            }
        }
        return SKIP_BODY;
    }
}

@@ -1,8 +1,633 @@
+/*%0A * Copyright 1998-2012 Linux.org.ru%0A *    Licensed under the Apache License, Version 2.0 (the %22License%22);%0A *    you may not use this file except in compliance with the License.%0A *    You may obtain a copy of the License at%0A *%0A *        http://www.apache.org/licenses/LICENSE-2.0%0A *%0A *    Unless required by applicable law or agreed to in writing, software%0A *    distributed under the License is distributed on an %22AS IS%22 BASIS,%0A *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A *    See the License for the specific language governing permissions and%0A *    limitations under the License.%0A */%0A
 package 
@@ -1005,18 +1005,16 @@
 pport %7B%0A
-  
   privat
@@ -1035,18 +1035,16 @@
  list;%0A%0A
-  
   public
@@ -1082,20 +1082,16 @@
 ) %7B%0A    
-  
-  
 this.lis
@@ -1104,18 +1104,16 @@
 st;%0A
-  
   %7D%0A%0A%0A  
   @O
@@ -1108,18 +1108,16 @@
   %7D%0A%0A%0A  
- 
- 
 @Overrid
@@ -1118,18 +1118,16 @@
 verride%0A
-  
   public
@@ -1170,20 +1170,16 @@
 n %7B%0A    
-  
-  
 JspWrite
@@ -1212,22 +1212,19 @@
 ();%0A    
-   
- 
 if
+ 
 (list !=
@@ -1228,30 +1228,24 @@
  != null) %7B%0A
-      
       try %7B%0A
@@ -1252,24 +1252,16 @@
         
-   
-     
 StringBu
@@ -1305,19 +1305,12 @@
     
-     
-   
 for
+ 
 (Str
@@ -1322,34 +1322,24 @@
 l : list) %7B%0A
-          
           if
@@ -1338,16 +1338,17 @@
       if
+ 
 (buf.len
@@ -1356,36 +1356,24 @@
 th() %3E 0) %7B%0A
-            
             
@@ -1400,33 +1400,18 @@
         
- 
-         %7D%0A     
+%7D%0A
         
@@ -1412,23 +1412,19 @@
         
-     
 if
+ 
 (TagServ
@@ -1460,36 +1460,12 @@
     
-        
-    buf%0A            
+buf%0A
     
@@ -1516,28 +1516,16 @@
 ef=%5C%22%22)%0A
-            
         
@@ -1588,36 +1588,24 @@
             
-            
 .append(%22%5C%22%3E
@@ -1607,28 +1607,16 @@
 (%22%5C%22%3E%22)%0A
-            
         
@@ -1670,36 +1670,24 @@
             
-   
-         
 .append(%22%3C/a
@@ -1701,26 +1701,16 @@
         
-     
-     
 %7D else %7B
@@ -1722,28 +1722,16 @@
         
-         
-   
 buf.appe
@@ -1775,36 +1775,8 @@
     
-          %7D%0A                
 %7D%0A  
@@ -1780,24 +1780,27 @@
 %0A       
+ %7D%0A
         
  out%0A   
@@ -1795,21 +1795,12 @@
     
- 
 out%0A
-        
     
@@ -1852,32 +1852,24 @@
             
-     
-   
 .append(buf)
@@ -1869,24 +1869,16 @@
 nd(buf)%0A
-        
         
@@ -1904,22 +1904,16 @@
 ;%0A      
-   
-   
 %7D catch 
@@ -1926,32 +1926,24 @@
 eption e) %7B%0A
-        
         thro
@@ -1995,38 +1995,24 @@
 ;%0A      
-   
-   %7D%0A   
+%7D%0A
     
- 
 %7D%0A
-    
     retu
@@ -2025,16 +2025,14 @@
 P_BODY;%0A
-  
   %7D%0A%7D%0A

reformat and copyright TagsTag code