001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.html;
017    
018    import org.apache.commons.lang3.StringUtils;
019    import org.kuali.common.util.Counter;
020    
021    /**
022     * Utility methods for generating html
023     */
024    public class HtmlUtils {
025    
026            public static String getIndentedContent(String content, Counter indent) {
027                    return getIndent(indent) + content;
028            }
029    
030            public static String getIndent(Counter indent) {
031                    return StringUtils.repeat(" ", indent.getValue());
032            }
033    
034            /**
035             * Return an HTML ahref tag
036             */
037            public static String getHref(String dest, String show, Counter indent) {
038                    return getIndent(indent) + "<a href=\"" + dest + "\">" + show + "</a>";
039            }
040    
041            /**
042             * Return an HTML img tag
043             */
044            public static String getImage(String image, Counter indent) {
045                    return getIndent(indent) + "<img src=\"" + image + "\">";
046            }
047    
048            public static String openTag(Tag tag, Counter indent) {
049                    StringBuffer sb = new StringBuffer();
050                    sb.append(getIndent(indent));
051                    indent.increment();
052                    sb.append("<" + tag.getName());
053                    if (tag.getId() != null) {
054                            sb.append(" id=\"" + tag.getId() + '"');
055                    }
056                    if (tag.getClazz() != null) {
057                            sb.append(" class=\"" + tag.getClazz() + '"');
058                    }
059                    sb.append(">\n");
060                    return sb.toString();
061            }
062    
063            public static String closeTag(Tag tag, Counter indent) {
064                    indent.decrement();
065                    return getIndent(indent) + "</" + tag.getName() + ">\n";
066            }
067    
068            public static String getTag(Tag tag, String content, Counter indent) {
069                    StringBuffer sb = new StringBuffer();
070                    sb.append(openTag(tag, indent));
071                    sb.append(getIndent(indent));
072                    sb.append(content);
073                    sb.append("\n");
074                    sb.append(closeTag(tag, indent));
075                    return sb.toString();
076            }
077    }