All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.html.AbstractElement Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.html;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import br.com.objectos.core.io.File;
import br.com.objectos.html.io.HtmlWritable;
import br.com.objectos.html.io.HtmlWriter;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class AbstractElement implements Element {

  private final String tagName;
  private final ContentModel contentModel;

  private final Map attributeMap = new LinkedHashMap<>(5);
  private final List childList = new ArrayList<>();

  AbstractElement(String tagName, ContentModel contentModel) {
    this.tagName = tagName;
    this.contentModel = contentModel;
  }

  public E comment(String text) {
    Objects.requireNonNull(text);
    childList.add(new Comment(text));
    return self();
  }

  @Override
  public Element end() {
    return self();
  }

  public E text(String text) {
    Objects.requireNonNull(text);
    childList.add(new Text(text));
    return self();
  }

  @Override
  public final String toString() {
    StringBuilder out = new StringBuilder();
    HtmlWriter writer = HtmlWriter.of(out);
    writeTo(writer);
    return out.toString();
  }

  @Override
  public void writeTo(File file) {
    try (HtmlWriter writer = HtmlWriter.of(file.openWriter())) {
      writeTo(writer);
    }
  }

  @Override
  public void writeTo(HtmlWriter writer) {
    if (selfClosing()) {
      writeSelfClosing(writer);
    } else {
      writeStandard(writer);
    }
  }

   R addElement(R element) {
    childList.add(element);
    return element;
  }

  E addManyElements(Iterable children) {
    Objects.requireNonNull(children);
    for (Element child : children) {
      Objects.requireNonNull(child);
      childList.add(child);
    }
    return self();
  }

  E addOneElement(Element child) {
    Objects.requireNonNull(child);
    childList.add(child);
    return self();
  }

  void attr(String name, boolean value) {
    attr(name, Boolean.toString(value));
  }

  void attr(String name, int value) {
    attr(name, Integer.toString(value));
  }

  void attr(String name, Enum value) {
    attr(name, value.toString());
  }

  void attr(String name, String value) {
    Objects.requireNonNull(value);
    attributeMap.put(name, value);
  }

  abstract E self();

  boolean selfClosing() {
    return ContentModel.VOID.equals(contentModel);
  }

  private void writeSelfClosing(HtmlWriter writer) {
    writer.openSelfClosingTag(tagName).writeAttributeMap(attributeMap).closeTag();
  }

  private void writeStandard(HtmlWriter writer) {
    writer.openTag(tagName).writeAttributeMap(attributeMap).writeAll(childList).closeTag();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy