/*------------------------------------------------------------------------------------
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;

abstract public class DTDValueDefImpl implements DTDValueDef
{
  /**
  Attribute Data Type: one of DT_XXX
  */
  private int m_dataType = DT_CDATA;

  private String m_valueTypeName = "StringField";

  /**
  Default Modifier: one of DM_XXX
  */
  private int m_defaultModifier = DM_IMPLIED;

  private Vector m_enumValues = null;
  private String m_defaultValue = new String();

  //--------------------------------------------------------------------
  public DTDValueDefImpl()
  {
  }

  //--------------------------------------------------------------------
  public int getDataType()
  {
    return m_dataType;
  }

  //--------------------------------------------------------------------
  public void setDataType (int dataType)
  {
    m_dataType = dataType;

    switch (dataType)
    {
      case DT_ENUMERATED:
        m_enumValues = new Vector();
        break;
    }
  }

  //--------------------------------------------------------------------
  public void setValueTypeName (String typeName)
  {
    m_valueTypeName = typeName;
  }

  //--------------------------------------------------------------------
  public String getValueTypeName()
  {
    return m_valueTypeName;
  }

  //--------------------------------------------------------------------
  public void addEnumValue (String value)
  {
    if (m_enumValues != null)
      m_enumValues.addElement (value);
  }
  
  //--------------------------------------------------------------------
  public Vector getEnumValues()
  {
    return m_enumValues;
  }

  //--------------------------------------------------------------------
  public int getDefaultModifier()
  {
    return m_defaultModifier;
  }

  //--------------------------------------------------------------------
  public void setDefaultModifier (int newDefaultModifier)
  {
    m_defaultModifier = newDefaultModifier;
  }

  //--------------------------------------------------------------------
  public String getDefaultValue()
  {
    return m_defaultValue;
  }
  
  //--------------------------------------------------------------------
  public void setDefaultValue (String value)
  {
    m_defaultValue = (value != null) ? value : new String();
  }

  //--------------------------------------------------------------------
  public void load (ConfigProperties prop, String prefix)
  {
    String s;

    // value type name
    s = prop.getProperty (prefix + "valueType");
    if (s != null) m_valueTypeName = s;

    // enumeration values
    s = prop.getProperty (prefix + "values");
    if (s != null)
    {
      setDataType (DT_ENUMERATED);
      try
      {
        int c, index, len = s.length();
        for (index = 0; index < len; )
        {
          c = s.charAt (index ++);
          if (c == ' ' || c == '\t')
            continue;

          if (c == '\'' || c == '\"')
          {
            int index2 = s.indexOf (c, index);
            if (index2 < 0) index2 = len;

            String value = s.substring (index, index2);
            addEnumValue (value);

            for (index = index2 + 1; index < len; )
            {
              c = s.charAt (index ++);
              if (c == ' ' || c == '\t')
                continue;

              if (c == ',')
                break;

              throw new IllegalArgumentException();
            }

            continue;
          }

          throw new IllegalArgumentException();
        }
      }
      catch (IllegalArgumentException e) { }
    }


    // editor class name
    s = prop.getProperty (prefix + "editorClass");
    if (s != null)
    {
      // ...
    }
  }
}
