com.hfg.xml.XMLCDATA 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.xml;
import java.io.OutputStream;
import java.io.Writer;
import java.io.IOException;
import com.hfg.exception.ProgrammingException;
//------------------------------------------------------------------------------
/**
* Represents a CDATA block.
*
* @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.
See http://www.sitepoint.com/forums/showthread.php?p=1955911
or this extracted page: http://web-development.tuljo.com/cdata-comments.html
*/
public class XMLCDATA implements XMLizable
{
private String mContent;
private boolean mHideWithCommentsForLegacyBrowsers;
private static boolean sDefaultHideWithCommentsForLegacyBrowsers = false;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public XMLCDATA()
{
mHideWithCommentsForLegacyBrowsers = sDefaultHideWithCommentsForLegacyBrowsers;
}
//--------------------------------------------------------------------------
public XMLCDATA(String inContent)
{
this();
mContent = inContent;
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
public XMLCDATA clone()
{
XMLCDATA cloneObj;
try
{
cloneObj = (XMLCDATA) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new ProgrammingException(e);
}
return cloneObj;
}
//---------------------------------------------------------------------------
public static void setDefaultHideWithCommentsForLegacyBrowsers(boolean inValue)
{
sDefaultHideWithCommentsForLegacyBrowsers = inValue;
}
//---------------------------------------------------------------------------
public static boolean getDefaultHideWithCommentsForLegacyBrowsers()
{
return sDefaultHideWithCommentsForLegacyBrowsers;
}
//---------------------------------------------------------------------------
/**
* Sets whether or not to 'hide' the CDATA structure from browsers that are too
* old to support it. This is done by adding '//><!--\n' and '\n//--><!' inside
* the CDATA.
* For a discussion of where this syntax comes from, see
* this discussion.
*/
public XMLCDATA setHideWithCommentsForLegacyBrowsers(boolean inValue)
{
mHideWithCommentsForLegacyBrowsers = inValue;
return this;
}
//---------------------------------------------------------------------------
public boolean getHideWithCommentsForLegacyBrowsers()
{
return mHideWithCommentsForLegacyBrowsers;
}
//---------------------------------------------------------------------------
public boolean hasContent()
{
return true;
}
//---------------------------------------------------------------------------
public void setContent(CharSequence inContent)
{
clearContent();
addContent(inContent);
}
//---------------------------------------------------------------------------
/**
Clears content (not subtags).
*/
public void clearContent()
{
mContent = null;
}
//---------------------------------------------------------------------------
public void addContent(CharSequence inContent)
{
if (inContent != null)
{
if (mContent != null)
{
mContent += inContent;
}
else
{
mContent = inContent.toString();
}
}
}
//---------------------------------------------------------------------------
/**
The returned content does not contain any embedded subtags.
*/
public String getContent()
{
return mContent;
}
//---------------------------------------------------------------------------
public String toXML()
{
// return "";
StringBuilder buffer = new StringBuilder(mContent != null ? mContent.length() : 30);
buffer.append("");
return buffer.toString();
}
//---------------------------------------------------------------------------
public void toXML(Writer inWriter)
{
try
{
inWriter.write(toXML());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
//---------------------------------------------------------------------------
public void toXML(OutputStream inStream)
{
try
{
inStream.write(toXML().getBytes());
}
catch (Exception e)
{
throw new XMLException(e);
}
}
//--------------------------------------------------------------------------
public String toIndentedXML(int inInitialIndentLevel, int inIndentSize)
{
return toXML();
}
//--------------------------------------------------------------------------
public void toIndentedXML(OutputStream inOutputStream, int inInitialIndentLevel, int inIndentSize)
{
toXML(inOutputStream);
}
//--------------------------------------------------------------------------
public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize)
{
toXML(inWriter);
}
}