View Javadoc
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  
22  import org.apache.commons.lang3.StringUtils;
23  import org.jdom2.CDATA;
24  import org.jdom2.Element;
25  import org.jdom2.Text;
26  import org.osgi.annotation.versioning.ProviderType;
27  
28  /**
29   * Html script element.
30   * This script block is initialized by default for "text/javascript" type.
31   *
32   * <p>
33   * Script blocks are generated in a special XHTML conformant way, to work around some browser XHTML JavaScript problems:
34   * </p>
35   *
36   * <pre>
37   * &lt;script type="text/javascript"&gt;
38   * //&lt;![CDATA[
39   * script...
40   * //]]&gt;
41   * &lt;/script&gt;
42   * </pre>
43   */
44  @ProviderType
45  @SuppressWarnings("java:S110") // # parent inheritance
46  public final class Script extends AbstractNonSelfClosingHtmlElement {
47  
48    private static final long serialVersionUID = 1L;
49  
50    private static final String ELEMENT_NAME = "script";
51  
52    private static final String ATTRIBUTE_TYPE = "type";
53    private static final String ATTRIBUTE_SRC = "src";
54  
55    /**
56     * Type "text/javascript".
57     */
58    public static final String TYPE_JAVASCRIPT = "text/javascript";
59  
60    /**
61     * Initializes html element.
62     */
63    public Script() {
64      super(ELEMENT_NAME);
65      setType(TYPE_JAVASCRIPT);
66    }
67  
68    /**
69     * Initializes html element.
70     * @param script Script block
71     */
72    public Script(String script) {
73      this();
74      setText(script);
75    }
76  
77    /**
78     * Html "type" attribute.
79     * @return Value of attribute
80     */
81    public String getType() {
82      return getAttributeValue(ATTRIBUTE_TYPE);
83    }
84  
85    /**
86     * Html "type" attribute.
87     * @param value Value of attribute
88     * @return Self reference
89     */
90    public Script setType(String value) {
91      setAttribute(ATTRIBUTE_TYPE, value);
92      return this;
93    }
94  
95    /**
96     * Html "src" attribute.
97     * @return Value of attribute
98     */
99    public String getSrc() {
100     return getAttributeValue(ATTRIBUTE_SRC);
101   }
102 
103   /**
104    * Html "src" attribute.
105    * @param value Value of attribute
106    * @return Self reference
107    */
108   public Script setSrc(String value) {
109     setAttribute(ATTRIBUTE_SRC, value);
110     return this;
111   }
112 
113   /**
114    * {@inheritDoc} Overrides standard setText method to add special xHTML conformant CDATA block
115    * as workaround for browsers that does not interpret XHTML-encoded script blocks correctly.
116    */
117   @Override
118   public Element setText(String script) {
119     if (StringUtils.isEmpty(script)) {
120       super.setText(script);
121     }
122     else {
123       this.addContent(new Text("\n//"));
124 
125       // encapsulate script block in CDATA block
126       this.addContent(new CDATA("\n"
127           + script
128           + "\n//"));
129 
130       this.addContent("\n");
131     }
132     return this;
133   }
134 
135   // -- overwrite methods for builder pattern with covariant return types --
136 
137   @Override
138   protected Script setEmptyAttributeValueAsBoolean(String attributeName, boolean value) {
139     return (Script)super.setEmptyAttributeValueAsBoolean(attributeName, value);
140   }
141 
142   @Override
143   public Script setId(String value) {
144     return (Script)super.setId(value);
145   }
146 
147   @Override
148   public Script setCssClass(String value) {
149     return (Script)super.setCssClass(value);
150   }
151 
152   @Override
153   public Script addCssClass(String value) {
154     return (Script)super.addCssClass(value);
155   }
156 
157   @Override
158   public Script setStyleString(String value) {
159     return (Script)super.setStyleString(value);
160   }
161 
162   @Override
163   public Script setStyle(String styleAttribute, String styleValue) {
164     return (Script)super.setStyle(styleAttribute, styleValue);
165   }
166 
167   @Override
168   public Script setTitle(String value) {
169     return (Script)super.setTitle(value);
170   }
171 
172   @Override
173   public Script setData(String attributeName, String value) {
174     return (Script)super.setData(attributeName, value);
175   }
176 
177   @Override
178   public Script setAttributeValueAsLong(String name, long value) {
179     return (Script)super.setAttributeValueAsLong(name, value);
180   }
181 
182 }