LRPersistence.java
01 /*
02  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
03  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04  *
05  *  This file is part of GATE (see http://gate.ac.uk/), and is free
06  *  software, licenced under the GNU Library General Public License,
07  *  Version 2, June 1991 (in the distribution as file licence.html,
08  *  and also available at http://gate.ac.uk/gate/licence.html).
09  *
10  *  Valentin Tablan 26/10/2001
11  *
12  *  $Id: LRPersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13  *
14  */
15 package gate.util.persistence;
16 
17 import java.io.Serializable;
18 import java.util.Map;
19 
20 import gate.DataStore;
21 import gate.LanguageResource;
22 import gate.creole.ResourceInstantiationException;
23 import gate.persist.PersistenceException;
24 
25 public class LRPersistence extends ResourcePersistence {
26 
27   /**
28    * Populates this Persistence with the data that needs to be stored from the
29    * original source object.
30    */
31   public void extractDataFromSource(Object source)throws PersistenceException{
32     //check input
33     if((source instanceof LanguageResource)){
34       throw new UnsupportedOperationException(
35                 getClass().getName() " can only be used for " +
36                 LanguageResource.class.getName() +
37                 " objects!\n" + source.getClass().getName() +
38                 " is not a " + LanguageResource.class.getName());
39     }
40 
41     super.extractDataFromSource(source);
42     //LR's will have the features saved by their respective persistence
43     //mechanism
44     features = null;
45 
46     LanguageResource lr = (LanguageResource)source;
47     if(lr.getDataStore() == null){
48       dsData = null;
49     }else{
50       dsData = PersistenceManager.
51                getPersistentRepresentation(lr.getDataStore());
52       persistenceID = lr.getLRPersistenceId();
53     }
54   }
55 
56   /**
57    * Creates a new object from the data contained. This new object is supposed
58    * to be a copy for the original object used as source for data extraction.
59    */
60   public Object createObject()throws PersistenceException,
61                                      ResourceInstantiationException{
62     if(dsData == nullreturn super.createObject();
63     else{
64       //persistent doc
65       initParams = PersistenceManager.getTransientRepresentation(initParams);
66 
67       DataStore ds = (DataStore)PersistenceManager.
68                      getTransientRepresentation(dsData);
69       ((Map)initParams).put(DataStore.DATASTORE_FEATURE_NAME, ds);
70       ((Map)initParams).put(DataStore.LR_ID_FEATURE_NAME, persistenceID);
71       return super.createObject();
72     }
73   }
74 
75   protected Serializable dsData;
76   protected Object persistenceID;
77   static final long serialVersionUID = 3541034274225534363L;
78 }