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

com.hfg.html.Script Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.html;

import com.hfg.xml.XMLComment;
import com.hfg.xml.XMLCDATA;
import com.hfg.util.mime.MimeType;
import com.hfg.util.StringUtil;

import java.nio.charset.Charset;

//------------------------------------------------------------------------------
/**
 * Represents a script (<script>) tag.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

/*
For XHTML or XML the script should be escaped like so:


 However, older browser (such as IE5 on Mac) won't take kindly to this.
 IE6 doesn't seem to like such escaping in returned AJAX content.
*/
public class Script extends HTMLTag
{
   private boolean  mUseCDATA = true;
   private XMLCDATA mCDATA;

   //##########################################################################
   // PUBLIC FIELDS
   //##########################################################################

   public static final String JAVASCRIPT    = "JavaScript";
   public static final String JAVASCRIPT1_1 = "JavaScript1.1";
   public static final String JAVASCRIPT1_2 = "JavaScript1.2";
   public static final String JAVASCRIPT1_3 = "JavaScript1.3";
   public static final String VBSCRIPT      = "VBScript";

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public Script()
   {
      super(HTML.SCRIPT);
      // Force the use of a closing tag. Some browsers will barf if they see an
      // empty script tag in the HTML. Not necessary for XHTML but oh, well.
      setContent("");
   }

   //--------------------------------------------------------------------------
   public Script(MimeType inType)
   {
      this();
      setType(inType);
   }

   //--------------------------------------------------------------------------
   public Script(String inContent)
   {
      this();
      addContent(inContent);
   }

   //--------------------------------------------------------------------------
   public Script(CharSequence inContent)
   {
      this();
      addContent(inContent);
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   /**
    Defaults to true.
    */
   public Script useCDATA(boolean inValue)
   {
      if (inValue != mUseCDATA)
      {
         String content = getContent();
         mUseCDATA = inValue;

         if (! mUseCDATA)
         {
            clearSubtags();
            mCDATA = null;
         }

         setContent(content);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   /**
    Sets the script language.
    @deprecated Use setType() - The 'language' attribute has been deprecated
                in favor of 'type' in the html standard.
    */
   public Script setLanguage(String inValue)
   {
      setAttribute(HTML.LANGUAGE, inValue);

      return this;
   }

   //--------------------------------------------------------------------------
   /**
    Sets the charset.
    @param inValue the charset of the referenced script.
    @return this Script object - for method chaining
    */
   public Script setCharset(Charset inValue)
   {
      setAttribute(HTML.CHARSET, inValue.displayName());
      return this;
   }

   //--------------------------------------------------------------------------
   /**
    Sets the script type.
    */
   public Script setType(MimeType inValue)
   {
      return setType(inValue.toString());
   }

   //--------------------------------------------------------------------------
   /**
    Sets the script type.
    */
   public Script setType(String inValue)
   {
      setAttribute(HTML.TYPE, inValue);

      if (inValue != null
          && inValue.equalsIgnoreCase(MimeType.TEXT_JAVASCRIPT.toString()))
      {
         // TODO: Is this necessary?
         getCDATA().setHideWithCommentsForLegacyBrowsers(true);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   /**
    When set, this boolean attribute provides a hint to the user agent that the
    script is not going to generate any document content (e.g., no "document.write"
    in javascript) and thus, the user agent can continue parsing and rendering.
    */
   public Script setDefer(boolean inValue)
   {
      setAttribute(HTML.DEFER, inValue);

      return this;
   }

   //--------------------------------------------------------------------------
   /**
    Specifies the location of an external script.
    */
   public Script setSrc(String inValue)
   {
      setAttribute(HTML.SRC, inValue);

      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public Script setContent(CharSequence inContent)
   {
      clearContent();

      addContent(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public Script appendln(CharSequence inContent)
   {
      addContent(inContent + "\n");
      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public Script addContent(CharSequence inContent)
   {
      if (StringUtil.isSet(inContent))
      {
         if (mUseCDATA)
         {
            getCDATA().addContent(inContent);
         }
         else
         {
            super.addContentWithoutEscaping(inContent);
         }
      }
      else
      {
         super.addContent(inContent);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public void clearContent()
   {
      if (mCDATA != null) mCDATA.clearContent();
      super.clearContent();
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean hasContent()
   {
      boolean result = true;
      if (mUseCDATA)
      {
         result = mCDATA != null && mCDATA.hasContent();
      }
      else
      {
         result = super.hasContent();
      }

      return result;
   }

   //--------------------------------------------------------------------------
   @Override
   public String getContent()
   {
      String result = "";
      if (mUseCDATA)
      {
         if (mCDATA != null) result = mCDATA.getContent();
      }
      else
      {
         result = super.getContent();
      }

      return result;
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   private XMLCDATA getCDATA()
   {
      if (null == mCDATA)
      {
         XMLComment commentTag = new XMLComment("//");
         addSubtag(commentTag);
         mCDATA = new XMLCDATA("\n");
         addSubtag(mCDATA);
      }

      return mCDATA;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy