All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.cybergarage.xml.Node Maven / Gradle / Ivy
/******************************************************************
*
* CyberXML for Java
*
* Copyright (C) Satoshi Konno 2002
*
* File: Element.java
*
* Revision;
*
* 11/27/02
* - first revision.
* 11/01/03
* - Terje Bakken
* - fixed missing escaping of reserved XML characters
* 11/19/04
* - Theo Beisch
* - Added "&" and "\"" "\\" to toXMLString().
* 11/19/04
* - Theo Beisch
* - Changed XML::output() to use short notation when the tag value is null.
* 12/02/04
* - Brian Owens
* - Fixed toXMLString() to convert from "'" to "'" instead of "\".
* 11/07/05
* - Changed toString() to return as utf-8 string.
* 02/08/08
* - Added addValue().
*
******************************************************************/
package org.cybergarage.xml;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
public class Node
{
/**
* Create a Node with empty UserData and no Parent Node
*
*/
public Node()
{
setUserData(null);
setParentNode(null);
}
public Node(String name)
{
this();
setName(name);
}
public Node(String ns, String name)
{
this();
setName(ns, name);
}
public Node(Node otherNode)
{
this();
set(otherNode);
}
////////////////////////////////////////////////
// parent node
////////////////////////////////////////////////
private Node parentNode = null;
public void setParentNode(Node node)
{
parentNode = node;
}
public Node getParentNode()
{
return parentNode;
}
////////////////////////////////////////////////
// root node
////////////////////////////////////////////////
public Node getRootNode()
{
Node rootNode = null;
Node parentNode = getParentNode();
while (parentNode != null) {
rootNode = parentNode;
parentNode = rootNode.getParentNode();
}
return rootNode;
}
////////////////////////////////////////////////
// name
////////////////////////////////////////////////
private String name = new String();
public void setName(String name)
{
this.name = name;
}
public void setName(String ns, String name)
{
this.name = ns + ":" + name;
}
public String getName()
{
return name;
}
public boolean isName(String value)
{
return name.equals(value);
}
////////////////////////////////////////////////
// value
////////////////////////////////////////////////
private String value = new String();
public void setValue(String value)
{
this.value = value;
}
public void setValue(int value)
{
setValue(Integer.toString(value));
}
public void addValue(String value)
{
if (this.value == null) {
this.value = value;
return;
}
if (value != null)
this.value += value;
}
public String getValue()
{
return value;
}
////////////////////////////////////////////////
// Attribute (Basic)
////////////////////////////////////////////////
private AttributeList attrList = new AttributeList();
public int getNAttributes() {
return attrList.size();
}
public Attribute getAttribute(int index) {
return attrList.getAttribute(index);
}
public Attribute getAttribute(String name)
{
return attrList.getAttribute(name);
}
public void addAttribute(Attribute attr) {
attrList.add(attr);
}
public void insertAttributeAt(Attribute attr, int index) {
attrList.insertElementAt(attr, index);
}
public void addAttribute(String name, String value) {
Attribute attr = new Attribute(name, value);
addAttribute(attr);
}
public boolean removeAttribute(Attribute attr) {
return attrList.remove(attr);
}
public boolean removeAttribute(String name) {
return removeAttribute(getAttribute(name));
}
public void removeAllAttributes()
{
attrList.clear();
}
public boolean hasAttributes()
{
if (0 < getNAttributes())
return true;
return false;
}
////////////////////////////////////////////////
// Attribute (Extention)
////////////////////////////////////////////////
public void setAttribute(String name, String value) {
Attribute attr = getAttribute(name);
if (attr != null) {
attr.setValue(value);
return;
}
attr = new Attribute(name, value);
addAttribute(attr);
}
public void setAttribute(String name, int value) {
setAttribute(name, Integer.toString(value));
}
public String getAttributeValue(String name) {
Attribute attr = getAttribute(name);
if (attr != null)
return attr.getValue();
return "";
}
public int getAttributeIntegerValue(String name) {
String val = getAttributeValue(name);
try {
return Integer.parseInt(val);
}
catch (Exception e) {}
return 0;
}
////////////////////////////////////////////////
// Attribute (xmlns)
////////////////////////////////////////////////
public void setNameSpace(String ns, String value)
{
setAttribute("xmlns:" + ns, value);
}
////////////////////////////////////////////////
// set
////////////////////////////////////////////////
public boolean set(Node otherNode) {
if (otherNode == null)
return false;
setName(otherNode.getName());
setValue(otherNode.getValue());
removeAllAttributes();
int nOtherAttributes = otherNode.getNAttributes();
for (int n=0; n i = nodeList.iterator(); i.hasNext();) {
index++;
Node n = i.next();
if(n.getName().equals(name))
return index;
}
return index;
}
public boolean removeNode(Node node) {
node.setParentNode(null);
return nodeList.remove(node);
}
public boolean removeNode(String name) {
return nodeList.remove(getNode(name));
}
public void removeAllNodes()
{
nodeList.clear();
}
public boolean hasNodes()
{
if (0 < getNNodes())
return true;
return false;
}
////////////////////////////////////////////////
// Element (Child Node)
////////////////////////////////////////////////
public boolean hasNode(String name) {
Node node = getNode(name);
if (node != null) {
return true;
}
return false;
}
public void setNode(String name) {
if (hasNode(name)) {
return;
}
Node node = new Node(name);
addNode(node);
}
public void setNode(String name, String value) {
Node node = getNode(name);
if (node == null) {
node = new Node(name);
addNode(node);
}
node.setValue(value);
}
public String getNodeValue(String name) {
Node node = getNode(name);
if (node != null)
return node.getValue();
return "";
}
////////////////////////////////////////////////
// userData
////////////////////////////////////////////////
private Object userData = null;
public void setUserData(Object data)
{
userData = data;
}
public Object getUserData()
{
return userData;
}
////////////////////////////////////////////////
// toString
////////////////////////////////////////////////
/**
* Inovoke {@link #getIndentLevelString(int, String)} with " "
as String
*
* @see #getIndentLevelString(int, String)
*/
public String getIndentLevelString(int nIndentLevel)
{
return getIndentLevelString(nIndentLevel," ");
}
/**
*
* @param nIndentLevel the level of indentation to produce
* @param space the String to use for the intendation
* @since 1.8.0
* @return an indentation String
*/
public String getIndentLevelString(int nIndentLevel,String space)
{
StringBuffer indentString = new StringBuffer(nIndentLevel*space.length());
for (int n=0; n because it cause compatibility trouble
ps.println(">" + name + ">");
} else {
ps.println(">" + XML.escapeXMLChars(value) + "" + name + ">");
}
return;
}
ps.print(indentString + "<" + name);
outputAttributes(ps);
ps.println(">");
int nChildNodes = getNNodes();
for (int n=0; n");
}
public String toString(String enc, boolean hasChildNode)
{
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
PrintWriter pr = new PrintWriter(byteOut);
output(pr, 0, hasChildNode);
pr.flush();
try {
if (enc != null && 0 < enc.length())
return byteOut.toString(enc);
}
catch (UnsupportedEncodingException e) {
}
return byteOut.toString();
}
public String toString()
{
return toString(XML.CHARSET_UTF8, true);
}
public String toXMLString(boolean hasChildNode)
{
String xmlStr = toString();
xmlStr = xmlStr.replaceAll("<", "<");
xmlStr = xmlStr.replaceAll(">", ">");
// Thanks for Theo Beisch (11/09/04)
xmlStr = xmlStr.replaceAll("&", "&");
xmlStr = xmlStr.replaceAll("\"", """);
// Thanks for Brian Owens (12/02/04)
xmlStr = xmlStr.replaceAll("'", "'");
return xmlStr;
}
public String toXMLString()
{
return toXMLString(true);
}
public void print(boolean hasChildNode)
{
PrintWriter pr = new PrintWriter(System.out);
output(pr, 0, hasChildNode);
pr.flush();
}
public void print()
{
print(true);
}
}