CorpusPersistence.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: CorpusPersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13  *
14  */
15 
16 package gate.util.persistence;
17 
18 
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 
22 import gate.Corpus;
23 import gate.creole.ResourceInstantiationException;
24 import gate.persist.PersistenceException;
25 
26 
27 public class CorpusPersistence extends LRPersistence {
28   /**
29    * Populates this Persistence with the data that needs to be stored from the
30    * original source object.
31    */
32   public void extractDataFromSource(Object source)throws PersistenceException{
33     //check input
34     if((source instanceof Corpus)){
35       throw new UnsupportedOperationException(
36                 getClass().getName() " can only be used for " +
37                 Corpus.class.getName() +
38                 " objects!\n" + source.getClass().getName() +
39                 " is not a " + Corpus.class.getName());
40     }
41 
42     Corpus corpus = (Corpus)source;
43     super.extractDataFromSource(source);
44     if(dsData == null){
45       //transient corpus; we still need to save the docs
46       docList = new ArrayList();
47       Iterator docIter = corpus.iterator();
48       while(docIter.hasNext()){
49         docList.add(PersistenceManager.
50                     getPersistentRepresentation(docIter.next()));
51       }
52     }else{
53       //persistent corpus; it takes care of documents by itself
54       //nothing to do :)
55       docList = null;
56     }
57   }
58 
59 
60   /**
61    * Creates a new object from the data contained. This new object is supposed
62    * to be a copy for the original object used as source for data extraction.
63    */
64   public Object createObject()throws PersistenceException,
65                                      ResourceInstantiationException{
66     Corpus corpus = (Corpus)super.createObject();
67     if(docList != null){
68       //transient corpus; we need to recreate the docs
69       if(!docList.isEmpty() && corpus.isEmpty()){
70         Iterator docIter = docList.iterator();
71         while(docIter.hasNext()){
72           corpus.add(PersistenceManager.
73                      getTransientRepresentation(docIter.next()));
74         }
75 
76       }
77     }
78     return corpus;
79   }
80 
81 
82   protected ArrayList docList;
83   static final long serialVersionUID = 6181534551802883626L;
84 }