/*------------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved. 
------------------------------------------------------------------------------------*/ 

package com.togethersoft.modules.xmleditor.dtd.impl;

import java.util.*;
import com.togethersoft.modules.xmleditor.dtd.*;
import com.togethersoft.modules.ejbframework.utils.ConfigProperties;

public class DTDElementDefImpl extends DTDValueDefImpl implements DTDElementDef, Comparable
{
  /**
   * @supplierCardinality 1 
   */
  private DTDDocumentTypeImpl m_documentType;

  private String   m_tagName;
  private int      m_elementType;  
  private String   m_fullName = null;

  private String      m_rwiElementProviderClassName = null;
  private Properties  m_rwiElementProviderParam = new Properties();

/** @directed
 * @supplierCardinality 0..1
 * @label contains*/
  private DTDItemDefImpl m_childItemDef = null;

  /**
       *@link aggregation
       *     @associates <{com.togethersoft.modules.xmleditor.dtd.impl.DTDAttributeDefImpl}>
       * @label attributes
       * @supplierCardinality 0..*
       */
  private Vector m_attributeDefs = new Vector();

  //--------------------------------------------------------------------
  public DTDElementDefImpl (DTDDocumentTypeImpl documentType, String tagName, int elementType)
  {
    m_documentType = documentType;
    m_tagName = tagName;
    m_elementType = elementType;
    setDefaultModifier (DM_REQUIRED);
  }

  //--------------------------------------------------------------------
  public DTDDocumentType getDocumentType()
  {
    return m_documentType;
  }

  //--------------------------------------------------------------------
  public String getTagName()
  {
    return m_tagName;
  }

  //--------------------------------------------------------------------
  public int getElementType()
  {
    return m_elementType;
  }

  //--------------------------------------------------------------------
  public String getFullName()
  {
    return (m_fullName != null) ? m_fullName : m_tagName;
  }

  //--------------------------------------------------------------------
  public void setFullName (String fullName)
  {
    m_fullName = fullName;
  }

  //--------------------------------------------------------------------
  public void setChildrenItemDef (DTDItemDef childItemDef)
  {
    m_childItemDef = (DTDItemDefImpl) childItemDef;
  }

  //--------------------------------------------------------------------
  public DTDItemDef getChildrenItemDef()
  {
    return m_childItemDef;
  }

  //--------------------------------------------------------------------
  public Vector getChildElementDefs()
  {
    return (m_childItemDef != null) ? m_childItemDef.getChildElementDefs()
                                    : new Vector();
  }

  //--------------------------------------------------------------------
  public Enumeration attributeDefs()
  {
    return m_attributeDefs.elements();
  }

  //--------------------------------------------------------------------
  public void addAttributeDef (DTDAttributeDef attrDef)
  {
    m_attributeDefs.addElement (attrDef);
    ((DTDAttributeDefImpl) attrDef).setContainingElementDef (this);
  }

  //--------------------------------------------------------------------
  public int compareTo (Object o)
  {
    return m_tagName.compareToIgnoreCase (((DTDElementDefImpl) o).m_tagName);
  }

  //--------------------------------------------------------------------
  public String toString()
  {
    return getFullName();
  }

  //--------------------------------------------------------------------
  public void load (ConfigProperties prop)
  {
    String prefix = m_tagName + '.';

    m_fullName = prop.getProperty (prefix + "title");

    if (getElementType() == ET_PCDATA)
      super.load (prop, prefix);

    // attributes
    for (Enumeration e = m_attributeDefs.elements(); e.hasMoreElements(); )
    {
      DTDAttributeDefImpl attrDefImpl = (DTDAttributeDefImpl) e.nextElement();
      attrDefImpl.load (prop, prefix);
    }

    // RWI-element provider class name
    m_rwiElementProviderClassName = prop.getProperty (prefix + "type");

    // provider's parameters
    try
    {
      Enumeration params = prop.subkeys (prefix + "param");
      while (params.hasMoreElements())
      {
        String paramName = (String) params.nextElement();
        String paramValue = prop.getProperty (prefix + "param." + paramName);

        m_rwiElementProviderParam.put (paramName, paramValue);
      }
    }
    catch (IllegalArgumentException ignore)
    {
      // Do nothing - properties are not defined
    }
  }

  //--------------------------------------------------------------------
  public String getRwiElementProviderClassName()
  {
    return m_rwiElementProviderClassName;
  }

  //--------------------------------------------------------------------
  public Properties getRwiElementProviderParam()
  {
    return m_rwiElementProviderParam;
  }

}
