context: /*
      Uiml.Net: a .Net UIML renderer (http://lumumba.uhasselt.be/kris/research/uiml.net)
  
  	 Copyright (C) 2003  Kris Luyten (kris.luyten@uhasselt.be)
  	                     Expertise Centre for Digital Media (http://edm.uhasselt.be)
  								Hasselt University
  
  	This program is free software; you can redistribute it and/or
  	modify it under the terms of the GNU Lesser General Public License
  	as published by the Free Software Foundation; either version 2.1
  	of	the License, 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 Lesser General Public License for more details.
  
  	You should have received a copy of the GNU Lesser General Public License
  	along with this program; if not, write to the Free Software
  	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
  	Author: 
  		Jo Vermeulen
  		jo.vermeulen@student.luc.ac.be
  */
  
  using System;
  using System.Xml;
  using System.Collections;
  using System.Collections.Generic;
  
  namespace Uiml.Peers
  {
  	/// <summary>
  	/// This class represents a &lt;d-method&gt; element in the vocabulary, specified by the following DTD:
  	/// &lt;!ELEMENT d-method (d-param*, script?)&gt;
  	/// &lt;!ATTLIST d-method
  	///           id NMTOKEN #REQUIRED
  	///           source CDATA #IMPLIED
  	///           how (append|cascade|replace) "replace"
  	///           export (hidden|optional|required) "optional"
  	///           maps-to CDATA #REQUIRED
  	///           return-type CDATA #IMPLIED&gt;
  	/// </summary>
  	public class DMethod : UimlAttributes, IUimlElement
  	{
  		protected ArrayList m_children = null;
  
  		protected string m_mapsTo;
  		protected string m_returnType;
  
  		public DMethod()
  		{}
  
  		public DMethod(XmlNode n)
  		{
  			Process(n);
  		}
  
          public object Clone()
          {
              DMethod clone = new DMethod();
              clone.CopyAttributesFrom(this);
              clone.m_mapsTo = m_mapsTo;
              clone.m_returnType = m_returnType;
  
              if(m_children != null)
              {
                  clone.m_children = new ArrayList();
                  for(int i = 0; i < m_children.Count; i++)
                  {
                      IUimlElement element = (IUimlElement)m_children[i];
                      clone.m_children.Add(element.Clone());
                  }
              }
  
              return clone;
          }
  
  		public void Process(XmlNode n)
  		{
  			if(n.Name != IAM)
  				return;
  
  			base.ReadAttributes(n);
  			XmlAttributeCollection attr = n.Attributes;
  			if(attr.GetNamedItem(MAPS_TO) != null)
  				MapsTo = attr.GetNamedItem(MAPS_TO).Value;
  
  			if(attr.GetNamedItem(RETURN_TYPE) != null)
  				ReturnType = attr.GetNamedItem(RETURN_TYPE).Value;
  			
  			ProcessChildren(n.ChildNodes);
  		}        
  
  		protected void ProcessChildren(XmlNodeList l)
  		{
  			IEnumerator enumChildren = l.GetEnumerator();
  
  			while(enumChildren.MoveNext())
  			{
  				XmlNode c = (XmlNode) enumChildren.Current;
  				switch(c.Name)
  				{
  					case DParam.IAM:
  						AddChild(new DParam(c));
  						break;
  					case Script.IAM:
  						AddChild(new Script(c));
  						break;
  				}
  			}
  		}
  
          public override XmlNode Serialize(XmlDocument doc)
          {
              XmlNode node = doc.CreateElement(IAM);
              List<XmlAttribute> attributes = CreateAttributes(doc);
              if (MapsTo.Length > 0)
              {
                  XmlAttribute attr = doc.CreateAttribute(MAPS_TO);
                  attr.Value = MapsTo;
                  attributes.Add(attr);
              }
              if (ReturnType.Length > 0)
              {
                  XmlAttribute attr = doc.CreateAttribute(RETURN_TYPE);
                  attr.Value = ReturnType;
                  attributes.Add(attr);
              }
  
              foreach (XmlAttribute attr in attributes)
              {
                  node.Attributes.Append(attr);
              }
  
              if (HasChildren)
              {
                  for (int i = 0; i < Children.Count; i++)
                  {
                      IUimlElement element = (IUimlElement)Children[i];
                      node.AppendChild(element.Serialize(doc));
                  }
              }
  
              return node;
  
          }
  
          public bool HasChildren
  		{
  			get { return m_children != null; }
  		}
  
  		public void AddChild(object o)
  		{
  			if(m_children == null)
  				m_children = new ArrayList();
  			m_children.Add(o);
  		}
  
  		public IEnumerator GetEnumerator()
  		{
  			return m_children.GetEnumerator();
  		}
  
  		public ArrayList Search(Type t)
  		{
  			ArrayList l = new ArrayList();
  
  			if(HasChildren)
  			{
  				IEnumerator e = GetEnumerator();
  
  				while(e.MoveNext())
  				{
  					if(e.Current.GetType().Equals(t))
  					{
  						l.Add(e.Current);
  					}
  				}
  			}
  
  			return l; 						
  		}
  
  		public ArrayList Children
  		{
  			get { return m_children; }
  		}
  
  		public string MapsTo
  		{
  			get { return m_mapsTo; }
  			set { m_mapsTo = value;	}
  		}
  
  		public string ReturnType
  		{
  			get { return m_returnType;	}
  
gt: 			set { m_returnType = value;	}