View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.html;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.kuali.common.util.Counter;
20  
21  /**
22   * Utility methods for generating html
23   */
24  public class HtmlUtils {
25  
26  	public static String getIndentedContent(String content, Counter indent) {
27  		return getIndent(indent) + content;
28  	}
29  
30  	public static String getIndent(Counter indent) {
31  		return StringUtils.repeat(" ", indent.getValue());
32  	}
33  
34  	/**
35  	 * Return an HTML ahref tag
36  	 */
37  	public static String getHref(String dest, String show, Counter indent) {
38  		return getIndent(indent) + "<a href=\"" + dest + "\">" + show + "</a>";
39  	}
40  
41  	/**
42  	 * Return an HTML img tag
43  	 */
44  	public static String getImage(String image, Counter indent) {
45  		return getIndent(indent) + "<img src=\"" + image + "\">";
46  	}
47  
48  	public static String openTag(Tag tag, Counter indent) {
49  		StringBuffer sb = new StringBuffer();
50  		sb.append(getIndent(indent));
51  		indent.increment();
52  		sb.append("<" + tag.getName());
53  		if (tag.getId() != null) {
54  			sb.append(" id=\"" + tag.getId() + '"');
55  		}
56  		if (tag.getClazz() != null) {
57  			sb.append(" class=\"" + tag.getClazz() + '"');
58  		}
59  		sb.append(">\n");
60  		return sb.toString();
61  	}
62  
63  	public static String closeTag(Tag tag, Counter indent) {
64  		indent.decrement();
65  		return getIndent(indent) + "</" + tag.getName() + ">\n";
66  	}
67  
68  	public static String getTag(Tag tag, String content, Counter indent) {
69  		StringBuffer sb = new StringBuffer();
70  		sb.append(openTag(tag, indent));
71  		sb.append(getIndent(indent));
72  		sb.append(content);
73  		sb.append("\n");
74  		sb.append(closeTag(tag, indent));
75  		return sb.toString();
76  	}
77  }