DefaultMetaDataProvider.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DefaultMetaDataProvider.java,v 1.2 2005/10/10 17:01:07 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.metadata;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28.  * @author Jeanette Winzenburg
  29.  */
  30. public class DefaultMetaDataProvider implements MetaDataProvider {
  31.   private List fieldNames;
  32.   private Map metaData;
  33.   public DefaultMetaDataProvider() {
  34.     
  35.   }
  36.   public DefaultMetaDataProvider(MetaDataProvider provider) {
  37.     this(provider.getMetaData());
  38.   }
  39.   public DefaultMetaDataProvider(MetaData[] metaDatas) {
  40.     
  41.     setMetaData(metaDatas);
  42. }
  43. //------------------------ metaDataProvider  
  44.   public MetaData[] getMetaData() {
  45.     MetaData[] metaData = new MetaData[getFieldCount()];
  46.     for (int i = 0; i < metaData.length; i++) {
  47.       metaData[i] = getMetaData(getFieldName(i));
  48.     }
  49.     return metaData;
  50.   }
  51.   public MetaData getMetaData(String dataID) {
  52.     return (MetaData) getMetaDataMap().get(dataID);
  53.   }
  54.   public String[] getFieldNames() {
  55.     return (String[]) getFieldNameList().toArray(new String[getFieldCount()]);
  56.   }
  57.   public int getFieldCount() {
  58.     return getFieldNameList().size();
  59.   }
  60. //-------------------------- convenience accessing
  61.   
  62.   /**
  63.    * PRE: 0 <= index < getFieldCount()
  64.    * @param index
  65.    * @return
  66.    */
  67.   public String getFieldName(int index) {
  68.     return (String) getFieldNameList().get(index);
  69.   }
  70.   /**
  71.    * PRE: 0 <= index < getFieldCount()
  72.    * @param index
  73.    * @return
  74.    */
  75.   public MetaData getMetaData(int index) {
  76.     String fieldName = getFieldName(index);
  77.     return getMetaData(fieldName);
  78.   }
  79.   
  80.   public int getFieldIndex(String fieldName) {
  81.     for (int i = 0; i < getFieldCount(); i++) {
  82.       if (fieldName.equals(getFieldName(i))) {
  83.         return i;
  84.       }
  85.     }
  86.     return -1;
  87.   }
  88.   public boolean hasField(String fieldName) {
  89.     return getFieldNameList().contains(fieldName);
  90.   }
  91. //-------------------------- mutating (use on init mostly)
  92.   public void setMetaData(MetaData[] metaData) {
  93.     clear();
  94.     for (int i = 0; i < metaData.length; i++) {
  95.       addField(metaData[i]);
  96.     }
  97.   }
  98.   public void setMetaData(List metaData) {
  99.     clear();
  100.     for (Iterator iter = metaData.iterator(); iter.hasNext(); ) {
  101.       addField((MetaData) iter.next());
  102.     }
  103.   }
  104.   public void addField(MetaData data) {
  105.     getFieldNameList().add(data.getName());
  106.     getMetaDataMap().put(data.getName(), data);
  107.   }
  108.   public void clear() {
  109.     fieldNames = null;
  110.     metaData = null;
  111.   }
  112. //-------------------------- helper
  113.   private List getFieldNameList() {
  114.     if (fieldNames == null) {
  115.       fieldNames = new ArrayList();
  116.     }
  117.     return fieldNames;
  118.   }
  119.   private Map getMetaDataMap() {
  120.     if (metaData == null) {
  121.       metaData = new HashMap();
  122.     }
  123.     return metaData;
  124.   }
  125. }