001    /*
002     *  Copyright 2012 GWT-Bootstrap
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.github.gwtbootstrap.client.ui.resources;
017    
018    import com.google.gwt.dom.client.Document;
019    import com.google.gwt.dom.client.HeadElement;
020    import com.google.gwt.dom.client.ScriptElement;
021    
022    /**
023     * Methods to inject JavaScript code into the document header.
024     * 
025     * @since 2.0.4.0
026     * 
027     * @author Carlos Alexandro Becker
028     */
029    public class JavaScriptInjector extends AbstractInjector {
030    
031            /**
032             * Injects the JavaScript code into a
033             * {@code <script type="text/javascript">...</script>} element in the
034             * document header.
035             * 
036             * @param javascript
037             *            the JavaScript code
038             */
039            public static void inject(String javascript) {
040                    HeadElement head = getHead();
041                    ScriptElement element = createScriptElement();
042                    element.setText(javascript);
043                    head.appendChild(element);
044            }
045    
046            private static ScriptElement createScriptElement() {
047                    ScriptElement script = Document.get().createScriptElement();
048                    script.setAttribute("type", "text/javascript");
049            script.setAttribute("charset", "UTF-8");
050                    return script;
051            }
052    
053    }