⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamicbean.java

📁 里面包含dwr于struts
💻 JAVA
字号:
package dev.trade.netopt.form;

import java.io.*;
import java.text.*;
import java.util.*;
import org.apache.commons.beanutils.*;

/**
 * <p>Title: 网优分析系统</p>
 *
 * <p>Description: 动态的Bean, 是DynamicForm的基础</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Newland</p>
 *
 * @author 
 * @version 1.0
 */
public class DynamicBean implements Map, DynaBean, DynaClass, Serializable {
	
private static final long serialVersionUID = 1L;
private Map map = null;
  private Map formatters = null;
//  private Map msgs = null;

  public DynamicBean() {
    this.map = new HashMap();
  }

  public DynamicBean( Map map ) {
    this.map = map;
  }

  ///////////////////////////////////
  // Formatters
  ///////////////////////////////////
  public void setFormat( String key, Format format ) {
    if ( this.formatters == null ) this.formatters = new HashMap();
    this.formatters.put( key, format );
  }

  public Format getFormat( String key ) {
    return (Format) this.formatters.get( key );
  }

  public void removeFormat( String key ) {
    this.formatters.remove( key );
  }

  ///////////////////////////////////
  // Dynamic methods
  ///////////////////////////////////
  /**
   * @param namePath is a dot separated name that will correspond with hierarchically nested maps.
   * @param format is the formatter to apply for the property namePath
   */
  public void dynamicSetFormat( String namePath, Format format ) {
    if ( namePath == null ) throw new RuntimeException( "Can't set formatter dynamically for null-path on DynamicMap " + this );

    int index = namePath.indexOf( "." );
    if ( index != -1 ) {
      String name = namePath.substring( 0, index );
      try {
        DynamicBean dynamicMap = (DynamicBean) this.map.get( name );
        dynamicMap.dynamicSetFormat( namePath.substring( index + 1 ), format );
      } catch ( ClassCastException e ) {
        throw new ClassCastException( "A dynamicSetFormat requires that all beans are DynamicMap's.  Requested namePath=" + namePath + ", DynamicMap=" + this + " : " + e.getMessage() );
      }
    } else {
      if ( this.formatters == null ) this.formatters = new HashMap();
      this.formatters.put( namePath, format );
    }
  }

//  private void addMsg( String key, Msg msg ) {
//    if ( this.msgs == null ) this.msgs = new HashMap();
//    this.msgs.put( key, msg );
//  }
//
//  public Map getMsgs() {
//    return msgs;
//  }

  public Map getMap() {
    return this.map;
  }

  ///////////////////////////////////
  // DynaBean methods
  ///////////////////////////////////
  public boolean contains( String name, String key ) {
    // loginfo( "checking existance of name " + name + " and key " + key );
    Map mappedProperty = (Map) this.map.get( name );
    if ( mappedProperty == null ) return false;
    Object value = mappedProperty.get( key );
    return ( value != null );
  }

  public Object get( String name ) {
    Format format = null;
    if ( this.formatters != null ) {
      format = (Format) this.formatters.get( name );
    }

    if ( format != null ) {
      return format.format( this.map.get( name ) );
    } else {
      return this.map.get( (Object) name );
    }
  }

  public Object get( String name, String key ) {
    // loginfo( "getting name " + name + " and key " + key );
    Map mappedProperty = (Map) this.map.get( name );
    if ( mappedProperty == null ) return null;
    return mappedProperty.get( key );
  }

  public Object get( String name, int index ) {
    // loginfo( "getting name " + name + ", index " + Integer.toString( index ) );
    List indexedProperty = (List) this.map.get( name );
    if ( indexedProperty == null ) return null;
    Format format=null;
    if(formatters!=null)
      format = (Format) this.formatters.get( name );
    if ( format != null ) {
      return format.format( indexedProperty.get( index ) );
    } else {
      if(index>=indexedProperty.size())
        return null;
      else
        return indexedProperty.get(index );
    }
  }

  public DynaClass getDynaClass() {
    return this;
  }

  public void remove( String name, String key ) {
    Map mappedProperty = (Map) this.map.get( name );
    if ( mappedProperty == null ) return;
    mappedProperty.remove( key );
  }

  public void set( String name, Object value ) {
    this.put( name, value );
  }

  public void set( String name, String key, Object value ) {
    Map mappedProperty = (Map) this.map.get( name );
    if ( mappedProperty == null ) {
      mappedProperty = new HashMap();
      this.map.put( name, mappedProperty );
    }
    mappedProperty.put( key, value );
  }

  public void set( String name, int index, Object value ) {
    List indexedProperty = (List) this.map.get( name );
    if ( indexedProperty == null ) {
      indexedProperty = new ArrayList();
      this.map.put( name, indexedProperty );
    }
    Format format=null;
    if(formatters!=null)
      format = (Format) this.formatters.get( name );
    if ( format != null ) {
      try {
        indexedProperty.set( index, format.parseObject( (String) value ) );
      } catch ( ClassCastException e ) {
        new ClassCastException( "Couldn't set non-String value " + value + " for property " + name + " on DynamicMap " + this + " because property " + name + " has an associated formatter " + format + " : " + e.getMessage() );
      } catch ( ParseException e ) {
//        this.addMsg( name + "[" + Integer.toString( index ) + "]", new Msg( "parseexception" ) );
      }
    } else {
      indexedProperty.add(value);
    }
  }

  ///////////////////////////////////
  // DynaClass methods
  ///////////////////////////////////
  public DynaProperty[] getDynaProperties() {
    Set keySet = map.keySet();
    DynaProperty[] dynaProperties = new DynaProperty[ keySet.size() ];

    int index = 0;
    Iterator iter = keySet.iterator();
    while ( iter.hasNext() ) {
      String name = (String) iter.next();
      Object value = this.map.get( name );
      dynaProperties[ index ] = new DynaProperty( name, value.getClass() );
      index++;
    }

    return dynaProperties;
  }

  public DynaProperty getDynaProperty( String name ) {
    Class clazz = null;
    Object value = this.map.get( name );
    if ( value != null ) {
      clazz = value.getClass();
    } else {
      //logwarn( "Requesting uninitialized property " + name + ", assuming it will be a String..." );
      clazz = String.class;
    }
    return new DynaProperty( name, clazz );
  }

  public String getName() {
    return "DynamicBean";
  }

  public DynaBean newInstance() {
    // loginfo( "creating new instance" );
    return new DynamicBean();
  }


  ///////////////////////////////////
  // Map methods
  ///////////////////////////////////
  public void clear() {
    map.clear();
  }

  public boolean containsKey(Object key) {
    return map.containsKey(key);
  }

  public boolean containsValue(Object value) {
    return map.containsValue(value);
  }

  public Set entrySet() {
    return map.entrySet();
  }

  public Object get(Object key) {
    return map.get(key);
  }

  public boolean isEmpty() {
    return map.isEmpty();
  }

  public Set keySet() {
    return map.keySet();
  }

  public Object put(Object key, Object value) {
    Format format = null;

    if ( this.formatters != null ) {
      format = (Format) this.formatters.get( key );
    }

    if ( format != null ) {
      try {
        return map.put( key, format.parseObject( (String) value ) );
      } catch ( ClassCastException e ) {
        throw new ClassCastException( "Can't put a non-String-value into a DynamicMap for which a formatter has been set. key=" + key + ", value=" + value + ", DynamicMap=" + this + " because property " + key + " has an associated formatter " + format + " : " + e.getMessage() );
      } catch ( ParseException e ) {
//        this.addMsg( (String) key, new Msg( "parseexception" ) );
        return null;
      }
    } else {
      return map.put( key, value );
    }
  }

  public void putAll(Map t) {
    map.putAll(t);
  }

  public Object remove(Object key) {
    return map.remove(key);
  }

  public int size() {
    return map.size();
  }

  public Collection values() {
    return map.values();
  }

  public String toString() {
    return "DynamicMap" + map.toString();
  }

  public int hashCode() {
    return map.hashCode();
  }

  public boolean equals(Object object) {
    return map.equals(object);
  }

  public void reset(){
    if(formatters !=null)
      this.formatters.clear();
    if(map!=null)
      this.map.clear();
  }

//  private void logwarn( String msg ) {
//    Logger.getLogger("DYNAMICFORM").warn( msg );
//  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -