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

jadex.xml.StackElement Maven / Gradle / Ivy

Go to download

Jadex XML is an XML data binding framework for Java and also for other representations. The main idea of Jadex XML is that neither the XML-Schema on the one side nor the Java classes on the other side should define other binding. Instead, a separate mapping between both is used as a mediation. This allows designing the XML representation independent of the Java side but still being able to connect both as desired. This idea was first put forward by the JiBX data binding framework. Jadex XML pushes it further by combining it with the configuration by exception principle. The framework can detect obvious correspondences between both sides automatically and only needs configuration information when translations are necessary. The configuration information is currently specified directly in form of Java configuration classes.

There is a newer version: 4.0.267
Show newest version
package jadex.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jadex.commons.SUtil;
import jadex.xml.reader.IObjectReaderHandler;
import jadex.xml.stax.ILocation;
import jadex.xml.stax.QName;

/**
 *  A struct to represent an element on the stack while parsing.
 */
public class StackElement
{
	//-------- attributes --------
	
	/** The reader handler (if any). */
	protected IObjectReaderHandler	readerhandler;

	/** The xml tag. */
	protected QName tag;
	
	/** The created object. */
	protected Object object;
	
	/** The collected content. */
	protected String content;
	
	/** Collecting content. */
//	protected StringBuffer cbuf;
	protected List parts;
	
	// todo: remove rest somehow
	
	/** The raw attributes. */
	protected Map rawattrs;
	
	/** The type info. */
	protected TypeInfo typeinfo;
	
	/** The location of the start tag. */
	protected ILocation	location;
	
	//-------- constructors --------
	
	/**
	 *  Create a new stack element.
	 */
	// Used for writing, thus no reader handler
	public StackElement(QName tag, Object object)
	{
		this(null, tag, object, null);
	}
	
	/**
	 *  Create a new stack element.
	 */
	public StackElement(IObjectReaderHandler readerhandler, QName tag, Object object)
	{
		this(readerhandler, tag, object, null);
	}
	
	/**
	 *  Create a new stack element.
	 */
	public StackElement(IObjectReaderHandler readerhandler, QName tag, Object object, Map rawattrs)
	{
		this(readerhandler, tag, object, rawattrs, null, null);
	}
	
	/**
	 *  Create a new stack element.
	 */
	public StackElement(IObjectReaderHandler readerhandler, QName tag, Object object, Map rawattrs, TypeInfo typeinfo, ILocation location)
	{
		this.readerhandler	= readerhandler;
		this.tag = tag;
		this.object = object;
		this.rawattrs = rawattrs;
		this.typeinfo = typeinfo;
		this.location	= location;
	}
	
	//-------- methods --------
	
	/**
	 *  Get the tag.
	 *  @return The tag.
	 */
	public QName getTag()
	{
		return this.tag;
	}

	/**
	 *  Get the object.
	 *  @return The object.
	 */
	public Object getObject()
	{
		return this.object;
	}
	
	/**
	 *  Set the object.
	 *  @param object The object to set.
	 */
	public void setObject(Object object)
	{
		this.object = object;
	}

	/**
	 *  Get the content (if any).
	 *  @return The content or null for empty elements.
	 */
	public String getContent()
	{
		if(parts!=null)
		{
			assert content==null;
			
			if(parts.size()==1)
			{
				content = (String)parts.get(0);
			}
			else
			{
				int len = 0;
				for(int i=0; i getRawAttributes()
	{
		return this.rawattrs;
	}

	/**
	 *  Get the typeinfo.
	 *  @return The typeinfo.
	 */
	public TypeInfo getTypeInfo()
	{
		return this.typeinfo;
	}
	
	/**
	 *  Get the location.
	 *  @return The location.
	 */
	public ILocation getLocation()
	{
		return this.location;
	}
	
	/**
	 *  Add content to the already collected content (if any).
	 *  @param content	The content to add.
	 */
	public void	addContent(String content)
	{
		assert content!=null;
		
		if(parts==null)
			parts = new ArrayList();
//		System.out.println("Added content: '"+content+"'");

		parts.add(content);
	}

	/**
	 *  Get the string representation.
	 *  @return The string representation.
	 */
	public String toString()
	{
		return "StackElement(tag="+this.tag+", object=" + this.object + ")";
	}

	/**
	 *  Get the hash code.
	 */
	public int hashCode()
	{
		// Content/object set afterwards, cannot use for hashcode!
		int result = 31 + ((rawattrs==null) ? 0 : rawattrs.hashCode());
		result = 31*result + ((tag==null) ? 0 : tag.hashCode());
		result = 31*result + ((typeinfo==null) ? 0 : typeinfo.hashCode());
		result = 31*result + ((location==null) ? 0 : location.hashCode());
		return result;
	}

	/**
	 *  Test if two stack elements are equal.
	 */
	public boolean equals(Object obj)
	{
		boolean	ret	= this==obj;
		if(!ret && obj instanceof StackElement)
		{
			// Content/object set afterwards, cannot use for equals!
			StackElement other = (StackElement)obj;
			ret	= SUtil.equals(rawattrs, other.rawattrs)
				&& SUtil.equals(tag, other.tag)
				&& SUtil.equals(typeinfo, other.typeinfo)
				&& SUtil.equals(location, other.location);
		}
		return ret;
	}

	/**
	 *  Get the current reader handler.
	 */
	public IObjectReaderHandler getReaderHandler()
	{
		return readerhandler;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy