/**
 * Copyright (c) 2015 deniskir@gmail.com. All rights reserved.
 *
 * @author Denis Kiriusin
 */

package com.dksoftware.combo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * Simple JSP tag to constructs URL. The tag has four attributes including <tt>type</tt>, <tt>path</tt>, <tt>csv_resources</tt> and <tt>theme</tt>.
 * <br><br>
 * <b>Usage example</b>:
 * 
 * <pre>
 * <cb:combo type="css" path="${path}">
 *     <jsp:attribute name="theme">${theme}</jsp:attribute>
 *     <jsp:attribute name="csv_resources">${resources}</jsp:attribute>
 * </cb:combo>
 * </pre>
 */
public class ComboTag extends SimpleTagSupport {

	private String type;
	private String path;
	private String csv_resources;
	private String theme;

	@Override
	public void doTag() throws JspException, IOException {
		getJspContext().getOut().write(constructHTMLTag());
	}

	/**
	 * Constructs CSS or JavaScript tag string.<br>
	 * 
	 * <pre>
	 *{@code
	 *<link href="/combo/&type=css&resources=optional_css/opt_css_1.css&theme=blue&v=1443201442000" rel="stylesheet" type="text/css" />
	 *}
	 *{@code
	 *<script type="text/javascript" src="/combo/&type=js&v=1443201442000"></script>
	 *}
	 * </pre>
	 * 
	 * @return tag as a String
	 * @throws UnsupportedEncodingException
	 */
	String constructHTMLTag() throws UnsupportedEncodingException {
		StringBuilder sb = new StringBuilder();
		if (getType().equalsIgnoreCase("css")) {
			sb.append("<link href=\"");
			sb.append(getPath());

			final String url = constructURLAttributes();
			sb.append(url);

			sb.append(" rel=\"stylesheet\"");
			sb.append(" type=\"");
			sb.append(MimeType.css.getName());
			sb.append("\" />");
		} else if (getType().equalsIgnoreCase("js")) {
			sb.append("<script type=\"");
			sb.append(MimeType.js.getName());
			sb.append("\"");
			sb.append(" src=\"");
			sb.append(getPath());

			final String url = constructURLAttributes();
			sb.append(url);

			sb.append("></script>");
		}
		return sb.toString();
	}

	/**
	 * Constructs URL parameters in the following format: <br>
	 * <code>resources=file_1.css,embedded/file_2.css&theme=green&v=16546547</code>.
	 * 
	 * @throws UnsupportedEncodingException
	 */
	String constructURLAttributes() throws UnsupportedEncodingException {
		StringBuilder sb = new StringBuilder();
		sb.append("&type=");
		sb.append(URLEncoder.encode(getType(), "UTF-8"));
		if (getResources() != null && getResources() != "") {
			sb.append("&resources=");
			sb.append(getResources());
		}
		if (getTheme() != null && getTheme() != "") {
			sb.append("&theme=");
			sb.append(URLEncoder.encode(getTheme(), "UTF-8"));
		}
		PageContext pageContext = (PageContext) getJspContext();
		ServletContext servletContext = pageContext.getServletContext();
		Long v = (Long) servletContext.getAttribute("com.dksoftware.combo.v_" + getType());
		if (v == null) {
			v = System.currentTimeMillis();
		}
		sb.append("&v=");
		sb.append(v.toString());
		sb.append("\"");
		return sb.toString();
	}

	// getters and setters

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getResources() {
		return csv_resources;
	}

	public void setCsv_resources(String csv_resources) {
		this.csv_resources = csv_resources;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

}

@@ -597,17 +597,20 @@
 pre%3E%0A * 
-%3C
+&lt;
 cb:combo
@@ -635,25 +635,28 @@
 $%7Bpath%7D%22
-%3E
+&gt;
 %0A *     
 %3Cjsp:att
@@ -639,33 +639,36 @@
 th%7D%22&gt;%0A *     
-%3C
+&lt;
 jsp:attribute na
@@ -681,17 +681,20 @@
 eme%22
-%3E
+&gt;
 $%7Btheme%7D
 %3C/js
@@ -689,17 +689,20 @@
 $%7Btheme%7D
-%3C
+&lt;
 /jsp:att
@@ -711,17 +711,20 @@
 bute
-%3E
+&gt;
 %0A *     
 %3Cjsp
@@ -719,17 +719,20 @@
 %0A *     
-%3C
+&lt;
 jsp:attr
@@ -757,17 +757,20 @@
 sources%22
-%3E
+&gt;
 $%7Bresour
@@ -773,17 +773,20 @@
 sources%7D
-%3C
+&lt;
 /jsp:att
@@ -791,22 +791,28 @@
 ttribute
-%3E%0A * %3C
+&gt;%0A * &lt;
 /cb:comb
@@ -812,17 +812,20 @@
 cb:combo
-%3E
+&gt;
 %0A * %3C/pr

Improve ComboTag JavaDocs