All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.util.xml.XMLNode Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package net.anotheria.util.xml;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * XMLNode. Actually 'Anotheria' custom XML representation.
 *
 * @author another
 * @version $Id: $Id
 */
public class XMLNode {

	private static final Logger log = LoggerFactory.getLogger(XMLNode.class);

	/**
	 * SubNodes of this node.
	 */
	private List nodes;
	/**
	 * Attributes of the node.
	 */
	private List attributes;
	
	/**
	 * The name of the node.
	 */
	private String name;
	/**
	 * The content of the node.
	 */
	private String content;
	
	/**
	 * Constructor. Creates a new XMLNode.
	 *
	 * @param aName node name
	 */
	public XMLNode(String aName){
		name = aName;
		attributes = new ArrayList<>();
		nodes = new ArrayList<>();
	}

	
	/**
	 * 

Getter for the field nodes.

* * @return a {@link java.util.List} object. */ public List getNodes() { return nodes; } /** * Set child nodes for current. * * @param aChildren xmlNodes list */ public void setChildren(List aChildren){ this.nodes = aChildren; } /** *

Setter for the field nodes.

* * @param aNodes a {@link java.util.List} object. */ public void setNodes(List aNodes) { this.nodes = aNodes; } /** * Allow add child node for current. * * @param aChild xmlNode */ public void addChildNode(XMLNode aChild){ nodes.add(aChild); } /** *

Getter for the field attributes.

* * @return a {@link java.util.List} object. */ public List getAttributes() { return attributes; } /** *

Setter for the field attributes.

* * @param aAttributes a {@link java.util.List} object. */ public void setAttributes(List aAttributes) { this.attributes = aAttributes; } /** *

addAttribute.

* * @param aAttribute a {@link net.anotheria.util.xml.XMLAttribute} object. */ public void addAttribute(XMLAttribute aAttribute){ attributes.add(aAttribute); } /** *

Getter for the field name.

* * @return a {@link java.lang.String} object. */ public String getName() { return name; } /** *

Setter for the field name.

* * @param aName a {@link java.lang.String} object. */ public void setName(String aName) { this.name = aName; } /** *

Getter for the field content.

* * @return a {@link java.lang.String} object. */ public String getContent() { return content; } /** *

Setter for the field content.

* * @param aContent a {@link java.lang.String} object. */ public void setContent(String aContent) { this.content = aContent; } /** *

Setter for the field content.

* * @param aContent a boolean. */ public void setContent(boolean aContent){ this.content = String.valueOf(aContent); } /** *

Setter for the field content.

* * @param aContent a float. */ public void setContent(float aContent){ this.content = String.valueOf(aContent); } /** *

Setter for the field content.

* * @param aContent a double. */ public void setContent(double aContent){ this.content = String.valueOf(aContent); } /** *

Setter for the field content.

* * @param aContent a long. */ public void setContent(long aContent){ this.content = String.valueOf(aContent); } /** *

Setter for the field content.

* * @param aContent a int. */ public void setContent(int aContent){ this.content = String.valueOf(aContent); } /** *

Setter for the field content.

* * @param aL a {@link java.util.List} object. */ public void setContent(List aL){ this.content = aL.toString(); } /** {@inheritDoc} */ @Override public String toString(){ return "name: "+name+", "+" attributes: "+attributes+", nodes: "+nodes; } /** * Areates attribute string. */ private String createAttributeString(){ String ret = ""; if (attributes == null || attributes.isEmpty()) return ret; for (XMLAttribute a : attributes){ ret += ' ' +a.toXMLString(); } return ret; } /** * Recursive - Write method. * * @param aWriter PrintStream writer. * @param aTabs position to start */ public void write(PrintStream aWriter, int aTabs) { String attributeString = createAttributeString(); String ident = XMLHelper.makeIdent(aTabs); aWriter.println(ident+XMLHelper.entag(name +attributeString)); for (XMLNode child : nodes) child.write(aWriter, aTabs+1); if (content!=null) aWriter.println(XMLHelper.makeIdent(aTabs+1)+"\n"); aWriter.println(ident+XMLHelper.detag(name)); } /** * Recursive - Write method. * * @param aWriter PrintWriter * @param aTabs position to start */ public void write(PrintWriter aWriter, int aTabs) { String attributeString = createAttributeString(); String ident = XMLHelper.makeIdent(aTabs); aWriter.write(ident+XMLHelper.entag(name +attributeString)); for (XMLNode child : nodes) child.write(aWriter, aTabs+1); if (content!=null) aWriter.write(XMLHelper.makeIdent(aTabs+1)+"\n"); aWriter.write(ident+XMLHelper.detag(name)); } /** * Recursive - Write method. * * @param aWriter OutputStreamWriter * @param aTabs position to start */ public void write(OutputStreamWriter aWriter, int aTabs) { try{ String attributeString = createAttributeString(); String ident = XMLHelper.makeIdent(aTabs); aWriter.write(ident+XMLHelper.entag(name +attributeString)); for (XMLNode child : nodes) child.write(aWriter, aTabs+1); if (content!=null) aWriter.write(XMLHelper.makeIdent(aTabs+1)+"\n"); aWriter.write(ident+XMLHelper.detag(name)); }catch(Throwable t){ log.error(t.getMessage(), t); } } /** * Recursive - Write method. * * @param aWriter java.io.Writer * @param aTabs tabs */ public void write(Writer aWriter, int aTabs) { try{ String attributeString = createAttributeString(); String ident = XMLHelper.makeIdent(aTabs); aWriter.write(ident+XMLHelper.entag(name +attributeString)); for (XMLNode child : nodes) child.write(aWriter, aTabs+1); if (content!=null) aWriter.write(XMLHelper.makeIdent(aTabs+1)+"\n"); aWriter.write(ident+XMLHelper.detag(name)); }catch(Throwable t){ log.error(t.getMessage(), t); } } }