Coverage Report - net.fckeditor.tool.XHtmlTagTool
 
Classes in this File Line Coverage Branch Coverage Complexity
XHtmlTagTool
60%
15/25
50%
4/8
2,2
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2004-2010 Frederico Caldeira Knabben
 4  
  * 
 5  
  * == BEGIN LICENSE ==
 6  
  * 
 7  
  * Licensed under the terms of any of the following licenses at your
 8  
  * choice:
 9  
  * 
 10  
  *  - GNU General Public License Version 2 or later (the "GPL")
 11  
  *    http://www.gnu.org/licenses/gpl.html
 12  
  * 
 13  
  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  
  *    http://www.gnu.org/licenses/lgpl.html
 15  
  * 
 16  
  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  
  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  
  * 
 19  
  * == END LICENSE ==
 20  
  */
 21  
 package net.fckeditor.tool;
 22  
 
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * Java representation of an XHTML tag.
 28  
  * <p>
 29  
  * Usage:
 30  
  * <pre>
 31  
  * XHtmlTagTool tag = XHtmlTagTool(&quot;a&quot;, &quot;link&quot;);
 32  
  * tag.addAttribute(&quot;href&quot;, &quot;http://google.com&quot;);
 33  
  * tag.toString();: &lt;a href=&quot;http://google.com&quot;&gt;link&lt;/a&gt;
 34  
  * </pre>
 35  
  * </p>
 36  
  * <em>Note</em>:
 37  
  * <ul>
 38  
  * <li>Attributes are not ordered.</li>
 39  
  * <li>If you want to avoid self-closing tags without supplying a value, set
 40  
  * {@link #SPACE} as the tag's value.</li>
 41  
  * </ul>
 42  
  * 
 43  
  * @version $Id: XHtmlTagTool.java 4785 2009-12-21 20:10:28Z mosipov $
 44  
  */
 45  
 public class XHtmlTagTool {
 46  
 
 47  
         /** Name of the tag. */
 48  
         private String name;
 49  
 
 50  
         /** Container for the attributes. */
 51  4
         private Map<String, String> attributes = new HashMap<String, String>();
 52  
 
 53  
         /** Value of the tag. */
 54  
         private String value;
 55  
 
 56  
         /** Indicator to uses non self-closing tag. */
 57  
         public static final String SPACE = " ";
 58  
 
 59  
         /**
 60  
          * Class constructor with tag name.
 61  
          * 
 62  
          * @param name
 63  
          *            the tag name of the new XHtmlTagTool
 64  
          * @throws IllegalArgumentException
 65  
          *             if <code>name</code> is empty
 66  
          */
 67  4
         public XHtmlTagTool(final String name) {
 68  4
                 if (Utils.isEmpty(name))
 69  0
                         throw new IllegalArgumentException(
 70  
                                         "Parameter 'name' shouldn't be empty!");
 71  4
                 this.name = name;
 72  4
         }
 73  
 
 74  
         /**
 75  
          * Class constructor with name and value.
 76  
          * 
 77  
          * @param name
 78  
          *             the tag name of the new XHtmlTagTool
 79  
          * @param value
 80  
          *            the tag value of the new XHtmlTagTool which is the tag body
 81  
          * @throws IllegalArgumentException
 82  
          *             if <code>name</code> is empty
 83  
          */
 84  
         public XHtmlTagTool(final String name, final String value) {
 85  3
                 this(name);
 86  3
                 this.value = value;
 87  3
         }
 88  
 
 89  
         /**
 90  
          * Sets the tag value.
 91  
          * 
 92  
          * @param value
 93  
          *            the tag value which is the tag body
 94  
          */
 95  
         public void setValue(final String value) {
 96  0
                 this.value = value;
 97  0
         }
 98  
 
 99  
         /**
 100  
          * Adds an attribute to this tag.
 101  
          * 
 102  
          * @param name
 103  
          *            attribute name
 104  
          * @param value
 105  
          *            attribute value
 106  
          * @throws IllegalArgumentException
 107  
          *             if <code>name</code> is empty
 108  
          */
 109  
         public void addAttribute(final String name, final String value) {
 110  0
                 if (Utils.isEmpty(name))
 111  0
                         throw new IllegalArgumentException("Name is null or empty");
 112  0
                 attributes.put(name, value);
 113  0
         }
 114  
 
 115  
         /**
 116  
          * Creates the HTML representation of this tag. It follows the XHTML
 117  
          * standard.
 118  
          * 
 119  
          * @return HTML representation of this tag
 120  
          */
 121  
         @Override
 122  
         public String toString() {
 123  4
                 StringBuffer tag = new StringBuffer();
 124  
 
 125  
                 // open tag
 126  4
                 tag.append("<").append(name);
 127  
 
 128  
                 // add attributes
 129  4
                 for (String key : attributes.keySet()) {
 130  0
                         String val = attributes.get(key);
 131  0
                         tag.append(' ').append(key).append('=').append('\"').append(val)
 132  
                                         .append('\"');
 133  0
                 }
 134  
 
 135  
                 // close the tag
 136  4
                 if (Utils.isNotEmpty(value)) {
 137  2
                         tag.append(">").append(value).append("</").append(name).append('>');
 138  
                 } else
 139  2
                         tag.append(" />");
 140  
 
 141  4
                 return tag.toString();
 142  
         }
 143  
 
 144  
 }