1
14 package gate.util.persistence;
15
16 import java.util.*;
17
18 import gate.*;
19 import gate.creole.*;
20 import gate.persist.PersistenceException;
21 import gate.util.NameBearer;
22
23
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 initParams = Factory.newFeatureMap();
51 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 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 }