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 26/10/2001
10   *
11   *  $Id: CorpusPersistence.java,v 1.2 2001/10/29 17:11:26 valyt Exp $
12   *
13   */
14  
15  package gate.util.persistence;
16  
17  
18  import gate.*;
19  import gate.creole.*;
20  import gate.util.*;
21  import gate.persist.PersistenceException;
22  
23  import java.util.*;
24  import java.io.*;
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  }