com.hfg.svg.SvgScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.svg;
import com.hfg.util.StringUtil;
import com.hfg.util.mime.MimeType;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLCDATA;
import com.hfg.xml.XMLComment;
import com.hfg.xml.XMLNamespace;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
* Object representation of an SVG (Scalable Vector Graphics) '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]
//------------------------------------------------------------------------------
public class SvgScript extends AbstractSvgNode implements SvgNode
{
private boolean mUseCDATA = true;
private XMLCDATA mCDATA;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SvgScript()
{
super(SVG.script);
}
//--------------------------------------------------------------------------
public SvgScript(MimeType inType)
{
this();
setType(inType);
}
//--------------------------------------------------------------------------
public SvgScript(String inContent)
{
this();
addContent(inContent);
}
//--------------------------------------------------------------------------
public SvgScript(CharSequence inContent)
{
this();
addContent(inContent);
}
//---------------------------------------------------------------------------
public SvgScript(XMLTag inXMLTag)
{
this();
initFromXMLTag(inXMLTag);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public SvgScript setHref(String inURL)
{
XMLAttribute attr = new XMLAttribute(SvgAttr.href, inURL);
attr.setNamespace(XMLNamespace.getNamespaceViaPrefix("xlink"));
setAttribute(attr);
return this;
}
//--------------------------------------------------------------------------
/**
Sets the script type.
*/
public SvgScript setType(MimeType inValue)
{
return setType(inValue.toString());
}
//--------------------------------------------------------------------------
/**
Sets the script type.
*/
public SvgScript setType(String inValue)
{
setAttribute(SvgAttr.type, inValue);
if (inValue != null
&& inValue.equalsIgnoreCase(MimeType.TEXT_JAVASCRIPT.toString()))
{
// TODO: Is this necessary?
getCDATA().setHideWithCommentsForLegacyBrowsers(true);
}
return this;
}
//--------------------------------------------------------------------------
/**
Defaults to true.
*/
public SvgScript useCDATA(boolean inValue)
{
if (inValue != mUseCDATA)
{
String content = getContent();
mUseCDATA = inValue;
if (! mUseCDATA)
{
clearSubtags();
mCDATA = null;
}
setContent(content);
}
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgScript setContent(CharSequence inContent)
{
clearContent();
addContent(inContent);
return this;
}
//--------------------------------------------------------------------------
public SvgScript appendln(CharSequence inContent)
{
addContent(inContent + "\n");
return this;
}
//--------------------------------------------------------------------------
@Override
public SvgScript 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;
}
}