
com.sun.xml.tree.DataNode Maven / Gradle / Ivy
/*
* $Id: DataNode.java,v 1.5 1999/04/17 01:06:07 mode Exp $
*
* Copyright (c) 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
package com.sun.xml.tree;
import java.io.Writer;
import java.io.IOException;
import org.w3c.dom.*;
/**
* Node representing XML character data, such as text (including
* CDATA sections and comments).
*
* At this time this uses an unsophisticated representation
* which is not suited for complex editing operations.
*
* @author David Brownell
* @version $Revision: 1.5 $
*/
// public
abstract class DataNode extends NodeBase implements CharacterData
{
// package private
char data [];
static NodeListImpl childNodes = new NodeListImpl ();
/*
* Constructs a data object with no text and unattached
* to any document.
*/
DataNode () { }
/*
* Constructs data node by copying text from the input buffer.
*/
DataNode (char buf [], int offset, int len)
{
data = new char [len];
System.arraycopy (buf, offset, data, 0, len);
}
/*
* Constructs a data node by copying text from the string.
*/
DataNode (String s)
{
if (s != null) {
data = new char [s.length ()];
s.getChars (0, data.length, data, 0);
} else
data = new char [0];
}
/**
* Returns the text of the node. This may be modified by
* the caller only if the length remains unchanged.
*/
public char [] getText () { return data; }
/**
* Assigns the text of the node. The buffer is consumed; the
* caller should make copies accordingly.
*/
public void setText (char buf []) { data = buf; }
/**
* Returns the contents of this text as a String.
*/
public String toString () { return new String (data); }
// DOM support
/** DOM: Returns the text data as a string. */
public String getData () { return toString (); }
/** DOM: Assigns the text data. */
public void setData (String data) {
if (isReadonly ())
throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
if (data == null) {
setText (new char [0]);
} else {
setText (data.toCharArray ());
}
}
/** DOM: Returns the length of the node's data. */
public int getLength ()
{ return data == null ? 0 : data.length; }
/**
* DOM: Returns the specified substring of the data in this node.
*/
public String substringData (int offset, int count)
throws DOMException
{
if (offset < 0 || offset > data.length || count < 0)
throw new DomEx (DOMException.INDEX_SIZE_ERR);
count = Math.min (count, data.length - offset);
return new String (data, offset, count);
}
/**
* DOM: Appends the string to the existing stored data.
*/
public void appendData (String newData)
{
if (isReadonly ())
throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
int length = newData.length ();
char tmp [] = new char [length + data.length];
System.arraycopy (data, 0, tmp, 0, data.length);
newData.getChars (0, length, tmp, data.length);
data = tmp;
}
/**
* DOM: Inserts the given data into the existing stored data
* at the specified offset.
*/
public void insertData (int offset, String newData)
throws DOMException
{
if (isReadonly ())
throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
if (offset < 0 || offset >= data.length)
throw new DomEx (DOMException.INDEX_SIZE_ERR);
int length = newData.length ();
char tmp [] = new char [length + data.length];
System.arraycopy (data, 0, tmp, 0, offset);
newData.getChars (0, length, tmp, offset);
System.arraycopy (data, offset,
tmp, offset + length,
data.length - offset);
data = tmp;
}
/**
* DOM: Removes a range of characters from the text.
*/
public void deleteData (int offset, int count)
throws DOMException
{
char tmp [];
if (isReadonly ())
throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
if (offset < 0 || offset >= data.length || count < 0)
throw new DomEx (DOMException.INDEX_SIZE_ERR);
count = Math.min (count, data.length - offset);
tmp = new char [data.length - count];
System.arraycopy (data, 0, tmp, 0, offset);
System.arraycopy (data, offset + count, tmp, offset,
tmp.length - offset);
data = tmp;
}
/**
* DOM: Replaces count characters starting at the specified
* offset in the data with the characters from the
* specified arg.
*/
public void replaceData (int offset, int count, String arg)
throws DOMException
{
if (isReadonly ())
throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR);
if (offset < 0 || offset >= data.length || count < 0)
throw new DomEx (DOMException.INDEX_SIZE_ERR);
if ((offset + count) >= data.length) {
deleteData (offset, count);
appendData (arg);
} else if (arg.length () == count) {
arg.getChars (0, (arg.length ()), data, offset);
} else {
char tmp [] = new char [data.length + (arg.length () - count)];
System.arraycopy (data, 0, tmp, 0, offset);
arg.getChars (0, (arg.length ()), tmp, offset);
System.arraycopy (data, (offset + count), tmp, (offset +
arg.length ()), data.length -(offset + count));
data = tmp;
}
}
/**
* DOM: Returns the children of this node.
*/
public NodeList getChildNodes () { return childNodes; }
/**
* DOM: Returns the node's character data.
*/
public String getNodeValue () { return getData (); }
/**
* DOM: Assigns the node's character data.
*/
public void setNodeValue (String value) { setData (value); }
static final class NodeListImpl implements NodeList {
public Node item (int i) { return null;}
public int getLength () { return 0;}
}
}