www.pudn.com > sxg.rar > GwType.java


/*
 * Copyright (c) 2003 Jens Mueller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 	An instance of this class represents a XSD SimpleType.

 	@author Jens Mueller
 */

import java.util.Vector;
import java.util.Iterator;

public class GwType
{
	private String _name;
	private String _minLength;
	private String _maxLength;
	private String _minInclusive;
	private String _maxInclusive;
	private String _displayHint;
	private String _xsdBaseType;
	private boolean _isList;
	public Vector _enum;
	
	/**
	 	contains the ancestor this SimpleType is derived from. 
	 	If this SimpleType is of type xsd:union, all united types should be found in this Vector. 	 */
	public Vector _ancestors;

	/**
	 	@param name  the name of the constructed type 	 */
	public GwType( String name )
	{
		_name = name;
		_isList = false;
		_enum = new Vector();
		_ancestors = new Vector();
	}
	
	
	public String getName()
	{
		return _name;
	}

	public boolean isEnumeration()
	{
		return !_enum.isEmpty();
	}

	public boolean isUnion()
	{
		return ( _ancestors.size() > 1 );
	}
	
	public void setMinLength( String minLength )
	{
		_minLength = minLength;
	}
	
	public void setMaxLength( String maxLength )
	{
		_maxLength = maxLength;
	}

	public void setMinInclusive( String minInclusive )
	{
		_minInclusive = minInclusive;
	}
	
	public void setMaxInclusive( String maxInclusive )
	{
		_maxInclusive = maxInclusive;
	}
	
	public void setDisplayHint( String displayHint )
	{
		_displayHint = displayHint;
	}
	
	public void setXsdBaseType( String basetype )
	{
		_xsdBaseType = basetype;
	}
	
	public void setList( boolean isList )
	{
		_isList = isList;
	}
	
	public String getXsdBaseType()
	{
		return _xsdBaseType;
	}
	
	public String getDisplayHint()
	{
		return _displayHint;
	}
	
	public boolean isList()
	{
		return _isList;
	}
	
	/**
	 	Finds out the approbiate cast-type of this SimpleType by recursively proceeding
	 	the ancestor until the ancestor containing the XsdBaseType is found.

	 	@return the type to which should be casted before performing a SNMP set-request
	 				on the GwElement agregating this GwType ("int" or "string"). 	 */
	public String getCastType()
	{
		if ( _isList )
			return "list";

		if( _xsdBaseType != null )
		{
			if( _xsdBaseType.equals( "NMTOKEN" ) && isEnumeration() )
				return "int";
			if( _xsdBaseType.equals( "unsignedInt" ) )
				return "int";
			if( _xsdBaseType.equals( "unsignedLong" ) )
				return "int";
			if( _xsdBaseType.equals( "int" ) )
				return "int";

			if( _xsdBaseType.equals( "string" ) )
				return "string";
			if( _xsdBaseType.equals( "NMTOKEN" ) )
				return "string";
			
			return "string";
		}

		if( _name.equals( "OctetString" ) )
			return "string";
		
		if( !_ancestors.isEmpty() )
			return (( GwType )_ancestors.get( 0 )).getCastType();
		else
			return "string";
	}
	
	
	/**
	 	Recursively checks if this SimpleType is derived from smi:ObjectIdentifier 
	 	@return true if this SimpleType represents an ObjectIdentifier 	 */
	public boolean isOidType()
	{
		if( _name.equals( "ObjectIdentifier" ) )
			return true;
		if( _ancestors.isEmpty() )
			return false;
		else
			return (( GwType )_ancestors.elementAt( 0 )).isOidType();
	}
	
	
	/**
	 	@return an textual representation of this SimpleType for debugging purposes 	 */
	public String dump()
	{
		String rep = 	"SimpleType "+_name+": Enum: "+isEnumeration()+
							" ,List: "+isList()+", Union: "+isUnion()+", minL "+_minLength+
							", maxL "+_maxLength+", minIncl "+_minInclusive+
							", maxIncl "+_maxInclusive+". displayHint "+_displayHint+
							", baseType "+_xsdBaseType+"
"; if (!_enum.isEmpty()) { rep += "Enumeration:
"; for (Iterator it =_enum.iterator(); it.hasNext(); ) { EnumElement element = (EnumElement)it.next(); rep += "enumElement: ( "+element.getIntVal()+" / "+element.getValue()+" )
"; } } for (Iterator it =_ancestors.iterator(); it.hasNext(); ) rep += ((GwType)it.next()).dump(); return rep; } }