net.metanotion.web.html.Tag.java Source code

Java tutorial

Introduction

Here is the source code for net.metanotion.web.html.Tag.java

Source

/***************************************************************************
   Copyright 2014 Emily Estes
    
   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 net.metanotion.web.html;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.text.StringEscapeUtils;

import net.metanotion.util.ChainedIterator;
import net.metanotion.util.DictionaryIndex;
import net.metanotion.util.MapDictionaryAdapter;

public final class Tag implements Html<Tag> {
    private final String tagName;
    private final DictionaryIndex<String, Object> attributes;
    private final Iterable<Object> children;
    private final boolean forceSeparateCloseTag;

    public static Iterator<Object> writeTag(String tagName, Map<String, Object> attributes, boolean selfClose) {
        final ArrayList<Object> t = new ArrayList<>((attributes.size() * 5) + 2);
        t.add("<" + tagName);
        for (final Map.Entry<String, Object> e : attributes.entrySet()) {
            t.add(" ");
            t.add(e.getKey());
            final Object val = e.getValue();
            if (val != null) {
                t.add("=\"");
                t.add(StringEscapeUtils.escapeHtml4(val.toString()));
                t.add("\"");
            }
        }
        t.add(selfClose ? "/>" : ">");
        return t.iterator();
    }

    public Tag(final String tagName) {
        this(tagName, new MapDictionaryAdapter<String, Object>(new HashMap<String, Object>()), new ArrayList<>(0),
                false);
    }

    public Tag(final String tagName, final DictionaryIndex<String, Object> attributes,
            final Iterable<? extends Object> children) {
        this(tagName, attributes, children, false);
    }

    public Tag(final String tagName, final DictionaryIndex<String, Object> attributes,
            final Iterable<? extends Object> children, final boolean forceSeparateCloseTag) {
        this.tagName = tagName;
        this.attributes = attributes;
        this.children = (Iterable<Object>) children;
        this.forceSeparateCloseTag = forceSeparateCloseTag;
    }

    @Override
    public Tag add(Object child) {
        final ArrayList<Object> newChildren = new ArrayList<>();
        for (final Object h : children) {
            newChildren.add(h);
        }
        newChildren.add(child);
        return new Tag(tagName, attributes, newChildren);
    }

    public Tag addAll(Iterable<? extends Object> more) {
        final ArrayList<Object> newChildren = new ArrayList<>();
        for (final Object h : children) {
            newChildren.add(h);
        }
        for (final Object h : more) {
            newChildren.add(h);
        }
        return new Tag(tagName, attributes, newChildren);
    }

    public Tag attribute(String attrib) {
        return this.attribute(attrib, null);
    }

    @Override
    public Tag attribute(String attrib, Object value) {
        final Map<String, Object> newAttributes = new HashMap<String, Object>();
        for (final String key : attributes.keySet()) {
            newAttributes.put(key, attributes.get(key));
        }
        newAttributes.put(attrib, value);
        return new Tag(tagName, new MapDictionaryAdapter<String, Object>(newAttributes), children);
    }

    @Override
    public String tag() {
        return tagName;
    }

    @Override
    public Object get(final String attribute) {
        return attributes.get(attribute);
    }

    @Override
    public Set<String> keySet() {
        return attributes.keySet();
    }

    @Override
    public Iterator<Object> iterator() {
        final Map<String, Object> theAttributes = new HashMap<String, Object>();
        for (final String key : attributes.keySet()) {
            theAttributes.put(key, attributes.get(key));
        }
        if (children.iterator().hasNext()) {
            return new ChainedIterator<Object>(Arrays.asList(writeTag(tagName, theAttributes, false),
                    children.iterator(), Arrays.asList((Object) "</", tagName, ">").iterator()));
        } else if (forceSeparateCloseTag) {
            return new ChainedIterator<Object>(Arrays.asList(writeTag(tagName, theAttributes, false),
                    Arrays.asList((Object) "</", tagName, ">").iterator()));
        } else {
            return writeTag(tagName, theAttributes, true);
        }
    }

    @Override
    public Iterator<Object> children() {
        return children.iterator();
    }
}