AbstractHtmlElementFactory.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.commons.dom;

  21. import org.osgi.annotation.versioning.ConsumerType;


  22. /**
  23.  * Contains factory methods for creating and adding Html elements and specialized types.
  24.  * This class cannot be instanciated directly, but provides factory methods for HtmlElement-based classes.
  25.  */
  26. @ConsumerType
  27. public abstract class AbstractHtmlElementFactory extends AbstractElement {
  28.   private static final long serialVersionUID = 1L;

  29.   /**
  30.    * Initializes html element factory.
  31.    * @param name Element name
  32.    */
  33.   protected AbstractHtmlElementFactory(String name) {
  34.     super(name);
  35.   }

  36.   /**
  37.    * Creates and adds html element.
  38.    * @param elementName Element name
  39.    * @return Html element.
  40.    */
  41.   public final HtmlElement create(String elementName) {
  42.     return this.add(new HtmlElement(elementName));
  43.   }

  44.   /**
  45.    * Creates and adds html comment.
  46.    * @param text Comment
  47.    * @return Html comment.
  48.    */
  49.   public final HtmlComment createComment(String text) {
  50.     HtmlComment comment = new HtmlComment(text);
  51.     this.addContent(comment);
  52.     return comment;
  53.   }

  54.   /**
  55.    * Creates and adds div element.
  56.    * @return Html element.
  57.    */
  58.   public final Div createDiv() {
  59.     return this.add(new Div());
  60.   }

  61.   /**
  62.    * Creates and adds span element.
  63.    * @return Html element.
  64.    */
  65.   public final Span createSpan() {
  66.     return this.add(new Span());
  67.   }

  68.   /**
  69.    * Creates and adds span element.
  70.    * @param text Text
  71.    * @return Html element.
  72.    */
  73.   public final Span createSpan(String text) {
  74.     return this.add(new Span(text));
  75.   }

  76.   /**
  77.    * Creates and adds anchor (a) element.
  78.    * @return Html element.
  79.    */
  80.   public final Anchor createAnchor() {
  81.     return this.add(new Anchor());
  82.   }

  83.   /**
  84.    * Creates and adds anchor (a) element.
  85.    * @param href Html "href" attribute.
  86.    * @return Html element.
  87.    */
  88.   public final Anchor createAnchor(String href) {
  89.     return this.add(new Anchor(href));
  90.   }

  91.   /**
  92.    * Creates and adds anchor (a) element.
  93.    * @param href Html "href" attribute.
  94.    * @param target Html "target" attribute.
  95.    * @return Html element.
  96.    */
  97.   public final Anchor createAnchor(String href, String target) {
  98.     return this.add(new Anchor(href, target));
  99.   }

  100.   /**
  101.    * Creates and adds imgage (img) element.
  102.    * @return Html element.
  103.    */
  104.   public final Image createImage() {
  105.     return this.add(new Image());
  106.   }

  107.   /**
  108.    * Creates and adds imgage (img) element.
  109.    * @param src Html "src" attribute.
  110.    * @return Html element.
  111.    */
  112.   public final Image createImage(String src) {
  113.     return this.add(new Image(src));
  114.   }

  115.   /**
  116.    * Creates and adds imgage (img) element.
  117.    * @param src Html "src" attribute.
  118.    * @param alt Html "alt" attribute.
  119.    * @return Html element.
  120.    */
  121.   public final Image createImage(String src, String alt) {
  122.     return this.add(new Image(src, alt));
  123.   }

  124.   /**
  125.    * Creates and adds imgage (img) element.
  126.    * @param src Html "src" attribute.
  127.    * @param width Html "width" attribute.
  128.    * @param height Html "height" attribute.
  129.    * @return Html element.
  130.    */
  131.   public final Image createImage(String src, int width, int height) {
  132.     return this.add(new Image(src, width, height));
  133.   }

  134.   /**
  135.    * Creates and adds imgage (img) element.
  136.    * @param src Html "src" attribute.
  137.    * @param alt Html "alt" attribute.
  138.    * @param width Html "width" attribute.
  139.    * @param height Html "height" attribute.
  140.    * @return Html element.
  141.    */
  142.   public final Image createImage(String src, String alt, int width, int height) {
  143.     return this.add(new Image(src, alt, width, height));
  144.   }

  145.   /**
  146.    * Creates and adds script element.
  147.    * @return Html element.
  148.    */
  149.   public final Script createScript() {
  150.     return this.add(new Script());
  151.   }

  152.   /**
  153.    * Creates and adds script element.
  154.    * @param script Script block
  155.    * @return Html element.
  156.    */
  157.   public final Script createScript(String script) {
  158.     return this.add(new Script(script));
  159.   }

  160.   /**
  161.    * Creates and adds noscript element.
  162.    * @return Html element.
  163.    */
  164.   public final NoScript createNoScript() {
  165.     return this.add(new NoScript());
  166.   }

  167.   /**
  168.    * Creates and adds figure element.
  169.    * @return Html element.
  170.    */
  171.   public final Figure createFigure() {
  172.     return this.add(new Figure());
  173.   }

  174.   /**
  175.    * Creates and adds figure caption element.
  176.    * @return Html element.
  177.    */
  178.   public final FigCaption createFigCaption() {
  179.     return this.add(new FigCaption());
  180.   }

  181.   /**
  182.    * Creates and adds video element.
  183.    * @return Html element.
  184.    */
  185.   public final Video createVideo() {
  186.     return this.add(new Video());
  187.   }

  188.   /**
  189.    * Creates and adds audio element.
  190.    * @return Html element.
  191.    */
  192.   public final Audio createAudio() {
  193.     return this.add(new Audio());
  194.   }

  195.   /**
  196.    * Creates and adds source element.
  197.    * @return Html element.
  198.    */
  199.   public final Source createSource() {
  200.     return this.add(new Source());
  201.   }

  202. }