001package com.hfg.svg; 002 003import com.hfg.util.StringUtil; 004import com.hfg.util.mime.MimeType; 005import com.hfg.xml.*; 006 007//------------------------------------------------------------------------------ 008/** 009 * Object representation of an SVG (Scalable Vector Graphics) 'script' tag. 010 * <div> 011 * @author J. Alex Taylor, hairyfatguy.com 012 * </div> 013 */ 014//------------------------------------------------------------------------------ 015// com.hfg XML/HTML Coding Library 016// 017// This library is free software; you can redistribute it and/or 018// modify it under the terms of the GNU Lesser General Public 019// License as published by the Free Software Foundation; either 020// version 2.1 of the License, or (at your option) any later version. 021// 022// This library is distributed in the hope that it will be useful, 023// but WITHOUT ANY WARRANTY; without even the implied warranty of 024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025// Lesser General Public License for more details. 026// 027// You should have received a copy of the GNU Lesser General Public 028// License along with this library; if not, write to the Free Software 029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030// 031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 032// jataylor@hairyfatguy.com 033//------------------------------------------------------------------------------ 034 035public class SvgScript extends AbstractSvgNode implements SvgNode 036{ 037 private boolean mUseCDATA = true; 038 private XMLCDATA mCDATA; 039 040 //########################################################################## 041 // CONSTRUCTORS 042 //########################################################################## 043 044 //--------------------------------------------------------------------------- 045 public SvgScript() 046 { 047 super(SVG.script); 048 } 049 050 //-------------------------------------------------------------------------- 051 public SvgScript(MimeType inType) 052 { 053 this(); 054 setType(inType); 055 } 056 057 //-------------------------------------------------------------------------- 058 public SvgScript(String inContent) 059 { 060 this(); 061 addContent(inContent); 062 } 063 064 //-------------------------------------------------------------------------- 065 public SvgScript(CharSequence inContent) 066 { 067 this(); 068 addContent(inContent); 069 } 070 071 //--------------------------------------------------------------------------- 072 public SvgScript(XMLTag inXMLTag) 073 { 074 this(); 075 initFromXMLTag(inXMLTag); 076 } 077 078 //########################################################################## 079 // PUBLIC METHODS 080 //########################################################################## 081 082 083 //-------------------------------------------------------------------------- 084 public SvgScript setHref(String inURL) 085 { 086 XMLAttribute attr = new XMLAttribute(SvgAttr.href, inURL); 087 attr.setNamespace(XMLNamespace.getNamespaceViaPrefix("xlink")); 088 setAttribute(attr); 089 return this; 090 } 091 092 //-------------------------------------------------------------------------- 093 /** 094 Sets the script type. 095 */ 096 public SvgScript setType(MimeType inValue) 097 { 098 return setType(inValue.toString()); 099 } 100 101 //-------------------------------------------------------------------------- 102 /** 103 Sets the script type. 104 */ 105 public SvgScript setType(String inValue) 106 { 107 setAttribute(SvgAttr.type, inValue); 108 109 if (inValue != null 110 && inValue.equalsIgnoreCase(MimeType.TEXT_JAVASCRIPT.toString())) 111 { 112 // TODO: Is this necessary? 113 getCDATA().setHideWithCommentsForLegacyBrowsers(true); 114 } 115 116 return this; 117 } 118 119 //-------------------------------------------------------------------------- 120 /** 121 Defaults to true. 122 */ 123 public SvgScript useCDATA(boolean inValue) 124 { 125 if (inValue != mUseCDATA) 126 { 127 String content = getContent(); 128 mUseCDATA = inValue; 129 130 if (! mUseCDATA) 131 { 132 clearSubtags(); 133 mCDATA = null; 134 } 135 136 setContent(content); 137 } 138 139 return this; 140 } 141 142 //-------------------------------------------------------------------------- 143 @Override 144 public SvgScript setContent(CharSequence inContent) 145 { 146 clearContent(); 147 148 addContent(inContent); 149 150 return this; 151 } 152 153 //-------------------------------------------------------------------------- 154 public SvgScript appendln(CharSequence inContent) 155 { 156 addContent(inContent + "\n"); 157 return this; 158 } 159 160 //-------------------------------------------------------------------------- 161 public void addSubtag(XMLCDATA inCDATA) 162 { 163 mUseCDATA = true; 164 super.addSubtag(inCDATA); 165 } 166 167 //-------------------------------------------------------------------------- 168 @Override 169 public SvgScript addContent(CharSequence inContent) 170 { 171 if (StringUtil.isSet(inContent)) 172 { 173 if (mUseCDATA) 174 { 175 getCDATA().addContent(inContent); 176 } 177 else 178 { 179 super.addContentWithoutEscaping(inContent); 180 } 181 } 182 else 183 { 184 super.addContent(inContent); 185 } 186 187 return this; 188 } 189 190 //-------------------------------------------------------------------------- 191 @Override 192 public void clearContent() 193 { 194 if (mCDATA != null) mCDATA.clearContent(); 195 super.clearContent(); 196 } 197 198 //-------------------------------------------------------------------------- 199 @Override 200 public boolean hasContent() 201 { 202 boolean result = true; 203 if (mUseCDATA) 204 { 205 result = mCDATA != null && mCDATA.hasContent(); 206 } 207 else 208 { 209 result = super.hasContent(); 210 } 211 212 return result; 213 } 214 215 //-------------------------------------------------------------------------- 216 @Override 217 public String getContent() 218 { 219 String result = ""; 220 if (mUseCDATA) 221 { 222 if (mCDATA != null) result = mCDATA.getContent(); 223 } 224 else 225 { 226 result = super.getContent(); 227 } 228 229 return result; 230 } 231 //--------------------------------------------------------------------------- 232 @Override 233 protected void initFromXMLTag(XMLTag inXMLTag) 234 { 235 mUseCDATA = false; 236 super.initFromXMLTag(inXMLTag); 237 } 238 239 //########################################################################## 240 // PRIVATE METHODS 241 //########################################################################## 242 243 //-------------------------------------------------------------------------- 244 private XMLCDATA getCDATA() 245 { 246 if (null == mCDATA) 247 { 248 XMLComment commentTag = new XMLComment("//"); 249 addSubtag(commentTag); 250 mCDATA = new XMLCDATA("\n"); 251 addSubtag(mCDATA); 252 } 253 254 return mCDATA; 255 } 256}