Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.cdlflex.ui.markup.html;
 
 import java.util.List;
 import java.util.Map;

Helper class to build markup tag strings.
 
 public class Tag implements Serializable {
 
     private static final long serialVersionUID = 1L;
 
     private CharSequence name;
     private Map<StringObjectattributes;
     private List<Tagchildren;
 
     private boolean shortTag;
 
     public Tag(CharSequence name) {
         this(namenew HashMap<StringObject>(), (Tagnull);
     }
 
     public Tag(CharSequence nameCharSequence body) {
         this(namenew HashMap<StringObject>(), body);
     }
 
     public Tag(CharSequence nameTag body) {
         this(namenew HashMap<StringObject>(), body);
     }
 
     public Tag(CharSequence nameMap<StringObjectattributesCharSequence body) {
         this(nameattributesnew TextNode(body));
     }
 
     public Tag(CharSequence nameMap<StringObjectattributesTag body) {
         this. = name;
         this. = attributes;
         this. = new ArrayList<>();
         add(body);
     }

    
Adds a new attribute with the given key and value.

Parameters:
key attribute key
value attribute value
Returns:
this for chaining
 
     public Tag attr(String keyObject value) {
         .put(keyvalue);
         return this;
     }

    
Adds a new attribute with the given key and value if the given condition is true.

Parameters:
key attribute key
value attribute value
condition condition
Returns:
this for chaining
 
     public Tag attr(String keyObject valueboolean condition) {
         return condition ? attr(keyvalue) : this;
     }

    
Adds all entries of the given map as attributes to this tag.

Parameters:
attributeMap a map containing key/value pairs.
Returns:
this for chaining
 
     public Tag attrs(Map<? extends String, ?> attributeMap) {
         if (attributeMap != null) {
             for (Map.Entry<? extends String, ?> entry : attributeMap.entrySet()) {
                 attr(entry.getKey(), entry.getValue());
             }
         }
 
         return this;
     }

    
Adds the given char sequence as a text node as child.

Parameters:
text the child as string
Returns:
this for chaining
    public Tag add(CharSequence text) {
        return add(new TextNode(text));
    }

    
Adds the given Tag as child.

Parameters:
child the child node
Returns:
this for chaining
    public Tag add(Tag child) {
        if (child != null) {
            getChildren().add(child);
        }
        return this;
    }

    
Sets whether the tag should be rendered as short tag.

A short tag is for example: <script/> vs. <script></script>

Parameters:
isShortTag a flag
Returns:
this for chaining
    public Tag setShortTag(boolean isShortTag) {
        this. = isShortTag;
        return this;
    }
    public boolean isShortTag() {
        return ;
    }
    public List<TaggetChildren() {
        return ;
    }
    public Map<StringObjectgetAttributes() {
        return ;
    }
    public CharSequence getName() {
        return ;
    }

    
Generates Markup code from the current tag.

Returns:
a markup string
    public String toMarkup() {
        StringBuilder str = new StringBuilder();
        str.append("<").append();
        appendAttributes(str);
        if (isShortTag()) {
            str.append("/>");
        } else {
            str.append(">");
            List<TagchildrenTags = getChildren();
            if (childrenTags != null && !childrenTags.isEmpty()) {
                join(childrenTags""str);
            }
            str.append("</").append().append(">");
        }
        return str.toString();
    }
    @Override
    public String toString() {
        return toMarkup();
    }
    private void appendAttributes(StringBuilder str) {
        for (Map.Entry<StringObjectentry : .entrySet()) {
            str.append(" ");
            appendAttribute(entry.getKey(), entry.getValue(), str);
        }
    }
    private void appendAttribute(String keyObject valueStringBuilder str) {
        str.append(key).append("=").append('"');
        appendAttributeValue(valuestr);
        str.append('"');
    }
    private void appendAttributeValue(Object valueStringBuilder str) {
        if (value.getClass().isArray()) {
            join((Object[]) value" "str);
        } else if (value instanceof Iterable) {
            join((Iterable<?>) value" "str);
        } else {
            str.append(String.valueOf(value));
        }
    }
    private void join(Iterator<?> iteratorString separatorStringBuilder str) {
        while (iterator.hasNext()) {
            str.append(String.valueOf(iterator.next()));
            if (iterator.hasNext()) {
                str.append(separator);
            }
        }
    }
    private void join(Iterable<?> collectionString separatorStringBuilder str) {
        join(collection.iterator(), separatorstr);
    }
    private void join(Object[] arrayString separatorStringBuilder str) {
        if (array.length == 0) {
            return;
        }
        int i = 0;
        for (; i < (array.length - 1); i++) {
            str.append(String.valueOf(array[i]));
            str.append(separator);
        }
        str.append(String.valueOf(array[i]));
    }

    
Special case Tag that only consists of text.
    public static class TextNode extends Tag {
        private static final long serialVersionUID = 1L;
        public TextNode(CharSequence text) {
            super(text);
        }
        @Override
        public String toMarkup() {
            return String.valueOf(getName());
        }
    }
New to GrepCode? Check out our FAQ X