| ResourcePersistence |

    1   /*
    2    *  Copyright (c) 1998-2001, The University of Sheffield.
    3    *
    4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    5    *  software, licenced under the GNU Library General Public License,
    6    *  Version 2, June 1991 (in the distribution as file licence.html,
    7    *  and also available at http://gate.ac.uk/gate/licence.html).
    8    *
    9    *  Valentin Tablan 25/10/2001
    10   *
    11   *  $Id: ResourcePersistence.java,v 1.3 2001/11/13 15:32:05 valyt Exp $
    12   *
    13   */
    14  package gate.util.persistence;
    15
    16  import gate.*;
    17  import gate.creole.*;
    18  import gate.util.*;
    19  import gate.persist.PersistenceException;
    20
    21  import java.util.*;
    22
    23  /**
    24   * Holds the data needed to serialise and recreate a {@link Resource}.
    25   * This data is considered to be: the resource class name, the resource name,
    26   * the resource features and the resource initialistion parameters.
    27   */
    28  class ResourcePersistence implements Persistence{
    29
    30    public void extractDataFromSource(Object source) throws PersistenceException{
    31      if(! (source instanceof Resource)){
    32        throw new UnsupportedOperationException(
    33                  getClass().getName() + " can only be used for " +
    34                  Resource.class.getName() +
    35                  " objects!\n" + source.getClass().getName() +
    36                  " is not a " + Resource.class.getName());
    37      }
    38      Resource res = (Resource)source;
    39      resourceType = res.getClass().getName();
    40      if(res instanceof NameBearer) resourceName = ((NameBearer)res).getName();
    41
    42      ResourceData rData = (ResourceData)
    43                           Gate.getCreoleRegister().get(resourceType);
    44      if(rData == null) throw new PersistenceException(
    45                                  "Could not find CREOLE data for " +
    46                                  resourceType);
    47      ParameterList params = rData.getParameterList();
    48      try{
    49        //get the values for the init time parameters
    50        initParams = Factory.newFeatureMap();
    51        //this is a list of lists
    52        Iterator parDisjIter = ((List)params.getInitimeParameters()).iterator();
    53        while(parDisjIter.hasNext()){
    54          Iterator parIter = ((List)parDisjIter.next()).iterator();
    55          while(parIter.hasNext()){
    56            Parameter parameter = (Parameter)parIter.next();
    57            String parName = parameter.getName();
    58            Object parValue = res.getParameterValue(parName);
    59            ((Map)initParams).put(parName, parValue);
    60          }
    61        }
    62        initParams = PersistenceManager.getPersistentRepresentation(initParams);
    63
    64        //get the features
    65        if(res.getFeatures() != null){
    66          features = Factory.newFeatureMap();
    67          ((Map)features).putAll(res.getFeatures());
    68          features = PersistenceManager.getPersistentRepresentation(features);
    69        }
    70      }catch(ResourceInstantiationException rie){
    71        throw new PersistenceException(rie);
    72      }
    73    }
    74
    75
    76    public Object createObject()throws PersistenceException,
    77                                       ResourceInstantiationException {
    78      if(initParams != null)
    79        initParams = PersistenceManager.getTransientRepresentation(initParams);
    80      if(features != null)
    81        features = PersistenceManager.getTransientRepresentation(features);
    82      Resource res = Factory.createResource(resourceType, (FeatureMap)initParams,
    83                                            (FeatureMap)features,resourceName);
    84      return res;
    85    }
    86
    87    protected String resourceType;
    88    protected String resourceName;
    89    protected Object initParams;
    90    protected Object features;
    91    static final long serialVersionUID = -3196664486112887875L;
    92  }

| ResourcePersistence |